Andrew Boyson / crypto

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Thu Oct 10 07:38:13 2019 +0000
Revision:
17:93feb2a51d58
Parent:
14:03a0b8fd6ddc
Defined a lot of lengths eg 20 -> SHA1_HASH_LENGTH

Who changed what in which revision?

UserRevisionLine numberNew 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 10:e269fd7b9500 20 p->id = 0;
andrewboyson 10:e269fd7b9500 21 p->lastUsed = 0;
andrewboyson 10:e269fd7b9500 22 p->toDo = 0;
andrewboyson 10:e269fd7b9500 23 p->sessionId = 0;
andrewboyson 14:03a0b8fd6ddc 24 p->resume = false;
andrewboyson 8:5e66a6b4b38c 25 Sha256Start(&p->handshakeSha); //This just clears any information previously calculated
andrewboyson 6:819c17738dc2 26 p->clientEncrypted = false;
andrewboyson 6:819c17738dc2 27 p->serverEncrypted = false;
andrewboyson 17:93feb2a51d58 28 for (int i = 0; i < TLS_LENGTH_RANDOM; i++) p->clientRandom[i] = 0;
andrewboyson 17:93feb2a51d58 29 for (int i = 0; i < TLS_LENGTH_RANDOM; i++) p->serverRandom[i] = 0;
andrewboyson 8:5e66a6b4b38c 30 for (int i = 0; i < TLS_DEFERRED_CONTENT_SIZE; i++) p->deferredContent[i] = 0;
andrewboyson 10:e269fd7b9500 31 p->clientSequence = 0;
andrewboyson 10:e269fd7b9500 32 p->serverSequence = 0;
andrewboyson 10:e269fd7b9500 33 p->clientPositionInStreamOffset = 0;
andrewboyson 10:e269fd7b9500 34 p->serverPositionInStreamOffset = 0;
andrewboyson 14:03a0b8fd6ddc 35 p->slotPriKeyDecryption = 0;
andrewboyson 5:ee5489ee1117 36 }
andrewboyson 5:ee5489ee1117 37
andrewboyson 10:e269fd7b9500 38 struct TlsConnection* TlsConnectionNew(int id) //Never fails so never returns NULL
andrewboyson 5:ee5489ee1117 39 {
andrewboyson 5:ee5489ee1117 40 struct TlsConnection* p;
andrewboyson 5:ee5489ee1117 41
andrewboyson 5:ee5489ee1117 42 //Look for an existing connection
andrewboyson 5:ee5489ee1117 43 for (p = connections; p < connections + MAX_CONNECTIONS; p++)
andrewboyson 5:ee5489ee1117 44 {
andrewboyson 10:e269fd7b9500 45 if (p->id == id) goto end;
andrewboyson 5:ee5489ee1117 46 }
andrewboyson 5:ee5489ee1117 47
andrewboyson 5:ee5489ee1117 48 //look for an empty connection
andrewboyson 5:ee5489ee1117 49 {
andrewboyson 5:ee5489ee1117 50 struct TlsConnection* pOldest = 0;
andrewboyson 5:ee5489ee1117 51 uint32_t ageOldest = 0;
andrewboyson 5:ee5489ee1117 52 for (p = connections; p < connections + MAX_CONNECTIONS; p++)
andrewboyson 5:ee5489ee1117 53 {
andrewboyson 5:ee5489ee1117 54 if (!p->id) goto end;
andrewboyson 5:ee5489ee1117 55
andrewboyson 5:ee5489ee1117 56 //Otherwise record the oldest and keep going
andrewboyson 5:ee5489ee1117 57 uint32_t age = MsTimerCount - p->lastUsed;
andrewboyson 5:ee5489ee1117 58 if (age >= ageOldest)
andrewboyson 5:ee5489ee1117 59 {
andrewboyson 5:ee5489ee1117 60 ageOldest = age;
andrewboyson 5:ee5489ee1117 61 pOldest = p;
andrewboyson 5:ee5489ee1117 62 }
andrewboyson 5:ee5489ee1117 63 }
andrewboyson 5:ee5489ee1117 64 //No empty ones found so use the oldest
andrewboyson 5:ee5489ee1117 65 p = pOldest;
andrewboyson 5:ee5489ee1117 66 }
andrewboyson 10:e269fd7b9500 67
andrewboyson 5:ee5489ee1117 68
andrewboyson 5:ee5489ee1117 69 end:
andrewboyson 5:ee5489ee1117 70 zeroConnection(p);
andrewboyson 10:e269fd7b9500 71 p->id = id;
andrewboyson 5:ee5489ee1117 72 p->lastUsed = MsTimerCount;
andrewboyson 5:ee5489ee1117 73 return p;
andrewboyson 5:ee5489ee1117 74 }
andrewboyson 10:e269fd7b9500 75 struct TlsConnection* TlsConnectionOrNull(int id)
andrewboyson 5:ee5489ee1117 76 {
andrewboyson 5:ee5489ee1117 77 for (struct TlsConnection* p = connections; p < connections + MAX_CONNECTIONS; p++)
andrewboyson 5:ee5489ee1117 78 {
andrewboyson 10:e269fd7b9500 79 if (p->id == id)
andrewboyson 5:ee5489ee1117 80 {
andrewboyson 5:ee5489ee1117 81 p->lastUsed = MsTimerCount;
andrewboyson 5:ee5489ee1117 82 return p;
andrewboyson 5:ee5489ee1117 83 }
andrewboyson 5:ee5489ee1117 84 }
andrewboyson 5:ee5489ee1117 85 return NULL;
andrewboyson 5:ee5489ee1117 86 }
andrewboyson 10:e269fd7b9500 87 void TlsConnectionReset(int id)
andrewboyson 5:ee5489ee1117 88 {
andrewboyson 5:ee5489ee1117 89 for (struct TlsConnection* p = connections; p < connections + MAX_CONNECTIONS; p++)
andrewboyson 5:ee5489ee1117 90 {
andrewboyson 10:e269fd7b9500 91 if (p->id == id) zeroConnection(p);
andrewboyson 5:ee5489ee1117 92 }
andrewboyson 5:ee5489ee1117 93 }
andrewboyson 8:5e66a6b4b38c 94