A simple library to support serving https.

Dependents:   oldheating gps motorhome heating

tls/tls-connection.h

Committer:
andrewboyson
Date:
2019-09-24
Revision:
10:e269fd7b9500
Parent:
8:5e66a6b4b38c
Child:
14:03a0b8fd6ddc

File content as of revision 10:e269fd7b9500:

#include <stdbool.h>
#include <stdint.h>

#include "sha256.h"

#define DO_WAIT_CLIENT_HELLO                0
#define DO_SEND_SERVER_HELLO                1
#define DO_WAIT_CLIENT_CHANGE               2
#define DO_WAIT_DECRYPT_MASTER_SECRET       3
#define DO_SEND_SERVER_CHANGE               4
#define DO_APPLICATION                      5
#define DO_SEND_ALERT_ILLEGAL_PARAMETER     6
#define DO_SEND_ALERT_INTERNAL_ERROR        7

#define TLS_RANDOM_SIZE 32
#define TLS_VERIFY_SIZE 64

#define TLS_KEY_SIZE_MAC    20
#define TLS_KEY_SIZE_WRITE  16
#define TLS_DEFERRED_CONTENT_SIZE 100

struct TlsConnection
{
    int                id; //An id of zero means the record is empty
    uint32_t           lastUsed;
    int                toDo;
    uint32_t           sessionId;
    struct Sha256State handshakeSha;
    bool               clientEncrypted;
    bool               serverEncrypted;
    uint8_t            clientRandom[TLS_RANDOM_SIZE];
    uint8_t            serverRandom[TLS_RANDOM_SIZE];
    uint8_t            clientHandshakeHash[SHA256_HASH_SIZE];
    uint8_t            deferredContent[TLS_DEFERRED_CONTENT_SIZE];
    uint64_t           clientSequence;
    uint64_t           serverSequence;
    uint32_t           clientPositionInStreamOffset;
    uint32_t           serverPositionInStreamOffset;
    
    uint8_t            clientMacKey  [TLS_KEY_SIZE_MAC  ];
    uint8_t            serverMacKey  [TLS_KEY_SIZE_MAC  ];
    uint8_t            clientWriteKey[TLS_KEY_SIZE_WRITE];
    uint8_t            serverWriteKey[TLS_KEY_SIZE_WRITE];    
};

extern struct TlsConnection* TlsConnectionGetNext(void);

extern struct TlsConnection* TlsConnectionNew   (int id); //Never fails so never returns NULL
extern struct TlsConnection* TlsConnectionOrNull(int id);
extern void                  TlsConnectionReset (int id);