A simple library to support serving https.
Dependents: oldheating gps motorhome heating
tls/tls-session.c@22:af0b5ceb556e, 2019-10-23 (annotated)
- Committer:
- andrewboyson
- Date:
- Wed Oct 23 08:44:50 2019 +0000
- Revision:
- 22:af0b5ceb556e
- Parent:
- 14:03a0b8fd6ddc
Removed error if passing a 0 id to TlsSessionGetFromIdOrNull; now it just returns NULL without complaining.
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 | for (int i = 0; i < TLS_KEY_SIZE_MASTER; i++) p->masterSecret[i] = 0; |
andrewboyson | 10:e269fd7b9500 | 20 | } |
andrewboyson | 10:e269fd7b9500 | 21 | |
andrewboyson | 10:e269fd7b9500 | 22 | struct TlsSession* TlsSessionNew() //Never fails so never returns NULL |
andrewboyson | 10:e269fd7b9500 | 23 | { |
andrewboyson | 10:e269fd7b9500 | 24 | struct TlsSession* p; |
andrewboyson | 10:e269fd7b9500 | 25 | |
andrewboyson | 10:e269fd7b9500 | 26 | //look for an empty connection |
andrewboyson | 2:82268409e83f | 27 | struct TlsSession* pOldest = 0; |
andrewboyson | 2:82268409e83f | 28 | uint32_t ageOldest = 0; |
andrewboyson | 10:e269fd7b9500 | 29 | for (p = sessions; p < sessions + TLS_MAX_SESSIONS; p++) |
andrewboyson | 2:82268409e83f | 30 | { |
andrewboyson | 10:e269fd7b9500 | 31 | if (!p->id) goto end; //Found an empty slot so just return it |
andrewboyson | 5:ee5489ee1117 | 32 | uint32_t age = MsTimerCount - p->lastUsed; |
andrewboyson | 2:82268409e83f | 33 | if (age >= ageOldest) |
andrewboyson | 2:82268409e83f | 34 | { |
andrewboyson | 2:82268409e83f | 35 | ageOldest = age; |
andrewboyson | 5:ee5489ee1117 | 36 | pOldest = p; |
andrewboyson | 2:82268409e83f | 37 | } |
andrewboyson | 2:82268409e83f | 38 | } |
andrewboyson | 10:e269fd7b9500 | 39 | |
andrewboyson | 10:e269fd7b9500 | 40 | //No empty ones found so use the oldest |
andrewboyson | 10:e269fd7b9500 | 41 | p = pOldest; |
andrewboyson | 10:e269fd7b9500 | 42 | |
andrewboyson | 10:e269fd7b9500 | 43 | end: |
andrewboyson | 10:e269fd7b9500 | 44 | zeroSession(p); |
andrewboyson | 10:e269fd7b9500 | 45 | uint32_t id = 0; |
andrewboyson | 10:e269fd7b9500 | 46 | while (!id) id = HrTimerCount(); //This is used as a convenient unique identifier which is never zero; |
andrewboyson | 10:e269fd7b9500 | 47 | p->id = id; |
andrewboyson | 10:e269fd7b9500 | 48 | p->lastUsed = MsTimerCount; |
andrewboyson | 10:e269fd7b9500 | 49 | return p; |
andrewboyson | 2:82268409e83f | 50 | } |
andrewboyson | 2:82268409e83f | 51 | |
andrewboyson | 10:e269fd7b9500 | 52 | struct TlsSession* TlsSessionOrNull(uint32_t id) |
andrewboyson | 2:82268409e83f | 53 | { |
andrewboyson | 22:af0b5ceb556e | 54 | if (!id) return NULL; |
andrewboyson | 10:e269fd7b9500 | 55 | for (struct TlsSession* p = sessions; p < sessions + TLS_MAX_SESSIONS; p++) |
andrewboyson | 10:e269fd7b9500 | 56 | { |
andrewboyson | 10:e269fd7b9500 | 57 | if (p->id == id) return p; |
andrewboyson | 10:e269fd7b9500 | 58 | } |
andrewboyson | 22:af0b5ceb556e | 59 | return NULL; |
andrewboyson | 2:82268409e83f | 60 | } |
andrewboyson | 10:e269fd7b9500 | 61 | void TlsSessionReset(uint32_t id) |
andrewboyson | 2:82268409e83f | 62 | { |
andrewboyson | 22:af0b5ceb556e | 63 | if (!id) return; |
andrewboyson | 10:e269fd7b9500 | 64 | for (struct TlsSession* p = sessions; p < sessions + TLS_MAX_SESSIONS; p++) |
andrewboyson | 10:e269fd7b9500 | 65 | { |
andrewboyson | 10:e269fd7b9500 | 66 | if (p->id == id) zeroSession(p); |
andrewboyson | 10:e269fd7b9500 | 67 | } |
andrewboyson | 2:82268409e83f | 68 | } |