A simple library to support serving https.
Dependents: oldheating gps motorhome heating
tls/tls-session.c@13:0a80b49a5e78, 2019-09-27 (annotated)
- Committer:
- andrewboyson
- Date:
- Fri Sep 27 11:31:18 2019 +0000
- Revision:
- 13:0a80b49a5e78
- Parent:
- 10:e269fd7b9500
- Child:
- 14:03a0b8fd6ddc
Removed bug with TLS session. Unable to test as compiler has moved to v6 again (I think)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 5:ee5489ee1117 | 1 | #include <stdlib.h> |
andrewboyson | 2:82268409e83f | 2 | #include <stdint.h> |
andrewboyson | 2:82268409e83f | 3 | #include <stdbool.h> |
andrewboyson | 2:82268409e83f | 4 | |
andrewboyson | 2:82268409e83f | 5 | #include "tls-session.h" |
andrewboyson | 2:82268409e83f | 6 | #include "mstimer.h" |
andrewboyson | 10:e269fd7b9500 | 7 | #include "hrtimer.h" |
andrewboyson | 13:0a80b49a5e78 | 8 | #include "log.h" |
andrewboyson | 2:82268409e83f | 9 | |
andrewboyson | 2:82268409e83f | 10 | #define TLS_MAX_SESSIONS 4 |
andrewboyson | 2:82268409e83f | 11 | |
andrewboyson | 2:82268409e83f | 12 | static struct TlsSession sessions[TLS_MAX_SESSIONS]; |
andrewboyson | 2:82268409e83f | 13 | |
andrewboyson | 10:e269fd7b9500 | 14 | static void zeroSession(struct TlsSession* p) |
andrewboyson | 2:82268409e83f | 15 | { |
andrewboyson | 10:e269fd7b9500 | 16 | p->id = 0; |
andrewboyson | 10:e269fd7b9500 | 17 | p->lastUsed = 0; |
andrewboyson | 10:e269fd7b9500 | 18 | p->valid = false; |
andrewboyson | 10:e269fd7b9500 | 19 | p->slotPriKeyDecryption = 0; |
andrewboyson | 10:e269fd7b9500 | 20 | for (int i = 0; i < TLS_KEY_SIZE_MASTER; i++) p->masterSecret[i] = 0; |
andrewboyson | 10:e269fd7b9500 | 21 | } |
andrewboyson | 10:e269fd7b9500 | 22 | |
andrewboyson | 10:e269fd7b9500 | 23 | struct TlsSession* TlsSessionNew() //Never fails so never returns NULL |
andrewboyson | 10:e269fd7b9500 | 24 | { |
andrewboyson | 10:e269fd7b9500 | 25 | struct TlsSession* p; |
andrewboyson | 10:e269fd7b9500 | 26 | |
andrewboyson | 10:e269fd7b9500 | 27 | //look for an empty connection |
andrewboyson | 2:82268409e83f | 28 | struct TlsSession* pOldest = 0; |
andrewboyson | 2:82268409e83f | 29 | uint32_t ageOldest = 0; |
andrewboyson | 10:e269fd7b9500 | 30 | for (p = sessions; p < sessions + TLS_MAX_SESSIONS; p++) |
andrewboyson | 2:82268409e83f | 31 | { |
andrewboyson | 10:e269fd7b9500 | 32 | if (!p->id) goto end; //Found an empty slot so just return it |
andrewboyson | 5:ee5489ee1117 | 33 | uint32_t age = MsTimerCount - p->lastUsed; |
andrewboyson | 2:82268409e83f | 34 | if (age >= ageOldest) |
andrewboyson | 2:82268409e83f | 35 | { |
andrewboyson | 2:82268409e83f | 36 | ageOldest = age; |
andrewboyson | 5:ee5489ee1117 | 37 | pOldest = p; |
andrewboyson | 2:82268409e83f | 38 | } |
andrewboyson | 2:82268409e83f | 39 | } |
andrewboyson | 10:e269fd7b9500 | 40 | |
andrewboyson | 10:e269fd7b9500 | 41 | //No empty ones found so use the oldest |
andrewboyson | 10:e269fd7b9500 | 42 | p = pOldest; |
andrewboyson | 10:e269fd7b9500 | 43 | |
andrewboyson | 10:e269fd7b9500 | 44 | end: |
andrewboyson | 10:e269fd7b9500 | 45 | zeroSession(p); |
andrewboyson | 10:e269fd7b9500 | 46 | uint32_t id = 0; |
andrewboyson | 10:e269fd7b9500 | 47 | while (!id) id = HrTimerCount(); //This is used as a convenient unique identifier which is never zero; |
andrewboyson | 10:e269fd7b9500 | 48 | p->id = id; |
andrewboyson | 10:e269fd7b9500 | 49 | p->lastUsed = MsTimerCount; |
andrewboyson | 10:e269fd7b9500 | 50 | return p; |
andrewboyson | 2:82268409e83f | 51 | } |
andrewboyson | 2:82268409e83f | 52 | |
andrewboyson | 10:e269fd7b9500 | 53 | struct TlsSession* TlsSessionOrNull(uint32_t id) |
andrewboyson | 2:82268409e83f | 54 | { |
andrewboyson | 13:0a80b49a5e78 | 55 | if (!id) |
andrewboyson | 13:0a80b49a5e78 | 56 | { |
andrewboyson | 13:0a80b49a5e78 | 57 | Log("TlsSessionOrNull - session id cannot be 0\r\n"); |
andrewboyson | 13:0a80b49a5e78 | 58 | return 0; |
andrewboyson | 13:0a80b49a5e78 | 59 | } |
andrewboyson | 10:e269fd7b9500 | 60 | for (struct TlsSession* p = sessions; p < sessions + TLS_MAX_SESSIONS; p++) |
andrewboyson | 10:e269fd7b9500 | 61 | { |
andrewboyson | 10:e269fd7b9500 | 62 | if (p->id == id) return p; |
andrewboyson | 10:e269fd7b9500 | 63 | } |
andrewboyson | 10:e269fd7b9500 | 64 | return 0; |
andrewboyson | 2:82268409e83f | 65 | } |
andrewboyson | 10:e269fd7b9500 | 66 | void TlsSessionReset(uint32_t id) |
andrewboyson | 2:82268409e83f | 67 | { |
andrewboyson | 13:0a80b49a5e78 | 68 | if (!id) |
andrewboyson | 13:0a80b49a5e78 | 69 | { |
andrewboyson | 13:0a80b49a5e78 | 70 | Log("TlsSessionReset - session id cannot be 0\r\n"); |
andrewboyson | 13:0a80b49a5e78 | 71 | return; |
andrewboyson | 13:0a80b49a5e78 | 72 | } |
andrewboyson | 10:e269fd7b9500 | 73 | for (struct TlsSession* p = sessions; p < sessions + TLS_MAX_SESSIONS; p++) |
andrewboyson | 10:e269fd7b9500 | 74 | { |
andrewboyson | 10:e269fd7b9500 | 75 | if (p->id == id) zeroSession(p); |
andrewboyson | 10:e269fd7b9500 | 76 | } |
andrewboyson | 2:82268409e83f | 77 | } |