ESP8266
ssl.h
1 /*
2  * Copyright (c) 2007, Cameron Rich
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * * Neither the name of the axTLS project nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
63 #ifndef HEADER_SSL_H
64 #define HEADER_SSL_H
65 
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69 
70 /* need to predefine before ssl_lib.h gets to it */
71 #define SSL_SESSION_ID_SIZE 32
72 
73 #define EXP_FUNC
74 #define STDCALL
75 // struct SSL_CTX_;
76 typedef struct SSL_CTX_ SSL_CTX;
77 typedef struct SSL_ SSL;
78 
79 /* The optional parameters that can be given to the client/server SSL engine */
80 #define SSL_CLIENT_AUTHENTICATION 0x00010000
81 #define SSL_SERVER_VERIFY_LATER 0x00020000
82 #define SSL_NO_DEFAULT_KEY 0x00040000
83 #define SSL_DISPLAY_STATES 0x00080000
84 #define SSL_DISPLAY_BYTES 0x00100000
85 #define SSL_DISPLAY_CERTS 0x00200000
86 #define SSL_DISPLAY_RSA 0x00400000
87 #define SSL_CONNECT_IN_PARTS 0x00800000
88 
89 /* errors that can be generated */
90 #define SSL_OK 0
91 #define SSL_NOT_OK -1
92 #define SSL_ERROR_DEAD -2
93 #define SSL_CLOSE_NOTIFY -3
94 #define SSL_ERROR_CONN_LOST -256
95 #define SSL_ERROR_SOCK_SETUP_FAILURE -258
96 #define SSL_ERROR_INVALID_HANDSHAKE -260
97 #define SSL_ERROR_INVALID_PROT_MSG -261
98 #define SSL_ERROR_INVALID_HMAC -262
99 #define SSL_ERROR_INVALID_VERSION -263
100 #define SSL_ERROR_INVALID_SESSION -265
101 #define SSL_ERROR_NO_CIPHER -266
102 #define SSL_ERROR_BAD_CERTIFICATE -268
103 #define SSL_ERROR_INVALID_KEY -269
104 #define SSL_ERROR_FINISHED_INVALID -271
105 #define SSL_ERROR_NO_CERT_DEFINED -272
106 #define SSL_ERROR_NO_CLIENT_RENOG -273
107 #define SSL_ERROR_NOT_SUPPORTED -274
108 #define SSL_X509_OFFSET -512
109 #define SSL_X509_ERROR(A) (SSL_X509_OFFSET+A)
110 
111 /* alert types that are recognized */
112 #define SSL_ALERT_TYPE_WARNING 1
113 #define SLL_ALERT_TYPE_FATAL 2
114 
115 /* these are all the alerts that are recognized */
116 #define SSL_ALERT_CLOSE_NOTIFY 0
117 #define SSL_ALERT_UNEXPECTED_MESSAGE 10
118 #define SSL_ALERT_BAD_RECORD_MAC 20
119 #define SSL_ALERT_HANDSHAKE_FAILURE 40
120 #define SSL_ALERT_BAD_CERTIFICATE 42
121 #define SSL_ALERT_ILLEGAL_PARAMETER 47
122 #define SSL_ALERT_DECODE_ERROR 50
123 #define SSL_ALERT_DECRYPT_ERROR 51
124 #define SSL_ALERT_INVALID_VERSION 70
125 #define SSL_ALERT_NO_RENEGOTIATION 100
126 
127 /* The ciphers that are supported */
128 #define SSL_AES128_SHA 0x2f
129 #define SSL_AES256_SHA 0x35
130 #define SSL_RC4_128_SHA 0x05
131 #define SSL_RC4_128_MD5 0x04
132 
133 /* build mode ids' */
134 #define SSL_BUILD_SKELETON_MODE 0x01
135 #define SSL_BUILD_SERVER_ONLY 0x02
136 #define SSL_BUILD_ENABLE_VERIFICATION 0x03
137 #define SSL_BUILD_ENABLE_CLIENT 0x04
138 #define SSL_BUILD_FULL_MODE 0x05
139 
140 /* offsets to retrieve configuration information */
141 #define SSL_BUILD_MODE 0
142 #define SSL_MAX_CERT_CFG_OFFSET 1
143 #define SSL_MAX_CA_CERT_CFG_OFFSET 2
144 #define SSL_HAS_PEM 3
145 
146 /* default session sizes */
147 #define SSL_DEFAULT_SVR_SESS 5
148 #define SSL_DEFAULT_CLNT_SESS 1
149 
150 /* X.509/X.520 distinguished name types */
151 #define SSL_X509_CERT_COMMON_NAME 0
152 #define SSL_X509_CERT_ORGANIZATION 1
153 #define SSL_X509_CERT_ORGANIZATIONAL_NAME 2
154 #define SSL_X509_CA_CERT_COMMON_NAME 3
155 #define SSL_X509_CA_CERT_ORGANIZATION 4
156 #define SSL_X509_CA_CERT_ORGANIZATIONAL_NAME 5
157 
158 /* SSL object loader types */
159 #define SSL_OBJ_X509_CERT 1
160 #define SSL_OBJ_X509_CACERT 2
161 #define SSL_OBJ_RSA_KEY 3
162 #define SSL_OBJ_PKCS8 4
163 #define SSL_OBJ_PKCS12 5
164 
209 EXP_FUNC SSL_CTX * STDCALL ssl_ctx_new(uint32_t options, int num_sessions);
210 
218 EXP_FUNC void STDCALL ssl_ctx_free(SSL_CTX *ssl_ctx);
219 
229 EXP_FUNC SSL * STDCALL ssl_server_new(SSL_CTX *ssl_ctx, int client_fd);
230 
249 EXP_FUNC SSL * STDCALL ssl_client_new(SSL_CTX *ssl_ctx, int client_fd, const uint8_t *session_id, uint8_t sess_id_size);
250 
258 EXP_FUNC void STDCALL ssl_free(SSL *ssl);
259 
276 EXP_FUNC int STDCALL ssl_read(SSL *ssl, uint8_t **in_data);
277 
288 EXP_FUNC int STDCALL ssl_write(SSL *ssl, const uint8_t *out_data, int out_len);
289 
300 EXP_FUNC SSL * STDCALL ssl_find(SSL_CTX *ssl_ctx, int client_fd);
301 
311 EXP_FUNC const uint8_t * STDCALL ssl_get_session_id(const SSL *ssl);
312 
320 EXP_FUNC uint8_t STDCALL ssl_get_session_id_size(const SSL *ssl);
321 
331 EXP_FUNC uint8_t STDCALL ssl_get_cipher_id(const SSL *ssl);
332 
339 EXP_FUNC int STDCALL ssl_handshake_status(const SSL *ssl);
340 
355 EXP_FUNC int STDCALL ssl_get_config(int offset);
356 
364 EXP_FUNC void STDCALL ssl_display_error(int error_code);
365 
374 EXP_FUNC int STDCALL ssl_verify_cert(const SSL *ssl);
375 
383 EXP_FUNC int STDCALL ssl_match_fingerprint(const SSL *ssl, const uint8_t* fp);
384 
405 EXP_FUNC const char * STDCALL ssl_get_cert_dn(const SSL *ssl, int component);
406 
421 EXP_FUNC const char * STDCALL ssl_get_cert_subject_alt_dnsname(const SSL *ssl, int dnsindex);
422 
434 EXP_FUNC int STDCALL ssl_renegotiate(SSL *ssl);
435 
456 EXP_FUNC int STDCALL ssl_obj_load(SSL_CTX *ssl_ctx, int obj_type, const char *filename, const char *password);
457 
471 EXP_FUNC int STDCALL ssl_obj_memory_load(SSL_CTX *ssl_ctx, int obj_type, const uint8_t *data, int len, const char *password);
472 
473 #ifdef CONFIG_SSL_GENERATE_X509_CERT
474 
496 EXP_FUNC int STDCALL ssl_x509_create(SSL_CTX *ssl_ctx, uint32_t options, const char * dn[], uint8_t **cert_data);
497 #endif
498 
502 EXP_FUNC const char * STDCALL ssl_version(void);
503 
506 #ifdef __cplusplus
507 }
508 #endif
509 
510 #endif
const char * ssl_get_cert_dn(const SSL *ssl, int component)
Retrieve an X.509 distinguished name component.
const uint8_t * ssl_get_session_id(const SSL *ssl)
Get the session id for a handshake.
int ssl_get_config(int offset)
Retrieve various parameters about the axTLS engine.
int ssl_match_fingerprint(const SSL *ssl, const uint8_t *fp)
Check if certificate fingerprint (SHA1) matches the one given.
int ssl_handshake_status(const SSL *ssl)
Return the status of the handshake.
uint8_t ssl_get_cipher_id(const SSL *ssl)
Return the cipher id (in the SSL form).
int ssl_renegotiate(SSL *ssl)
Force the client to perform its handshake again.
int ssl_read(SSL *ssl, uint8_t **in_data)
Read the SSL data stream. If the socket is non-blocking and data is blocked then SSO_OK will be retur...
SSL * ssl_client_new(SSL_CTX *ssl_ctx, int client_fd, const uint8_t *session_id, uint8_t sess_id_size)
(client only) Establish a new SSL connection to an SSL server.
void ssl_free(SSL *ssl)
Free any used resources on this connection.
int ssl_verify_cert(const SSL *ssl)
Authenticate a received certificate.
int ssl_write(SSL *ssl, const uint8_t *out_data, int out_len)
Write to the SSL data stream. if the socket is non-blocking and data is blocked then a check is made ...
SSL * ssl_find(SSL_CTX *ssl_ctx, int client_fd)
Find an ssl object based on a file descriptor.
uint8_t ssl_get_session_id_size(const SSL *ssl)
Get the session id size for a handshake.
void ssl_display_error(int error_code)
Display why the handshake failed.
const char * ssl_get_cert_subject_alt_dnsname(const SSL *ssl, int dnsindex)
Retrieve a Subject Alternative DNSName.
int ssl_obj_load(SSL_CTX *ssl_ctx, int obj_type, const char *filename, const char *password)
Process a file that is in binary DER or ASCII PEM format.
int ssl_obj_memory_load(SSL_CTX *ssl_ctx, int obj_type, const uint8_t *data, int len, const char *password)
Process binary data.
SSL * ssl_server_new(SSL_CTX *ssl_ctx, int client_fd)
(server only) Establish a new SSL connection to an SSL client.
const char * ssl_version(void)
Return the axTLS library version as a string.
SSL_CTX * ssl_ctx_new(uint32_t options, int num_sessions)
Establish a new client/server context.
void ssl_ctx_free(SSL_CTX *ssl_ctx)
Remove a client/server context.