xref: /dragonfly/crypto/libressl/ssl/ssl_locl.h (revision 71990c18)
1 /* $OpenBSD: ssl_locl.h,v 1.128 2015/09/12 15:08:54 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 /* ====================================================================
59  * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
60  *
61  * Redistribution and use in source and binary forms, with or without
62  * modification, are permitted provided that the following conditions
63  * are met:
64  *
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer.
67  *
68  * 2. Redistributions in binary form must reproduce the above copyright
69  *    notice, this list of conditions and the following disclaimer in
70  *    the documentation and/or other materials provided with the
71  *    distribution.
72  *
73  * 3. All advertising materials mentioning features or use of this
74  *    software must display the following acknowledgment:
75  *    "This product includes software developed by the OpenSSL Project
76  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77  *
78  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79  *    endorse or promote products derived from this software without
80  *    prior written permission. For written permission, please contact
81  *    openssl-core@openssl.org.
82  *
83  * 5. Products derived from this software may not be called "OpenSSL"
84  *    nor may "OpenSSL" appear in their names without prior written
85  *    permission of the OpenSSL Project.
86  *
87  * 6. Redistributions of any form whatsoever must retain the following
88  *    acknowledgment:
89  *    "This product includes software developed by the OpenSSL Project
90  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91  *
92  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103  * OF THE POSSIBILITY OF SUCH DAMAGE.
104  * ====================================================================
105  *
106  * This product includes cryptographic software written by Eric Young
107  * (eay@cryptsoft.com).  This product includes software written by Tim
108  * Hudson (tjh@cryptsoft.com).
109  *
110  */
111 /* ====================================================================
112  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113  * ECC cipher suite support in OpenSSL originally developed by
114  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
115  */
116 /* ====================================================================
117  * Copyright 2005 Nokia. All rights reserved.
118  *
119  * The portions of the attached software ("Contribution") is developed by
120  * Nokia Corporation and is licensed pursuant to the OpenSSL open source
121  * license.
122  *
123  * The Contribution, originally written by Mika Kousa and Pasi Eronen of
124  * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
125  * support (see RFC 4279) to OpenSSL.
126  *
127  * No patent licenses or other rights except those expressly stated in
128  * the OpenSSL open source license shall be deemed granted or received
129  * expressly, by implication, estoppel, or otherwise.
130  *
131  * No assurances are provided by Nokia that the Contribution does not
132  * infringe the patent or other intellectual property rights of any third
133  * party or that the license provides you with all the necessary rights
134  * to make use of the Contribution.
135  *
136  * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
137  * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
138  * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
139  * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
140  * OTHERWISE.
141  */
142 
143 #ifndef HEADER_SSL_LOCL_H
144 #define HEADER_SSL_LOCL_H
145 
146 #include <sys/types.h>
147 
148 #include <errno.h>
149 #include <stdlib.h>
150 #include <string.h>
151 #include <time.h>
152 #include <unistd.h>
153 
154 #include <openssl/opensslconf.h>
155 #include <openssl/bio.h>
156 #include <openssl/buffer.h>
157 #include <openssl/dsa.h>
158 #include <openssl/err.h>
159 #include <openssl/rsa.h>
160 #include <openssl/ssl.h>
161 #include <openssl/stack.h>
162 
163 #define c2l(c,l)	(l = ((unsigned long)(*((c)++)))     , \
164 			 l|=(((unsigned long)(*((c)++)))<< 8), \
165 			 l|=(((unsigned long)(*((c)++)))<<16), \
166 			 l|=(((unsigned long)(*((c)++)))<<24))
167 
168 /* NOTE - c is not incremented as per c2l */
169 #define c2ln(c,l1,l2,n)	{ \
170 			c+=n; \
171 			l1=l2=0; \
172 			switch (n) { \
173 			case 8: l2 =((unsigned long)(*(--(c))))<<24; \
174 			case 7: l2|=((unsigned long)(*(--(c))))<<16; \
175 			case 6: l2|=((unsigned long)(*(--(c))))<< 8; \
176 			case 5: l2|=((unsigned long)(*(--(c))));     \
177 			case 4: l1 =((unsigned long)(*(--(c))))<<24; \
178 			case 3: l1|=((unsigned long)(*(--(c))))<<16; \
179 			case 2: l1|=((unsigned long)(*(--(c))))<< 8; \
180 			case 1: l1|=((unsigned long)(*(--(c))));     \
181 				} \
182 			}
183 
184 #define l2c(l,c)	(*((c)++)=(unsigned char)(((l)    )&0xff), \
185 			 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
186 			 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
187 			 *((c)++)=(unsigned char)(((l)>>24)&0xff))
188 
189 #define n2l(c,l)	(l =((unsigned long)(*((c)++)))<<24, \
190 			 l|=((unsigned long)(*((c)++)))<<16, \
191 			 l|=((unsigned long)(*((c)++)))<< 8, \
192 			 l|=((unsigned long)(*((c)++))))
193 
194 #define l2n(l,c)	(*((c)++)=(unsigned char)(((l)>>24)&0xff), \
195 			 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
196 			 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
197 			 *((c)++)=(unsigned char)(((l)    )&0xff))
198 
199 #define l2n8(l,c)	(*((c)++)=(unsigned char)(((l)>>56)&0xff), \
200 			 *((c)++)=(unsigned char)(((l)>>48)&0xff), \
201 			 *((c)++)=(unsigned char)(((l)>>40)&0xff), \
202 			 *((c)++)=(unsigned char)(((l)>>32)&0xff), \
203 			 *((c)++)=(unsigned char)(((l)>>24)&0xff), \
204 			 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
205 			 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
206 			 *((c)++)=(unsigned char)(((l)    )&0xff))
207 
208 /* NOTE - c is not incremented as per l2c */
209 #define l2cn(l1,l2,c,n)	{ \
210 			c+=n; \
211 			switch (n) { \
212 			case 8: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
213 			case 7: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
214 			case 6: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
215 			case 5: *(--(c))=(unsigned char)(((l2)    )&0xff); \
216 			case 4: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
217 			case 3: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
218 			case 2: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
219 			case 1: *(--(c))=(unsigned char)(((l1)    )&0xff); \
220 				} \
221 			}
222 
223 #define n2s(c,s)	((s=(((unsigned int)(c[0]))<< 8)| \
224 			    (((unsigned int)(c[1]))    )),c+=2)
225 #define s2n(s,c)	((c[0]=(unsigned char)(((s)>> 8)&0xff), \
226 			  c[1]=(unsigned char)(((s)    )&0xff)),c+=2)
227 
228 #define n2l3(c,l)	((l =(((unsigned long)(c[0]))<<16)| \
229 			     (((unsigned long)(c[1]))<< 8)| \
230 			     (((unsigned long)(c[2]))    )),c+=3)
231 
232 #define l2n3(l,c)	((c[0]=(unsigned char)(((l)>>16)&0xff), \
233 			  c[1]=(unsigned char)(((l)>> 8)&0xff), \
234 			  c[2]=(unsigned char)(((l)    )&0xff)),c+=3)
235 
236 /* LOCAL STUFF */
237 
238 #define SSL_DECRYPT	0
239 #define SSL_ENCRYPT	1
240 
241 /*
242  * Define the Bitmasks for SSL_CIPHER.algorithms.
243  * This bits are used packed as dense as possible. If new methods/ciphers
244  * etc will be added, the bits a likely to change, so this information
245  * is for internal library use only, even though SSL_CIPHER.algorithms
246  * can be publicly accessed.
247  * Use the according functions for cipher management instead.
248  *
249  * The bit mask handling in the selection and sorting scheme in
250  * ssl_create_cipher_list() has only limited capabilities, reflecting
251  * that the different entities within are mutually exclusive:
252  * ONLY ONE BIT PER MASK CAN BE SET AT A TIME.
253  */
254 
255 /* Bits for algorithm_mkey (key exchange algorithm) */
256 #define SSL_kRSA		0x00000001L /* RSA key exchange */
257 #define SSL_kDHE		0x00000008L /* tmp DH key no DH cert */
258 #define SSL_kECDHr		0x00000020L /* ECDH cert, RSA CA cert */
259 #define SSL_kECDHe		0x00000040L /* ECDH cert, ECDSA CA cert */
260 #define SSL_kECDHE		0x00000080L /* ephemeral ECDH */
261 #define SSL_kGOST		0x00000200L /* GOST key exchange */
262 
263 /* Bits for algorithm_auth (server authentication) */
264 #define SSL_aRSA		0x00000001L /* RSA auth */
265 #define SSL_aDSS 		0x00000002L /* DSS auth */
266 #define SSL_aNULL 		0x00000004L /* no auth (i.e. use ADH or AECDH) */
267 #define SSL_aECDH 		0x00000010L /* Fixed ECDH auth (kECDHe or kECDHr) */
268 #define SSL_aECDSA              0x00000040L /* ECDSA auth*/
269 #define SSL_aGOST01 		0x00000200L /* GOST R 34.10-2001 signature auth */
270 
271 
272 /* Bits for algorithm_enc (symmetric encryption) */
273 #define SSL_DES			0x00000001L
274 #define SSL_3DES		0x00000002L
275 #define SSL_RC4			0x00000004L
276 #define SSL_IDEA		0x00000008L
277 #define SSL_eNULL		0x00000010L
278 #define SSL_AES128		0x00000020L
279 #define SSL_AES256		0x00000040L
280 #define SSL_CAMELLIA128		0x00000080L
281 #define SSL_CAMELLIA256		0x00000100L
282 #define SSL_eGOST2814789CNT	0x00000200L
283 #define SSL_AES128GCM		0x00000400L
284 #define SSL_AES256GCM		0x00000800L
285 #define SSL_CHACHA20POLY1305	0x00001000L
286 #define SSL_CHACHA20POLY1305_OLD	0x00002000L
287 
288 #define SSL_AES        		(SSL_AES128|SSL_AES256|SSL_AES128GCM|SSL_AES256GCM)
289 #define SSL_CAMELLIA		(SSL_CAMELLIA128|SSL_CAMELLIA256)
290 
291 
292 /* Bits for algorithm_mac (symmetric authentication) */
293 
294 #define SSL_MD5			0x00000001L
295 #define SSL_SHA1		0x00000002L
296 #define SSL_GOST94      0x00000004L
297 #define SSL_GOST89MAC   0x00000008L
298 #define SSL_SHA256		0x00000010L
299 #define SSL_SHA384		0x00000020L
300 /* Not a real MAC, just an indication it is part of cipher */
301 #define SSL_AEAD		0x00000040L
302 #define SSL_STREEBOG256		0x00000080L
303 #define SSL_STREEBOG512		0x00000100L
304 
305 /* Bits for algorithm_ssl (protocol version) */
306 #define SSL_SSLV3		0x00000002L
307 #define SSL_TLSV1		SSL_SSLV3	/* for now */
308 #define SSL_TLSV1_2		0x00000004L
309 
310 
311 /* Bits for algorithm2 (handshake digests and other extra flags) */
312 
313 #define SSL_HANDSHAKE_MAC_MD5 0x10
314 #define SSL_HANDSHAKE_MAC_SHA 0x20
315 #define SSL_HANDSHAKE_MAC_GOST94 0x40
316 #define SSL_HANDSHAKE_MAC_SHA256 0x80
317 #define SSL_HANDSHAKE_MAC_SHA384 0x100
318 #define SSL_HANDSHAKE_MAC_STREEBOG256 0x200
319 #define SSL_HANDSHAKE_MAC_STREEBOG512 0x400
320 #define SSL_HANDSHAKE_MAC_DEFAULT (SSL_HANDSHAKE_MAC_MD5 | SSL_HANDSHAKE_MAC_SHA)
321 
322 /* When adding new digest in the ssl_ciph.c and increment SSM_MD_NUM_IDX
323  * make sure to update this constant too */
324 #define SSL_MAX_DIGEST 8
325 
326 #define SSL3_CK_ID		0x03000000
327 #define SSL3_CK_VALUE_MASK	0x0000ffff
328 
329 #define TLS1_PRF_DGST_MASK	(0xff << TLS1_PRF_DGST_SHIFT)
330 
331 #define TLS1_PRF_DGST_SHIFT 10
332 #define TLS1_PRF_MD5 (SSL_HANDSHAKE_MAC_MD5 << TLS1_PRF_DGST_SHIFT)
333 #define TLS1_PRF_SHA1 (SSL_HANDSHAKE_MAC_SHA << TLS1_PRF_DGST_SHIFT)
334 #define TLS1_PRF_SHA256 (SSL_HANDSHAKE_MAC_SHA256 << TLS1_PRF_DGST_SHIFT)
335 #define TLS1_PRF_SHA384 (SSL_HANDSHAKE_MAC_SHA384 << TLS1_PRF_DGST_SHIFT)
336 #define TLS1_PRF_GOST94 (SSL_HANDSHAKE_MAC_GOST94 << TLS1_PRF_DGST_SHIFT)
337 #define TLS1_PRF_STREEBOG256 (SSL_HANDSHAKE_MAC_STREEBOG256 << TLS1_PRF_DGST_SHIFT)
338 #define TLS1_PRF (TLS1_PRF_MD5 | TLS1_PRF_SHA1)
339 
340 /* Stream MAC for GOST ciphersuites from cryptopro draft
341  * (currently this also goes into algorithm2) */
342 #define TLS1_STREAM_MAC 0x04
343 
344 /*
345  * SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_IN_RECORD is an algorithm2 flag that
346  * indicates that the variable part of the nonce is included as a prefix of
347  * the record (AES-GCM, for example, does this with an 8-byte variable nonce.)
348  */
349 #define SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_IN_RECORD (1 << 22)
350 
351 /*
352  * SSL_CIPHER_ALGORITHM2_AEAD is an algorithm2 flag that indicates the cipher
353  * is implemented via an EVP_AEAD.
354  */
355 #define SSL_CIPHER_ALGORITHM2_AEAD (1 << 23)
356 
357 /*
358  * SSL_CIPHER_AEAD_FIXED_NONCE_LEN returns the number of bytes of fixed nonce
359  * for an SSL_CIPHER with the SSL_CIPHER_ALGORITHM2_AEAD flag.
360  */
361 #define SSL_CIPHER_AEAD_FIXED_NONCE_LEN(ssl_cipher) \
362 	(((ssl_cipher->algorithm2 >> 24) & 0xf) * 2)
363 
364 /*
365  * Cipher strength information.
366  */
367 #define SSL_STRONG_MASK		0x000001fcL
368 #define SSL_STRONG_NONE		0x00000004L
369 #define SSL_LOW			0x00000020L
370 #define SSL_MEDIUM		0x00000040L
371 #define SSL_HIGH		0x00000080L
372 
373 /*
374  * The keylength (measured in RSA key bits, I guess)  for temporary keys.
375  * Cipher argument is so that this can be variable in the future.
376  */
377 #define SSL_C_PKEYLENGTH(c)	1024
378 
379 /* Check if an SSL structure is using DTLS. */
380 #define SSL_IS_DTLS(s) (s->method->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS)
381 
382 /* See if we need explicit IV. */
383 #define SSL_USE_EXPLICIT_IV(s) \
384 	(s->method->ssl3_enc->enc_flags & SSL_ENC_FLAG_EXPLICIT_IV)
385 
386 /* See if we use signature algorithms extension. */
387 #define SSL_USE_SIGALGS(s) \
388 	(s->method->ssl3_enc->enc_flags & SSL_ENC_FLAG_SIGALGS)
389 
390 /* Allow TLS 1.2 ciphersuites: applies to DTLS 1.2 as well as TLS 1.2. */
391 #define SSL_USE_TLS1_2_CIPHERS(s) \
392 	(s->method->ssl3_enc->enc_flags & SSL_ENC_FLAG_TLS1_2_CIPHERS)
393 
394 /* Mostly for SSLv3 */
395 #define SSL_PKEY_RSA_ENC	0
396 #define SSL_PKEY_RSA_SIGN	1
397 #define SSL_PKEY_DSA_SIGN	2
398 #define SSL_PKEY_DH_RSA		3
399 #define SSL_PKEY_DH_DSA		4
400 #define SSL_PKEY_ECC            5
401 #define SSL_PKEY_GOST01		6
402 #define SSL_PKEY_NUM		7
403 
404 /* SSL_kRSA <- RSA_ENC | (RSA_TMP & RSA_SIGN) |
405  * 	    <- (EXPORT & (RSA_ENC | RSA_TMP) & RSA_SIGN)
406  * SSL_kDH  <- DH_ENC & (RSA_ENC | RSA_SIGN | DSA_SIGN)
407  * SSL_kDHE <- RSA_ENC | RSA_SIGN | DSA_SIGN
408  * SSL_aRSA <- RSA_ENC | RSA_SIGN
409  * SSL_aDSS <- DSA_SIGN
410  */
411 
412 /*
413 #define CERT_INVALID		0
414 #define CERT_PUBLIC_KEY		1
415 #define CERT_PRIVATE_KEY	2
416 */
417 
418 /* From ECC-TLS draft, used in encoding the curve type in
419  * ECParameters
420  */
421 #define EXPLICIT_PRIME_CURVE_TYPE  1
422 #define EXPLICIT_CHAR2_CURVE_TYPE  2
423 #define NAMED_CURVE_TYPE           3
424 
425 typedef struct cert_pkey_st {
426 	X509 *x509;
427 	EVP_PKEY *privatekey;
428 	/* Digest to use when signing */
429 	const EVP_MD *digest;
430 } CERT_PKEY;
431 
432 typedef struct cert_st {
433 	/* Current active set */
434 	CERT_PKEY *key; /* ALWAYS points to an element of the pkeys array
435 			 * Probably it would make more sense to store
436 			 * an index, not a pointer. */
437 
438 	/* The following masks are for the key and auth
439 	 * algorithms that are supported by the certs below */
440 	int valid;
441 	unsigned long mask_k;
442 	unsigned long mask_a;
443 
444 	DH *dh_tmp;
445 	DH *(*dh_tmp_cb)(SSL *ssl, int is_export, int keysize);
446 	int dh_tmp_auto;
447 
448 	EC_KEY *ecdh_tmp;
449 	EC_KEY *(*ecdh_tmp_cb)(SSL *ssl, int is_export, int keysize);
450 	int ecdh_tmp_auto;
451 
452 	CERT_PKEY pkeys[SSL_PKEY_NUM];
453 
454 	int references; /* >1 only if SSL_copy_session_id is used */
455 } CERT;
456 
457 
458 typedef struct sess_cert_st {
459 	STACK_OF(X509) *cert_chain; /* as received from peer */
460 
461 	/* The 'peer_...' members are used only by clients. */
462 	int peer_cert_type;
463 
464 	CERT_PKEY *peer_key; /* points to an element of peer_pkeys (never NULL!) */
465 	CERT_PKEY peer_pkeys[SSL_PKEY_NUM];
466 	/* Obviously we don't have the private keys of these,
467 	 * so maybe we shouldn't even use the CERT_PKEY type here. */
468 
469 	DH *peer_dh_tmp;
470 	EC_KEY *peer_ecdh_tmp;
471 
472 	int references; /* actually always 1 at the moment */
473 } SESS_CERT;
474 
475 
476 /*#define SSL_DEBUG	*/
477 /*#define RSA_DEBUG	*/
478 
479 /* This is for the SSLv3/TLSv1.0 differences in crypto/hash stuff
480  * It is a bit of a mess of functions, but hell, think of it as
481  * an opaque structure :-) */
482 typedef struct ssl3_enc_method {
483 	int (*enc)(SSL *, int);
484 	int (*mac)(SSL *, unsigned char *, int);
485 	int (*setup_key_block)(SSL *);
486 	int (*generate_master_secret)(SSL *, unsigned char *,
487 	    unsigned char *, int);
488 	int (*change_cipher_state)(SSL *, int);
489 	int (*final_finish_mac)(SSL *,  const char *, int, unsigned char *);
490 	int finish_mac_length;
491 	int (*cert_verify_mac)(SSL *, int, unsigned char *);
492 	const char *client_finished_label;
493 	int client_finished_label_len;
494 	const char *server_finished_label;
495 	int server_finished_label_len;
496 	int (*alert_value)(int);
497 	int (*export_keying_material)(SSL *, unsigned char *, size_t,
498 	    const char *, size_t, const unsigned char *, size_t,
499 	    int use_context);
500 	/* Flags indicating protocol version requirements. */
501 	unsigned int enc_flags;
502 } SSL3_ENC_METHOD;
503 
504 /*
505  * Flag values for enc_flags.
506  */
507 
508 /* Uses explicit IV. */
509 #define SSL_ENC_FLAG_EXPLICIT_IV        (1 << 0)
510 
511 /* Uses signature algorithms extension. */
512 #define SSL_ENC_FLAG_SIGALGS            (1 << 1)
513 
514 /* Uses SHA256 default PRF. */
515 #define SSL_ENC_FLAG_SHA256_PRF         (1 << 2)
516 
517 /* Is DTLS. */
518 #define SSL_ENC_FLAG_DTLS               (1 << 3)
519 
520 /* Allow TLS 1.2 ciphersuites: applies to DTLS 1.2 as well as TLS 1.2. */
521 #define SSL_ENC_FLAG_TLS1_2_CIPHERS     (1 << 4)
522 
523 /*
524  * ssl_aead_ctx_st contains information about an AEAD that is being used to
525  * encrypt an SSL connection.
526  */
527 struct ssl_aead_ctx_st {
528 	EVP_AEAD_CTX ctx;
529 	/*
530 	 * fixed_nonce contains any bytes of the nonce that are fixed for all
531 	 * records.
532 	 */
533 	unsigned char fixed_nonce[12];
534 	unsigned char fixed_nonce_len;
535 	unsigned char variable_nonce_len;
536 	unsigned char xor_fixed_nonce;
537 	unsigned char tag_len;
538 	/*
539 	 * variable_nonce_in_record is non-zero if the variable nonce
540 	 * for a record is included as a prefix before the ciphertext.
541 	 */
542 	char variable_nonce_in_record;
543 };
544 
545 extern SSL3_ENC_METHOD ssl3_undef_enc_method;
546 extern SSL_CIPHER ssl3_ciphers[];
547 
548 const char *ssl_version_string(int ver);
549 uint16_t ssl_max_server_version(SSL *s);
550 
551 extern SSL3_ENC_METHOD DTLSv1_enc_data;
552 extern SSL3_ENC_METHOD TLSv1_enc_data;
553 extern SSL3_ENC_METHOD TLSv1_1_enc_data;
554 extern SSL3_ENC_METHOD TLSv1_2_enc_data;
555 
556 void ssl_clear_cipher_ctx(SSL *s);
557 int ssl_clear_bad_session(SSL *s);
558 CERT *ssl_cert_new(void);
559 CERT *ssl_cert_dup(CERT *cert);
560 int ssl_cert_inst(CERT **o);
561 void ssl_cert_free(CERT *c);
562 SESS_CERT *ssl_sess_cert_new(void);
563 void ssl_sess_cert_free(SESS_CERT *sc);
564 int ssl_get_new_session(SSL *s, int session);
565 int ssl_get_prev_session(SSL *s, unsigned char *session, int len,
566     const unsigned char *limit);
567 int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b);
568 DECLARE_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id);
569 int ssl_cipher_ptr_id_cmp(const SSL_CIPHER * const *ap,
570     const SSL_CIPHER * const *bp);
571 STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const unsigned char *p,
572     int num);
573 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
574     unsigned char *p);
575 STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *meth,
576     STACK_OF(SSL_CIPHER) **pref, STACK_OF(SSL_CIPHER) **sorted,
577     const char *rule_str);
578 void ssl_update_cache(SSL *s, int mode);
579 int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
580     const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size);
581 int ssl_cipher_get_evp_aead(const SSL_SESSION *s, const EVP_AEAD **aead);
582 int ssl_get_handshake_digest(int i, long *mask, const EVP_MD **md);
583 
584 int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk);
585 int ssl_undefined_function(SSL *s);
586 int ssl_undefined_void_function(void);
587 int ssl_undefined_const_function(const SSL *s);
588 CERT_PKEY *ssl_get_server_send_pkey(const SSL *s);
589 X509 *ssl_get_server_send_cert(const SSL *);
590 EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *c, const EVP_MD **pmd);
591 DH *ssl_get_auto_dh(SSL *s);
592 int ssl_cert_type(X509 *x, EVP_PKEY *pkey);
593 void ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher);
594 STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s);
595 int ssl_verify_alarm_type(long type);
596 void ssl_load_ciphers(void);
597 
598 const SSL_CIPHER *ssl3_get_cipher_by_char(const unsigned char *p);
599 int ssl3_put_cipher_by_char(const SSL_CIPHER *c, unsigned char *p);
600 int ssl3_send_server_certificate(SSL *s);
601 int ssl3_send_newsession_ticket(SSL *s);
602 int ssl3_send_cert_status(SSL *s);
603 int ssl3_get_finished(SSL *s, int state_a, int state_b);
604 int ssl3_send_change_cipher_spec(SSL *s, int state_a, int state_b);
605 int ssl3_do_write(SSL *s, int type);
606 int ssl3_send_alert(SSL *s, int level, int desc);
607 int ssl3_get_req_cert_type(SSL *s, unsigned char *p);
608 long ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok);
609 int ssl3_send_finished(SSL *s, int a, int b, const char *sender, int slen);
610 int ssl3_num_ciphers(void);
611 const SSL_CIPHER *ssl3_get_cipher(unsigned int u);
612 const SSL_CIPHER *ssl3_get_cipher_by_id(unsigned int id);
613 const SSL_CIPHER *ssl3_get_cipher_by_value(uint16_t value);
614 uint16_t ssl3_cipher_get_value(const SSL_CIPHER *c);
615 int ssl3_renegotiate(SSL *ssl);
616 
617 int ssl3_renegotiate_check(SSL *ssl);
618 
619 int ssl3_dispatch_alert(SSL *s);
620 int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek);
621 int ssl3_write_bytes(SSL *s, int type, const void *buf, int len);
622 unsigned long ssl3_output_cert_chain(SSL *s, X509 *x);
623 SSL_CIPHER *ssl3_choose_cipher(SSL *ssl, STACK_OF(SSL_CIPHER) *clnt,
624     STACK_OF(SSL_CIPHER) *srvr);
625 int	ssl3_setup_buffers(SSL *s);
626 int	ssl3_setup_init_buffer(SSL *s);
627 int	ssl3_setup_read_buffer(SSL *s);
628 int	ssl3_setup_write_buffer(SSL *s);
629 int	ssl3_release_read_buffer(SSL *s);
630 int	ssl3_release_write_buffer(SSL *s);
631 int	ssl3_new(SSL *s);
632 void	ssl3_free(SSL *s);
633 int	ssl3_accept(SSL *s);
634 int	ssl3_connect(SSL *s);
635 int	ssl3_read(SSL *s, void *buf, int len);
636 int	ssl3_peek(SSL *s, void *buf, int len);
637 int	ssl3_write(SSL *s, const void *buf, int len);
638 int	ssl3_shutdown(SSL *s);
639 void	ssl3_clear(SSL *s);
640 long	ssl3_ctrl(SSL *s, int cmd, long larg, void *parg);
641 long	ssl3_ctx_ctrl(SSL_CTX *s, int cmd, long larg, void *parg);
642 long	ssl3_callback_ctrl(SSL *s, int cmd, void (*fp)(void));
643 long	ssl3_ctx_callback_ctrl(SSL_CTX *s, int cmd, void (*fp)(void));
644 int	ssl3_pending(const SSL *s);
645 
646 int ssl3_handshake_msg_hdr_len(SSL *s);
647 unsigned char *ssl3_handshake_msg_start(SSL *s, uint8_t htype);
648 void ssl3_handshake_msg_finish(SSL *s, unsigned int len);
649 int ssl3_handshake_write(SSL *s);
650 
651 void tls1_record_sequence_increment(unsigned char *seq);
652 int ssl3_do_change_cipher_spec(SSL *ssl);
653 
654 int ssl23_read(SSL *s, void *buf, int len);
655 int ssl23_peek(SSL *s, void *buf, int len);
656 int ssl23_write(SSL *s, const void *buf, int len);
657 long ssl23_default_timeout(void);
658 
659 long tls1_default_timeout(void);
660 int dtls1_do_write(SSL *s, int type);
661 int ssl3_read_n(SSL *s, int n, int max, int extend);
662 int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek);
663 int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
664     unsigned int len);
665 unsigned char *dtls1_set_message_header(SSL *s, unsigned char *p,
666     unsigned char mt, unsigned long len, unsigned long frag_off,
667     unsigned long frag_len);
668 
669 int dtls1_write_app_data_bytes(SSL *s, int type, const void *buf, int len);
670 int dtls1_write_bytes(SSL *s, int type, const void *buf, int len);
671 
672 int dtls1_send_change_cipher_spec(SSL *s, int a, int b);
673 unsigned long dtls1_output_cert_chain(SSL *s, X509 *x);
674 int dtls1_read_failed(SSL *s, int code);
675 int dtls1_buffer_message(SSL *s, int ccs);
676 int dtls1_retransmit_message(SSL *s, unsigned short seq,
677     unsigned long frag_off, int *found);
678 int dtls1_get_queue_priority(unsigned short seq, int is_ccs);
679 int dtls1_retransmit_buffered_messages(SSL *s);
680 void dtls1_clear_record_buffer(SSL *s);
681 int dtls1_get_message_header(unsigned char *data,
682     struct hm_header_st *msg_hdr);
683 void dtls1_get_ccs_header(unsigned char *data, struct ccs_header_st *ccs_hdr);
684 void dtls1_reset_seq_numbers(SSL *s, int rw);
685 void dtls1_build_sequence_number(unsigned char *dst, unsigned char *seq,
686     unsigned short epoch);
687 long dtls1_default_timeout(void);
688 struct timeval* dtls1_get_timeout(SSL *s, struct timeval* timeleft);
689 int dtls1_check_timeout_num(SSL *s);
690 int dtls1_handle_timeout(SSL *s);
691 const SSL_CIPHER *dtls1_get_cipher(unsigned int u);
692 void dtls1_start_timer(SSL *s);
693 void dtls1_stop_timer(SSL *s);
694 int dtls1_is_timer_expired(SSL *s);
695 void dtls1_double_timeout(SSL *s);
696 unsigned int dtls1_min_mtu(void);
697 
698 /* some client-only functions */
699 int ssl3_client_hello(SSL *s);
700 int ssl3_get_server_hello(SSL *s);
701 int ssl3_get_certificate_request(SSL *s);
702 int ssl3_get_new_session_ticket(SSL *s);
703 int ssl3_get_cert_status(SSL *s);
704 int ssl3_get_server_done(SSL *s);
705 int ssl3_send_client_verify(SSL *s);
706 int ssl3_send_client_certificate(SSL *s);
707 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey);
708 int ssl3_send_client_key_exchange(SSL *s);
709 int ssl3_get_key_exchange(SSL *s);
710 int ssl3_get_server_certificate(SSL *s);
711 int ssl3_check_cert_and_algorithm(SSL *s);
712 int ssl3_check_finished(SSL *s);
713 int ssl3_send_next_proto(SSL *s);
714 
715 int dtls1_send_client_certificate(SSL *s);
716 
717 /* some server-only functions */
718 int ssl3_get_client_hello(SSL *s);
719 int ssl3_send_server_hello(SSL *s);
720 int ssl3_send_hello_request(SSL *s);
721 int ssl3_send_server_key_exchange(SSL *s);
722 int ssl3_send_certificate_request(SSL *s);
723 int ssl3_send_server_done(SSL *s);
724 int ssl3_get_client_certificate(SSL *s);
725 int ssl3_get_client_key_exchange(SSL *s);
726 int ssl3_get_cert_verify(SSL *s);
727 int ssl3_get_next_proto(SSL *s);
728 
729 int dtls1_send_server_certificate(SSL *s);
730 
731 int ssl23_accept(SSL *s);
732 int ssl23_connect(SSL *s);
733 int ssl23_read_bytes(SSL *s, int n);
734 int ssl23_write_bytes(SSL *s);
735 
736 int tls1_new(SSL *s);
737 void tls1_free(SSL *s);
738 void tls1_clear(SSL *s);
739 long tls1_ctrl(SSL *s, int cmd, long larg, void *parg);
740 long tls1_callback_ctrl(SSL *s, int cmd, void (*fp)(void));
741 
742 int dtls1_new(SSL *s);
743 int dtls1_accept(SSL *s);
744 int dtls1_connect(SSL *s);
745 void dtls1_free(SSL *s);
746 void dtls1_clear(SSL *s);
747 long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg);
748 int dtls1_shutdown(SSL *s);
749 
750 long dtls1_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok);
751 int dtls1_get_record(SSL *s);
752 int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
753     unsigned int len);
754 int dtls1_dispatch_alert(SSL *s);
755 int dtls1_enc(SSL *s, int snd);
756 
757 int ssl_init_wbio_buffer(SSL *s, int push);
758 void ssl_free_wbio_buffer(SSL *s);
759 
760 int tls1_init_finished_mac(SSL *s);
761 void tls1_finish_mac(SSL *s, const unsigned char *buf, int len);
762 void tls1_free_digest_list(SSL *s);
763 void tls1_cleanup_key_block(SSL *s);
764 int tls1_digest_cached_records(SSL *s);
765 int tls1_change_cipher_state(SSL *s, int which);
766 int tls1_setup_key_block(SSL *s);
767 int tls1_enc(SSL *s, int snd);
768 int tls1_final_finish_mac(SSL *s, const char *str, int slen, unsigned char *p);
769 int tls1_cert_verify_mac(SSL *s, int md_nid, unsigned char *p);
770 int tls1_mac(SSL *ssl, unsigned char *md, int snd);
771 int tls1_generate_master_secret(SSL *s, unsigned char *out,
772     unsigned char *p, int len);
773 int tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen,
774     const char *label, size_t llen, const unsigned char *p, size_t plen,
775     int use_context);
776 int tls1_alert_code(int code);
777 int ssl_ok(SSL *s);
778 
779 int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s);
780 
781 SSL_COMP *ssl3_comp_find(STACK_OF(SSL_COMP) *sk, int n);
782 
783 int tls1_ec_curve_id2nid(uint16_t curve_id);
784 uint16_t tls1_ec_nid2curve_id(int nid);
785 int tls1_check_curve(SSL *s, const unsigned char *p, size_t len);
786 int tls1_get_shared_curve(SSL *s);
787 
788 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p,
789     unsigned char *limit);
790 
791 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p,
792     unsigned char *limit);
793 
794 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **data,
795     unsigned char *d, int n, int *al);
796 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **data,
797     unsigned char *d, int n, int *al);
798 int ssl_check_clienthello_tlsext_early(SSL *s);
799 int ssl_check_clienthello_tlsext_late(SSL *s);
800 int ssl_check_serverhello_tlsext(SSL *s);
801 
802 #define tlsext_tick_md	EVP_sha256
803 int tls1_process_ticket(SSL *s, const unsigned char *session_id, int len,
804     const unsigned char *limit, SSL_SESSION **ret);
805 int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk,
806     const EVP_MD *md);
807 int tls12_get_sigid(const EVP_PKEY *pk);
808 const EVP_MD *tls12_get_hash(unsigned char hash_alg);
809 
810 void ssl_clear_hash_ctx(EVP_MD_CTX **hash);
811 int ssl_add_serverhello_renegotiate_ext(SSL *s, unsigned char *p,
812     int *len, int maxlen);
813 int ssl_parse_serverhello_renegotiate_ext(SSL *s, const unsigned char *d,
814     int len, int *al);
815 int ssl_add_clienthello_renegotiate_ext(SSL *s, unsigned char *p,
816     int *len, int maxlen);
817 int ssl_parse_clienthello_renegotiate_ext(SSL *s, const unsigned char *d,
818     int len, int *al);
819 long ssl_get_algorithm2(SSL *s);
820 int tls1_process_sigalgs(SSL *s, const unsigned char *data, int dsize);
821 int tls12_get_req_sig_algs(SSL *s, unsigned char *p);
822 
823 int tls1_check_ec_server_key(SSL *s);
824 int tls1_check_ec_tmp_key(SSL *s);
825 
826 int ssl_add_clienthello_use_srtp_ext(SSL *s, unsigned char *p,
827     int *len, int maxlen);
828 int ssl_parse_clienthello_use_srtp_ext(SSL *s, const unsigned char *d,
829     int len, int *al);
830 int ssl_add_serverhello_use_srtp_ext(SSL *s, unsigned char *p,
831     int *len, int maxlen);
832 int ssl_parse_serverhello_use_srtp_ext(SSL *s, const unsigned char *d,
833     int len, int *al);
834 
835 /* s3_cbc.c */
836 void ssl3_cbc_copy_mac(unsigned char *out, const SSL3_RECORD *rec,
837     unsigned md_size, unsigned orig_len);
838 int tls1_cbc_remove_padding(const SSL *s, SSL3_RECORD *rec,
839     unsigned block_size, unsigned mac_size);
840 char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx);
841 int ssl3_cbc_digest_record(const EVP_MD_CTX *ctx, unsigned char *md_out,
842     size_t *md_out_size, const unsigned char header[13],
843     const unsigned char *data, size_t data_plus_mac_size,
844     size_t data_plus_mac_plus_padding_size, const unsigned char *mac_secret,
845     unsigned mac_secret_length, char is_sslv3);
846 
847 #endif
848