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@6:819c17738dc2, 2019-09-01 (annotated)
- Committer:
- andrewboyson
- Date:
- Sun Sep 01 18:15:12 2019 +0000
- Revision:
- 6:819c17738dc2
- Parent:
- 5:ee5489ee1117
- Child:
- 8:5e66a6b4b38c
Making progress - now have decryption working.
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 | 5:ee5489ee1117 | 10 | static void zeroConnection(struct TlsConnection* p) |
| andrewboyson | 5:ee5489ee1117 | 11 | { |
| andrewboyson | 5:ee5489ee1117 | 12 | p->id = 0; |
| andrewboyson | 5:ee5489ee1117 | 13 | p->lastUsed = 0; |
| andrewboyson | 5:ee5489ee1117 | 14 | p->toDo = 0; |
| andrewboyson | 5:ee5489ee1117 | 15 | p->session = 0; |
| andrewboyson | 5:ee5489ee1117 | 16 | Sha256Start(&p->handshakeHash); //This just clears any information previously calculated |
| andrewboyson | 6:819c17738dc2 | 17 | p->clientEncrypted = false; |
| andrewboyson | 6:819c17738dc2 | 18 | p->serverEncrypted = false; |
| andrewboyson | 6:819c17738dc2 | 19 | for (int i = 0; i < TLS_RANDOM_SIZE; i++) p->clientRandom[i] = 0; |
| andrewboyson | 6:819c17738dc2 | 20 | for (int i = 0; i < TLS_RANDOM_SIZE; i++) p->serverRandom[i] = 0; |
| andrewboyson | 6:819c17738dc2 | 21 | for (int i = 0; i < TLS_VERIFY_SIZE; i++) p->clientVerify[i] = 0; |
| andrewboyson | 5:ee5489ee1117 | 22 | } |
| andrewboyson | 5:ee5489ee1117 | 23 | |
| andrewboyson | 5:ee5489ee1117 | 24 | struct TlsConnection* TlsConnectionNew(int connectionId) //Never fails so never returns NULL |
| andrewboyson | 5:ee5489ee1117 | 25 | { |
| andrewboyson | 5:ee5489ee1117 | 26 | struct TlsConnection* p; |
| andrewboyson | 5:ee5489ee1117 | 27 | |
| andrewboyson | 5:ee5489ee1117 | 28 | //Look for an existing connection |
| andrewboyson | 5:ee5489ee1117 | 29 | for (p = connections; p < connections + MAX_CONNECTIONS; p++) |
| andrewboyson | 5:ee5489ee1117 | 30 | { |
| andrewboyson | 5:ee5489ee1117 | 31 | if (p->id == connectionId) goto end; |
| andrewboyson | 5:ee5489ee1117 | 32 | } |
| andrewboyson | 5:ee5489ee1117 | 33 | |
| andrewboyson | 5:ee5489ee1117 | 34 | //look for an empty connection |
| andrewboyson | 5:ee5489ee1117 | 35 | { |
| andrewboyson | 5:ee5489ee1117 | 36 | struct TlsConnection* pOldest = 0; |
| andrewboyson | 5:ee5489ee1117 | 37 | uint32_t ageOldest = 0; |
| andrewboyson | 5:ee5489ee1117 | 38 | for (p = connections; p < connections + MAX_CONNECTIONS; p++) |
| andrewboyson | 5:ee5489ee1117 | 39 | { |
| andrewboyson | 5:ee5489ee1117 | 40 | if (!p->id) goto end; |
| andrewboyson | 5:ee5489ee1117 | 41 | |
| andrewboyson | 5:ee5489ee1117 | 42 | //Otherwise record the oldest and keep going |
| andrewboyson | 5:ee5489ee1117 | 43 | uint32_t age = MsTimerCount - p->lastUsed; |
| andrewboyson | 5:ee5489ee1117 | 44 | if (age >= ageOldest) |
| andrewboyson | 5:ee5489ee1117 | 45 | { |
| andrewboyson | 5:ee5489ee1117 | 46 | ageOldest = age; |
| andrewboyson | 5:ee5489ee1117 | 47 | pOldest = p; |
| andrewboyson | 5:ee5489ee1117 | 48 | } |
| andrewboyson | 5:ee5489ee1117 | 49 | } |
| andrewboyson | 5:ee5489ee1117 | 50 | |
| andrewboyson | 5:ee5489ee1117 | 51 | //No empty ones found so use the oldest |
| andrewboyson | 5:ee5489ee1117 | 52 | p = pOldest; |
| andrewboyson | 5:ee5489ee1117 | 53 | } |
| andrewboyson | 5:ee5489ee1117 | 54 | |
| andrewboyson | 5:ee5489ee1117 | 55 | end: |
| andrewboyson | 5:ee5489ee1117 | 56 | zeroConnection(p); |
| andrewboyson | 5:ee5489ee1117 | 57 | p->id = connectionId; |
| andrewboyson | 5:ee5489ee1117 | 58 | p->lastUsed = MsTimerCount; |
| andrewboyson | 5:ee5489ee1117 | 59 | return p; |
| andrewboyson | 5:ee5489ee1117 | 60 | } |
| andrewboyson | 5:ee5489ee1117 | 61 | struct TlsConnection* TlsConnectionOrNull(int connectionId) |
| andrewboyson | 5:ee5489ee1117 | 62 | { |
| andrewboyson | 5:ee5489ee1117 | 63 | for (struct TlsConnection* p = connections; p < connections + MAX_CONNECTIONS; p++) |
| andrewboyson | 5:ee5489ee1117 | 64 | { |
| andrewboyson | 5:ee5489ee1117 | 65 | if (p->id == connectionId) |
| andrewboyson | 5:ee5489ee1117 | 66 | { |
| andrewboyson | 5:ee5489ee1117 | 67 | p->lastUsed = MsTimerCount; |
| andrewboyson | 5:ee5489ee1117 | 68 | return p; |
| andrewboyson | 5:ee5489ee1117 | 69 | } |
| andrewboyson | 5:ee5489ee1117 | 70 | } |
| andrewboyson | 5:ee5489ee1117 | 71 | return NULL; |
| andrewboyson | 5:ee5489ee1117 | 72 | } |
| andrewboyson | 5:ee5489ee1117 | 73 | void TlsConnectionReset(int connectionId) |
| andrewboyson | 5:ee5489ee1117 | 74 | { |
| andrewboyson | 5:ee5489ee1117 | 75 | for (struct TlsConnection* p = connections; p < connections + MAX_CONNECTIONS; p++) |
| andrewboyson | 5:ee5489ee1117 | 76 | { |
| andrewboyson | 5:ee5489ee1117 | 77 | if (p->id == connectionId) zeroConnection(p); |
| andrewboyson | 5:ee5489ee1117 | 78 | } |
| andrewboyson | 5:ee5489ee1117 | 79 | } |