3 * Copyright (C) 2006-2011 Sawtooth Consulting Ltd.
5 * This file is part of CyaSSL.
7 * CyaSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * CyaSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
43 #ifdef CYASSL_CALLBACKS
44 #include "cyassl_callbacks.h"
48 #ifdef USE_WINDOWS_API
50 #elif defined(THREADX)
51 #ifndef SINGLE_THREADED
54 #elif defined(MICRIUM)
55 /* do nothing, just don't pick Unix */
57 #ifndef SINGLE_THREADED
58 #define CYASSL_PTHREADS
61 #if defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS)
62 #include <unistd.h> /* for close of BIO */
71 /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
72 #pragma warning(disable: 4996)
76 #if !defined (ALIGN16)
86 #ifdef USE_WINDOWS_API
87 typedef unsigned int SOCKET_T
;
93 typedef byte word24
[3];
95 /* Define or comment out the cipher suites you'd like to be compiled in
96 make sure to use at least one BUILD_SSL_xxx or BUILD_TLS_xxx is defined
98 When adding cipher suites, add name to cipher_names, idx to cipher_name_idx
101 #define BUILD_SSL_RSA_WITH_RC4_128_SHA
102 #define BUILD_SSL_RSA_WITH_RC4_128_MD5
103 #if !defined(NO_TLS) && defined(HAVE_NTRU)
104 #define BUILD_TLS_NTRU_RSA_WITH_RC4_128_SHA
109 #define BUILD_SSL_RSA_WITH_3DES_EDE_CBC_SHA
110 #if !defined(NO_TLS) && defined(HAVE_NTRU)
111 #define BUILD_TLS_NTRU_RSA_WITH_3DES_EDE_CBC_SHA
115 #if !defined(NO_AES) && !defined(NO_TLS)
116 #define BUILD_TLS_RSA_WITH_AES_128_CBC_SHA
117 #define BUILD_TLS_RSA_WITH_AES_256_CBC_SHA
118 #if !defined (NO_PSK)
119 #define BUILD_TLS_PSK_WITH_AES_128_CBC_SHA
120 #define BUILD_TLS_PSK_WITH_AES_256_CBC_SHA
122 #if defined(HAVE_NTRU)
123 #define BUILD_TLS_NTRU_RSA_WITH_AES_128_CBC_SHA
124 #define BUILD_TLS_NTRU_RSA_WITH_AES_256_CBC_SHA
128 #if !defined(NO_HC128) && !defined(NO_TLS)
129 #define BUILD_TLS_RSA_WITH_HC_128_CBC_MD5
130 #define BUILD_TLS_RSA_WITH_HC_128_CBC_SHA
133 #if !defined(NO_RABBIT) && !defined(NO_TLS)
134 #define BUILD_TLS_RSA_WITH_RABBIT_CBC_SHA
137 #if !defined(NO_DH) && !defined(NO_AES) && !defined(NO_TLS) && defined(OPENSSL_EXTRA)
138 #define BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
139 #define BUILD_TLS_DHE_RSA_WITH_AES_256_CBC_SHA
142 #if defined(HAVE_ECC) && !defined(NO_TLS)
144 #define BUILD_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
145 #define BUILD_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
146 #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
147 #define BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
150 #define BUILD_TLS_ECDHE_RSA_WITH_RC4_128_SHA
151 #define BUILD_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA
153 #if !defined(NO_DES3)
154 #define BUILD_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
155 #define BUILD_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA
160 #if defined(BUILD_SSL_RSA_WITH_RC4_128_SHA) || \
161 defined(BUILD_SSL_RSA_WITH_RC4_128_MD5)
165 #if defined(BUILD_SSL_RSA_WITH_3DES_EDE_CBC_SHA)
169 #if defined(BUILD_TLS_RSA_WITH_AES_128_CBC_SHA) || \
170 defined(BUILD_TLS_RSA_WITH_AES_256_CBC_SHA)
174 #if defined(BUILD_TLS_RSA_WITH_HC_128_CBC_SHA) || \
175 defined(BUILD_TLS_RSA_WITH_HC_128_CBC_MD5)
179 #if defined(BUILD_TLS_RSA_WITH_RABBIT_CBC_SHA)
184 #define DES_BLOCK_SIZE 8
188 #define AES_BLOCK_SIZE 16
192 /* actual cipher values, 2nd byte */
194 TLS_DHE_RSA_WITH_AES_256_CBC_SHA
= 0x39,
195 TLS_DHE_RSA_WITH_AES_128_CBC_SHA
= 0x33,
196 TLS_RSA_WITH_AES_256_CBC_SHA
= 0x35,
197 TLS_RSA_WITH_AES_128_CBC_SHA
= 0x2F,
198 TLS_PSK_WITH_AES_256_CBC_SHA
= 0x8d,
199 TLS_PSK_WITH_AES_128_CBC_SHA
= 0x8c,
200 SSL_RSA_WITH_RC4_128_SHA
= 0x05,
201 SSL_RSA_WITH_RC4_128_MD5
= 0x04,
202 SSL_RSA_WITH_3DES_EDE_CBC_SHA
= 0x0A,
204 /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
205 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
= 0x14,
206 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
= 0x13,
207 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
= 0x0A,
208 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
= 0x09,
209 TLS_ECDHE_RSA_WITH_RC4_128_SHA
= 0x11,
210 TLS_ECDHE_ECDSA_WITH_RC4_128_SHA
= 0x07,
211 TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
= 0x12,
212 TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA
= 0x08,
214 /* CyaSSL extension - eSTREAM */
215 TLS_RSA_WITH_HC_128_CBC_MD5
= 0xFB,
216 TLS_RSA_WITH_HC_128_CBC_SHA
= 0xFC,
217 TLS_RSA_WITH_RABBIT_CBC_SHA
= 0xFD,
219 /* CyaSSL extension - NTRU */
220 TLS_NTRU_RSA_WITH_RC4_128_SHA
= 0x65,
221 TLS_NTRU_RSA_WITH_3DES_EDE_CBC_SHA
= 0x66,
222 TLS_NTRU_RSA_WITH_AES_128_CBC_SHA
= 0x67,
223 TLS_NTRU_RSA_WITH_AES_256_CBC_SHA
= 0x68
231 ECC_BYTE
= 0xC0, /* ECC first cipher suite byte */
236 DTLS_MAJOR
= 0xfe, /* DTLS major version number */
237 DTLS_MINOR
= 0xff, /* DTLS minor version number */
238 SSLv3_MAJOR
= 3, /* SSLv3 and TLSv1+ major version number */
239 SSLv3_MINOR
= 0, /* TLSv1 minor version number */
240 TLSv1_MINOR
= 1, /* TLSv1 minor version number */
241 TLSv1_1_MINOR
= 2, /* TLSv1_1 minor version number */
242 TLSv1_2_MINOR
= 3, /* TLSv1_2 minor version number */
244 ZLIB_COMPRESSION
= 221, /* CyaSSL zlib compression */
245 SECRET_LEN
= 48, /* pre RSA and all master */
246 ENCRYPT_LEN
= 256, /* allow 2048 bit static buffer */
247 SIZEOF_SENDER
= 4, /* clnt or srvr */
248 FINISHED_SZ
= MD5_DIGEST_SIZE
+ SHA_DIGEST_SIZE
,
249 MAX_RECORD_SIZE
= 16384, /* 2^14, max size by standard */
250 MAX_UDP_SIZE
= 1400, /* don't exceed MTU */
251 MAX_MSG_EXTRA
= 68, /* max added to msg, mac + pad */
252 MAX_COMP_EXTRA
= 1024, /* max compression extra */
253 MAX_MTU
= 1500, /* max expected MTU */
254 MAX_DH_SZ
= 612, /* 2240 p, pub, g + 2 byte size for each */
255 MAX_STR_VERSION
= 8, /* string rep of protocol version */
257 PAD_MD5
= 48, /* pad length for finished */
258 PAD_SHA
= 40, /* pad length for finished */
259 PEM_LINE_LEN
= 80, /* PEM line max + fudge */
260 LENGTH_SZ
= 2, /* length field for HMAC, data only */
261 VERSION_SZ
= 2, /* length of proctocol version */
262 SEQ_SZ
= 8, /* 64 bit sequence number */
263 BYTE3_LEN
= 3, /* up to 24 bit byte lengths */
264 ALERT_SIZE
= 2, /* level + description */
265 REQUEST_HEADER
= 2, /* always use 2 bytes */
266 VERIFY_HEADER
= 2, /* always use 2 bytes */
268 MAX_SUITE_SZ
= 200, /* 100 suites for now! */
269 RAN_LEN
= 32, /* random length */
270 SEED_LEN
= RAN_LEN
* 2, /* tls prf seed length */
271 ID_LEN
= 32, /* session id length */
272 MAX_COOKIE_LEN
= 32, /* max dtls cookie size */
273 SUITE_LEN
= 2, /* cipher suite sz length */
274 ENUM_LEN
= 1, /* always a byte */
275 COMP_LEN
= 1, /* compression length */
276 CURVE_LEN
= 2, /* ecc named curve length */
278 HANDSHAKE_HEADER_SZ
= 4, /* type + length(3) */
279 RECORD_HEADER_SZ
= 5, /* type + version + len(2) */
280 CERT_HEADER_SZ
= 3, /* always 3 bytes */
281 REQ_HEADER_SZ
= 2, /* cert request header sz */
282 HINT_LEN_SZ
= 2, /* length of hint size field */
284 DTLS_HANDSHAKE_HEADER_SZ
= 12, /* normal + seq(2) + offset(3) + length(3) */
285 DTLS_RECORD_HEADER_SZ
= 13, /* normal + epoch(2) + seq_num(6) */
286 DTLS_HANDSHAKE_EXTRA
= 8, /* diff from normal */
287 DTLS_RECORD_EXTRA
= 8, /* diff from normal */
289 FINISHED_LABEL_SZ
= 15, /* TLS finished label size */
290 TLS_FINISHED_SZ
= 12, /* TLS has a shorter size */
291 MASTER_LABEL_SZ
= 13, /* TLS master secret label sz */
292 KEY_LABEL_SZ
= 13, /* TLS key block expansion sz */
293 MAX_PRF_HALF
= 128, /* Maximum half secret len */
294 MAX_PRF_LABSEED
= 80, /* Maximum label + seed len */
295 MAX_PRF_DIG
= 148, /* Maximum digest len */
296 MAX_REQUEST_SZ
= 256, /* Maximum cert req len (no auth yet */
297 SESSION_FLUSH_COUNT
= 256, /* Flush session cache unless user turns off */
299 RC4_KEY_SIZE
= 16, /* always 128bit */
300 DES_KEY_SIZE
= 8, /* des */
301 DES3_KEY_SIZE
= 24, /* 3 des ede */
302 DES_IV_SIZE
= DES_BLOCK_SIZE
,
303 AES_256_KEY_SIZE
= 32, /* for 256 bit */
304 AES_192_KEY_SIZE
= 24, /* for 192 bit */
305 AES_IV_SIZE
= 16, /* always block size */
306 AES_128_KEY_SIZE
= 16, /* for 128 bit */
308 HC_128_KEY_SIZE
= 16, /* 128 bits */
309 HC_128_IV_SIZE
= 16, /* also 128 bits */
311 RABBIT_KEY_SIZE
= 16, /* 128 bits */
312 RABBIT_IV_SIZE
= 8, /* 64 bits for iv */
314 EVP_SALT_SIZE
= 8, /* evp salt size 64 bits */
316 ECDHE_SIZE
= 32, /* ECHDE server size defaults to 256 bit */
317 MAX_EXPORT_ECC_SZ
= 256, /* Export ANS X9.62 max future size */
319 MAX_HELLO_SZ
= 128, /* max client or server hello */
320 MAX_CERT_VERIFY_SZ
= 1024, /* max */
321 CLIENT_HELLO_FIRST
= 35, /* Protocol + RAN_LEN + sizeof(id_len) */
322 MAX_SUITE_NAME
= 48, /* maximum length of cipher suite string */
323 DEFAULT_TIMEOUT
= 500, /* default resumption timeout in seconds */
325 MAX_PSK_ID_LEN
= 128, /* max psk identity/hint supported */
326 MAX_PSK_KEY_LEN
= 64, /* max psk key supported */
328 MAX_CHAIN_DEPTH
= 4, /* max cert chain peer depth */
329 MAX_X509_SIZE
= 2048, /* max static x509 buffer size */
330 FILE_BUFFER_SIZE
= 1024, /* default static file buffer size for input,
331 will use dynamic buffer if not big enough */
333 MAX_NTRU_PUB_KEY_SZ
= 1027, /* NTRU max for now */
334 MAX_NTRU_ENCRYPT_SZ
= 1027, /* NTRU max for now */
335 MAX_NTRU_BITS
= 256, /* max symmetric bit strength */
336 NO_SNIFF
= 0, /* not sniffing */
337 SNIFF
= 1, /* currently sniffing */
339 HASH_SIG_SIZE
= 2, /* default SHA1 RSA */
341 NO_COPY
= 0, /* should we copy static buffer for write */
342 COPY
= 1 /* should we copy static buffer for write */
350 SERVER_HELLOVERIFYREQUEST_COMPLETE
,
351 SERVER_HELLO_COMPLETE
,
352 SERVER_CERT_COMPLETE
,
353 SERVER_KEYEXCHANGE_COMPLETE
,
354 SERVER_HELLODONE_COMPLETE
,
355 SERVER_FINISHED_COMPLETE
,
357 CLIENT_HELLO_COMPLETE
,
358 CLIENT_KEYEXCHANGE_COMPLETE
,
359 CLIENT_FINISHED_COMPLETE
,
365 #ifndef SSL_TYPES_DEFINED
366 typedef struct SSL_METHOD SSL_METHOD
;
367 typedef struct SSL_CTX SSL_CTX
;
368 typedef struct SSL_SESSION SSL_SESSION
;
369 typedef struct SSL_CIPHER SSL_CIPHER
;
370 typedef struct SSL SSL
;
371 typedef struct X509 X509
;
372 typedef struct X509_CHAIN X509_CHAIN
;
373 typedef struct BIO BIO
;
374 typedef struct BIO_METHOD BIO_METHOD
;
377 typedef struct X509_NAME X509_NAME
;
379 typedef struct X509_STORE_CTX
{
382 X509
* current_cert
; /* stunnel dereference */
383 char* domain
; /* subject CN domain name */
387 typedef int (*pem_password_cb
)(char*, int, int, void*);
388 typedef int (*CallbackIORecv
)(char *buf
, int sz
, void *ctx
);
389 typedef int (*CallbackIOSend
)(char *buf
, int sz
, void *ctx
);
390 typedef int (*VerifyCallback
)(int, X509_STORE_CTX
*);
392 /* make sure external "C" linkage for C++ programs with callbacks */
393 void CyaSSL_SetIORecv(SSL_CTX
*, CallbackIORecv
);
394 void CyaSSL_SetIOSend(SSL_CTX
*, CallbackIOSend
);
396 void CyaSSL_SetIOReadCtx(SSL
* ssl
, void *ctx
);
397 void CyaSSL_SetIOWriteCtx(SSL
* ssl
, void *ctx
);
398 #endif /* SSL_TYPES_DEFINED */
402 typedef struct ProtocolVersion
{
408 ProtocolVersion
MakeSSLv3(void);
409 ProtocolVersion
MakeTLSv1(void);
410 ProtocolVersion
MakeTLSv1_1(void);
411 ProtocolVersion
MakeTLSv1_2(void);
414 ProtocolVersion
MakeDTLSv1(void);
425 /* OpenSSL BIO_METHOD type */
427 byte type
; /* method type */
431 /* OpenSSL BIO type */
433 byte type
; /* method type */
434 byte close
; /* close flag */
435 byte eof
; /* eof flag */
436 SSL
* ssl
; /* possible associated ssl */
437 int fd
; /* possible file descriptor */
438 BIO
* prev
; /* previous in chain */
439 BIO
* next
; /* next in chain */
443 /* OpenSSL method type */
445 ProtocolVersion version
;
446 int side
; /* connection side, server or client */
447 int verifyPeer
; /* request or send certificate */
448 int verifyNone
; /* whether to verify certificate */
449 int failNoCert
; /* fail if no certificate */
450 int downgrade
; /* whether to downgrade version, default no */
454 /* defautls to client */
455 void InitSSL_Method(SSL_METHOD
*, ProtocolVersion
);
458 int DoFinished(SSL
* ssl
, const byte
* input
, word32
* inOutIdx
, int sniff
);
459 int DoApplicationData(SSL
* ssl
, byte
* input
, word32
* inOutIdx
);
462 /* CyaSSL buffer type */
463 typedef struct buffer
{
475 /* only use compression extra if using compression */
477 #define COMP_EXTRA MAX_COMP_EXTRA
482 /* only the sniffer needs space in the buffer for an extra MTU record */
483 #ifdef CYASSL_SNIFFER
484 #define MTU_EXTRA MAX_MTU
489 /* give user option to use 16K static buffers, sniffer needs them too */
490 #if defined(LARGE_STATIC_BUFFERS) || defined(CYASSL_SNIFFER)
491 #define RECORD_SIZE MAX_RECORD_SIZE
494 #define RECORD_SIZE 1500
496 #define RECORD_SIZE 128
501 /* user option to turn off 16K output option */
502 /* if using small static buffers (default) and SSL_write tries to write data
503 larger than the record we have, dynamically get it, unless user says only
504 write in static buffer chuncks */
505 #ifndef STATIC_CHUNKS_ONLY
506 #define OUTPUT_RECORD_SIZE MAX_RECORD_SIZE
508 #define OUTPUT_RECORD_SIZE RECORD_SIZE
511 /* CyaSSL input buffer
516 The length (in bytes) of the following TLSPlaintext.fragment.
517 The length should not exceed 2^14.
519 #define STATIC_BUFFER_LEN RECORD_HEADER_SZ + RECORD_SIZE + COMP_EXTRA + \
520 MTU_EXTRA + MAX_MSG_EXTRA
523 word32 length
; /* total buffer length used */
524 word32 idx
; /* idx to part of length already consumed */
525 byte
* buffer
; /* place holder for static or dynamic buffer */
526 ALIGN16 byte staticBuffer
[STATIC_BUFFER_LEN
];
527 word32 bufferSize
; /* current buffer size */
528 byte dynamicFlag
; /* dynamic memory currently in use */
531 /* Cipher Suites holder */
532 typedef struct Suites
{
533 int setSuites
; /* user set suites from default */
534 byte suites
[MAX_SUITE_SZ
];
535 word16 suiteSz
; /* suite length in bytes */
539 void InitSuites(Suites
*, ProtocolVersion
, byte
, byte
, byte
, byte
, int);
540 int SetCipherList(SSL_CTX
* ctx
, const char* list
);
542 #ifndef PSK_TYPES_DEFINED
543 typedef unsigned int (*psk_client_callback
)(SSL
*, const char*, char*,
544 unsigned int, unsigned char*, unsigned int);
545 typedef unsigned int (*psk_server_callback
)(SSL
*, const char*,
546 unsigned char*, unsigned int);
547 #endif /* PSK_TYPES_DEFINED */
550 #ifndef CYASSL_USER_IO
551 /* default IO callbacks */
552 int EmbedReceive(char *buf
, int sz
, void *ctx
);
553 int EmbedSend(char *buf
, int sz
, void *ctx
);
561 /* OpenSSL Cipher type just points back to SSL */
567 /* OpenSSL context type */
572 Signer
* caList
; /* SSL_CTX owns this, SSL will reference */
574 void* heap
; /* for user memory overrides */
578 byte sessionCacheOff
;
579 byte sessionCacheFlushOff
;
580 byte sendVerify
; /* for client side */
581 byte haveDH
; /* server DH parms set by user */
582 byte haveNTRU
; /* server private NTRU key loaded */
583 byte haveECDSA
; /* server private ECDSA key loaded */
584 byte partialWrite
; /* only one msg per write call */
585 byte quietShutdown
; /* don't send close notify */
586 CallbackIORecv CBIORecv
;
587 CallbackIOSend CBIOSend
;
588 VerifyCallback verifyCallback
; /* cert verification callback */
590 byte havePSK
; /* psk key set by user */
591 psk_client_callback client_psk_cb
; /* client callback */
592 psk_server_callback server_psk_cb
; /* server callback */
593 char server_hint
[MAX_PSK_ID_LEN
];
595 #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
596 pem_password_cb passwd_cb
;
598 #endif /* OPENSSL_EXTRA */
602 void InitSSL_Ctx(SSL_CTX
*, SSL_METHOD
*);
603 void FreeSSL_Ctx(SSL_CTX
*);
604 void SSL_CtxResourceFree(SSL_CTX
*);
606 int DeriveTlsKeys(SSL
* ssl
);
607 int ProcessOldClientHello(SSL
* ssl
, const byte
* input
, word32
* inOutIdx
,
608 word32 inSz
, word16 sz
);
610 /* All cipher suite related info */
611 typedef struct CipherSpecs
{
612 byte bulk_cipher_algorithm
;
613 byte cipher_type
; /* block or stream */
615 byte kea
; /* key exchange algo */
626 /* Supported Ciphers from page 43 */
627 enum BulkCipherAlgorithm
{
632 triple_des
, /* leading 3 (3des) not valid identifier */
636 hc128
, /* CyaSSL extensions */
641 /* Supported Message Authentication Codes from page 43 */
654 /* Supported Key Exchange Protocols */
655 enum KeyExchangeAlgorithm
{
662 ecc_diffie_hellman_kea
666 /* Supported Authentication Schemes */
667 enum SignatureAlgorithm
{
668 anonymous_sa_algo
= 0,
675 /* Supprted ECC Curve Types */
681 /* Supprted ECC Named Curves */
682 enum EccNamedCurves
{
683 secp256r1
= 0x17, /* default, OpenSSL also calls it prime256v1 */
688 secp192r1
= 0x13, /* Openssl also call it prime192v1 */
693 /* Valid client certificate request types from page 27 */
694 enum ClientCertificateType
{
699 rsa_ephemeral_dh
= 5,
700 dss_ephemeral_dh
= 6,
701 fortezza_kea_cert
= 20
705 enum CipherType
{ stream
, block
};
708 /* keys and secrets */
709 typedef struct Keys
{
710 byte client_write_MAC_secret
[SHA_DIGEST_SIZE
]; /* max sizes */
711 byte server_write_MAC_secret
[SHA_DIGEST_SIZE
];
712 byte client_write_key
[AES_256_KEY_SIZE
]; /* max sizes */
713 byte server_write_key
[AES_256_KEY_SIZE
];
714 byte client_write_IV
[AES_IV_SIZE
]; /* max sizes */
715 byte server_write_IV
[AES_IV_SIZE
];
717 word32 peer_sequence_number
;
718 word32 sequence_number
;
721 word32 dtls_sequence_number
;
722 word32 dtls_peer_sequence_number
;
723 word16 dtls_handshake_number
;
725 word16 dtls_peer_epoch
;
728 word32 encryptSz
; /* last size of encrypted data */
729 byte encryptionOn
; /* true after change cipher spec */
754 typedef struct Hashes
{
755 byte md5
[MD5_DIGEST_SIZE
];
756 byte sha
[SHA_DIGEST_SIZE
];
760 /* Static x509 buffer */
761 typedef struct x509_buffer
{
762 int length
; /* actual size */
763 byte buffer
[MAX_X509_SIZE
]; /* max static cert size */
767 /* CyaSSL X509_CHAIN, for no dynamic memory SESSION_CACHE */
769 int count
; /* total number in chain */
770 x509_buffer certs
[MAX_CHAIN_DEPTH
]; /* only allow max depth 4 for now */
774 /* openSSL session type */
776 byte sessionID
[ID_LEN
];
777 byte masterSecret
[SECRET_LEN
];
778 word32 bornOn
; /* create time in seconds */
779 word32 timeout
; /* timeout in seconds */
781 X509_CHAIN chain
; /* peer cert chain, static */
782 ProtocolVersion version
;
783 byte cipherSuite0
; /* first byte, normally 0 */
784 byte cipherSuite
; /* 2nd byte, actual suite */
789 SSL_SESSION
* GetSession(SSL
*, byte
*);
790 int SetSession(SSL
*, SSL_SESSION
*);
792 typedef void (*hmacfp
) (SSL
*, byte
*, const byte
*, word32
, int, int);
795 /* client connect state for nonblocking restart */
799 HELLO_AGAIN
, /* HELLO_AGAIN s for DTLS case */
811 /* server accpet state for nonblocking restart */
814 ACCEPT_CLIENT_HELLO_DONE
,
816 ACCEPT_FIRST_REPLY_DONE
,
822 ACCEPT_SECOND_REPLY_DONE
,
824 ACCEPT_FINISHED_DONE
,
825 ACCEPT_THIRD_REPLY_DONE
829 typedef struct Buffers
{
830 buffer certificate
; /* SSL_CTX owns */
831 buffer key
; /* SSL_CTX owns */
832 buffer domainName
; /* for client check */
836 buffer serverDH_Priv
;
837 bufferStatic inputBuffer
;
838 bufferStatic outputBuffer
;
839 buffer clearOutputBuffer
;
840 int prevSent
; /* previous plain text bytes sent
841 when got WANT_WRITE */
842 int plainSz
; /* plain text bytes in buffer to send
843 when got WANT_WRITE */
847 typedef struct Options
{
848 byte sessionCacheOff
;
849 byte sessionCacheFlushOff
;
850 byte cipherSuite0
; /* first byte, normally 0 */
851 byte cipherSuite
; /* second byte, actual suite */
855 byte side
; /* client or server end */
859 byte downgrade
; /* allow downgrade of versions */
860 byte sendVerify
; /* false = 0, true = 1, sendBlank = 2 */
862 byte tls
; /* using TLS ? */
863 byte tls1_1
; /* using TLSv1.1+ ? */
864 byte dtls
; /* using datagrams ? */
865 byte connReset
; /* has the peer reset */
866 byte isClosed
; /* if we consider conn closed */
867 byte closeNotify
; /* we've recieved a close notify */
868 byte sentNotify
; /* we've sent a close notify */
869 byte connectState
; /* nonblocking resume */
870 byte acceptState
; /* nonblocking resume */
871 byte usingCompression
; /* are we using compression */
872 byte haveDH
; /* server DH parms set by user */
873 byte haveNTRU
; /* server NTRU private key loaded */
874 byte haveECDSA
; /* server ECDSA private key loaded */
875 byte havePeerCert
; /* do we have peer's cert */
876 byte usingPSK_cipher
; /* whether we're using psk as cipher */
877 byte sendAlertState
; /* nonblocking resume */
878 byte processReply
; /* nonblocking resume */
879 byte partialWrite
; /* only one msg per write call */
880 byte quietShutdown
; /* don't send close notify */
882 byte havePSK
; /* psk key set by user */
883 psk_client_callback client_psk_cb
;
884 psk_server_callback server_psk_cb
;
889 typedef struct Arrays
{
890 byte clientRandom
[RAN_LEN
];
891 byte serverRandom
[RAN_LEN
];
892 byte sessionID
[ID_LEN
];
893 byte preMasterSecret
[ENCRYPT_LEN
];
894 byte masterSecret
[SECRET_LEN
];
896 byte cookie
[MAX_COOKIE_LEN
];
899 char client_identity
[MAX_PSK_ID_LEN
];
900 char server_hint
[MAX_PSK_ID_LEN
];
901 byte psk_key
[MAX_PSK_KEY_LEN
];
902 word32 psk_keySz
; /* acutal size */
904 word32 preMasterSz
; /* differs for DH, actual size */
911 char name
[ASN_NAME_MAX
];
922 /* record layer header for PlainText, Compressed, and CipherText */
923 typedef struct RecordLayerHeader
{
925 ProtocolVersion version
;
930 /* record layer header for DTLS PlainText, Compressed, and CipherText */
931 typedef struct DtlsRecordLayerHeader
{
933 ProtocolVersion version
;
934 byte epoch
[2]; /* increment on cipher state change */
935 byte sequence_number
[6]; /* per record */
937 } DtlsRecordLayerHeader
;
940 /* OpenSSL ssl type */
944 ProtocolVersion version
; /* negotiated version */
945 ProtocolVersion chVersion
; /* client hello version */
951 int rfd
; /* read file descriptor */
952 int wfd
; /* write file descriptor */
953 BIO
* biord
; /* socket bio read to free/close */
954 BIO
* biowr
; /* socket bio write to free/close */
958 Md5 hashMd5
; /* md5 hash of handshake msgs */
959 Sha hashSha
; /* sha hash of handshake msgs */
961 Sha256 hashSha256
; /* sha256 hash of handshake msgs */
964 Hashes certHashes
; /* for cert verify */
965 Signer
* caList
; /* SSL_CTX owns */
971 byte peerRsaKeyPresent
;
973 word16 peerNtruKeyLen
;
974 byte peerNtruKey
[MAX_NTRU_PUB_KEY_SZ
];
975 byte peerNtruKeyPresent
;
978 ecc_key peerEccKey
; /* peer's ECDHE key */
979 byte peerEccKeyPresent
;
980 ecc_key peerEccDsaKey
; /* peer's ECDSA key */
981 byte peerEccDsaKeyPresent
;
982 ecc_key eccTempKey
; /* private ECDHE key */
983 byte eccTempKeyPresent
;
984 ecc_key eccDsaKey
; /* private ECDSA key */
985 byte eccDsaKeyPresent
;
988 void* heap
; /* for user overrides */
989 RecordLayerHeader curRL
;
993 z_stream c_stream
; /* compression stream */
994 z_stream d_stream
; /* decompression stream */
995 byte didStreamInit
; /* for stream init and end */
997 #ifdef CYASSL_CALLBACKS
998 HandShakeInfo handShakeInfo
; /* info saved during handshake */
999 TimeoutInfo timeoutInfo
; /* info saved during handshake */
1000 byte hsInfoOn
; /* track handshake info */
1001 byte toInfoOn
; /* track timeout info */
1003 #ifdef OPENSSL_EXTRA
1004 X509 peerCert
; /* X509 peer cert */
1009 int InitSSL(SSL
*, SSL_CTX
*);
1011 void SSL_ResourceFree(SSL
*);
1015 IV_SZ
= 32, /* max iv sz */
1016 NAME_SZ
= 80, /* max one line */
1020 typedef struct EncryptedInfo
{
1028 #ifdef CYASSL_CALLBACKS
1029 void InitHandShakeInfo(HandShakeInfo
*);
1030 void FinishHandShakeInfo(HandShakeInfo
*, const SSL
*);
1031 void AddPacketName(const char*, HandShakeInfo
*);
1033 void InitTimeoutInfo(TimeoutInfo
*);
1034 void FreeTimeoutInfo(TimeoutInfo
*, void*);
1035 void AddPacketInfo(const char*, TimeoutInfo
*, const byte
*, int, void*);
1036 void AddLateName(const char*, TimeoutInfo
*);
1037 void AddLateRecordHeader(const RecordLayerHeader
* rl
, TimeoutInfo
* info
);
1041 /* Record Layer Header identifier from page 12 */
1044 change_cipher_spec
= 20,
1047 application_data
= 23
1051 /* handshake header, same for each message type, pgs 20/21 */
1052 typedef struct HandShakeHeader
{
1058 /* DTLS handshake header, same for each message type */
1059 typedef struct DtlsHandShakeHeader
{
1062 byte message_seq
[2]; /* start at 0, restransmit gets same # */
1063 word24 fragment_offset
; /* bytes in previous fragments */
1064 word24 fragment_length
; /* length of this fragment */
1065 } DtlsHandShakeHeader
;
1068 enum HandShakeType
{
1073 hello_verify_request
= 3, /* DTLS addition */
1075 server_key_exchange
= 12,
1076 certificate_request
= 13,
1077 server_hello_done
= 14,
1078 certificate_verify
= 15,
1079 client_key_exchange
= 16,
1084 /* Valid Alert types from page 16/17 */
1085 enum AlertDescription
{
1087 unexpected_message
= 10,
1088 bad_record_mac
= 20,
1089 decompression_failure
= 30,
1090 handshake_failure
= 40,
1091 no_certificate
= 41,
1092 bad_certificate
= 42,
1093 unsupported_certificate
= 43,
1094 certificate_revoked
= 44,
1095 certificate_expired
= 45,
1096 certificate_unknown
= 46,
1097 illegal_parameter
= 47,
1102 /* I/O Callback default errors */
1104 IO_ERR_GENERAL
= -1, /* general unexpected err, not in below group */
1105 IO_ERR_WANT_READ
= -2, /* need to call read again */
1106 IO_ERR_WANT_WRITE
= -2, /* need to call write again */
1107 IO_ERR_CONN_RST
= -3, /* connection reset */
1108 IO_ERR_ISR
= -4, /* interrupt */
1109 IO_ERR_CONN_CLOSE
= -5 /* connection closed or epipe */
1119 static const byte client
[SIZEOF_SENDER
] = { 0x43, 0x4C, 0x4E, 0x54 };
1120 static const byte server
[SIZEOF_SENDER
] = { 0x53, 0x52, 0x56, 0x52 };
1122 static const byte tls_client
[FINISHED_LABEL_SZ
+ 1] = "client finished";
1123 static const byte tls_server
[FINISHED_LABEL_SZ
+ 1] = "server finished";
1126 /* internal functions */
1127 int SendChangeCipher(SSL
*);
1128 int SendData(SSL
*, const void*, int);
1129 int SendCertificate(SSL
*);
1130 int SendCertificateRequest(SSL
*);
1131 int SendServerKeyExchange(SSL
*);
1132 int SendBuffered(SSL
*);
1133 int ReceiveData(SSL
*, byte
*, int);
1134 int SendFinished(SSL
*);
1135 int SendAlert(SSL
*, int, int);
1136 int ProcessReply(SSL
*);
1138 int SetCipherSpecs(SSL
*);
1139 int MakeMasterSecret(SSL
*);
1141 int AddSession(SSL
*);
1142 int DeriveKeys(SSL
* ssl
);
1143 int StoreKeys(SSL
* ssl
, const byte
* keyData
);
1145 int IsTLS(const SSL
* ssl
);
1146 int IsAtLeastTLSv1_2(const SSL
* ssl
);
1148 void ShrinkInputBuffer(SSL
* ssl
, int forcedFree
);
1149 void ShrinkOutputBuffer(SSL
* ssl
);
1151 #ifndef NO_CYASSL_CLIENT
1152 int SendClientHello(SSL
*);
1153 int SendClientKeyExchange(SSL
*);
1154 int SendCertificateVerify(SSL
*);
1155 #endif /* NO_CYASSL_CLIENT */
1157 #ifndef NO_CYASSL_SERVER
1158 int SendServerHello(SSL
*);
1159 int SendServerHelloDone(SSL
*);
1161 int SendHelloVerifyRequest(SSL
*);
1163 #endif /* NO_CYASSL_SERVER */
1173 typedef double timer_d
;
1175 timer_d
Timer(void);
1176 word32
LowResTimer(void);
1179 #ifdef SINGLE_THREADED
1180 typedef int CyaSSL_Mutex
;
1181 #else /* MULTI_THREADED */
1182 #ifdef USE_WINDOWS_API
1183 typedef CRITICAL_SECTION CyaSSL_Mutex
;
1184 #elif defined(CYASSL_PTHREADS)
1185 typedef pthread_mutex_t CyaSSL_Mutex
;
1186 #elif defined(THREADX)
1187 typedef TX_MUTEX CyaSSL_Mutex
;
1188 #elif defined(MICRIUM)
1189 typedef OS_MUTEX CyaSSL_Mutex
;
1191 #error Need a mutex type in multithreaded mode
1192 #endif /* USE_WINDOWS_API */
1193 #endif /* SINGLE_THREADED */
1195 int InitMutex(CyaSSL_Mutex
*);
1196 int FreeMutex(CyaSSL_Mutex
*);
1197 int LockMutex(CyaSSL_Mutex
*);
1198 int UnLockMutex(CyaSSL_Mutex
*);
1203 void CYASSL_ENTER(const char* msg
);
1204 void CYASSL_LEAVE(const char* msg
, int ret
);
1206 void CYASSL_ERROR(int);
1207 void CYASSL_MSG(const char* msg
);
1209 #else /* DEBUG_CYASSL */
1211 #define CYASSL_ENTER(m)
1212 #define CYASSL_LEAVE(m, r)
1214 #define CYASSL_ERROR(e)
1215 #define CYASSL_MSG(m)
1217 #endif /* DEBUG_CYASSL */
1224 #endif /* CyaSSL_INT_H */