xyssl_interface.h
12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
* \file xyssl_interface.h
*/
#ifndef XYSSL_INTERFACE_H
#define XYSSL_INTERFACE_H
#include "xyssl_ssl.h"
#include "xyssl_mem.h"
#include "xyssl_time.h"
/*
* Rteurn Errror Values from Xyssl Library
*/
#define XYSSL_ADP_ERR_SSL_FEATURE_UNAVAILABLE -0x1000
#define XYSSL_ADP_ERR_SSL_BAD_INPUT_DATA -0x1800
#define XYSSL_ADP_ERR_SSL_INVALID_MAC -0x2000
#define XYSSL_ADP_ERR_SSL_INVALID_RECORD -0x2800
#define XYSSL_ADP_ERR_SSL_INVALID_MODULUS_SIZE -0x3000
#define XYSSL_ADP_ERR_SSL_UNKNOWN_CIPHER -0x3800
#define XYSSL_ADP_ERR_SSL_NO_CIPHER_CHOSEN -0x4000
#define XYSSL_ADP_ERR_SSL_NO_SESSION_FOUND -0x4800
#define XYSSL_ADP_ERR_SSL_NO_CLIENT_CERTIFICATE -0x5000
#define XYSSL_ADP_ERR_SSL_CERTIFICATE_TOO_LARGE -0x5800
#define XYSSL_ADP_ERR_SSL_CERTIFICATE_REQUIRED -0x6000
#define XYSSL_ADP_ERR_SSL_PRIVATE_KEY_REQUIRED -0x6800
#define XYSSL_ADP_ERR_SSL_CA_CHAIN_REQUIRED -0x7000
#define XYSSL_ADP_ERR_SSL_UNEXPECTED_MESSAGE -0x7800
#define XYSSL_ADP_ERR_SSL_FATAL_ALERT_MESSAGE -0x8000
#define XYSSL_ADP_ERR_SSL_PEER_VERIFY_FAILED -0x8800
#define XYSSL_ADP_ERR_SSL_PEER_CLOSE_NOTIFY -0x9000
#define XYSSL_ADP_ERR_SSL_BAD_HS_CLIENT_HELLO -0x9800
#define XYSSL_ADP_ERR_SSL_BAD_HS_SERVER_HELLO -0xA000
#define XYSSL_ADP_ERR_SSL_BAD_HS_CERTIFICATE -0xA800
#define XYSSL_ADP_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST -0xB000
#define XYSSL_ADP_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE -0xB800
#define XYSSL_ADP_ERR_SSL_BAD_HS_SERVER_HELLO_DONE -0xC000
#define XYSSL_ADP_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE -0xC800
#define XYSSL_ADP_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY -0xD000
#define XYSSL_ADP_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC -0xD800
#define XYSSL_ADP_ERR_SSL_BAD_HS_FINISHED -0xE000
/*
* XYSSL VERIFY RESULT ERROR VALUES
*/
#define XYSSL_ADP_BADCERT_EXPIRED 0x01 /**< The certificate validity has expired. */
#define XYSSL_ADP_BADCERT_REVOKED 0x02 /**< The certificate has been revoked (is on a CRL). */
#define XYSSL_ADP_BADCERT_CN_MISMATCH 0x04 /**< The certificate Common Name (CN) does not match with the expected CN. */
#define XYSSL_ADP_BADCERT_NOT_TRUSTED 0x08 /**< The certificate is not correctly signed by the trusted CA. */
/*
* XYSSL X509 ERROR VALUES
*/
#define XYSSL_ADP_ERR_X509_FEATURE_UNAVAILABLE -0x0020
#define XYSSL_ADP_ERR_X509_CERT_INVALID_PEM -0x0040
#define XYSSL_ADP_ERR_X509_CERT_INVALID_FORMAT -0x0060
#define XYSSL_ADP_ERR_X509_CERT_INVALID_VERSION -0x0080
#define XYSSL_ADP_ERR_X509_CERT_INVALID_SERIAL -0x00A0
#define XYSSL_ADP_ERR_X509_CERT_INVALID_ALG -0x00C0
#define XYSSL_ADP_ERR_X509_CERT_INVALID_NAME -0x00E0
#define XYSSL_ADP_ERR_X509_CERT_INVALID_DATE -0x0100
#define XYSSL_ADP_ERR_X509_CERT_INVALID_PUBKEY -0x0120
#define XYSSL_ADP_ERR_X509_CERT_INVALID_SIGNATURE -0x0140
#define XYSSL_ADP_ERR_X509_CERT_INVALID_EXTENSIONS -0x0160
#define XYSSL_ADP_ERR_X509_CERT_UNKNOWN_VERSION -0x0180
#define XYSSL_ADP_ERR_X509_CERT_UNKNOWN_SIG_ALG -0x01A0
#define XYSSL_ADP_ERR_X509_CERT_UNKNOWN_PK_ALG -0x01C0
#define XYSSL_ADP_ERR_X509_CERT_SIG_MISMATCH -0x01E0
#define XYSSL_ADP_ERR_X509_CERT_VERIFY_FAILED -0x0200
#define XYSSL_ADP_ERR_X509_KEY_INVALID_PEM -0x0220
#define XYSSL_ADP_ERR_X509_KEY_INVALID_VERSION -0x0240
#define XYSSL_ADP_ERR_X509_KEY_INVALID_FORMAT -0x0260
#define XYSSL_ADP_ERR_X509_KEY_INVALID_ENC_IV -0x0280
#define XYSSL_ADP_ERR_X509_KEY_UNKNOWN_ENC_ALG -0x02A0
#define XYSSL_ADP_ERR_X509_KEY_PASSWORD_REQUIRED -0x02C0
#define XYSSL_ADP_ERR_X509_KEY_PASSWORD_MISMATCH -0x02E0
#define XYSSL_ADP_ERR_X509_POINT_ERROR -0x0300
#define XYSSL_ADP_ERR_X509_VALUE_TO_LENGTH -0x0320
/*
* XYSSL BASE64 ERROR VALUES
*/
#define XYSSL_ADP_ERR_BASE64_BUFFER_TOO_SMALL -0x0010
#define XYSSL_ADP_ERR_BASE64_INVALID_CHARACTER -0x0012
typedef struct
{
ssl_session xyssl_adp_ssn;
ssl_context xyssl_adp_cntx;
x509_cert xyssl_ca_chain;
}xyssl_adp_context_struct;
/*
* Various constants
*/
#define XYSSL_ADP_SSL_IS_CLIENT 0
#define XYSSL_ADP_SSL_IS_SERVER 1
#define XYSSL_ADP_SSL_COMPRESS_NULL 0
#define XYSSL_ADP_SSL_VERIFY_NONE 0
#define XYSSL_ADP_SSL_VERIFY_OPTIONAL 1
#define XYSSL_ADP_SSL_VERIFY_REQUIRED 2
/*********************************** XYSSL Interfaces ************************************/
/**
* \brief Initialize an SSL context
*
* \param ssl SSL context
* \param alloc Memory allocate callback function
* \param free Memory Free callback function
*
* \return 0 if successful, 1 if memory allocation failed ,-1 failed for some other reason
*/
int xyssl_adp_ssl_init( ssl_context *ssl, xyssl_adp_malloc_cb alloc , xyssl_adp_free_cb free ,void* user_data);
/**
* \brief Set the current endpoint type
*
* \param ssl SSL context
* \param endpoint must be SSL_IS_CLIENT or SSL_IS_SERVER
*/
void xyssl_adp_ssl_set_endpoint( ssl_context *ssl, int endpoint );
/**
* \brief Set the certificate verification mode
*
* \param ssl SSL context
* \param mode can be:
*
* SSL_VERIFY_NONE: peer certificate is not checked (default),
* this is insecure and SHOULD be avoided.
*
* SSL_VERIFY_OPTIONAL: peer certificate is checked, however the
* handshake continues even if verification failed;
* ssl_get_verify_result() can be called after the
* handshake is complete.
*
* SSL_VERIFY_REQUIRED: peer *must* present a valid certificate,
* handshake is aborted if verification failed.
*/
void xyssl_adp_ssl_set_authmode( ssl_context *ssl, int authmode );
/**
* \brief Set the underlying BIO read and write callbacks
*
* \param ssl SSL context
* \param f_recv read callback
* \param p_recv read parameter
* \param f_send write callback
* \param p_send write parameter
*/
void xyssl_adp_ssl_set_bio( ssl_context *ssl,
int (*f_recv)(void *, unsigned char *, int), void *p_recv,
int (*f_send)(void *, unsigned char *, int), void *p_send );
/**
* \brief Set the session resuming flag, timeout and data
*
* \param ssl SSL context
* \param resume if 0 (default), the session will not be resumed
* \param timeout session timeout in seconds, or 0 (no timeout)
* \param session session context
*/
void xyssl_adp_ssl_set_session( ssl_context *ssl, int resume, int timeout,
ssl_session *session );
/*****************************************************************************
* FUNCTION
* stk_tls_adp_md5_hash
* DESCRIPTION
* Calculate hash of input
* PARAMETERS
* None
* RETURNS
* None
*****************************************************************************/
void xyssl_adp_md5_hash(unsigned char *input, int ilen, unsigned char output[16]);
/**
* \brief Set the data required to verify peer certificate
*
* \param ssl SSL context
* \param ca_chain trusted CA chain
* \param peer_cn expected peer CommonName (or NULL)
*
* \note TODO: add two more parameters: depth and crl
*/
void xyssl_adp_ssl_set_ca_chain( ssl_context *ssl, x509_cert *ca_chain,
char *peer_cn );
/**
* \brief Return the name of the current cipher
*
* \param ssl SSL context
*
* \return a string containing the cipher name
*/
char *xyssl_adp_ssl_get_cipher( ssl_context *ssl );
/**
* \brief Perform the SSL handshake
*
* \param ssl SSL context
*
* \return 0 if successful, XYSSL_ERR_NET_TRY_AGAIN,
* or a specific SSL error code.
*/
int xyssl_adp_ssl_handshake( ssl_context *ssl );
/**
* \brief Read at most 'len' application data bytes
*
* \param ssl SSL context
* \param buf buffer that will hold the data
* \param len how many bytes must be read
*
* \return This function returns the number of bytes read,
* or a negative error code.
*/
int xyssl_adp_ssl_read( ssl_context *ssl, unsigned char *buf, int len );
/**
* \brief Write exactly 'len' application data bytes
*
* \param ssl SSL context
* \param buf buffer holding the data
* \param len how many bytes must be written
*
* \return This function returns the number of bytes written,
* or a negative error code.
*
* \note When this function returns XYSSL_ERR_NET_TRY_AGAIN,
* it must be called later with the *same* arguments,
* until it returns a positive value.
*/
int xyssl_adp_ssl_write( ssl_context *ssl, unsigned char *buf, int len );
/**
* \brief Notify the peer that the connection is being closed
*/
int xyssl_adp_ssl_close_notify( ssl_context *ssl );
/**
* \brief Free an SSL context
*/
void xyssl_adp_ssl_deinit( ssl_context *ssl );
/**
* \brief Encode a buffer into base64 format
*
* \param dst destination buffer
* \param dlen size of the buffer
* \param src source buffer
* \param slen amount of data to be encoded
*
* \return 0 if successful, or XYSSL_ERR_BASE64_BUFFER_TOO_SMALL.
* *dlen is always updated to reflect the amount
* of data that has (or would have) been written.
*
* \note Call this function with *dlen = 0 to obtain the
* required buffer size in *dlen
*/
int xyssl_adp_base64_encode( unsigned char *dst, int *dlen,
unsigned char *src, int slen );
/**
* \brief Decode a base64-formatted buffer
*
* \param dst destination buffer
* \param dlen size of the buffer
* \param src source buffer
* \param slen amount of data to be decoded
*
* \return 0 if successful, XYSSL_ERR_BASE64_BUFFER_TOO_SMALL, or
* XYSSL_ERR_BASE64_INVALID_DATA if the input data is not
* correct. *dlen is always updated to reflect the amount
* of data that has (or would have) been written.
*
* \note Call this function with *dlen = 0 to obtain the
* required buffer size in *dlen
*/
int xyxxl_adp_base64_decode( unsigned char *dst, int *dlen,
unsigned char *src, int slen );
/**
* \brief Output = HMAC-SHA-1( hmac key, input buffer )
*
* \param key HMAC secret key
* \param keylen length of the HMAC key
* \param input buffer holding the data
* \param ilen length of the input data
* \param output HMAC-SHA-1 result
*/
void xyssl_adp_sha1_hmac( unsigned char *key, int keylen,
unsigned char *input, int ilen,
unsigned char output[20] );
/**
* \brief Parse one or more certificates and add them
* to the chained list
*
* \param chain points to the start of the chain
* \param buf buffer holding the certificate data
* \param buflen size of the buffer
*
* \return 0 if successful, or a specific X509 error code
*/
int xyssl_adp_x509parse_crt(ssl_context *ssl, x509_cert *crt, unsigned char *buf, int buflen );
/**
* \brief Unallocate all certificate data
*/
void xyssl_adp_x509_free(ssl_context *ssl, x509_cert *crt );
/**
* \brief Return the result of the certificate verification
*
* \param ssl SSL context
*
* \return 0 if successful, or a combination of:
* XYSSL_ADP_BADCERT_EXPIRED
* XYSSL_ADP_BADCERT_REVOKED
* XYSSL_ADP_BADCERT_CN_MISMATCH
* XYSSL_ADP_BADCERT_NOT_TRUSTED
*/
int xyssl_adp_ssl_get_verify_result( ssl_context *ssl );
#endif //End XYSSL_INTERFACE_H