1/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to.  The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 *    notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 *    notice, this list of conditions and the following disclaimer in the
29 *    documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 *    must display the following acknowledgement:
32 *    "This product includes cryptographic software written by
33 *     Eric Young (eay@cryptsoft.com)"
34 *    The word 'cryptographic' can be left out if the rouines from the library
35 *    being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 *    the apps directory (application code) you must include an acknowledgement:
38 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed.  i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 *    notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 *    notice, this list of conditions and the following disclaimer in
69 *    the documentation and/or other materials provided with the
70 *    distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 *    software must display the following acknowledgment:
74 *    "This product includes software developed by the OpenSSL Project
75 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 *    endorse or promote products derived from this software without
79 *    prior written permission. For written permission, please contact
80 *    openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 *    nor may "OpenSSL" appear in their names without prior written
84 *    permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 *    acknowledgment:
88 *    "This product includes software developed by the OpenSSL Project
89 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com).  This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 *
113 * Portions of the attached software ("Contribution") are developed by
114 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
115 *
116 * The Contribution is licensed pursuant to the OpenSSL open source
117 * license provided above.
118 *
119 * ECC cipher suite support in OpenSSL originally written by
120 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
121 *
122 */
123/* ====================================================================
124 * Copyright 2005 Nokia. All rights reserved.
125 *
126 * The portions of the attached software ("Contribution") is developed by
127 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
128 * license.
129 *
130 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
131 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
132 * support (see RFC 4279) to OpenSSL.
133 *
134 * No patent licenses or other rights except those expressly stated in
135 * the OpenSSL open source license shall be deemed granted or received
136 * expressly, by implication, estoppel, or otherwise.
137 *
138 * No assurances are provided by Nokia that the Contribution does not
139 * infringe the patent or other intellectual property rights of any third
140 * party or that the license provides you with all the necessary rights
141 * to make use of the Contribution.
142 *
143 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
144 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
145 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
146 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
147 * OTHERWISE. */
148
149#include <assert.h>
150#include <stdio.h>
151#include <string.h>
152
153#include <openssl/buf.h>
154#include <openssl/dh.h>
155#include <openssl/err.h>
156#include <openssl/md5.h>
157#include <openssl/mem.h>
158#include <openssl/obj.h>
159
160#include "internal.h"
161
162
163const SSL3_ENC_METHOD SSLv3_enc_data = {
164    ssl3_prf,
165    tls1_setup_key_block,
166    tls1_generate_master_secret,
167    tls1_change_cipher_state,
168    ssl3_final_finish_mac,
169    ssl3_cert_verify_mac,
170    SSL3_MD_CLIENT_FINISHED_CONST, 4,
171    SSL3_MD_SERVER_FINISHED_CONST, 4,
172    ssl3_alert_code,
173    tls1_export_keying_material,
174    0,
175};
176
177int ssl3_supports_cipher(const SSL_CIPHER *cipher) {
178  return 1;
179}
180
181int ssl3_set_handshake_header(SSL *s, int htype, unsigned long len) {
182  uint8_t *p = (uint8_t *)s->init_buf->data;
183  *(p++) = htype;
184  l2n3(len, p);
185  s->init_num = (int)len + SSL3_HM_HEADER_LENGTH;
186  s->init_off = 0;
187
188  /* Add the message to the handshake hash. */
189  return ssl3_finish_mac(s, (uint8_t *)s->init_buf->data, s->init_num);
190}
191
192int ssl3_handshake_write(SSL *s) { return ssl3_do_write(s, SSL3_RT_HANDSHAKE); }
193
194int ssl3_new(SSL *s) {
195  SSL3_STATE *s3;
196
197  s3 = OPENSSL_malloc(sizeof *s3);
198  if (s3 == NULL) {
199    goto err;
200  }
201  memset(s3, 0, sizeof *s3);
202  memset(s3->rrec.seq_num, 0, sizeof(s3->rrec.seq_num));
203
204  s->s3 = s3;
205
206  /* Set the version to the highest supported version for TLS. This controls the
207   * initial state of |s->enc_method| and what the API reports as the version
208   * prior to negotiation.
209   *
210   * TODO(davidben): This is fragile and confusing. */
211  s->version = TLS1_2_VERSION;
212  return 1;
213err:
214  return 0;
215}
216
217void ssl3_free(SSL *s) {
218  if (s == NULL || s->s3 == NULL) {
219    return;
220  }
221
222  BUF_MEM_free(s->s3->sniff_buffer);
223  ssl3_cleanup_key_block(s);
224  ssl3_release_read_buffer(s);
225  ssl3_release_write_buffer(s);
226  DH_free(s->s3->tmp.dh);
227  EC_KEY_free(s->s3->tmp.ecdh);
228
229  sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
230  OPENSSL_free(s->s3->tmp.certificate_types);
231  OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
232  OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
233  OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
234  BIO_free(s->s3->handshake_buffer);
235  ssl3_free_digest_list(s);
236  OPENSSL_free(s->s3->alpn_selected);
237
238  OPENSSL_cleanse(s->s3, sizeof *s->s3);
239  OPENSSL_free(s->s3);
240  s->s3 = NULL;
241}
242
243static int ssl3_set_req_cert_type(CERT *c, const uint8_t *p, size_t len);
244
245int SSL_session_reused(const SSL *ssl) {
246  return ssl->hit;
247}
248
249int SSL_total_renegotiations(const SSL *ssl) {
250  return ssl->s3->total_renegotiations;
251}
252
253int SSL_num_renegotiations(const SSL *ssl) {
254  return SSL_total_renegotiations(ssl);
255}
256
257int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) {
258  return 0;
259}
260
261int SSL_need_rsa(const SSL *ssl) {
262  return 0;
263}
264
265int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) {
266  return 1;
267}
268
269int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) {
270  return 1;
271}
272
273int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
274  DH_free(ctx->cert->dh_tmp);
275  ctx->cert->dh_tmp = DHparams_dup(dh);
276  if (ctx->cert->dh_tmp == NULL) {
277    OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_tmp_dh, ERR_R_DH_LIB);
278    return 0;
279  }
280  return 1;
281}
282
283int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
284  DH_free(ssl->cert->dh_tmp);
285  ssl->cert->dh_tmp = DHparams_dup(dh);
286  if (ssl->cert->dh_tmp == NULL) {
287    OPENSSL_PUT_ERROR(SSL, SSL_set_tmp_dh, ERR_R_DH_LIB);
288    return 0;
289  }
290  return 1;
291}
292
293int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
294  if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
295    OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_tmp_ecdh, ERR_R_PASSED_NULL_PARAMETER);
296    return 0;
297  }
298  ctx->cert->ecdh_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
299  return 1;
300}
301
302int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
303  if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
304    OPENSSL_PUT_ERROR(SSL, SSL_set_tmp_ecdh, ERR_R_PASSED_NULL_PARAMETER);
305    return 0;
306  }
307  ssl->cert->ecdh_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
308  return 1;
309}
310
311int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
312  ctx->tlsext_channel_id_enabled = 1;
313  return 1;
314}
315
316int SSL_enable_tls_channel_id(SSL *ssl) {
317  ssl->tlsext_channel_id_enabled = 1;
318  return 1;
319}
320
321int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
322  ctx->tlsext_channel_id_enabled = 1;
323  if (EVP_PKEY_id(private_key) != EVP_PKEY_EC ||
324      EVP_PKEY_bits(private_key) != 256) {
325    OPENSSL_PUT_ERROR(SSL, SSL_CTX_set1_tls_channel_id,
326                      SSL_R_CHANNEL_ID_NOT_P256);
327    return 0;
328  }
329  EVP_PKEY_free(ctx->tlsext_channel_id_private);
330  ctx->tlsext_channel_id_private = EVP_PKEY_up_ref(private_key);
331  return 1;
332}
333
334int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
335  ssl->tlsext_channel_id_enabled = 1;
336  if (EVP_PKEY_id(private_key) != EVP_PKEY_EC ||
337      EVP_PKEY_bits(private_key) != 256) {
338    OPENSSL_PUT_ERROR(SSL, SSL_set1_tls_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
339    return 0;
340  }
341  EVP_PKEY_free(ssl->tlsext_channel_id_private);
342  ssl->tlsext_channel_id_private = EVP_PKEY_up_ref(private_key);
343  return 1;
344}
345
346size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
347  if (!ssl->s3->tlsext_channel_id_valid) {
348    return 0;
349  }
350  memcpy(out, ssl->s3->tlsext_channel_id, (max_out < 64) ? max_out : 64);
351  return 64;
352}
353
354int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
355  OPENSSL_free(ssl->tlsext_hostname);
356  ssl->tlsext_hostname = NULL;
357
358  if (name == NULL) {
359    return 1;
360  }
361  if (strlen(name) > TLSEXT_MAXLEN_host_name) {
362    OPENSSL_PUT_ERROR(SSL, SSL_set_tlsext_host_name,
363                      SSL_R_SSL3_EXT_INVALID_SERVERNAME);
364    return 0;
365  }
366  ssl->tlsext_hostname = BUF_strdup(name);
367  if (ssl->tlsext_hostname == NULL) {
368    OPENSSL_PUT_ERROR(SSL, SSL_set_tlsext_host_name, ERR_R_MALLOC_FAILURE);
369    return 0;
370  }
371  return 1;
372}
373
374long ssl3_ctrl(SSL *s, int cmd, long larg, void *parg) {
375  int ret = 0;
376
377  switch (cmd) {
378    case SSL_CTRL_CHAIN:
379      if (larg) {
380        return ssl_cert_set1_chain(s->cert, (STACK_OF(X509) *)parg);
381      } else {
382        return ssl_cert_set0_chain(s->cert, (STACK_OF(X509) *)parg);
383      }
384
385    case SSL_CTRL_CHAIN_CERT:
386      if (larg) {
387        return ssl_cert_add1_chain_cert(s->cert, (X509 *)parg);
388      } else {
389        return ssl_cert_add0_chain_cert(s->cert, (X509 *)parg);
390      }
391
392    case SSL_CTRL_GET_CHAIN_CERTS:
393      *(STACK_OF(X509) **)parg = s->cert->key->chain;
394      ret = 1;
395      break;
396
397    case SSL_CTRL_SELECT_CURRENT_CERT:
398      return ssl_cert_select_current(s->cert, (X509 *)parg);
399
400    case SSL_CTRL_GET_CURVES: {
401      const uint16_t *clist = s->s3->tmp.peer_ellipticcurvelist;
402      size_t clistlen = s->s3->tmp.peer_ellipticcurvelist_length;
403      if (parg) {
404        size_t i;
405        int *cptr = parg;
406        int nid;
407        for (i = 0; i < clistlen; i++) {
408          nid = tls1_ec_curve_id2nid(clist[i]);
409          if (nid != NID_undef) {
410            cptr[i] = nid;
411          } else {
412            cptr[i] = TLSEXT_nid_unknown | clist[i];
413          }
414        }
415      }
416      return (int)clistlen;
417    }
418
419    case SSL_CTRL_SET_CURVES:
420      return tls1_set_curves(&s->tlsext_ellipticcurvelist,
421                             &s->tlsext_ellipticcurvelist_length, parg, larg);
422
423    case SSL_CTRL_SET_SIGALGS:
424      return tls1_set_sigalgs(s->cert, parg, larg, 0);
425
426    case SSL_CTRL_SET_CLIENT_SIGALGS:
427      return tls1_set_sigalgs(s->cert, parg, larg, 1);
428
429    case SSL_CTRL_GET_CLIENT_CERT_TYPES: {
430      const uint8_t **pctype = parg;
431      if (s->server || !s->s3->tmp.cert_req) {
432        return 0;
433      }
434      if (pctype) {
435        *pctype = s->s3->tmp.certificate_types;
436      }
437      return (int)s->s3->tmp.num_certificate_types;
438    }
439
440    case SSL_CTRL_SET_CLIENT_CERT_TYPES:
441      if (!s->server) {
442        return 0;
443      }
444      return ssl3_set_req_cert_type(s->cert, parg, larg);
445
446    case SSL_CTRL_BUILD_CERT_CHAIN:
447      return ssl_build_cert_chain(s->cert, s->ctx->cert_store, larg);
448
449    case SSL_CTRL_SET_VERIFY_CERT_STORE:
450      return ssl_cert_set_cert_store(s->cert, parg, 0, larg);
451
452    case SSL_CTRL_SET_CHAIN_CERT_STORE:
453      return ssl_cert_set_cert_store(s->cert, parg, 1, larg);
454
455    case SSL_CTRL_GET_SERVER_TMP_KEY:
456      if (s->server || !s->session || !s->session->sess_cert) {
457        return 0;
458      } else {
459        SESS_CERT *sc;
460        EVP_PKEY *ptmp;
461        int rv = 0;
462        sc = s->session->sess_cert;
463        if (!sc->peer_dh_tmp && !sc->peer_ecdh_tmp) {
464          return 0;
465        }
466        ptmp = EVP_PKEY_new();
467        if (!ptmp) {
468          return 0;
469        }
470        if (sc->peer_dh_tmp) {
471          rv = EVP_PKEY_set1_DH(ptmp, sc->peer_dh_tmp);
472        } else if (sc->peer_ecdh_tmp) {
473          rv = EVP_PKEY_set1_EC_KEY(ptmp, sc->peer_ecdh_tmp);
474        }
475        if (rv) {
476          *(EVP_PKEY **)parg = ptmp;
477          return 1;
478        }
479        EVP_PKEY_free(ptmp);
480        return 0;
481      }
482
483    case SSL_CTRL_GET_EC_POINT_FORMATS: {
484      const uint8_t **pformat = parg;
485      if (!s->s3->tmp.peer_ecpointformatlist) {
486        return 0;
487      }
488      *pformat = s->s3->tmp.peer_ecpointformatlist;
489      return (int)s->s3->tmp.peer_ecpointformatlist_length;
490    }
491
492    default:
493      break;
494  }
495
496  return ret;
497}
498
499long ssl3_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
500  switch (cmd) {
501    case SSL_CTRL_SET_TLSEXT_TICKET_KEYS:
502    case SSL_CTRL_GET_TLSEXT_TICKET_KEYS: {
503      uint8_t *keys = parg;
504      if (!keys) {
505        return 48;
506      }
507      if (larg != 48) {
508        OPENSSL_PUT_ERROR(SSL, ssl3_ctx_ctrl, SSL_R_INVALID_TICKET_KEYS_LENGTH);
509        return 0;
510      }
511      if (cmd == SSL_CTRL_SET_TLSEXT_TICKET_KEYS) {
512        memcpy(ctx->tlsext_tick_key_name, keys, 16);
513        memcpy(ctx->tlsext_tick_hmac_key, keys + 16, 16);
514        memcpy(ctx->tlsext_tick_aes_key, keys + 32, 16);
515      } else {
516        memcpy(keys, ctx->tlsext_tick_key_name, 16);
517        memcpy(keys + 16, ctx->tlsext_tick_hmac_key, 16);
518        memcpy(keys + 32, ctx->tlsext_tick_aes_key, 16);
519      }
520      return 1;
521    }
522
523    case SSL_CTRL_SET_CURVES:
524      return tls1_set_curves(&ctx->tlsext_ellipticcurvelist,
525                             &ctx->tlsext_ellipticcurvelist_length, parg, larg);
526
527    case SSL_CTRL_SET_SIGALGS:
528      return tls1_set_sigalgs(ctx->cert, parg, larg, 0);
529
530    case SSL_CTRL_SET_CLIENT_SIGALGS:
531      return tls1_set_sigalgs(ctx->cert, parg, larg, 1);
532
533    case SSL_CTRL_SET_CLIENT_CERT_TYPES:
534      return ssl3_set_req_cert_type(ctx->cert, parg, larg);
535
536    case SSL_CTRL_BUILD_CERT_CHAIN:
537      return ssl_build_cert_chain(ctx->cert, ctx->cert_store, larg);
538
539    case SSL_CTRL_SET_VERIFY_CERT_STORE:
540      return ssl_cert_set_cert_store(ctx->cert, parg, 0, larg);
541
542    case SSL_CTRL_SET_CHAIN_CERT_STORE:
543      return ssl_cert_set_cert_store(ctx->cert, parg, 1, larg);
544
545    case SSL_CTRL_EXTRA_CHAIN_CERT:
546      if (ctx->extra_certs == NULL) {
547        ctx->extra_certs = sk_X509_new_null();
548        if (ctx->extra_certs == NULL) {
549          return 0;
550        }
551      }
552      sk_X509_push(ctx->extra_certs, (X509 *)parg);
553      break;
554
555    case SSL_CTRL_GET_EXTRA_CHAIN_CERTS:
556      if (ctx->extra_certs == NULL && larg == 0) {
557        *(STACK_OF(X509) **)parg = ctx->cert->key->chain;
558      } else {
559        *(STACK_OF(X509) **)parg = ctx->extra_certs;
560      }
561      break;
562
563    case SSL_CTRL_CLEAR_EXTRA_CHAIN_CERTS:
564      sk_X509_pop_free(ctx->extra_certs, X509_free);
565      ctx->extra_certs = NULL;
566      break;
567
568    case SSL_CTRL_CHAIN:
569      if (larg) {
570        return ssl_cert_set1_chain(ctx->cert, (STACK_OF(X509) *)parg);
571      } else {
572        return ssl_cert_set0_chain(ctx->cert, (STACK_OF(X509) *)parg);
573      }
574
575    case SSL_CTRL_CHAIN_CERT:
576      if (larg) {
577        return ssl_cert_add1_chain_cert(ctx->cert, (X509 *)parg);
578      } else {
579        return ssl_cert_add0_chain_cert(ctx->cert, (X509 *)parg);
580      }
581
582    case SSL_CTRL_GET_CHAIN_CERTS:
583      *(STACK_OF(X509) **)parg = ctx->cert->key->chain;
584      break;
585
586    case SSL_CTRL_SELECT_CURRENT_CERT:
587      return ssl_cert_select_current(ctx->cert, (X509 *)parg);
588
589    default:
590      return 0;
591  }
592
593  return 1;
594}
595
596int SSL_CTX_set_tlsext_servername_callback(
597    SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
598  ctx->tlsext_servername_callback = callback;
599  return 1;
600}
601
602int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
603  ctx->tlsext_servername_arg = arg;
604  return 1;
605}
606
607int SSL_CTX_set_tlsext_ticket_key_cb(
608    SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
609                                  EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
610                                  int encrypt)) {
611  ctx->tlsext_ticket_key_cb = callback;
612  return 1;
613}
614
615struct ssl_cipher_preference_list_st *ssl_get_cipher_preferences(SSL *s) {
616  if (s->cipher_list != NULL) {
617    return s->cipher_list;
618  }
619
620  if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
621      s->ctx->cipher_list_tls11 != NULL) {
622    return s->ctx->cipher_list_tls11;
623  }
624
625  if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
626    return s->ctx->cipher_list;
627  }
628
629  return NULL;
630}
631
632const SSL_CIPHER *ssl3_choose_cipher(
633    SSL *s, STACK_OF(SSL_CIPHER) *clnt,
634    struct ssl_cipher_preference_list_st *server_pref) {
635  const SSL_CIPHER *c, *ret = NULL;
636  STACK_OF(SSL_CIPHER) *srvr = server_pref->ciphers, *prio, *allow;
637  size_t i;
638  int ok;
639  size_t cipher_index;
640  uint32_t alg_k, alg_a, mask_k, mask_a;
641  /* in_group_flags will either be NULL, or will point to an array of bytes
642   * which indicate equal-preference groups in the |prio| stack. See the
643   * comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
644   * struct. */
645  const uint8_t *in_group_flags;
646  /* group_min contains the minimal index so far found in a group, or -1 if no
647   * such value exists yet. */
648  int group_min = -1;
649
650  if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
651    prio = srvr;
652    in_group_flags = server_pref->in_group_flags;
653    allow = clnt;
654  } else {
655    prio = clnt;
656    in_group_flags = NULL;
657    allow = srvr;
658  }
659
660  ssl_get_compatible_server_ciphers(s, &mask_k, &mask_a);
661
662  for (i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
663    c = sk_SSL_CIPHER_value(prio, i);
664
665    ok = 1;
666
667    /* Skip TLS v1.2 only ciphersuites if not supported */
668    if ((c->algorithm_ssl & SSL_TLSV1_2) && !SSL_USE_TLS1_2_CIPHERS(s)) {
669      ok = 0;
670    }
671
672    alg_k = c->algorithm_mkey;
673    alg_a = c->algorithm_auth;
674
675    ok = ok && (alg_k & mask_k) && (alg_a & mask_a);
676
677    if (ok && sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
678      if (in_group_flags != NULL && in_group_flags[i] == 1) {
679        /* This element of |prio| is in a group. Update the minimum index found
680         * so far and continue looking. */
681        if (group_min == -1 || (size_t)group_min > cipher_index) {
682          group_min = cipher_index;
683        }
684      } else {
685        if (group_min != -1 && (size_t)group_min < cipher_index) {
686          cipher_index = group_min;
687        }
688        ret = sk_SSL_CIPHER_value(allow, cipher_index);
689        break;
690      }
691    }
692
693    if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
694      /* We are about to leave a group, but we found a match in it, so that's
695       * our answer. */
696      ret = sk_SSL_CIPHER_value(allow, group_min);
697      break;
698    }
699  }
700
701  return ret;
702}
703
704int ssl3_get_req_cert_type(SSL *s, uint8_t *p) {
705  int ret = 0;
706  const uint8_t *sig;
707  size_t i, siglen;
708  int have_rsa_sign = 0;
709  int have_ecdsa_sign = 0;
710
711  /* If we have custom certificate types set, use them */
712  if (s->cert->client_certificate_types) {
713    memcpy(p, s->cert->client_certificate_types,
714           s->cert->num_client_certificate_types);
715    return s->cert->num_client_certificate_types;
716  }
717
718  /* get configured sigalgs */
719  siglen = tls12_get_psigalgs(s, &sig);
720  for (i = 0; i < siglen; i += 2, sig += 2) {
721    switch (sig[1]) {
722      case TLSEXT_signature_rsa:
723        have_rsa_sign = 1;
724        break;
725
726      case TLSEXT_signature_ecdsa:
727        have_ecdsa_sign = 1;
728        break;
729    }
730  }
731
732  if (have_rsa_sign) {
733    p[ret++] = SSL3_CT_RSA_SIGN;
734  }
735
736  /* ECDSA certs can be used with RSA cipher suites as well so we don't need to
737   * check for SSL_kECDH or SSL_kECDHE. */
738  if (s->version >= TLS1_VERSION && have_ecdsa_sign) {
739      p[ret++] = TLS_CT_ECDSA_SIGN;
740  }
741
742  return ret;
743}
744
745static int ssl3_set_req_cert_type(CERT *c, const uint8_t *p, size_t len) {
746  OPENSSL_free(c->client_certificate_types);
747  c->client_certificate_types = NULL;
748  c->num_client_certificate_types = 0;
749
750  if (!p || !len) {
751    return 1;
752  }
753
754  if (len > 0xff) {
755    return 0;
756  }
757
758  c->client_certificate_types = BUF_memdup(p, len);
759  if (!c->client_certificate_types) {
760    return 0;
761  }
762
763  c->num_client_certificate_types = len;
764  return 1;
765}
766
767/* If we are using default SHA1+MD5 algorithms switch to new SHA256 PRF and
768 * handshake macs if required. */
769uint32_t ssl_get_algorithm2(SSL *s) {
770  static const uint32_t kMask = SSL_HANDSHAKE_MAC_DEFAULT;
771  uint32_t alg2 = s->s3->tmp.new_cipher->algorithm2;
772  if (s->enc_method->enc_flags & SSL_ENC_FLAG_SHA256_PRF &&
773      (alg2 & kMask) == kMask) {
774    return SSL_HANDSHAKE_MAC_SHA256;
775  }
776  return alg2;
777}
778