Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: oldheating gps motorhome heating
tls/tls-connection.c@8:5e66a6b4b38c, 2019-09-07 (annotated)
- Committer:
- andrewboyson
- Date:
- Sat Sep 07 18:56:01 2019 +0000
- Revision:
- 8:5e66a6b4b38c
- Parent:
- 6:819c17738dc2
- Child:
- 10:e269fd7b9500
Got TLS working as far as being able to verify finished message itself and to verify the MAC on the finished and the subsequent alert message.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| andrewboyson | 5:ee5489ee1117 | 1 | #include <stdlib.h> |
| andrewboyson | 5:ee5489ee1117 | 2 | |
| andrewboyson | 5:ee5489ee1117 | 3 | #include "tls-connection.h" |
| andrewboyson | 5:ee5489ee1117 | 4 | #include "mstimer.h" |
| andrewboyson | 5:ee5489ee1117 | 5 | |
| andrewboyson | 5:ee5489ee1117 | 6 | #define MAX_CONNECTIONS 4 |
| andrewboyson | 5:ee5489ee1117 | 7 | |
| andrewboyson | 5:ee5489ee1117 | 8 | static struct TlsConnection connections[MAX_CONNECTIONS]; |
| andrewboyson | 5:ee5489ee1117 | 9 | |
| andrewboyson | 8:5e66a6b4b38c | 10 | struct TlsConnection* TlsConnectionGetNext() |
| andrewboyson | 8:5e66a6b4b38c | 11 | { |
| andrewboyson | 8:5e66a6b4b38c | 12 | static struct TlsConnection* p = connections + MAX_CONNECTIONS - 1; //Initialise to last element |
| andrewboyson | 8:5e66a6b4b38c | 13 | p++; |
| andrewboyson | 8:5e66a6b4b38c | 14 | if (p >= connections + MAX_CONNECTIONS) p = connections; |
| andrewboyson | 8:5e66a6b4b38c | 15 | return p; |
| andrewboyson | 8:5e66a6b4b38c | 16 | } |
| andrewboyson | 8:5e66a6b4b38c | 17 | |
| andrewboyson | 5:ee5489ee1117 | 18 | static void zeroConnection(struct TlsConnection* p) |
| andrewboyson | 5:ee5489ee1117 | 19 | { |
| andrewboyson | 5:ee5489ee1117 | 20 | p->id = 0; |
| andrewboyson | 5:ee5489ee1117 | 21 | p->lastUsed = 0; |
| andrewboyson | 5:ee5489ee1117 | 22 | p->toDo = 0; |
| andrewboyson | 5:ee5489ee1117 | 23 | p->session = 0; |
| andrewboyson | 8:5e66a6b4b38c | 24 | Sha256Start(&p->handshakeSha); //This just clears any information previously calculated |
| andrewboyson | 6:819c17738dc2 | 25 | p->clientEncrypted = false; |
| andrewboyson | 6:819c17738dc2 | 26 | p->serverEncrypted = false; |
| andrewboyson | 6:819c17738dc2 | 27 | for (int i = 0; i < TLS_RANDOM_SIZE; i++) p->clientRandom[i] = 0; |
| andrewboyson | 6:819c17738dc2 | 28 | for (int i = 0; i < TLS_RANDOM_SIZE; i++) p->serverRandom[i] = 0; |
| andrewboyson | 8:5e66a6b4b38c | 29 | for (int i = 0; i < TLS_DEFERRED_CONTENT_SIZE; i++) p->deferredContent[i] = 0; |
| andrewboyson | 5:ee5489ee1117 | 30 | } |
| andrewboyson | 5:ee5489ee1117 | 31 | |
| andrewboyson | 5:ee5489ee1117 | 32 | struct TlsConnection* TlsConnectionNew(int connectionId) //Never fails so never returns NULL |
| andrewboyson | 5:ee5489ee1117 | 33 | { |
| andrewboyson | 5:ee5489ee1117 | 34 | struct TlsConnection* p; |
| andrewboyson | 5:ee5489ee1117 | 35 | |
| andrewboyson | 5:ee5489ee1117 | 36 | //Look for an existing connection |
| andrewboyson | 5:ee5489ee1117 | 37 | for (p = connections; p < connections + MAX_CONNECTIONS; p++) |
| andrewboyson | 5:ee5489ee1117 | 38 | { |
| andrewboyson | 5:ee5489ee1117 | 39 | if (p->id == connectionId) goto end; |
| andrewboyson | 5:ee5489ee1117 | 40 | } |
| andrewboyson | 5:ee5489ee1117 | 41 | |
| andrewboyson | 5:ee5489ee1117 | 42 | //look for an empty connection |
| andrewboyson | 5:ee5489ee1117 | 43 | { |
| andrewboyson | 5:ee5489ee1117 | 44 | struct TlsConnection* pOldest = 0; |
| andrewboyson | 5:ee5489ee1117 | 45 | uint32_t ageOldest = 0; |
| andrewboyson | 5:ee5489ee1117 | 46 | for (p = connections; p < connections + MAX_CONNECTIONS; p++) |
| andrewboyson | 5:ee5489ee1117 | 47 | { |
| andrewboyson | 5:ee5489ee1117 | 48 | if (!p->id) goto end; |
| andrewboyson | 5:ee5489ee1117 | 49 | |
| andrewboyson | 5:ee5489ee1117 | 50 | //Otherwise record the oldest and keep going |
| andrewboyson | 5:ee5489ee1117 | 51 | uint32_t age = MsTimerCount - p->lastUsed; |
| andrewboyson | 5:ee5489ee1117 | 52 | if (age >= ageOldest) |
| andrewboyson | 5:ee5489ee1117 | 53 | { |
| andrewboyson | 5:ee5489ee1117 | 54 | ageOldest = age; |
| andrewboyson | 5:ee5489ee1117 | 55 | pOldest = p; |
| andrewboyson | 5:ee5489ee1117 | 56 | } |
| andrewboyson | 5:ee5489ee1117 | 57 | } |
| andrewboyson | 5:ee5489ee1117 | 58 | |
| andrewboyson | 5:ee5489ee1117 | 59 | //No empty ones found so use the oldest |
| andrewboyson | 5:ee5489ee1117 | 60 | p = pOldest; |
| andrewboyson | 5:ee5489ee1117 | 61 | } |
| andrewboyson | 5:ee5489ee1117 | 62 | |
| andrewboyson | 5:ee5489ee1117 | 63 | end: |
| andrewboyson | 5:ee5489ee1117 | 64 | zeroConnection(p); |
| andrewboyson | 5:ee5489ee1117 | 65 | p->id = connectionId; |
| andrewboyson | 5:ee5489ee1117 | 66 | p->lastUsed = MsTimerCount; |
| andrewboyson | 5:ee5489ee1117 | 67 | return p; |
| andrewboyson | 5:ee5489ee1117 | 68 | } |
| andrewboyson | 5:ee5489ee1117 | 69 | struct TlsConnection* TlsConnectionOrNull(int connectionId) |
| andrewboyson | 5:ee5489ee1117 | 70 | { |
| andrewboyson | 5:ee5489ee1117 | 71 | for (struct TlsConnection* p = connections; p < connections + MAX_CONNECTIONS; p++) |
| andrewboyson | 5:ee5489ee1117 | 72 | { |
| andrewboyson | 5:ee5489ee1117 | 73 | if (p->id == connectionId) |
| andrewboyson | 5:ee5489ee1117 | 74 | { |
| andrewboyson | 5:ee5489ee1117 | 75 | p->lastUsed = MsTimerCount; |
| andrewboyson | 5:ee5489ee1117 | 76 | return p; |
| andrewboyson | 5:ee5489ee1117 | 77 | } |
| andrewboyson | 5:ee5489ee1117 | 78 | } |
| andrewboyson | 5:ee5489ee1117 | 79 | return NULL; |
| andrewboyson | 5:ee5489ee1117 | 80 | } |
| andrewboyson | 5:ee5489ee1117 | 81 | void TlsConnectionReset(int connectionId) |
| andrewboyson | 5:ee5489ee1117 | 82 | { |
| andrewboyson | 5:ee5489ee1117 | 83 | for (struct TlsConnection* p = connections; p < connections + MAX_CONNECTIONS; p++) |
| andrewboyson | 5:ee5489ee1117 | 84 | { |
| andrewboyson | 5:ee5489ee1117 | 85 | if (p->id == connectionId) zeroConnection(p); |
| andrewboyson | 5:ee5489ee1117 | 86 | } |
| andrewboyson | 5:ee5489ee1117 | 87 | } |
| andrewboyson | 8:5e66a6b4b38c | 88 |