blob: 553d4c95f65d07320b883b8d08ed78172971bdab [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */
114
David Benjaminf0ae1702015-04-07 23:05:04 -0400115#include <errno.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700116#include <stdio.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400117#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700118
119#include <openssl/bio.h>
120#include <openssl/bn.h>
David Benjamin676d1e72014-07-08 14:34:10 -0400121#include <openssl/buf.h>
David Benjamin680ca962015-06-18 12:37:23 -0400122#include <openssl/ec_key.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700123#include <openssl/dh.h>
124#include <openssl/err.h>
125#include <openssl/mem.h>
126#include <openssl/obj.h>
127#include <openssl/pem.h>
David Benjamin680ca962015-06-18 12:37:23 -0400128#include <openssl/x509.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700129#include <openssl/x509v3.h>
130
131#include "../crypto/dh/internal.h"
132#include "../crypto/directory.h"
Adam Langley0da323a2015-05-15 12:49:30 -0700133#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400134#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700135
Adam Langley95c29f32014-06-20 12:00:00 -0700136
Adam Langleyfcf25832014-12-18 17:42:32 -0800137int SSL_get_ex_data_X509_STORE_CTX_idx(void) {
David Benjaminaa585132015-06-29 23:36:17 -0400138 /* The ex_data index to go from |X509_STORE_CTX| to |SSL| always uses the
139 * reserved app_data slot. Before ex_data was introduced, app_data was used.
140 * Avoid breaking any software which assumes |X509_STORE_CTX_get_app_data|
141 * works. */
142 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800143}
Adam Langley95c29f32014-06-20 12:00:00 -0700144
Adam Langleyfcf25832014-12-18 17:42:32 -0800145CERT *ssl_cert_new(void) {
David Benjamind1d80782015-07-05 11:54:09 -0400146 CERT *ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
Adam Langleyfcf25832014-12-18 17:42:32 -0800147 if (ret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400148 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800149 return NULL;
150 }
151 memset(ret, 0, sizeof(CERT));
Adam Langley95c29f32014-06-20 12:00:00 -0700152
Adam Langleyfcf25832014-12-18 17:42:32 -0800153 return ret;
154}
Adam Langley95c29f32014-06-20 12:00:00 -0700155
Adam Langleyfcf25832014-12-18 17:42:32 -0800156CERT *ssl_cert_dup(CERT *cert) {
David Benjamind1d80782015-07-05 11:54:09 -0400157 CERT *ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
Adam Langleyfcf25832014-12-18 17:42:32 -0800158 if (ret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400159 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800160 return NULL;
161 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800162 memset(ret, 0, sizeof(CERT));
Adam Langley95c29f32014-06-20 12:00:00 -0700163
Adam Langleyfcf25832014-12-18 17:42:32 -0800164 ret->mask_k = cert->mask_k;
165 ret->mask_a = cert->mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -0700166
Adam Langleyfcf25832014-12-18 17:42:32 -0800167 if (cert->dh_tmp != NULL) {
168 ret->dh_tmp = DHparams_dup(cert->dh_tmp);
169 if (ret->dh_tmp == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400170 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800171 goto err;
172 }
173 if (cert->dh_tmp->priv_key) {
174 BIGNUM *b = BN_dup(cert->dh_tmp->priv_key);
175 if (!b) {
David Benjamin3570d732015-06-29 00:28:17 -0400176 OPENSSL_PUT_ERROR(SSL, ERR_R_BN_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800177 goto err;
178 }
179 ret->dh_tmp->priv_key = b;
180 }
181 if (cert->dh_tmp->pub_key) {
182 BIGNUM *b = BN_dup(cert->dh_tmp->pub_key);
183 if (!b) {
David Benjamin3570d732015-06-29 00:28:17 -0400184 OPENSSL_PUT_ERROR(SSL, ERR_R_BN_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800185 goto err;
186 }
187 ret->dh_tmp->pub_key = b;
188 }
189 }
190 ret->dh_tmp_cb = cert->dh_tmp_cb;
Adam Langley95c29f32014-06-20 12:00:00 -0700191
David Benjaminc0f763b2015-03-27 02:05:39 -0400192 ret->ecdh_nid = cert->ecdh_nid;
Adam Langleyfcf25832014-12-18 17:42:32 -0800193 ret->ecdh_tmp_cb = cert->ecdh_tmp_cb;
Adam Langley95c29f32014-06-20 12:00:00 -0700194
David Benjamind1d80782015-07-05 11:54:09 -0400195 if (cert->x509 != NULL) {
196 ret->x509 = X509_up_ref(cert->x509);
197 }
Adam Langley95c29f32014-06-20 12:00:00 -0700198
David Benjamind1d80782015-07-05 11:54:09 -0400199 if (cert->privatekey != NULL) {
200 ret->privatekey = EVP_PKEY_up_ref(cert->privatekey);
201 }
Adam Langley95c29f32014-06-20 12:00:00 -0700202
David Benjamind1d80782015-07-05 11:54:09 -0400203 if (cert->chain) {
204 ret->chain = X509_chain_up_ref(cert->chain);
205 if (!ret->chain) {
David Benjamin3570d732015-06-29 00:28:17 -0400206 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamind1d80782015-07-05 11:54:09 -0400207 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800208 }
209 }
Adam Langley95c29f32014-06-20 12:00:00 -0700210
David Benjaminb85a4c22015-03-19 14:47:27 -0400211 /* Copy over signature algorithm configuration. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800212 if (cert->conf_sigalgs) {
David Benjaminb85a4c22015-03-19 14:47:27 -0400213 ret->conf_sigalgs = BUF_memdup(cert->conf_sigalgs, cert->conf_sigalgslen);
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 if (!ret->conf_sigalgs) {
215 goto err;
216 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800217 ret->conf_sigalgslen = cert->conf_sigalgslen;
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 }
Adam Langley95c29f32014-06-20 12:00:00 -0700219
Adam Langleyfcf25832014-12-18 17:42:32 -0800220 if (cert->client_sigalgs) {
David Benjaminb85a4c22015-03-19 14:47:27 -0400221 ret->client_sigalgs = BUF_memdup(cert->client_sigalgs,
222 cert->client_sigalgslen);
Adam Langleyfcf25832014-12-18 17:42:32 -0800223 if (!ret->client_sigalgs) {
224 goto err;
225 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800226 ret->client_sigalgslen = cert->client_sigalgslen;
Adam Langleyfcf25832014-12-18 17:42:32 -0800227 }
David Benjaminb85a4c22015-03-19 14:47:27 -0400228
Adam Langleyfcf25832014-12-18 17:42:32 -0800229 /* Copy any custom client certificate types */
230 if (cert->client_certificate_types) {
231 ret->client_certificate_types = BUF_memdup(
232 cert->client_certificate_types, cert->num_client_certificate_types);
233 if (!ret->client_certificate_types) {
234 goto err;
235 }
236 ret->num_client_certificate_types = cert->num_client_certificate_types;
237 }
Adam Langley95c29f32014-06-20 12:00:00 -0700238
Adam Langleyfcf25832014-12-18 17:42:32 -0800239 ret->cert_cb = cert->cert_cb;
240 ret->cert_cb_arg = cert->cert_cb_arg;
241
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 return ret;
243
Adam Langley95c29f32014-06-20 12:00:00 -0700244err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800245 ssl_cert_free(ret);
246 return NULL;
247}
Adam Langley95c29f32014-06-20 12:00:00 -0700248
249/* Free up and clear all certificates and chains */
David Benjamind1d80782015-07-05 11:54:09 -0400250void ssl_cert_clear_certs(CERT *cert) {
251 if (cert == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 return;
253 }
Adam Langley95c29f32014-06-20 12:00:00 -0700254
David Benjamind1d80782015-07-05 11:54:09 -0400255 X509_free(cert->x509);
256 cert->x509 = NULL;
257 EVP_PKEY_free(cert->privatekey);
258 cert->privatekey = NULL;
259 sk_X509_pop_free(cert->chain, X509_free);
260 cert->chain = NULL;
David Benjamin71d2e542015-07-06 00:30:25 -0400261 cert->key_method = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800262}
Adam Langley95c29f32014-06-20 12:00:00 -0700263
Adam Langleyfcf25832014-12-18 17:42:32 -0800264void ssl_cert_free(CERT *c) {
265 if (c == NULL) {
266 return;
267 }
Adam Langley95c29f32014-06-20 12:00:00 -0700268
David Benjamin2755a3e2015-04-22 16:17:58 -0400269 DH_free(c->dh_tmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700270
Adam Langleyfcf25832014-12-18 17:42:32 -0800271 ssl_cert_clear_certs(c);
David Benjamin2755a3e2015-04-22 16:17:58 -0400272 OPENSSL_free(c->peer_sigalgs);
273 OPENSSL_free(c->conf_sigalgs);
274 OPENSSL_free(c->client_sigalgs);
275 OPENSSL_free(c->shared_sigalgs);
276 OPENSSL_free(c->client_certificate_types);
Adam Langley95c29f32014-06-20 12:00:00 -0700277
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 OPENSSL_free(c);
279}
Adam Langley95c29f32014-06-20 12:00:00 -0700280
David Benjamind1d80782015-07-05 11:54:09 -0400281int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) {
282 sk_X509_pop_free(cert->chain, X509_free);
283 cert->chain = chain;
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 return 1;
285}
Adam Langley95c29f32014-06-20 12:00:00 -0700286
David Benjamind1d80782015-07-05 11:54:09 -0400287int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) {
David Benjamin60da0cd2015-05-03 15:21:28 -0400288 STACK_OF(X509) *dchain;
David Benjamind1d80782015-07-05 11:54:09 -0400289 if (chain == NULL) {
290 return ssl_cert_set0_chain(cert, NULL);
Adam Langleyfcf25832014-12-18 17:42:32 -0800291 }
Adam Langley95c29f32014-06-20 12:00:00 -0700292
Adam Langleyfcf25832014-12-18 17:42:32 -0800293 dchain = X509_chain_up_ref(chain);
David Benjamind1d80782015-07-05 11:54:09 -0400294 if (dchain == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800295 return 0;
296 }
Adam Langley95c29f32014-06-20 12:00:00 -0700297
David Benjamind1d80782015-07-05 11:54:09 -0400298 if (!ssl_cert_set0_chain(cert, dchain)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800299 sk_X509_pop_free(dchain, X509_free);
300 return 0;
301 }
Adam Langley95c29f32014-06-20 12:00:00 -0700302
Adam Langleyfcf25832014-12-18 17:42:32 -0800303 return 1;
304}
Adam Langley95c29f32014-06-20 12:00:00 -0700305
David Benjamind1d80782015-07-05 11:54:09 -0400306int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) {
307 if (cert->chain == NULL) {
308 cert->chain = sk_X509_new_null();
Adam Langleyfcf25832014-12-18 17:42:32 -0800309 }
David Benjamind1d80782015-07-05 11:54:09 -0400310 if (cert->chain == NULL || !sk_X509_push(cert->chain, x509)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800311 return 0;
312 }
Adam Langley95c29f32014-06-20 12:00:00 -0700313
Adam Langleyfcf25832014-12-18 17:42:32 -0800314 return 1;
315}
Adam Langley95c29f32014-06-20 12:00:00 -0700316
David Benjamind1d80782015-07-05 11:54:09 -0400317int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) {
318 if (!ssl_cert_add0_chain_cert(cert, x509)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800319 return 0;
320 }
Adam Langley95c29f32014-06-20 12:00:00 -0700321
David Benjamind1d80782015-07-05 11:54:09 -0400322 X509_up_ref(x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800323 return 1;
324}
Adam Langley95c29f32014-06-20 12:00:00 -0700325
Adam Langleyfcf25832014-12-18 17:42:32 -0800326void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) {
327 c->cert_cb = cb;
328 c->cert_cb_arg = arg;
329}
Adam Langley95c29f32014-06-20 12:00:00 -0700330
Adam Langleyfcf25832014-12-18 17:42:32 -0800331SESS_CERT *ssl_sess_cert_new(void) {
332 SESS_CERT *ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700333
Adam Langleyfcf25832014-12-18 17:42:32 -0800334 ret = OPENSSL_malloc(sizeof *ret);
335 if (ret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400336 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800337 return NULL;
338 }
Adam Langley95c29f32014-06-20 12:00:00 -0700339
Adam Langleyfcf25832014-12-18 17:42:32 -0800340 memset(ret, 0, sizeof *ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700341
Adam Langleyfcf25832014-12-18 17:42:32 -0800342 return ret;
343}
Adam Langley95c29f32014-06-20 12:00:00 -0700344
David Benjamin680ca962015-06-18 12:37:23 -0400345SESS_CERT *ssl_sess_cert_dup(const SESS_CERT *sess_cert) {
346 SESS_CERT *ret = ssl_sess_cert_new();
347 if (ret == NULL) {
348 return NULL;
349 }
350
351 if (sess_cert->cert_chain != NULL) {
352 ret->cert_chain = X509_chain_up_ref(sess_cert->cert_chain);
353 if (ret->cert_chain == NULL) {
354 ssl_sess_cert_free(ret);
355 return NULL;
356 }
357 }
358 if (sess_cert->peer_cert != NULL) {
359 ret->peer_cert = X509_up_ref(sess_cert->peer_cert);
360 }
361 if (sess_cert->peer_dh_tmp != NULL) {
362 ret->peer_dh_tmp = sess_cert->peer_dh_tmp;
363 DH_up_ref(ret->peer_dh_tmp);
364 }
365 if (sess_cert->peer_ecdh_tmp != NULL) {
366 ret->peer_ecdh_tmp = sess_cert->peer_ecdh_tmp;
367 EC_KEY_up_ref(ret->peer_ecdh_tmp);
368 }
369 return ret;
370}
371
372void ssl_sess_cert_free(SESS_CERT *sess_cert) {
373 if (sess_cert == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 return;
375 }
Adam Langley95c29f32014-06-20 12:00:00 -0700376
David Benjamin680ca962015-06-18 12:37:23 -0400377 sk_X509_pop_free(sess_cert->cert_chain, X509_free);
378 X509_free(sess_cert->peer_cert);
379 DH_free(sess_cert->peer_dh_tmp);
380 EC_KEY_free(sess_cert->peer_ecdh_tmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700381
David Benjamin680ca962015-06-18 12:37:23 -0400382 OPENSSL_free(sess_cert);
Adam Langleyfcf25832014-12-18 17:42:32 -0800383}
Adam Langley95c29f32014-06-20 12:00:00 -0700384
David Benjamin60da0cd2015-05-03 15:21:28 -0400385int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800386 X509 *x;
387 int i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 X509_STORE_CTX ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700389
Adam Langleyfcf25832014-12-18 17:42:32 -0800390 if (sk == NULL || sk_X509_num(sk) == 0) {
391 return 0;
392 }
Adam Langley95c29f32014-06-20 12:00:00 -0700393
Adam Langleyfcf25832014-12-18 17:42:32 -0800394 x = sk_X509_value(sk, 0);
David Benjamind27441a2015-08-09 11:05:17 -0400395 if (!X509_STORE_CTX_init(&ctx, s->ctx->cert_store, x, sk)) {
David Benjamin3570d732015-06-29 00:28:17 -0400396 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800397 return 0;
398 }
399 X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s);
Adam Langley95c29f32014-06-20 12:00:00 -0700400
Adam Langleyfcf25832014-12-18 17:42:32 -0800401 /* We need to inherit the verify parameters. These can be determined by the
402 * context: if its a server it will verify SSL client certificates or vice
403 * versa. */
404 X509_STORE_CTX_set_default(&ctx, s->server ? "ssl_client" : "ssl_server");
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 /* Anything non-default in "param" should overwrite anything in the ctx. */
407 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700408
Adam Langleyfcf25832014-12-18 17:42:32 -0800409 if (s->verify_callback) {
410 X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback);
411 }
Adam Langley95c29f32014-06-20 12:00:00 -0700412
Adam Langleyfcf25832014-12-18 17:42:32 -0800413 if (s->ctx->app_verify_callback != NULL) {
414 i = s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg);
415 } else {
416 i = X509_verify_cert(&ctx);
417 }
Adam Langley95c29f32014-06-20 12:00:00 -0700418
Adam Langleyfcf25832014-12-18 17:42:32 -0800419 s->verify_result = ctx.error;
420 X509_STORE_CTX_cleanup(&ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700421
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 return i;
423}
Adam Langley95c29f32014-06-20 12:00:00 -0700424
David Benjamin60da0cd2015-05-03 15:21:28 -0400425static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,
426 STACK_OF(X509_NAME) *name_list) {
David Benjamin2755a3e2015-04-22 16:17:58 -0400427 sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
Adam Langleyfcf25832014-12-18 17:42:32 -0800428 *ca_list = name_list;
429}
Adam Langley95c29f32014-06-20 12:00:00 -0700430
David Benjamin60da0cd2015-05-03 15:21:28 -0400431STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800432 size_t i;
David Benjamin60da0cd2015-05-03 15:21:28 -0400433 STACK_OF(X509_NAME) *ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800434 X509_NAME *name;
435
436 ret = sk_X509_NAME_new_null();
437 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
438 name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
439 if (name == NULL || !sk_X509_NAME_push(ret, name)) {
440 sk_X509_NAME_pop_free(ret, X509_NAME_free);
441 return NULL;
442 }
443 }
444
445 return ret;
446}
447
David Benjamin60da0cd2015-05-03 15:21:28 -0400448void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800449 set_client_CA_list(&(s->client_CA), name_list);
450}
451
David Benjamin60da0cd2015-05-03 15:21:28 -0400452void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800453 set_client_CA_list(&(ctx->client_CA), name_list);
454}
455
David Benjamin60da0cd2015-05-03 15:21:28 -0400456STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800457 return ctx->client_CA;
458}
459
David Benjamin60da0cd2015-05-03 15:21:28 -0400460STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800461 if (s->server) {
462 if (s->client_CA != NULL) {
463 return s->client_CA;
464 } else {
465 return s->ctx->client_CA;
466 }
467 } else {
468 if ((s->version >> 8) == SSL3_VERSION_MAJOR && s->s3 != NULL) {
469 return s->s3->tmp.ca_names;
470 } else {
471 return NULL;
472 }
473 }
474}
475
David Benjamin60da0cd2015-05-03 15:21:28 -0400476static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800477 X509_NAME *name;
478
479 if (x == NULL) {
480 return 0;
481 }
482 if (*sk == NULL) {
483 *sk = sk_X509_NAME_new_null();
484 if (*sk == NULL) {
485 return 0;
486 }
487 }
488
489 name = X509_NAME_dup(X509_get_subject_name(x));
490 if (name == NULL) {
491 return 0;
492 }
493
494 if (!sk_X509_NAME_push(*sk, name)) {
495 X509_NAME_free(name);
496 return 0;
497 }
498
499 return 1;
500}
501
502int SSL_add_client_CA(SSL *ssl, X509 *x) {
503 return add_client_CA(&(ssl->client_CA), x);
504}
505
506int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x) {
507 return add_client_CA(&(ctx->client_CA), x);
508}
509
510static int xname_cmp(const X509_NAME **a, const X509_NAME **b) {
511 return X509_NAME_cmp(*a, *b);
512}
513
514/* Load CA certs from a file into a STACK. Note that it is somewhat misnamed;
Adam Langley95c29f32014-06-20 12:00:00 -0700515 * it doesn't really have anything to do with clients (except that a common use
516 * for a stack of CAs is to send it to the client). Actually, it doesn't have
517 * much to do with CAs, either, since it will load any old cert.
Adam Langleyfcf25832014-12-18 17:42:32 -0800518 *
Adam Langley95c29f32014-06-20 12:00:00 -0700519 * \param file the file containing one or more certs.
Adam Langleyfcf25832014-12-18 17:42:32 -0800520 * \return a ::STACK containing the certs. */
David Benjamin60da0cd2015-05-03 15:21:28 -0400521STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800522 BIO *in;
523 X509 *x = NULL;
524 X509_NAME *xn = NULL;
525 STACK_OF(X509_NAME) *ret = NULL, *sk;
Adam Langley95c29f32014-06-20 12:00:00 -0700526
Adam Langleyfcf25832014-12-18 17:42:32 -0800527 sk = sk_X509_NAME_new(xname_cmp);
528 in = BIO_new(BIO_s_file());
Adam Langley95c29f32014-06-20 12:00:00 -0700529
Adam Langleyfcf25832014-12-18 17:42:32 -0800530 if (sk == NULL || in == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400531 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800532 goto err;
533 }
Adam Langley95c29f32014-06-20 12:00:00 -0700534
Adam Langleyfcf25832014-12-18 17:42:32 -0800535 if (!BIO_read_filename(in, file)) {
536 goto err;
537 }
Adam Langley95c29f32014-06-20 12:00:00 -0700538
Adam Langleyfcf25832014-12-18 17:42:32 -0800539 for (;;) {
540 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) {
541 break;
542 }
543 if (ret == NULL) {
544 ret = sk_X509_NAME_new_null();
545 if (ret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400546 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800547 goto err;
548 }
549 }
550 xn = X509_get_subject_name(x);
551 if (xn == NULL) {
552 goto err;
553 }
Adam Langley95c29f32014-06-20 12:00:00 -0700554
Adam Langleyfcf25832014-12-18 17:42:32 -0800555 /* check for duplicates */
556 xn = X509_NAME_dup(xn);
557 if (xn == NULL) {
558 goto err;
559 }
560 if (sk_X509_NAME_find(sk, NULL, xn)) {
561 X509_NAME_free(xn);
562 } else {
563 sk_X509_NAME_push(sk, xn);
564 sk_X509_NAME_push(ret, xn);
565 }
566 }
Adam Langley95c29f32014-06-20 12:00:00 -0700567
Adam Langleyfcf25832014-12-18 17:42:32 -0800568 if (0) {
569 err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400570 sk_X509_NAME_pop_free(ret, X509_NAME_free);
Adam Langleyfcf25832014-12-18 17:42:32 -0800571 ret = NULL;
572 }
573
David Benjamin2755a3e2015-04-22 16:17:58 -0400574 sk_X509_NAME_free(sk);
575 BIO_free(in);
576 X509_free(x);
Adam Langleyfcf25832014-12-18 17:42:32 -0800577 if (ret != NULL) {
578 ERR_clear_error();
579 }
580 return ret;
581}
582
583/* Add a file of certs to a stack.
584 *
Adam Langley95c29f32014-06-20 12:00:00 -0700585 * \param stack the stack to add to.
586 * \param file the file to add from. All certs in this file that are not
Adam Langleyfcf25832014-12-18 17:42:32 -0800587 * already in the stack will be added.
Adam Langley95c29f32014-06-20 12:00:00 -0700588 * \return 1 for success, 0 for failure. Note that in the case of failure some
Adam Langleyfcf25832014-12-18 17:42:32 -0800589 * certs may have been added to \c stack. */
David Benjamin60da0cd2015-05-03 15:21:28 -0400590int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
Adam Langleyfcf25832014-12-18 17:42:32 -0800591 const char *file) {
592 BIO *in;
593 X509 *x = NULL;
594 X509_NAME *xn = NULL;
595 int ret = 1;
596 int (*oldcmp)(const X509_NAME **a, const X509_NAME **b);
Adam Langley95c29f32014-06-20 12:00:00 -0700597
Adam Langleyfcf25832014-12-18 17:42:32 -0800598 oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_cmp);
599 in = BIO_new(BIO_s_file());
Adam Langley95c29f32014-06-20 12:00:00 -0700600
Adam Langleyfcf25832014-12-18 17:42:32 -0800601 if (in == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400602 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800603 goto err;
604 }
Adam Langley95c29f32014-06-20 12:00:00 -0700605
Adam Langleyfcf25832014-12-18 17:42:32 -0800606 if (!BIO_read_filename(in, file)) {
607 goto err;
608 }
Adam Langley95c29f32014-06-20 12:00:00 -0700609
Adam Langleyfcf25832014-12-18 17:42:32 -0800610 for (;;) {
611 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) {
612 break;
613 }
614 xn = X509_get_subject_name(x);
615 if (xn == NULL) {
616 goto err;
617 }
618 xn = X509_NAME_dup(xn);
619 if (xn == NULL) {
620 goto err;
621 }
622 if (sk_X509_NAME_find(stack, NULL, xn)) {
623 X509_NAME_free(xn);
624 } else {
625 sk_X509_NAME_push(stack, xn);
626 }
627 }
Adam Langley95c29f32014-06-20 12:00:00 -0700628
Adam Langleyfcf25832014-12-18 17:42:32 -0800629 ERR_clear_error();
630
631 if (0) {
632 err:
633 ret = 0;
634 }
635
David Benjamin2755a3e2015-04-22 16:17:58 -0400636 BIO_free(in);
637 X509_free(x);
Adam Langleyfcf25832014-12-18 17:42:32 -0800638
Adam Langleya307dfd2015-01-09 15:42:58 -0800639 (void) sk_X509_NAME_set_cmp_func(stack, oldcmp);
Adam Langleyfcf25832014-12-18 17:42:32 -0800640
641 return ret;
642}
643
644/* Add a directory of certs to a stack.
645 *
Adam Langley95c29f32014-06-20 12:00:00 -0700646 * \param stack the stack to append to.
647 * \param dir the directory to append from. All files in this directory will be
Adam Langleyfcf25832014-12-18 17:42:32 -0800648 * examined as potential certs. Any that are acceptable to
649 * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will
650 * be included.
Adam Langley95c29f32014-06-20 12:00:00 -0700651 * \return 1 for success, 0 for failure. Note that in the case of failure some
Adam Langleyfcf25832014-12-18 17:42:32 -0800652 * certs may have been added to \c stack. */
David Benjamin60da0cd2015-05-03 15:21:28 -0400653int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
Adam Langleyfcf25832014-12-18 17:42:32 -0800654 const char *dir) {
655 OPENSSL_DIR_CTX *d = NULL;
656 const char *filename;
657 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700658
Adam Langleyfcf25832014-12-18 17:42:32 -0800659 /* Note that a side effect is that the CAs will be sorted by name */
660 while ((filename = OPENSSL_DIR_read(&d, dir))) {
661 char buf[1024];
662 int r;
Adam Langley95c29f32014-06-20 12:00:00 -0700663
Adam Langleyfcf25832014-12-18 17:42:32 -0800664 if (strlen(dir) + strlen(filename) + 2 > sizeof(buf)) {
David Benjamin3570d732015-06-29 00:28:17 -0400665 OPENSSL_PUT_ERROR(SSL, SSL_R_PATH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800666 goto err;
667 }
Adam Langley95c29f32014-06-20 12:00:00 -0700668
Adam Langleyfcf25832014-12-18 17:42:32 -0800669 r = BIO_snprintf(buf, sizeof buf, "%s/%s", dir, filename);
670 if (r <= 0 || r >= (int)sizeof(buf) ||
671 !SSL_add_file_cert_subjects_to_stack(stack, buf)) {
672 goto err;
673 }
674 }
Adam Langley95c29f32014-06-20 12:00:00 -0700675
Adam Langleyfcf25832014-12-18 17:42:32 -0800676 if (errno) {
David Benjamin3570d732015-06-29 00:28:17 -0400677 OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800678 ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')");
679 goto err;
680 }
Adam Langley95c29f32014-06-20 12:00:00 -0700681
Adam Langleyfcf25832014-12-18 17:42:32 -0800682 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700683
684err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800685 if (d) {
686 OPENSSL_DIR_end(&d);
687 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800688 return ret;
689}
Adam Langley95c29f32014-06-20 12:00:00 -0700690
691/* Add a certificate to a BUF_MEM structure */
Adam Langleyfcf25832014-12-18 17:42:32 -0800692static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x) {
693 int n;
694 uint8_t *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700695
Adam Langleyfcf25832014-12-18 17:42:32 -0800696 n = i2d_X509(x, NULL);
697 if (!BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) {
David Benjamin3570d732015-06-29 00:28:17 -0400698 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800699 return 0;
700 }
701 p = (uint8_t *)&(buf->data[*l]);
702 l2n3(n, p);
703 i2d_X509(x, &p);
704 *l += n + 3;
Adam Langley95c29f32014-06-20 12:00:00 -0700705
Adam Langleyfcf25832014-12-18 17:42:32 -0800706 return 1;
707}
Adam Langley95c29f32014-06-20 12:00:00 -0700708
Adam Langley24819752014-12-15 18:42:07 -0800709/* Add certificate chain to internal SSL BUF_MEM structure. */
David Benjamind1d80782015-07-05 11:54:09 -0400710int ssl_add_cert_chain(SSL *ssl, unsigned long *l) {
711 CERT *cert = ssl->cert;
712 BUF_MEM *buf = ssl->init_buf;
Adam Langleyfcf25832014-12-18 17:42:32 -0800713 int no_chain = 0;
714 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -0700715
David Benjamind1d80782015-07-05 11:54:09 -0400716 X509 *x = cert->x509;
717 STACK_OF(X509) *chain = cert->chain;
Adam Langley95c29f32014-06-20 12:00:00 -0700718
David Benjamin605641e2015-05-03 15:14:04 -0400719 if (x == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400720 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
David Benjamin605641e2015-05-03 15:14:04 -0400721 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800722 }
Adam Langley95c29f32014-06-20 12:00:00 -0700723
David Benjamind1d80782015-07-05 11:54:09 -0400724 if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) || chain != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800725 no_chain = 1;
726 }
Adam Langley95c29f32014-06-20 12:00:00 -0700727
David Benjamin605641e2015-05-03 15:14:04 -0400728 if (no_chain) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800729 if (!ssl_add_cert_to_buf(buf, l, x)) {
730 return 0;
731 }
David Benjamin605641e2015-05-03 15:14:04 -0400732
David Benjamind1d80782015-07-05 11:54:09 -0400733 for (i = 0; i < sk_X509_num(chain); i++) {
734 x = sk_X509_value(chain, i);
David Benjamin605641e2015-05-03 15:14:04 -0400735 if (!ssl_add_cert_to_buf(buf, l, x)) {
736 return 0;
737 }
738 }
739 } else {
740 X509_STORE_CTX xs_ctx;
741
David Benjamind27441a2015-08-09 11:05:17 -0400742 if (!X509_STORE_CTX_init(&xs_ctx, ssl->ctx->cert_store, x, NULL)) {
David Benjamin3570d732015-06-29 00:28:17 -0400743 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
David Benjamin605641e2015-05-03 15:14:04 -0400744 return 0;
745 }
746 X509_verify_cert(&xs_ctx);
747 /* Don't leave errors in the queue */
748 ERR_clear_error();
749 for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
750 x = sk_X509_value(xs_ctx.chain, i);
751
752 if (!ssl_add_cert_to_buf(buf, l, x)) {
753 X509_STORE_CTX_cleanup(&xs_ctx);
754 return 0;
755 }
756 }
757 X509_STORE_CTX_cleanup(&xs_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800758 }
759
760 return 1;
761}
Adam Langley95c29f32014-06-20 12:00:00 -0700762
David Benjamin11c0f8e2015-07-06 00:18:15 -0400763int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
764 return ssl_cert_set0_chain(ctx->cert, chain);
765}
766
767int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
768 return ssl_cert_set1_chain(ctx->cert, chain);
769}
770
771int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) {
772 return ssl_cert_set0_chain(ssl->cert, chain);
773}
774
775int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) {
776 return ssl_cert_set1_chain(ssl->cert, chain);
777}
778
779int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) {
780 return ssl_cert_add0_chain_cert(ctx->cert, x509);
781}
782
783int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) {
784 return ssl_cert_add1_chain_cert(ctx->cert, x509);
785}
786
787int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) {
788 return SSL_CTX_add0_chain_cert(ctx, x509);
789}
790
791int SSL_add0_chain_cert(SSL *ssl, X509 *x509) {
792 return ssl_cert_add0_chain_cert(ssl->cert, x509);
793}
794
795int SSL_add1_chain_cert(SSL *ssl, X509 *x509) {
796 return ssl_cert_add1_chain_cert(ssl->cert, x509);
797}
798
799int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) {
800 return SSL_CTX_set0_chain(ctx, NULL);
801}
802
803int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) {
804 return SSL_CTX_clear_chain_certs(ctx);
805}
806
807int SSL_clear_chain_certs(SSL *ssl) {
808 return SSL_set0_chain(ssl, NULL);
809}
810
811int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) {
812 *out_chain = ctx->cert->chain;
813 return 1;
814}
815
816int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx,
817 STACK_OF(X509) **out_chain) {
818 return SSL_CTX_get0_chain_certs(ctx, out_chain);
819}
820
821int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) {
822 *out_chain = ssl->cert->chain;
823 return 1;
824}