NetBurner 3.5.6
PDF Version
NbSslCtx.h
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
5#ifndef _NB_SSL_CTX_H_
6#define _NB_SSL_CTX_H_
7
8#include <predef.h>
9#include <nbrtos.h>
10#include <basictypes.h>
11#include <crypto/ssl.h>
12
13#ifndef _WOLFSSL_METHOD_DEFINED
14#define _WOLFSSL_METHOD_DEFINED
15/* SSL Version */
16typedef struct ProtocolVersion {
17 uint8_t major;
18 uint8_t minor;
19} __attribute__((packed)) ProtocolVersion;
20
21/* wolfSSL method type */
22struct WOLFSSL_METHOD {
23 ProtocolVersion version;
24 uint8_t side; /* connection side, server or client */
25 uint8_t downgrade; /* whether to downgrade version, default no */
26};
27#endif
28
29enum VerifyPeer
30{
31 VerifyPeer_default,
32 VerifyPeer_off,
33 VerifyPeer_on,
34};
35
36enum SSL_CertType {
37 Cert_User,
38 Cert_CA,
39#ifdef WOLFSSL_TRUST_PEER_CERT
40 Cert_TrustedPeer,
41#endif
42#ifdef HAVE_CRL
43 Cert_CRL,
44#endif
45 Cert_PrivKey,
46 Cert_MAXTYPE
47};
48
49enum SSL_Encoding {
50 Encoding_PEM = SSL_FILETYPE_PEM,
51 Encoding_DER = SSL_FILETYPE_ASN1,
52 Encoding_ASN1= SSL_FILETYPE_ASN1
53};
54
55//#define NBSSLCTX_PROTOCOL__MAKE_ENUMS
56enum SSL_Method {
57 TLS,
58 TLS_Client,
59 TLS_Server,
60 TLSv1_2,
61 TLSv1_2_client,
62 TLSv1_2_server,
63#ifdef WOLFSSL_TLS13
64 TLSv1_3,
65 TLSv1_3_client,
66 TLSv1_3_server,
67#endif
68//#include <crypto/NetBurner/Wolf_Method.inc>
69};
70//#undef NBSSLCTX_PROTOCOL__MAKE_ENUMS
71
72class NbSslCtx
73{
74 enum LoadedBits {
75 Loaded_User = (1 << Cert_User),
76 Loaded_CA = (1 << Cert_CA),
77#ifdef WOLFSSL_TRUST_PEER_CERT
78 Loaded_Peer = (1 << Cert_TrustedPeer),
79#endif
80#ifdef HAVE_CRL
81 Loaded_CRL = (1 << Cert_CRL),
82#endif
83 Loaded_Key = (1 << Cert_PrivKey),
84
85 Loaded_ManualReq = (1 << (Cert_MAXTYPE+1)),
86 };
87 public:
88 NbSslCtx(uint8_t *CertBuf=NULL, uint32_t CertBufLen=0, SSL_CertType type=Cert_User,SSL_Encoding encoding=Encoding_PEM);
89 NbSslCtx(SSL_Method method, uint8_t *CertBuf=NULL, uint32_t CertBufLen=0, SSL_CertType type=Cert_User,SSL_Encoding encoding=Encoding_PEM);
90 int Init();
91
92 WOLFSSL_CTX* GetCtx(){ return m_wolfCtx; }
93
94// void SetInit(bool init){ m_ctxInit = init; }
95 bool GetInit(){ return m_wolfCtx != NULL; }
96
97 inline bool HaveCert_CA(){ return m_certsLoaded & Loaded_CA; }
98 inline bool HaveCert_User(){ return m_certsLoaded & Loaded_User; }
99#ifdef WOLFSSL_TRUST_PEER_CERT
100 inline bool HaveCert_Peer(){ return m_certsLoaded & Loaded_Peer; }
101#endif
102#ifdef HAVE_CRL
103 inline bool HaveCert_CRL(){ return m_certsLoaded & Loaded_CRL; }
104#endif
105// void SetCaSet(bool set){ m_ctxCaSet = set; }
106
107// VerifyPeer GetVerifyPeer(){ return m_verifyPeer; }
108// void SetVerifyPeer( VerifyPeer verifYPeer ){ m_verifyPeer = verifYPeer; }
109
110 int AddCA(const uint8_t *CaBuf, uint32_t CaBufLen, SSL_Encoding encoding=Encoding_PEM);
111 int UseCert(const uint8_t *certBuf, uint32_t certLen, SSL_Encoding encoding=Encoding_PEM);
112 int UseKey(const uint8_t *certBuf, uint32_t certLen, SSL_Encoding encoding=Encoding_PEM);
113 int CheckKey();
114#ifdef WOLFSSL_TRUST_PEER_CERT
115 int AddPeer(const uint8_t *PeerCertBuf, uint32_t PeerCertBufLen, SSL_Encoding encoding=Encoding_PEM);
116#endif
117#ifdef HAVE_CRL
118 int AddCRL(const uint8_t *CRLBuf, uint32_t CRLBufLen, SSL_Encoding encoding=Encoding_PEM);
119#endif
120
121 int UnloadCAs();
122 int UnloadUserCertAndKey();
123#ifdef WOLFSSL_TRUST_PEER_CERT
124 int UnloadPeers();
125#endif
126#ifdef HAVE_CRL
127 int UnloadCRLs();
128#endif
129
130 int ResetStores();
131
132 int RequireCert(bool required);
133
134 int Connect(IPADDR ip, uint16_t remotePort,
135 const TickTimeout &timeout, const char *commonName,
136 int intf = -1, int verifyPeer = -1);
137 int AsyncConnect(IPADDR ip, uint16_t remotePort,
138 const char *commonName, int intf = -1,
139 int verifyPeer = -1);
140 int Accept(int fdListen, IPADDR *address,
141 uint16_t *port, const TickTimeout &timeout,
142 const char *commonName, int verifyPeer = -1);
143 int AsyncAccept(int fdListen, IPADDR *address,
144 uint16_t *port, const TickTimeout &timeout,
145 const char *commonName, int verifyPeer = -1);
146
147 inline int Connect(IPADDR ip, uint16_t remotePort,
148 uint32_t timeout, const char *commonName,
149 int intf = -1, int verifyPeer = -1)
150 {
151 TickTimeout tt(timeout);
152 return Connect(ip, remotePort, tt, commonName, intf, verifyPeer);
153 }
154 inline int Accept(int fdListen, IPADDR *address,
155 uint16_t *port, uint32_t timeout,
156 const char *commonName, int verifyPeer = -1)
157 {
158 TickTimeout tt(timeout);
159 return Accept(fdListen, address, port, tt, commonName, verifyPeer);
160 }
161 inline int AsyncAccept(int fdListen, IPADDR *address,
162 uint16_t *port, uint32_t timeout,
163 const char *commonName, int verifyPeer = -1)
164 {
165 TickTimeout tt(timeout);
166 return AsyncAccept(fdListen, address, port, tt, commonName, verifyPeer);
167 }
168
169 int MakeSecure(int tcpFd, const TickTimeout &timeout,
170 const char *commonName, bool bIsClient = true,
171 int verifyPeer = -1);
172 int AsyncMakeSecure(int tcpFd, const char *commonName,
173 bool bIsClient = true, int verifyPeer = -1);
174
175 inline int MakeSecure(int tcpFd, uint32_t timeout,
176 const char *commonName, bool bIsClient = true,
177 int verifyPeer = -1)
178 {
179 TickTimeout tt(timeout);
180 return MakeSecure(tcpFd, tt, commonName, verifyPeer);
181 }
182
183 int Negotiate(int tcpFd, const char *commonName,
184 bool bIsClient = true, bool reuseSession = true,
185 int verifyPeer = -1);
186
187 private:
188 WOLFSSL_CTX* m_wolfCtx = nullptr;
189 WOLFSSL_METHOD m_wolfMethod;
190 uint8_t *m_initCertBuf;
191 union {
192 uint32_t m_initCertLen;
193 uint32_t m_certsLoaded;
194 };
195 SSL_CertType m_initCertType;
196 SSL_Encoding m_initEncoding;
197
198 static const WOLFSSL_METHOD protocolMethods[];
199 friend class LockObj;
200};
201
202extern NbSslCtx SSL_gServerCtx;
203extern NbSslCtx SSL_gClientCtx;
204#endif /* _NB_SSL_CTX_H_ */
Used to hold and manipulate IPv4 and IPv6 addresses in dual stack mode.
Definition ipv6_addr.h:41
TickTimeout objects are used to facilitate sequential function calls with timeout parameters that nee...
Definition nbrtos.h:168
NetBurner Real-Time Operating System (NBRTOS) API.
NetBurner SSL/TLS API.