Add axTLS sourcecode
[project/luci.git] / libs / nixio / axTLS / ssl / tls1.c
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
31 /**
32  * Common ssl/tlsv1 code to both the client and server implementations.
33  */
34
35 #include <string.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <stdarg.h>
39 #include "ssl.h"
40
41 /* The session expiry time */
42 #define SSL_EXPIRY_TIME     (CONFIG_SSL_EXPIRY_TIME*3600)
43
44 static const uint8_t g_hello_request[] = { HS_HELLO_REQUEST, 0, 0, 0 };
45 static const uint8_t g_chg_cipher_spec_pkt[] = { 1 };
46 static const char * server_finished = "server finished";
47 static const char * client_finished = "client finished";
48
49 static int do_handshake(SSL *ssl, uint8_t *buf, int read_len);
50 static void set_key_block(SSL *ssl, int is_write);
51 static int verify_digest(SSL *ssl, int mode, const uint8_t *buf, int read_len);
52 static void *crypt_new(SSL *ssl, uint8_t *key, uint8_t *iv, int is_decrypt);
53 static int send_raw_packet(SSL *ssl, uint8_t protocol);
54
55 /**
56  * The server will pick the cipher based on the order that the order that the
57  * ciphers are listed. This order is defined at compile time.
58  */
59 #ifdef CONFIG_SSL_SKELETON_MODE
60 const uint8_t ssl_prot_prefs[NUM_PROTOCOLS] = 
61 { SSL_RC4_128_SHA };
62 #else
63 static void session_free(SSL_SESSION *ssl_sessions[], int sess_index);
64
65 const uint8_t ssl_prot_prefs[NUM_PROTOCOLS] = 
66 #ifdef CONFIG_SSL_PROT_LOW                  /* low security, fast speed */
67 { SSL_RC4_128_SHA, SSL_AES128_SHA, SSL_AES256_SHA, SSL_RC4_128_MD5 };
68 #elif CONFIG_SSL_PROT_MEDIUM                /* medium security, medium speed */
69 { SSL_AES128_SHA, SSL_AES256_SHA, SSL_RC4_128_SHA, SSL_RC4_128_MD5 };    
70 #else /* CONFIG_SSL_PROT_HIGH */            /* high security, low speed */
71 { SSL_AES256_SHA, SSL_AES128_SHA, SSL_RC4_128_SHA, SSL_RC4_128_MD5 };
72 #endif
73 #endif /* CONFIG_SSL_SKELETON_MODE */
74
75 /**
76  * The cipher map containing all the essentials for each cipher.
77  */
78 #ifdef CONFIG_SSL_SKELETON_MODE
79 static const cipher_info_t cipher_info[NUM_PROTOCOLS] = 
80 {
81     {   /* RC4-SHA */
82         SSL_RC4_128_SHA,                /* RC4-SHA */
83         16,                             /* key size */
84         0,                              /* iv size */ 
85         2*(SHA1_SIZE+16),               /* key block size */
86         0,                              /* no padding */
87         SHA1_SIZE,                      /* digest size */
88         hmac_sha1,                      /* hmac algorithm */
89         (crypt_func)RC4_crypt,          /* encrypt */
90         (crypt_func)RC4_crypt           /* decrypt */
91     },
92 };
93 #else
94 static const cipher_info_t cipher_info[NUM_PROTOCOLS] = 
95 {
96     {   /* AES128-SHA */
97         SSL_AES128_SHA,                 /* AES128-SHA */
98         16,                             /* key size */
99         16,                             /* iv size */ 
100         2*(SHA1_SIZE+16+16),            /* key block size */
101         16,                             /* block padding size */
102         SHA1_SIZE,                      /* digest size */
103         hmac_sha1,                      /* hmac algorithm */
104         (crypt_func)AES_cbc_encrypt,    /* encrypt */
105         (crypt_func)AES_cbc_decrypt     /* decrypt */
106     },
107     {   /* AES256-SHA */
108         SSL_AES256_SHA,                 /* AES256-SHA */
109         32,                             /* key size */
110         16,                             /* iv size */ 
111         2*(SHA1_SIZE+32+16),            /* key block size */
112         16,                             /* block padding size */
113         SHA1_SIZE,                      /* digest size */
114         hmac_sha1,                      /* hmac algorithm */
115         (crypt_func)AES_cbc_encrypt,    /* encrypt */
116         (crypt_func)AES_cbc_decrypt     /* decrypt */
117     },       
118     {   /* RC4-SHA */
119         SSL_RC4_128_SHA,                /* RC4-SHA */
120         16,                             /* key size */
121         0,                              /* iv size */ 
122         2*(SHA1_SIZE+16),               /* key block size */
123         0,                              /* no padding */
124         SHA1_SIZE,                      /* digest size */
125         hmac_sha1,                      /* hmac algorithm */
126         (crypt_func)RC4_crypt,          /* encrypt */
127         (crypt_func)RC4_crypt           /* decrypt */
128     },
129     /*
130      * This protocol is from SSLv2 days and is unlikely to be used - but was
131      * useful for testing different possible digest algorithms.
132      */
133     {   /* RC4-MD5 */
134         SSL_RC4_128_MD5,                /* RC4-MD5 */
135         16,                             /* key size */
136         0,                              /* iv size */ 
137         2*(MD5_SIZE+16),                /* key block size */
138         0,                              /* no padding */
139         MD5_SIZE,                       /* digest size */
140         hmac_md5,                       /* hmac algorithm */
141         (crypt_func)RC4_crypt,          /* encrypt */
142         (crypt_func)RC4_crypt           /* decrypt */
143     },
144 };
145 #endif
146
147 static void prf(const uint8_t *sec, int sec_len, uint8_t *seed, int seed_len,
148         uint8_t *out, int olen);
149 static const cipher_info_t *get_cipher_info(uint8_t cipher);
150 static void increment_read_sequence(SSL *ssl);
151 static void increment_write_sequence(SSL *ssl);
152 static void add_hmac_digest(SSL *ssl, int snd, uint8_t *hmac_header,
153         const uint8_t *buf, int buf_len, uint8_t *hmac_buf);
154
155 /* win32 VC6.0 doesn't have variadic macros */
156 #if defined(WIN32) && !defined(CONFIG_SSL_FULL_MODE)
157 void DISPLAY_BYTES(SSL *ssl, const char *format, 
158         const uint8_t *data, int size, ...) {}
159 #endif
160
161 /**
162  * Establish a new client/server context.
163  */
164 EXP_FUNC SSL_CTX *STDCALL ssl_ctx_new(uint32_t options, int num_sessions)
165 {
166     SSL_CTX *ssl_ctx = (SSL_CTX *)calloc(1, sizeof (SSL_CTX));
167     ssl_ctx->options = options;
168
169     if (load_key_certs(ssl_ctx) < 0)
170     {
171         free(ssl_ctx);  /* can't load our key/certificate pair, so die */
172         return NULL;
173     }
174
175 #ifndef CONFIG_SSL_SKELETON_MODE
176     ssl_ctx->num_sessions = num_sessions;
177 #endif
178
179     SSL_CTX_MUTEX_INIT(ssl_ctx->mutex);
180
181 #ifndef CONFIG_SSL_SKELETON_MODE
182     if (num_sessions)
183     {
184         ssl_ctx->ssl_sessions = (SSL_SESSION **)
185                         calloc(1, num_sessions*sizeof(SSL_SESSION *));
186     }
187 #endif
188
189     return ssl_ctx;
190 }
191
192 /*
193  * Remove a client/server context.
194  */
195 EXP_FUNC void STDCALL ssl_ctx_free(SSL_CTX *ssl_ctx)
196 {
197     SSL *ssl;
198     int i;
199
200     if (ssl_ctx == NULL)
201         return;
202
203     ssl = ssl_ctx->head;
204
205     /* clear out all the ssl entries */
206     while (ssl)
207     {
208         SSL *next = ssl->next;
209         ssl_free(ssl);
210         ssl = next;
211     }
212
213 #ifndef CONFIG_SSL_SKELETON_MODE
214     /* clear out all the sessions */
215     for (i = 0; i < ssl_ctx->num_sessions; i++)
216         session_free(ssl_ctx->ssl_sessions, i);
217
218     free(ssl_ctx->ssl_sessions);
219 #endif
220
221     i = 0;
222     while (i < CONFIG_SSL_MAX_CERTS && ssl_ctx->certs[i].buf)
223     {
224         free(ssl_ctx->certs[i].buf);
225         ssl_ctx->certs[i++].buf = NULL;
226     }
227
228 #ifdef CONFIG_SSL_CERT_VERIFICATION
229     remove_ca_certs(ssl_ctx->ca_cert_ctx);
230 #endif
231     ssl_ctx->chain_length = 0;
232     SSL_CTX_MUTEX_DESTROY(ssl_ctx->mutex);
233     RSA_free(ssl_ctx->rsa_ctx);
234     RNG_terminate();
235     free(ssl_ctx);
236 }
237
238 /*
239  * Free any used resources used by this connection.
240  */
241 EXP_FUNC void STDCALL ssl_free(SSL *ssl)
242 {
243     SSL_CTX *ssl_ctx;
244
245     if (ssl == NULL)        /* just ignore null pointers */
246         return;
247
248     /* spec says we must notify when we are dying */
249     send_alert(ssl, SSL_ALERT_CLOSE_NOTIFY);
250
251     ssl_ctx = ssl->ssl_ctx;
252
253     SSL_CTX_LOCK(ssl_ctx->mutex);
254
255     /* adjust the server SSL list */
256     if (ssl->prev)
257         ssl->prev->next = ssl->next;
258     else
259         ssl_ctx->head = ssl->next;
260
261     if (ssl->next)
262         ssl->next->prev = ssl->prev;
263     else
264         ssl_ctx->tail = ssl->prev;
265
266     SSL_CTX_UNLOCK(ssl_ctx->mutex);
267
268     /* may already be free - but be sure */
269     free(ssl->encrypt_ctx);
270     free(ssl->decrypt_ctx);
271     disposable_free(ssl);
272 #ifdef CONFIG_SSL_CERT_VERIFICATION
273     x509_free(ssl->x509_ctx);
274 #endif
275
276     free(ssl);
277 }
278
279 /*
280  * Read the SSL connection and send any alerts for various errors.
281  */
282 EXP_FUNC int STDCALL ssl_read(SSL *ssl, uint8_t **in_data)
283 {
284     int ret = basic_read(ssl, in_data);
285
286     /* check for return code so we can send an alert */
287     if (ret < SSL_OK)
288     {
289         if (ret != SSL_ERROR_CONN_LOST)
290         {
291             send_alert(ssl, ret);
292 #ifndef CONFIG_SSL_SKELETON_MODE
293             /* something nasty happened, so get rid of this session */
294             kill_ssl_session(ssl->ssl_ctx->ssl_sessions, ssl);
295 #endif
296         }
297     }
298
299     return ret;
300 }
301
302 /*
303  * Write application data to the client
304  */
305 EXP_FUNC int STDCALL ssl_write(SSL *ssl, const uint8_t *out_data, int out_len)
306 {
307     int n = out_len, nw, i, tot = 0;
308
309     /* maximum size of a TLS packet is around 16kB, so fragment */
310     do 
311     {
312         nw = n;
313
314         if (nw > RT_MAX_PLAIN_LENGTH)    /* fragment if necessary */
315             nw = RT_MAX_PLAIN_LENGTH;
316
317         if ((i = send_packet(ssl, PT_APP_PROTOCOL_DATA, 
318                                             &out_data[tot], nw)) <= 0)
319         {
320             out_len = i;    /* an error */
321             break;
322         }
323
324         tot += i;
325         n -= i;
326     } while (n > 0);
327
328     return out_len;
329 }
330
331 /**
332  * Add a certificate to the certificate chain.
333  */
334 int add_cert(SSL_CTX *ssl_ctx, const uint8_t *buf, int len)
335 {
336     int ret = SSL_ERROR_NO_CERT_DEFINED, i = 0;
337     SSL_CERT *ssl_cert;
338     X509_CTX *cert = NULL;
339     int offset;
340
341     while (ssl_ctx->certs[i].buf && i < CONFIG_SSL_MAX_CERTS) 
342         i++;
343
344     if (i == CONFIG_SSL_MAX_CERTS) /* too many certs */
345     {
346 #ifdef CONFIG_SSL_FULL_MODE
347         printf("Error: maximum number of certs added - change of "
348                 "compile-time configuration required\n");
349 #endif
350         goto error;
351     }
352
353     if ((ret = x509_new(buf, &offset, &cert)))
354         goto error;
355
356 #if defined (CONFIG_SSL_FULL_MODE)
357     if (ssl_ctx->options & SSL_DISPLAY_CERTS)
358         x509_print(cert, NULL);
359 #endif
360
361     ssl_cert = &ssl_ctx->certs[i];
362     ssl_cert->size = len;
363     ssl_cert->buf = (uint8_t *)malloc(len);
364     memcpy(ssl_cert->buf, buf, len);
365     ssl_ctx->chain_length++;
366     len -= offset;
367     ret = SSL_OK;           /* ok so far */
368
369     /* recurse? */
370     if (len > 0)
371     {
372         ret = add_cert(ssl_ctx, &buf[offset], len);
373     }
374
375 error:
376     x509_free(cert);        /* don't need anymore */
377     return ret;
378 }
379
380 #ifdef CONFIG_SSL_CERT_VERIFICATION
381 /**
382  * Add a certificate authority.
383  */
384 int add_cert_auth(SSL_CTX *ssl_ctx, const uint8_t *buf, int len)
385 {
386     int ret = SSL_ERROR_NO_CERT_DEFINED;
387     int i = 0;
388     int offset;
389     CA_CERT_CTX *ca_cert_ctx;
390
391     if (ssl_ctx->ca_cert_ctx == NULL)
392         ssl_ctx->ca_cert_ctx = (CA_CERT_CTX *)calloc(1, sizeof(CA_CERT_CTX));
393
394     ca_cert_ctx = ssl_ctx->ca_cert_ctx;
395
396     while (i < CONFIG_X509_MAX_CA_CERTS && ca_cert_ctx->cert[i]) 
397         i++;
398
399     if (i > CONFIG_X509_MAX_CA_CERTS)
400     {
401 #ifdef CONFIG_SSL_FULL_MODE
402         printf("Error: maximum number of CA certs added - change of "
403                 "compile-time configuration required\n");
404 #endif
405         goto error;
406     }
407
408     if ((ret = x509_new(buf, &offset, &ca_cert_ctx->cert[i])))
409         goto error;
410
411     len -= offset;
412     ret = SSL_OK;           /* ok so far */
413
414     /* recurse? */
415     if (len > 0)
416         ret = add_cert_auth(ssl_ctx, &buf[offset], len);
417
418 error:
419     return ret;
420 }
421
422 /*
423  * Retrieve an X.509 distinguished name component
424  */
425 EXP_FUNC const char * STDCALL ssl_get_cert_dn(const SSL *ssl, int component)
426 {
427     if (ssl->x509_ctx == NULL)
428         return NULL;
429
430     switch (component)
431     {
432         case SSL_X509_CERT_COMMON_NAME:
433             return ssl->x509_ctx->cert_dn[X509_COMMON_NAME];
434
435         case SSL_X509_CERT_ORGANIZATION:
436             return ssl->x509_ctx->cert_dn[X509_ORGANIZATION];
437
438         case SSL_X509_CERT_ORGANIZATIONAL_NAME:       
439             return ssl->x509_ctx->cert_dn[X509_ORGANIZATIONAL_UNIT];
440
441         case SSL_X509_CA_CERT_COMMON_NAME:
442             return ssl->x509_ctx->ca_cert_dn[X509_COMMON_NAME];
443
444         case SSL_X509_CA_CERT_ORGANIZATION:
445             return ssl->x509_ctx->ca_cert_dn[X509_ORGANIZATION];
446
447         case SSL_X509_CA_CERT_ORGANIZATIONAL_NAME:       
448             return ssl->x509_ctx->ca_cert_dn[X509_ORGANIZATIONAL_UNIT];
449
450         default:
451             return NULL;
452     }
453 }
454
455 #endif
456
457 /*
458  * Find an ssl object based on the client's file descriptor.
459  */
460 EXP_FUNC SSL * STDCALL ssl_find(SSL_CTX *ssl_ctx, int client_fd)
461 {
462     SSL *ssl;
463
464     SSL_CTX_LOCK(ssl_ctx->mutex);
465     ssl = ssl_ctx->head;
466
467     /* search through all the ssl entries */
468     while (ssl)
469     {
470         if (ssl->client_fd == client_fd)
471         {
472             SSL_CTX_UNLOCK(ssl_ctx->mutex);
473             return ssl;
474         }
475
476         ssl = ssl->next;
477     }
478
479     SSL_CTX_UNLOCK(ssl_ctx->mutex);
480     return NULL;
481 }
482
483 /*
484  * Force the client to perform its handshake again.
485  */
486 EXP_FUNC int STDCALL ssl_renegotiate(SSL *ssl)
487 {
488     int ret = SSL_OK;
489
490     disposable_new(ssl);
491 #ifdef CONFIG_SSL_ENABLE_CLIENT
492     if (IS_SET_SSL_FLAG(SSL_IS_CLIENT))
493     {
494         ret = do_client_connect(ssl);
495     }
496     else
497 #endif
498     {
499         send_packet(ssl, PT_HANDSHAKE_PROTOCOL, 
500                 g_hello_request, sizeof(g_hello_request));
501         SET_SSL_FLAG(SSL_NEED_RECORD);
502     }
503
504     return ret;
505 }
506
507 /**
508  * @brief Get what we need for key info.
509  * @param cipher    [in]    The cipher information we are after
510  * @param key_size  [out]   The key size for the cipher
511  * @param iv_size   [out]   The iv size for the cipher
512  * @return  The amount of key information we need.
513  */
514 static const cipher_info_t *get_cipher_info(uint8_t cipher)
515 {
516     int i;
517
518     for (i = 0; i < NUM_PROTOCOLS; i++)
519     {
520         if (cipher_info[i].cipher == cipher)
521         {
522             return &cipher_info[i];
523         }
524     }
525
526     return NULL;  /* error */
527 }
528
529 /*
530  * Get a new ssl context for a new connection.
531  */
532 SSL *ssl_new(SSL_CTX *ssl_ctx, int client_fd)
533 {
534     SSL *ssl = (SSL *)calloc(1, sizeof(SSL));
535     ssl->ssl_ctx = ssl_ctx;
536     ssl->need_bytes = SSL_RECORD_SIZE;      /* need a record */
537     ssl->client_fd = client_fd;
538     ssl->flag = SSL_NEED_RECORD;
539     ssl->bm_data = ssl->bm_all_data+BM_RECORD_OFFSET; /* space at the start */
540     ssl->hs_status = SSL_NOT_OK;            /* not connected */
541 #ifdef CONFIG_ENABLE_VERIFICATION
542     ssl->ca_cert_ctx = ssl_ctx->ca_cert_ctx;
543 #endif
544     disposable_new(ssl);
545
546     /* a bit hacky but saves a few bytes of memory */
547     ssl->flag |= ssl_ctx->options;
548     SSL_CTX_LOCK(ssl_ctx->mutex);
549
550     if (ssl_ctx->head == NULL)
551     {
552         ssl_ctx->head = ssl;
553         ssl_ctx->tail = ssl;
554     }
555     else
556     {
557         ssl->prev = ssl_ctx->tail;
558         ssl_ctx->tail->next = ssl;
559         ssl_ctx->tail = ssl;
560     }
561
562     SSL_CTX_UNLOCK(ssl_ctx->mutex);
563     return ssl;
564 }
565
566 /*
567  * Add a private key to a context.
568  */
569 int add_private_key(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj)
570 {
571     int ret = SSL_OK;
572
573     /* get the private key details */
574     if (asn1_get_private_key(ssl_obj->buf, ssl_obj->len, &ssl_ctx->rsa_ctx))
575     {
576         ret = SSL_ERROR_INVALID_KEY;
577         goto error;
578     }
579
580 error:
581     return ret;
582 }
583
584 /** 
585  * Increment the read sequence number (as a 64 bit endian indepenent #)
586  */     
587 static void increment_read_sequence(SSL *ssl)
588 {
589     int i;
590
591     for (i = 7; i >= 0; i--) 
592     {       
593         if (++ssl->read_sequence[i])
594             break;
595     }
596 }
597             
598 /**
599  * Increment the read sequence number (as a 64 bit endian indepenent #)
600  */      
601 static void increment_write_sequence(SSL *ssl)
602 {        
603     int i;                  
604          
605     for (i = 7; i >= 0; i--)
606     {                       
607         if (++ssl->write_sequence[i])
608             break;
609     }                       
610 }
611
612 /**
613  * Work out the HMAC digest in a packet.
614  */
615 static void add_hmac_digest(SSL *ssl, int mode, uint8_t *hmac_header,
616         const uint8_t *buf, int buf_len, uint8_t *hmac_buf)
617 {
618     int hmac_len = buf_len + 8 + SSL_RECORD_SIZE;
619     uint8_t *t_buf = (uint8_t *)alloca(hmac_len+10);
620
621     memcpy(t_buf, (mode == SSL_SERVER_WRITE || mode == SSL_CLIENT_WRITE) ? 
622                     ssl->write_sequence : ssl->read_sequence, 8);
623     memcpy(&t_buf[8], hmac_header, SSL_RECORD_SIZE);
624     memcpy(&t_buf[8+SSL_RECORD_SIZE], buf, buf_len);
625
626     ssl->cipher_info->hmac(t_buf, hmac_len, 
627             (mode == SSL_SERVER_WRITE || mode == SSL_CLIENT_READ) ? 
628                 ssl->server_mac : ssl->client_mac, 
629             ssl->cipher_info->digest_size, hmac_buf);
630
631 #if 0
632     print_blob("record", ssl->hmac_tx, SSL_RECORD_SIZE);
633     print_blob("buf", buf, buf_len);
634     if (mode == SSL_SERVER_WRITE || mode == SSL_CLIENT_WRITE)
635     {
636         print_blob("write seq", ssl->write_sequence, 8);
637     }
638     else
639     {
640         print_blob("read seq", ssl->read_sequence, 8);
641     }
642
643     if (mode == SSL_SERVER_WRITE || mode == SSL_CLIENT_READ)
644     {
645         print_blob("server mac", 
646                 ssl->server_mac, ssl->cipher_info->digest_size);
647     }
648     else
649     {
650         print_blob("client mac", 
651                 ssl->client_mac, ssl->cipher_info->digest_size);
652     }
653     print_blob("hmac", hmac_buf, SHA1_SIZE);
654 #endif
655 }
656
657 /**
658  * Verify that the digest of a packet is correct.
659  */
660 static int verify_digest(SSL *ssl, int mode, const uint8_t *buf, int read_len)
661 {   
662     uint8_t hmac_buf[SHA1_SIZE];
663     int hmac_offset;
664    
665     if (ssl->cipher_info->padding_size)
666     {
667         hmac_offset = read_len-buf[read_len-1]-ssl->cipher_info->digest_size-1;
668     }
669     else
670     {
671         hmac_offset = read_len - ssl->cipher_info->digest_size;
672     }
673
674     /* sanity check the offset */
675     if (hmac_offset < 0)
676     {
677         return SSL_ERROR_INVALID_HMAC;
678     }
679
680     ssl->hmac_header[3] = hmac_offset >> 8;      /* insert size */
681     ssl->hmac_header[4] = hmac_offset & 0xff;
682     add_hmac_digest(ssl, mode, ssl->hmac_header, buf, hmac_offset, hmac_buf);
683
684     if (memcmp(hmac_buf, &buf[hmac_offset], ssl->cipher_info->digest_size))
685     {
686         return SSL_ERROR_INVALID_HMAC;
687     }
688
689     return hmac_offset;
690 }
691
692 /**
693  * Add a packet to the end of our sent and received packets, so that we may use
694  * it to calculate the hash at the end.
695  */
696 void add_packet(SSL *ssl, const uint8_t *pkt, int len)
697 {
698     MD5_Update(&ssl->dc->md5_ctx, pkt, len);
699     SHA1_Update(&ssl->dc->sha1_ctx, pkt, len);
700 }
701
702 /**
703  * Work out the MD5 PRF.
704  */
705 static void p_hash_md5(const uint8_t *sec, int sec_len, 
706         uint8_t *seed, int seed_len, uint8_t *out, int olen)
707 {
708     uint8_t a1[128];
709
710     /* A(1) */
711     hmac_md5(seed, seed_len, sec, sec_len, a1);
712     memcpy(&a1[MD5_SIZE], seed, seed_len);
713     hmac_md5(a1, MD5_SIZE+seed_len, sec, sec_len, out);
714
715     while (olen > MD5_SIZE)
716     {
717         uint8_t a2[MD5_SIZE];
718         out += MD5_SIZE;
719         olen -= MD5_SIZE;
720
721         /* A(N) */
722         hmac_md5(a1, MD5_SIZE, sec, sec_len, a2);
723         memcpy(a1, a2, MD5_SIZE);
724
725         /* work out the actual hash */
726         hmac_md5(a1, MD5_SIZE+seed_len, sec, sec_len, out);
727     }
728 }
729
730 /**
731  * Work out the SHA1 PRF.
732  */
733 static void p_hash_sha1(const uint8_t *sec, int sec_len, 
734         uint8_t *seed, int seed_len, uint8_t *out, int olen)
735 {
736     uint8_t a1[128];
737
738     /* A(1) */
739     hmac_sha1(seed, seed_len, sec, sec_len, a1);
740     memcpy(&a1[SHA1_SIZE], seed, seed_len);
741     hmac_sha1(a1, SHA1_SIZE+seed_len, sec, sec_len, out);
742
743     while (olen > SHA1_SIZE)
744     {
745         uint8_t a2[SHA1_SIZE];
746         out += SHA1_SIZE;
747         olen -= SHA1_SIZE;
748
749         /* A(N) */
750         hmac_sha1(a1, SHA1_SIZE, sec, sec_len, a2);
751         memcpy(a1, a2, SHA1_SIZE);
752
753         /* work out the actual hash */
754         hmac_sha1(a1, SHA1_SIZE+seed_len, sec, sec_len, out);
755     }
756 }
757
758 /**
759  * Work out the PRF.
760  */
761 static void prf(const uint8_t *sec, int sec_len, uint8_t *seed, int seed_len,
762         uint8_t *out, int olen)
763 {
764     int len, i;
765     const uint8_t *S1, *S2;
766     uint8_t xbuf[256]; /* needs to be > the amount of key data */
767     uint8_t ybuf[256]; /* needs to be > the amount of key data */
768
769     len = sec_len/2;
770     S1 = sec;
771     S2 = &sec[len];
772     len += (sec_len & 1); /* add for odd, make longer */
773
774     p_hash_md5(S1, len, seed, seed_len, xbuf, olen);
775     p_hash_sha1(S2, len, seed, seed_len, ybuf, olen);
776
777     for (i = 0; i < olen; i++)
778         out[i] = xbuf[i] ^ ybuf[i];
779 }
780
781 /**
782  * Generate a master secret based on the client/server random data and the
783  * premaster secret.
784  */
785 void generate_master_secret(SSL *ssl, const uint8_t *premaster_secret)
786 {
787     uint8_t buf[128];   /* needs to be > 13+32+32 in size */
788     strcpy((char *)buf, "master secret");
789     memcpy(&buf[13], ssl->dc->client_random, SSL_RANDOM_SIZE);
790     memcpy(&buf[45], ssl->dc->server_random, SSL_RANDOM_SIZE);
791     prf(premaster_secret, SSL_SECRET_SIZE, buf, 77, ssl->dc->master_secret,
792             SSL_SECRET_SIZE);
793 }
794
795 /**
796  * Generate a 'random' blob of data used for the generation of keys.
797  */
798 static void generate_key_block(uint8_t *client_random, uint8_t *server_random,
799         uint8_t *master_secret, uint8_t *key_block, int key_block_size)
800 {
801     uint8_t buf[128];
802     strcpy((char *)buf, "key expansion");
803     memcpy(&buf[13], server_random, SSL_RANDOM_SIZE);
804     memcpy(&buf[45], client_random, SSL_RANDOM_SIZE);
805     prf(master_secret, SSL_SECRET_SIZE, buf, 77, key_block, key_block_size);
806 }
807
808 /** 
809  * Calculate the digest used in the finished message. This function also
810  * doubles up as a certificate verify function.
811  */
812 void finished_digest(SSL *ssl, const char *label, uint8_t *digest)
813 {
814     uint8_t mac_buf[128]; 
815     uint8_t *q = mac_buf;
816     MD5_CTX md5_ctx = ssl->dc->md5_ctx;
817     SHA1_CTX sha1_ctx = ssl->dc->sha1_ctx;
818
819     if (label)
820     {
821         strcpy((char *)q, label);
822         q += strlen(label);
823     }
824
825     MD5_Final(q, &md5_ctx);
826     q += MD5_SIZE;
827     
828     SHA1_Final(q, &sha1_ctx);
829     q += SHA1_SIZE;
830
831     if (label)
832     {
833         prf(ssl->dc->master_secret, SSL_SECRET_SIZE, mac_buf, (int)(q-mac_buf),
834             digest, SSL_FINISHED_HASH_SIZE);
835     }
836     else    /* for use in a certificate verify */
837     {
838         memcpy(digest, mac_buf, MD5_SIZE + SHA1_SIZE);
839     }
840
841 #if 0
842     printf("label: %s\n", label);
843     print_blob("master secret", ssl->dc->master_secret, 48);
844     print_blob("mac_buf", mac_buf, q-mac_buf);
845     print_blob("finished digest", digest, SSL_FINISHED_HASH_SIZE);
846 #endif
847 }   
848     
849 /**
850  * Retrieve (and initialise) the context of a cipher.
851  */
852 static void *crypt_new(SSL *ssl, uint8_t *key, uint8_t *iv, int is_decrypt)
853 {
854     switch (ssl->cipher)
855     {
856 #ifndef CONFIG_SSL_SKELETON_MODE
857         case SSL_AES128_SHA:
858             {
859                 AES_CTX *aes_ctx = (AES_CTX *)malloc(sizeof(AES_CTX));
860                 AES_set_key(aes_ctx, key, iv, AES_MODE_128);
861
862                 if (is_decrypt)
863                 {
864                     AES_convert_key(aes_ctx);
865                 }
866
867                 return (void *)aes_ctx;
868             }
869
870         case SSL_AES256_SHA:
871             {
872                 AES_CTX *aes_ctx = (AES_CTX *)malloc(sizeof(AES_CTX));
873                 AES_set_key(aes_ctx, key, iv, AES_MODE_256);
874
875                 if (is_decrypt)
876                 {
877                     AES_convert_key(aes_ctx);
878                 }
879
880                 return (void *)aes_ctx;
881             }
882             break;
883
884         case SSL_RC4_128_MD5:
885 #endif
886         case SSL_RC4_128_SHA:
887             {
888                 RC4_CTX *rc4_ctx = (RC4_CTX *)malloc(sizeof(RC4_CTX));
889                 RC4_setup(rc4_ctx, key, 16);
890                 return (void *)rc4_ctx;
891             }
892             break;
893     }
894
895     return NULL;    /* its all gone wrong */
896 }
897
898 /**
899  * Send a packet over the socket.
900  */
901 static int send_raw_packet(SSL *ssl, uint8_t protocol)
902 {
903     uint8_t *rec_buf = ssl->bm_all_data;
904     int pkt_size = SSL_RECORD_SIZE+ssl->bm_index;
905     int sent = 0;
906     int ret = SSL_OK;
907
908     rec_buf[0] = protocol;
909     rec_buf[1] = 0x03;      /* version = 3.1 (TLS) */
910     rec_buf[2] = 0x01;
911     rec_buf[3] = ssl->bm_index >> 8;
912     rec_buf[4] = ssl->bm_index & 0xff;
913
914     DISPLAY_BYTES(ssl, "sending %d bytes", ssl->bm_all_data, 
915                              pkt_size, pkt_size);
916
917     while (sent < pkt_size)
918     {
919         if ((ret = SOCKET_WRITE(ssl->client_fd, 
920                         &ssl->bm_all_data[sent], pkt_size)) < 0)
921         {
922             ret = SSL_ERROR_CONN_LOST;
923             break;
924         }
925
926         sent += ret;
927
928         /* keep going until the write buffer has some space */
929         if (sent != pkt_size)
930         {
931             fd_set wfds;
932             FD_ZERO(&wfds);
933             FD_SET(ssl->client_fd, &wfds);
934
935             if (select(ssl->client_fd + 1, NULL, &wfds, NULL, NULL) < 0)
936             {
937                 ret = SSL_ERROR_CONN_LOST;
938                 break;
939             }
940         }
941     }
942
943     SET_SSL_FLAG(SSL_NEED_RECORD);  /* reset for next time */
944     ssl->bm_index = 0;
945
946     if (protocol != PT_APP_PROTOCOL_DATA)  
947     {
948         /* always return SSL_OK during handshake */   
949         ret = SSL_OK;
950     }
951
952     return ret;
953 }
954
955 /**
956  * Send an encrypted packet with padding bytes if necessary.
957  */
958 int send_packet(SSL *ssl, uint8_t protocol, const uint8_t *in, int length)
959 {
960     int msg_length = length;
961     int ret, pad_bytes = 0;
962     ssl->bm_index = msg_length;
963
964     /* if our state is bad, don't bother */
965     if (ssl->hs_status == SSL_ERROR_DEAD)
966         return SSL_ERROR_CONN_LOST;
967
968     if (in) /* has the buffer already been initialised? */
969     {
970         memcpy(ssl->bm_data, in, length);
971     }
972
973     if (IS_SET_SSL_FLAG(SSL_TX_ENCRYPTED))
974     {
975         int mode = IS_SET_SSL_FLAG(SSL_IS_CLIENT) ? 
976                             SSL_CLIENT_WRITE : SSL_SERVER_WRITE;
977         uint8_t hmac_header[SSL_RECORD_SIZE];
978
979         hmac_header[0] = protocol;
980         hmac_header[1] = 0x03;
981         hmac_header[2] = 0x01;
982         hmac_header[3] = length >> 8; 
983         hmac_header[4] = length & 0xff;
984
985         if (protocol == PT_HANDSHAKE_PROTOCOL)
986         {
987             DISPLAY_STATE(ssl, 1, ssl->bm_data[0], 0);
988
989             if (ssl->bm_data[0] != HS_HELLO_REQUEST)
990             {
991                 add_packet(ssl, ssl->bm_data, ssl->bm_index);
992             }
993         }
994
995         /* add the packet digest */
996         msg_length += ssl->cipher_info->digest_size;
997         ssl->bm_index = msg_length;
998         add_hmac_digest(ssl, mode, hmac_header, ssl->bm_data, length, 
999                                                 &ssl->bm_data[length]);
1000
1001         /* add padding? */
1002         if (ssl->cipher_info->padding_size)
1003         {
1004             int last_blk_size = msg_length%ssl->cipher_info->padding_size;
1005             pad_bytes = ssl->cipher_info->padding_size - last_blk_size;
1006
1007             /* ensure we always have at least 1 padding byte */
1008             if (pad_bytes == 0)
1009                 pad_bytes += ssl->cipher_info->padding_size;
1010
1011             memset(&ssl->bm_data[msg_length], pad_bytes-1, pad_bytes);
1012             msg_length += pad_bytes;
1013             ssl->bm_index = msg_length;
1014         }
1015
1016         DISPLAY_BYTES(ssl, "unencrypted write", ssl->bm_data, msg_length);
1017         increment_write_sequence(ssl);
1018
1019         /* now encrypt the packet */
1020         ssl->cipher_info->encrypt(ssl->encrypt_ctx, ssl->bm_data, 
1021                                             ssl->bm_data, msg_length);
1022     }
1023     else if (protocol == PT_HANDSHAKE_PROTOCOL)
1024     {
1025         DISPLAY_STATE(ssl, 1, ssl->bm_data[0], 0);
1026
1027         if (ssl->bm_data[0] != HS_HELLO_REQUEST)
1028         {
1029             add_packet(ssl, ssl->bm_data, ssl->bm_index);
1030         }
1031     }
1032
1033     if ((ret = send_raw_packet(ssl, protocol)) <= 0)
1034         return ret;
1035
1036     return length;  /* just return what we wanted to send */
1037 }
1038
1039 /**
1040  * Work out the cipher keys we are going to use for this session based on the
1041  * master secret.
1042  */
1043 static void set_key_block(SSL *ssl, int is_write)
1044 {
1045     const cipher_info_t *ciph_info = get_cipher_info(ssl->cipher);
1046     uint8_t *q;
1047     uint8_t client_key[32], server_key[32]; /* big enough for AES256 */
1048     uint8_t client_iv[16], server_iv[16];   /* big enough for AES128/256 */
1049     int is_client = IS_SET_SSL_FLAG(SSL_IS_CLIENT);
1050
1051     /* only do once in a handshake */
1052     if (ssl->dc->key_block == NULL)
1053     {
1054         ssl->dc->key_block = (uint8_t *)malloc(ciph_info->key_block_size);
1055
1056 #if 0
1057         print_blob("client", ssl->dc->client_random, 32);
1058         print_blob("server", ssl->dc->server_random, 32);
1059         print_blob("master", ssl->dc->master_secret, SSL_SECRET_SIZE);
1060 #endif
1061         generate_key_block(ssl->dc->client_random, ssl->dc->server_random,
1062             ssl->dc->master_secret, ssl->dc->key_block, 
1063             ciph_info->key_block_size);
1064 #if 0
1065         print_blob("keyblock", ssl->key_block, ciph_info->key_block_size);
1066 #endif
1067     }
1068
1069     q = ssl->dc->key_block;
1070
1071     if ((is_client && is_write) || (!is_client && !is_write))
1072     {
1073         memcpy(ssl->client_mac, q, ciph_info->digest_size);
1074     }
1075
1076     q += ciph_info->digest_size;
1077
1078     if ((!is_client && is_write) || (is_client && !is_write))
1079     {
1080         memcpy(ssl->server_mac, q, ciph_info->digest_size);
1081     }
1082
1083     q += ciph_info->digest_size;
1084     memcpy(client_key, q, ciph_info->key_size);
1085     q += ciph_info->key_size;
1086     memcpy(server_key, q, ciph_info->key_size);
1087     q += ciph_info->key_size;
1088
1089 #ifndef CONFIG_SSL_SKELETON_MODE 
1090     if (ciph_info->iv_size)    /* RC4 has no IV, AES does */
1091     {
1092         memcpy(client_iv, q, ciph_info->iv_size);
1093         q += ciph_info->iv_size;
1094         memcpy(server_iv, q, ciph_info->iv_size);
1095         q += ciph_info->iv_size;
1096     }
1097 #endif
1098
1099     free(is_write ? ssl->encrypt_ctx : ssl->decrypt_ctx);
1100
1101     /* now initialise the ciphers */
1102     if (is_client)
1103     {
1104         finished_digest(ssl, server_finished, ssl->dc->final_finish_mac);
1105
1106         if (is_write)
1107             ssl->encrypt_ctx = crypt_new(ssl, client_key, client_iv, 0);
1108         else
1109             ssl->decrypt_ctx = crypt_new(ssl, server_key, server_iv, 1);
1110     }
1111     else
1112     {
1113         finished_digest(ssl, client_finished, ssl->dc->final_finish_mac);
1114
1115         if (is_write)
1116             ssl->encrypt_ctx = crypt_new(ssl, server_key, server_iv, 0);
1117         else
1118             ssl->decrypt_ctx = crypt_new(ssl, client_key, client_iv, 1);
1119     }
1120
1121     ssl->cipher_info = ciph_info;
1122 }
1123
1124 /**
1125  * Read the SSL connection.
1126  */
1127 int basic_read(SSL *ssl, uint8_t **in_data)
1128 {
1129     int ret = SSL_OK;
1130     int read_len, is_client = IS_SET_SSL_FLAG(SSL_IS_CLIENT);
1131     uint8_t *buf = ssl->bm_data;
1132
1133     read_len = SOCKET_READ(ssl->client_fd, &buf[ssl->bm_read_index], 
1134                             ssl->need_bytes-ssl->got_bytes);
1135
1136     /* connection has gone, so die */
1137     if (read_len <= 0)
1138     {
1139         ret = SSL_ERROR_CONN_LOST;
1140         ssl->hs_status = SSL_ERROR_DEAD;  /* make sure it stays dead */
1141         goto error;
1142     }
1143
1144     DISPLAY_BYTES(ssl, "received %d bytes", 
1145             &ssl->bm_data[ssl->bm_read_index], read_len, read_len);
1146
1147     ssl->got_bytes += read_len;
1148     ssl->bm_read_index += read_len;
1149
1150     /* haven't quite got what we want, so try again later */
1151     if (ssl->got_bytes < ssl->need_bytes)
1152         return SSL_OK;
1153
1154     read_len = ssl->got_bytes;
1155     ssl->got_bytes = 0;
1156
1157     if (IS_SET_SSL_FLAG(SSL_NEED_RECORD))
1158     {
1159         /* check for sslv2 "client hello" */
1160         if (buf[0] & 0x80 && buf[2] == 1 && buf[3] == 0x03)
1161         {
1162 #ifdef CONFIG_SSL_ENABLE_V23_HANDSHAKE
1163             DISPLAY_BYTES(ssl, "ssl2 record", buf, 5);
1164             add_packet(ssl, &buf[2], 3);
1165             ret = process_sslv23_client_hello(ssl); 
1166 #else
1167             printf("Error: no SSLv23 handshaking allowed\n"); TTY_FLUSH();
1168             ret = SSL_ERROR_NOT_SUPPORTED;
1169 #endif
1170             goto error; /* not an error - just get out of here */
1171         }
1172
1173         ssl->need_bytes = (buf[3] << 8) + buf[4];
1174
1175         /* do we violate the spec with the message size?  */
1176         if (ssl->need_bytes > RT_MAX_PLAIN_LENGTH+RT_EXTRA-BM_RECORD_OFFSET)
1177         {
1178             ret = SSL_ERROR_INVALID_PROT_MSG;              
1179             goto error;
1180         }
1181
1182         CLR_SSL_FLAG(SSL_NEED_RECORD);
1183         memcpy(ssl->hmac_header, buf, 3);       /* store for hmac */
1184         ssl->record_type = buf[0];
1185         goto error;                         /* no error, we're done */
1186     }
1187
1188     /* for next time - just do it now in case of an error */
1189     SET_SSL_FLAG(SSL_NEED_RECORD);
1190     ssl->need_bytes = SSL_RECORD_SIZE;
1191
1192     /* decrypt if we need to */
1193     if (IS_SET_SSL_FLAG(SSL_RX_ENCRYPTED))
1194     {
1195         ssl->cipher_info->decrypt(ssl->decrypt_ctx, buf, buf, read_len);
1196         read_len = verify_digest(ssl, 
1197                 is_client ? SSL_CLIENT_READ : SSL_SERVER_READ, buf, read_len);
1198
1199         /* does the hmac work? */
1200         if (read_len < 0)
1201         {
1202             ret = read_len;
1203             goto error;
1204         }
1205
1206         DISPLAY_BYTES(ssl, "decrypted", buf, read_len);
1207         increment_read_sequence(ssl);
1208     }
1209
1210     /* The main part of the SSL packet */
1211     switch (ssl->record_type)
1212     {
1213         case PT_HANDSHAKE_PROTOCOL:
1214             ssl->dc->bm_proc_index = 0;
1215             ret = do_handshake(ssl, buf, read_len);
1216             break;
1217
1218         case PT_CHANGE_CIPHER_SPEC:
1219             if (ssl->next_state != HS_FINISHED)
1220             {
1221                 ret = SSL_ERROR_INVALID_HANDSHAKE;
1222                 goto error;
1223             }
1224
1225             /* all encrypted from now on */
1226             SET_SSL_FLAG(SSL_RX_ENCRYPTED);
1227             set_key_block(ssl, 0);
1228             memset(ssl->read_sequence, 0, 8);
1229             break;
1230
1231         case PT_APP_PROTOCOL_DATA:
1232             if (in_data)
1233             {
1234                 *in_data = ssl->bm_data;   /* point to the work buffer */
1235                 (*in_data)[read_len] = 0;  /* null terminate just in case */
1236             }
1237
1238             ret = read_len;
1239             break;
1240
1241         case PT_ALERT_PROTOCOL:
1242             /* return the alert # with alert bit set */
1243             ret = -buf[1]; 
1244             DISPLAY_ALERT(ssl, buf[1]);
1245             break;
1246
1247         default:
1248             ret = SSL_ERROR_INVALID_PROT_MSG;
1249             break;
1250     }
1251
1252 error:
1253     ssl->bm_read_index = 0;          /* reset to go again */
1254
1255     if (ret < SSL_OK && in_data)/* if all wrong, then clear this buffer ptr */
1256         *in_data = NULL;
1257
1258     return ret;
1259 }
1260
1261 /**
1262  * Do some basic checking of data and then perform the appropriate handshaking.
1263  */
1264 static int do_handshake(SSL *ssl, uint8_t *buf, int read_len)
1265 {
1266     int hs_len = (buf[2]<<8) + buf[3];
1267     uint8_t handshake_type = buf[0];
1268     int ret = SSL_OK;
1269     int is_client = IS_SET_SSL_FLAG(SSL_IS_CLIENT);
1270
1271     /* some integrity checking on the handshake */
1272     PARANOIA_CHECK(read_len-SSL_HS_HDR_SIZE, hs_len);
1273
1274     if (handshake_type != ssl->next_state)
1275     {
1276         /* handle a special case on the client */
1277         if (!is_client || handshake_type != HS_CERT_REQ ||
1278                         ssl->next_state != HS_SERVER_HELLO_DONE)
1279         {
1280             ret = SSL_ERROR_INVALID_HANDSHAKE;
1281             goto error;
1282         }
1283     }
1284
1285     hs_len += SSL_HS_HDR_SIZE;  /* adjust for when adding packets */
1286     ssl->bm_index = hs_len;     /* store the size and check later */
1287     DISPLAY_STATE(ssl, 0, handshake_type, 0);
1288
1289     if (handshake_type != HS_CERT_VERIFY && handshake_type != HS_HELLO_REQUEST)
1290         add_packet(ssl, buf, hs_len); 
1291
1292 #if defined(CONFIG_SSL_ENABLE_CLIENT)
1293     ret = is_client ? 
1294         do_clnt_handshake(ssl, handshake_type, buf, hs_len) :
1295         do_svr_handshake(ssl, handshake_type, buf, hs_len);
1296 #else
1297     ret = do_svr_handshake(ssl, handshake_type, buf, hs_len);
1298 #endif
1299
1300     /* just use recursion to get the rest */
1301     if (hs_len < read_len && ret == SSL_OK)
1302         ret = do_handshake(ssl, &buf[hs_len], read_len-hs_len);
1303
1304 error:
1305     return ret;
1306 }
1307
1308 /**
1309  * Sends the change cipher spec message. We have just read a finished message
1310  * from the client.
1311  */
1312 int send_change_cipher_spec(SSL *ssl)
1313 {
1314     int ret = send_packet(ssl, PT_CHANGE_CIPHER_SPEC, 
1315             g_chg_cipher_spec_pkt, sizeof(g_chg_cipher_spec_pkt));
1316     SET_SSL_FLAG(SSL_TX_ENCRYPTED);
1317     set_key_block(ssl, 1);
1318     memset(ssl->write_sequence, 0, 8);
1319     return ret;
1320 }
1321
1322 /**
1323  * Send a "finished" message
1324  */
1325 int send_finished(SSL *ssl)
1326 {
1327     uint8_t *buf = ssl->bm_data;
1328
1329     buf[0] = HS_FINISHED;
1330     buf[1] = 0;
1331     buf[2] = 0;
1332     buf[3] = SSL_FINISHED_HASH_SIZE;
1333
1334     /* now add the finished digest mac (12 bytes) */
1335     finished_digest(ssl, 
1336         IS_SET_SSL_FLAG(SSL_IS_CLIENT) ?
1337                     client_finished : server_finished, &buf[4]);
1338
1339 #ifndef CONFIG_SSL_SKELETON_MODE
1340     /* store in the session cache */
1341     if (!IS_SET_SSL_FLAG(SSL_SESSION_RESUME) && ssl->ssl_ctx->num_sessions)
1342     {
1343         memcpy(ssl->session->master_secret,
1344                 ssl->dc->master_secret, SSL_SECRET_SIZE);
1345     }
1346 #endif
1347
1348     return send_packet(ssl, PT_HANDSHAKE_PROTOCOL,
1349                                 NULL, SSL_FINISHED_HASH_SIZE+4);
1350 }
1351
1352 /**
1353  * Send an alert message.
1354  * Return 1 if the alert was an "error".
1355  */
1356 int send_alert(SSL *ssl, int error_code)
1357 {
1358     int alert_num = 0;
1359     int is_warning = 0;
1360     uint8_t buf[2];
1361
1362     /* Don't bother we're already dead */
1363     if (ssl->hs_status == SSL_ERROR_DEAD)
1364     {
1365         return SSL_ERROR_CONN_LOST;
1366     }
1367
1368 #ifdef CONFIG_SSL_FULL_MODE
1369     if (IS_SET_SSL_FLAG(SSL_DISPLAY_STATES))
1370         ssl_display_error(error_code);
1371 #endif
1372
1373     switch (error_code)
1374     {
1375         case SSL_ALERT_CLOSE_NOTIFY:
1376             is_warning = 1;
1377             alert_num = SSL_ALERT_CLOSE_NOTIFY;
1378             break;
1379
1380         case SSL_ERROR_CONN_LOST:       /* don't send alert just yet */
1381             is_warning = 1;
1382             break;
1383
1384         case SSL_ERROR_INVALID_HANDSHAKE:
1385         case SSL_ERROR_INVALID_PROT_MSG:
1386             alert_num = SSL_ALERT_HANDSHAKE_FAILURE;
1387             break;
1388
1389         case SSL_ERROR_INVALID_HMAC:
1390         case SSL_ERROR_FINISHED_INVALID:
1391             alert_num = SSL_ALERT_BAD_RECORD_MAC;
1392             break;
1393
1394         case SSL_ERROR_INVALID_VERSION:
1395             alert_num = SSL_ALERT_INVALID_VERSION;
1396             break;
1397
1398         case SSL_ERROR_INVALID_SESSION:
1399         case SSL_ERROR_NO_CIPHER:
1400         case SSL_ERROR_INVALID_KEY:
1401             alert_num = SSL_ALERT_ILLEGAL_PARAMETER;
1402             break;
1403
1404         case SSL_ERROR_BAD_CERTIFICATE:
1405             alert_num = SSL_ALERT_BAD_CERTIFICATE;
1406             break;
1407
1408         default:
1409             /* a catch-all for any badly verified certificates */
1410             alert_num = (error_code <= SSL_X509_OFFSET) ?  
1411                 SSL_ALERT_BAD_CERTIFICATE : SSL_ALERT_UNEXPECTED_MESSAGE;
1412             break;
1413     }
1414
1415     buf[0] = is_warning ? 1 : 2;
1416     buf[1] = alert_num;
1417     send_packet(ssl, PT_ALERT_PROTOCOL, buf, sizeof(buf));
1418     DISPLAY_ALERT(ssl, alert_num);
1419     return is_warning ? 0 : 1;
1420 }
1421
1422 /**
1423  * Process a client finished message.
1424  */
1425 int process_finished(SSL *ssl, int hs_len)
1426 {
1427     uint8_t *buf = ssl->bm_data;
1428     int ret = SSL_OK;
1429     int is_client = IS_SET_SSL_FLAG(SSL_IS_CLIENT);
1430     int resume = IS_SET_SSL_FLAG(SSL_SESSION_RESUME);
1431
1432     PARANOIA_CHECK(ssl->bm_index, SSL_FINISHED_HASH_SIZE+4);
1433
1434     /* check that we all work before we continue */
1435     if (memcmp(ssl->dc->final_finish_mac, &buf[4], SSL_FINISHED_HASH_SIZE))
1436         return SSL_ERROR_FINISHED_INVALID;
1437
1438     if ((!is_client && !resume) || (is_client && resume))
1439     {
1440         if ((ret = send_change_cipher_spec(ssl)) == SSL_OK)
1441             ret = send_finished(ssl);
1442     }
1443
1444     /* if we ever renegotiate */
1445     ssl->next_state = is_client ? HS_HELLO_REQUEST : HS_CLIENT_HELLO;  
1446     ssl->hs_status = ret;  /* set the final handshake status */
1447
1448 error:
1449     return ret;
1450 }
1451
1452 /**
1453  * Send a certificate.
1454  */
1455 int send_certificate(SSL *ssl)
1456 {
1457     int i = 0;
1458     uint8_t *buf = ssl->bm_data;
1459     int offset = 7;
1460     int chain_length;
1461
1462     buf[0] = HS_CERTIFICATE;
1463     buf[1] = 0;
1464     buf[4] = 0;
1465
1466     while (i < ssl->ssl_ctx->chain_length)
1467     {
1468         SSL_CERT *cert = &ssl->ssl_ctx->certs[i];
1469         buf[offset++] = 0;        
1470         buf[offset++] = cert->size >> 8;        /* cert 1 length */
1471         buf[offset++] = cert->size & 0xff;
1472         memcpy(&buf[offset], cert->buf, cert->size);
1473         offset += cert->size;
1474         i++;
1475     }
1476
1477     chain_length = offset - 7;
1478     buf[5] = chain_length >> 8;        /* cert chain length */
1479     buf[6] = chain_length & 0xff;
1480     chain_length += 3;
1481     buf[2] = chain_length >> 8;        /* handshake length */
1482     buf[3] = chain_length & 0xff;
1483     ssl->bm_index = offset;
1484     return send_packet(ssl, PT_HANDSHAKE_PROTOCOL, NULL, offset);
1485 }
1486
1487 /**
1488  * Create a blob of memory that we'll get rid of once the handshake is
1489  * complete.
1490  */
1491 void disposable_new(SSL *ssl)
1492 {
1493     if (ssl->dc == NULL)
1494     {
1495         ssl->dc = (DISPOSABLE_CTX *)calloc(1, sizeof(DISPOSABLE_CTX));
1496         MD5_Init(&ssl->dc->md5_ctx);
1497         SHA1_Init(&ssl->dc->sha1_ctx);
1498     }
1499 }
1500
1501 /**
1502  * Remove the temporary blob of memory.
1503  */
1504 void disposable_free(SSL *ssl)
1505 {
1506     if (ssl->dc)
1507     {
1508             free(ssl->dc->key_block);
1509         memset(ssl->dc, 0, sizeof(DISPOSABLE_CTX));
1510         free(ssl->dc);
1511         ssl->dc = NULL;
1512     }
1513
1514 }
1515
1516 #ifndef CONFIG_SSL_SKELETON_MODE     /* no session resumption in this mode */
1517 /**
1518  * Find if an existing session has the same session id. If so, use the
1519  * master secret from this session for session resumption.
1520  */
1521 SSL_SESSION *ssl_session_update(int max_sessions, SSL_SESSION *ssl_sessions[], 
1522         SSL *ssl, const uint8_t *session_id)
1523 {
1524     time_t tm = time(NULL);
1525     time_t oldest_sess_time = tm;
1526     SSL_SESSION *oldest_sess = NULL;
1527     int i;
1528
1529     /* no sessions? Then bail */
1530     if (max_sessions == 0)
1531         return NULL;
1532
1533     SSL_CTX_LOCK(ssl->ssl_ctx->mutex);
1534     if (session_id)
1535     {
1536         for (i = 0; i < max_sessions; i++)
1537         {
1538             if (ssl_sessions[i])
1539             {
1540                 /* kill off any expired sessions */
1541                 if (tm > ssl_sessions[i]->conn_time + SSL_EXPIRY_TIME)
1542                 {
1543                     session_free(ssl_sessions, i);
1544                     continue;
1545                 }
1546
1547                 /* if the session id matches, it must still be less than 
1548                    the expiry time */
1549                 if (memcmp(ssl_sessions[i]->session_id, session_id,
1550                                                 SSL_SESSION_ID_SIZE) == 0)
1551                 {
1552                     ssl->session_index = i;
1553                     memcpy(ssl->dc->master_secret, 
1554                             ssl_sessions[i]->master_secret, SSL_SECRET_SIZE);
1555                     SET_SSL_FLAG(SSL_SESSION_RESUME);
1556                     SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
1557                     return ssl_sessions[i];  /* a session was found */
1558                 }
1559             }
1560         }
1561     }
1562
1563     /* If we've got here, no matching session was found - so create one */
1564     for (i = 0; i < max_sessions; i++)
1565     {
1566         if (ssl_sessions[i] == NULL)
1567         {
1568             /* perfect, this will do */
1569             ssl_sessions[i] = (SSL_SESSION *)calloc(1, sizeof(SSL_SESSION));
1570             ssl_sessions[i]->conn_time = tm;
1571             ssl->session_index = i;
1572             SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
1573             return ssl_sessions[i]; /* return the session object */
1574         }
1575         else if (ssl_sessions[i]->conn_time <= oldest_sess_time)
1576         {
1577             /* find the oldest session */
1578             oldest_sess_time = ssl_sessions[i]->conn_time;
1579             oldest_sess = ssl_sessions[i];
1580             ssl->session_index = i;
1581         }
1582     }
1583
1584     /* ok, we've used up all of our sessions. So blow the oldest session away */
1585     oldest_sess->conn_time = tm;
1586     memset(oldest_sess->session_id, 0, sizeof(SSL_SESSION_ID_SIZE));
1587     memset(oldest_sess->master_secret, 0, sizeof(SSL_SECRET_SIZE));
1588     SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
1589     return oldest_sess;
1590 }
1591
1592 /**
1593  * Free an existing session.
1594  */
1595 static void session_free(SSL_SESSION *ssl_sessions[], int sess_index)
1596 {
1597     if (ssl_sessions[sess_index])
1598     {
1599         free(ssl_sessions[sess_index]);
1600         ssl_sessions[sess_index] = NULL;
1601     }
1602 }
1603
1604 /**
1605  * This ssl object doesn't want this session anymore.
1606  */
1607 void kill_ssl_session(SSL_SESSION **ssl_sessions, SSL *ssl)
1608 {
1609     SSL_CTX_LOCK(ssl->ssl_ctx->mutex);
1610
1611     if (ssl->ssl_ctx->num_sessions)
1612     {
1613         session_free(ssl_sessions, ssl->session_index);
1614         ssl->session = NULL;
1615     }
1616
1617     SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
1618 }
1619 #endif /* CONFIG_SSL_SKELETON_MODE */
1620
1621 /*
1622  * Get the session id for a handshake. This will be a 32 byte sequence.
1623  */
1624 EXP_FUNC const uint8_t * STDCALL ssl_get_session_id(const SSL *ssl)
1625 {
1626     return ssl->session_id;
1627 }
1628
1629 /*
1630  * Get the session id size for a handshake. 
1631  */
1632 EXP_FUNC uint8_t STDCALL ssl_get_session_id_size(const SSL *ssl)
1633 {
1634     return ssl->sess_id_size;
1635 }
1636
1637 /*
1638  * Return the cipher id (in the SSL form).
1639  */
1640 EXP_FUNC uint8_t STDCALL ssl_get_cipher_id(const SSL *ssl)
1641 {
1642     return ssl->cipher;
1643 }
1644
1645 /*
1646  * Return the status of the handshake.
1647  */
1648 EXP_FUNC int STDCALL ssl_handshake_status(const SSL *ssl)
1649 {
1650     return ssl->hs_status;
1651 }
1652
1653 /*
1654  * Retrieve various parameters about the SSL engine.
1655  */
1656 EXP_FUNC int STDCALL ssl_get_config(int offset)
1657 {
1658     switch (offset)
1659     {
1660         /* return the appropriate build mode */
1661         case SSL_BUILD_MODE:
1662 #if defined(CONFIG_SSL_FULL_MODE)
1663             return SSL_BUILD_FULL_MODE;
1664 #elif defined(CONFIG_SSL_ENABLE_CLIENT)
1665             return SSL_BUILD_ENABLE_CLIENT;
1666 #elif defined(CONFIG_ENABLE_VERIFICATION)
1667             return SSL_BUILD_ENABLE_VERIFICATION;
1668 #elif defined(CONFIG_SSL_SERVER_ONLY )
1669             return SSL_BUILD_SERVER_ONLY;
1670 #else 
1671             return SSL_BUILD_SKELETON_MODE;
1672 #endif
1673
1674         case SSL_MAX_CERT_CFG_OFFSET:
1675             return CONFIG_SSL_MAX_CERTS;
1676
1677 #ifdef CONFIG_SSL_CERT_VERIFICATION
1678         case SSL_MAX_CA_CERT_CFG_OFFSET:
1679             return CONFIG_X509_MAX_CA_CERTS;
1680 #endif
1681 #ifdef CONFIG_SSL_HAS_PEM
1682         case SSL_HAS_PEM:
1683             return 1;
1684 #endif
1685         default:
1686             return 0;
1687     }
1688 }
1689
1690 #ifdef CONFIG_SSL_CERT_VERIFICATION
1691 /**
1692  * Authenticate a received certificate.
1693  */
1694 EXP_FUNC int STDCALL ssl_verify_cert(const SSL *ssl)
1695 {
1696     int ret;
1697     SSL_CTX_LOCK(ssl->ssl_ctx->mutex);
1698     ret = x509_verify(ssl->ssl_ctx->ca_cert_ctx, ssl->x509_ctx);
1699     SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
1700
1701     if (ret)        /* modify into an SSL error type */
1702     {
1703         ret = SSL_X509_ERROR(ret);
1704     }
1705
1706     return ret;
1707 }
1708
1709 /**
1710  * Process a certificate message.
1711  */
1712 int process_certificate(SSL *ssl, X509_CTX **x509_ctx)
1713 {
1714     int ret = SSL_OK;
1715     uint8_t *buf = &ssl->bm_data[ssl->dc->bm_proc_index];
1716     int pkt_size = ssl->bm_index;
1717     int cert_size, offset = 5;
1718     int total_cert_size = (buf[offset]<<8) + buf[offset+1];
1719     int is_client = IS_SET_SSL_FLAG(SSL_IS_CLIENT);
1720     X509_CTX **chain = x509_ctx;
1721     offset += 2;
1722
1723     PARANOIA_CHECK(total_cert_size, offset);
1724
1725     while (offset < total_cert_size)
1726     {
1727         offset++;       /* skip empty char */
1728         cert_size = (buf[offset]<<8) + buf[offset+1];
1729         offset += 2;
1730         
1731         if (x509_new(&buf[offset], NULL, chain))
1732         {
1733             ret = SSL_ERROR_BAD_CERTIFICATE;
1734             goto error;
1735         }
1736
1737         /* DISPLAY_CERT(ssl, *chain); */
1738         chain = &((*chain)->next);
1739         offset += cert_size;
1740     }
1741
1742     PARANOIA_CHECK(pkt_size, offset);
1743
1744     /* if we are client we can do the verify now or later */
1745     if (is_client && !IS_SET_SSL_FLAG(SSL_SERVER_VERIFY_LATER))
1746     {
1747         ret = ssl_verify_cert(ssl);
1748     }
1749
1750     ssl->next_state = is_client ? HS_SERVER_HELLO_DONE : HS_CLIENT_KEY_XCHG;
1751     ssl->dc->bm_proc_index += offset;
1752 error:
1753     return ret;
1754 }
1755
1756 #endif /* CONFIG_SSL_CERT_VERIFICATION */
1757
1758 /**
1759  * Debugging routine to display SSL handshaking stuff.
1760  */
1761 #ifdef CONFIG_SSL_FULL_MODE
1762 /**
1763  * Debugging routine to display SSL states.
1764  */
1765 void DISPLAY_STATE(SSL *ssl, int is_send, uint8_t state, int not_ok)
1766 {
1767     const char *str;
1768
1769     if (!IS_SET_SSL_FLAG(SSL_DISPLAY_STATES))
1770         return;
1771
1772     printf(not_ok ? "Error - invalid State:\t" : "State:\t");
1773     printf(is_send ? "sending " : "receiving ");
1774
1775     switch (state)
1776     {
1777         case HS_HELLO_REQUEST:
1778             str = "Hello Request (0)";
1779             break;
1780
1781         case HS_CLIENT_HELLO:
1782             str = "Client Hello (1)";
1783             break;
1784
1785         case HS_SERVER_HELLO:
1786             str = "Server Hello (2)";
1787             break;
1788
1789         case HS_CERTIFICATE:
1790             str = "Certificate (11)";
1791             break;
1792
1793         case HS_SERVER_KEY_XCHG:
1794             str = "Certificate Request (12)";
1795             break;
1796
1797         case HS_CERT_REQ:
1798             str = "Certificate Request (13)";
1799             break;
1800
1801         case HS_SERVER_HELLO_DONE:
1802             str = "Server Hello Done (14)";
1803             break;
1804
1805         case HS_CERT_VERIFY:
1806             str = "Certificate Verify (15)";
1807             break;
1808
1809         case HS_CLIENT_KEY_XCHG:
1810             str = "Client Key Exchange (16)";
1811             break;
1812
1813         case HS_FINISHED:
1814             str = "Finished (16)";
1815             break;
1816
1817         default:
1818             str = "Error (Unknown)";
1819             
1820             break;
1821     }
1822
1823     printf("%s\n", str);
1824     TTY_FLUSH();
1825 }
1826
1827 /**
1828  * Debugging routine to display X509 certificates.
1829  */
1830 void DISPLAY_CERT(SSL *ssl, const X509_CTX *x509_ctx)
1831 {
1832     if (!IS_SET_SSL_FLAG(SSL_DISPLAY_CERTS))
1833         return;
1834
1835     x509_print(x509_ctx, ssl->ssl_ctx->ca_cert_ctx);
1836     TTY_FLUSH();
1837 }
1838
1839 /**
1840  * Debugging routine to display RSA objects
1841  */
1842 void DISPLAY_RSA(SSL *ssl, const RSA_CTX *rsa_ctx)
1843 {
1844     if (!IS_SET_SSL_FLAG(SSL_DISPLAY_RSA))
1845         return;
1846
1847     RSA_print(rsa_ctx);
1848     TTY_FLUSH();
1849 }
1850
1851 /**
1852  * Debugging routine to display SSL handshaking bytes.
1853  */
1854 void DISPLAY_BYTES(SSL *ssl, const char *format, 
1855         const uint8_t *data, int size, ...)
1856 {
1857     va_list(ap);
1858
1859     if (!IS_SET_SSL_FLAG(SSL_DISPLAY_BYTES))
1860         return;
1861
1862     va_start(ap, size);
1863     print_blob(format, data, size, va_arg(ap, char *));
1864     va_end(ap);
1865     TTY_FLUSH();
1866 }
1867
1868 /**
1869  * Debugging routine to display SSL handshaking errors.
1870  */
1871 EXP_FUNC void STDCALL ssl_display_error(int error_code)
1872 {
1873     if (error_code == SSL_OK)
1874         return;
1875
1876     printf("Error: ");
1877
1878     /* X509 error? */
1879     if (error_code < SSL_X509_OFFSET)
1880     {
1881         printf("%s\n", x509_display_error(error_code - SSL_X509_OFFSET));
1882         return;
1883     }
1884
1885     /* SSL alert error code */
1886     if (error_code > SSL_ERROR_CONN_LOST)
1887     {
1888         printf("SSL error %d\n", -error_code);
1889         return;
1890     }
1891
1892     switch (error_code)
1893     {
1894         case SSL_ERROR_DEAD:
1895             printf("connection dead");
1896             break;
1897
1898         case SSL_ERROR_INVALID_HANDSHAKE:
1899             printf("invalid handshake");
1900             break;
1901
1902         case SSL_ERROR_INVALID_PROT_MSG:
1903             printf("invalid protocol message");
1904             break;
1905
1906         case SSL_ERROR_INVALID_HMAC:
1907             printf("invalid mac");
1908             break;
1909
1910         case SSL_ERROR_INVALID_VERSION:
1911             printf("invalid version");
1912             break;
1913
1914         case SSL_ERROR_INVALID_SESSION:
1915             printf("invalid session");
1916             break;
1917
1918         case SSL_ERROR_NO_CIPHER:
1919             printf("no cipher");
1920             break;
1921
1922         case SSL_ERROR_CONN_LOST:
1923             printf("connection lost");
1924             break;
1925
1926         case SSL_ERROR_BAD_CERTIFICATE:
1927             printf("bad certificate");
1928             break;
1929
1930         case SSL_ERROR_INVALID_KEY:
1931             printf("invalid key");
1932             break;
1933
1934         case SSL_ERROR_FINISHED_INVALID:
1935             printf("finished invalid");
1936             break;
1937
1938         case SSL_ERROR_NO_CERT_DEFINED:
1939             printf("no certificate defined");
1940             break;
1941
1942         case SSL_ERROR_NOT_SUPPORTED:
1943             printf("Option not supported");
1944             break;
1945
1946         default:
1947             printf("undefined as yet - %d", error_code);
1948             break;
1949     }
1950
1951     printf("\n");
1952     TTY_FLUSH();
1953 }
1954
1955 /**
1956  * Debugging routine to display alerts.
1957  */
1958 void DISPLAY_ALERT(SSL *ssl, int alert)
1959 {
1960     if (!IS_SET_SSL_FLAG(SSL_DISPLAY_STATES))
1961         return;
1962
1963     printf("Alert: ");
1964
1965     switch (alert)
1966     {
1967         case SSL_ALERT_CLOSE_NOTIFY:
1968             printf("close notify");
1969             break;
1970
1971         case SSL_ALERT_INVALID_VERSION:
1972             printf("invalid version");
1973             break;
1974
1975         case SSL_ALERT_BAD_CERTIFICATE:
1976             printf("bad certificate");
1977             break;
1978
1979         case SSL_ALERT_UNEXPECTED_MESSAGE:
1980             printf("unexpected message");
1981             break;
1982
1983         case SSL_ALERT_BAD_RECORD_MAC:
1984             printf("bad record mac");
1985             break;
1986
1987         case SSL_ALERT_HANDSHAKE_FAILURE:
1988             printf("handshake failure");
1989             break;
1990
1991         case SSL_ALERT_ILLEGAL_PARAMETER:
1992             printf("illegal parameter");
1993             break;
1994
1995         case SSL_ALERT_DECODE_ERROR:
1996             printf("decode error");
1997             break;
1998
1999         case SSL_ALERT_DECRYPT_ERROR:
2000             printf("decrypt error");
2001             break;
2002
2003         default:
2004             printf("alert - (unknown %d)", alert);
2005             break;
2006     }
2007
2008     printf("\n");
2009     TTY_FLUSH();
2010 }
2011
2012 #endif /* CONFIG_SSL_FULL_MODE */
2013
2014 /**
2015  * Return the version of this library.
2016  */
2017 EXP_FUNC const char  * STDCALL ssl_version()
2018 {
2019     static const char * axtls_version = AXTLS_VERSION;
2020     return axtls_version;
2021 }
2022
2023 /**
2024  * Enable the various language bindings to work regardless of the
2025  * configuration - they just return an error statement and a bad return code.
2026  */
2027 #if !defined(CONFIG_SSL_FULL_MODE)
2028 EXP_FUNC void STDCALL ssl_display_error(int error_code) {}
2029 #endif
2030
2031 #ifdef CONFIG_BINDINGS
2032 #if !defined(CONFIG_SSL_ENABLE_CLIENT)
2033 EXP_FUNC SSL * STDCALL ssl_client_new(SSL_CTX *ssl_ctx, int client_fd, const
2034         uint8_t *session_id, uint8_t sess_id_size)
2035 {
2036     printf(unsupported_str);
2037     return NULL;
2038 }
2039 #endif
2040
2041 #if !defined(CONFIG_SSL_CERT_VERIFICATION)
2042 EXP_FUNC int STDCALL ssl_verify_cert(const SSL *ssl)
2043 {
2044     printf(unsupported_str);
2045     return -1;
2046 }
2047
2048 EXP_FUNC const char * STDCALL ssl_get_cert_dn(const SSL *ssl, int component)
2049 {
2050     printf(unsupported_str);
2051     return NULL;
2052 }
2053
2054 #endif  /* CONFIG_SSL_CERT_VERIFICATION */
2055
2056 #endif /* CONFIG_BINDINGS */
2057