]>
Commit | Line | Data |
---|---|---|
d02b48c6 | 1 | /* ssl/ssl_rsa.c */ |
58964a49 | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
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. | |
0f113f3e | 8 | * |
d02b48c6 RE |
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). | |
0f113f3e | 15 | * |
d02b48c6 RE |
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. | |
0f113f3e | 22 | * |
d02b48c6 RE |
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 :-). | |
0f113f3e | 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
d02b48c6 RE |
38 | * the apps directory (application code) you must include an acknowledgement: |
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 40 | * |
d02b48c6 RE |
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. | |
0f113f3e | 52 | * |
d02b48c6 RE |
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 | #include <stdio.h> | |
7b63c0fa | 60 | #include "ssl_locl.h" |
ec577822 BM |
61 | #include <openssl/bio.h> |
62 | #include <openssl/objects.h> | |
63 | #include <openssl/evp.h> | |
64 | #include <openssl/x509.h> | |
65 | #include <openssl/pem.h> | |
d02b48c6 | 66 | |
d02b48c6 RE |
67 | static int ssl_set_cert(CERT *c, X509 *x509); |
68 | static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey); | |
6b691a5c | 69 | int SSL_use_certificate(SSL *ssl, X509 *x) |
0f113f3e MC |
70 | { |
71 | int rv; | |
72 | if (x == NULL) { | |
73 | SSLerr(SSL_F_SSL_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER); | |
74 | return (0); | |
75 | } | |
76 | rv = ssl_security_cert(ssl, NULL, x, 0, 1); | |
77 | if (rv != 1) { | |
78 | SSLerr(SSL_F_SSL_USE_CERTIFICATE, rv); | |
79 | return 0; | |
80 | } | |
81 | ||
0f113f3e MC |
82 | return (ssl_set_cert(ssl->cert, x)); |
83 | } | |
d02b48c6 | 84 | |
bc36ee62 | 85 | #ifndef OPENSSL_NO_STDIO |
303c0028 | 86 | int SSL_use_certificate_file(SSL *ssl, const char *file, int type) |
0f113f3e MC |
87 | { |
88 | int j; | |
89 | BIO *in; | |
90 | int ret = 0; | |
91 | X509 *x = NULL; | |
92 | ||
9982cbbb | 93 | in = BIO_new(BIO_s_file()); |
0f113f3e MC |
94 | if (in == NULL) { |
95 | SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB); | |
96 | goto end; | |
97 | } | |
98 | ||
99 | if (BIO_read_filename(in, file) <= 0) { | |
100 | SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB); | |
101 | goto end; | |
102 | } | |
103 | if (type == SSL_FILETYPE_ASN1) { | |
104 | j = ERR_R_ASN1_LIB; | |
105 | x = d2i_X509_bio(in, NULL); | |
106 | } else if (type == SSL_FILETYPE_PEM) { | |
107 | j = ERR_R_PEM_LIB; | |
108 | x = PEM_read_bio_X509(in, NULL, ssl->ctx->default_passwd_callback, | |
109 | ssl->ctx->default_passwd_callback_userdata); | |
110 | } else { | |
111 | SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE); | |
112 | goto end; | |
113 | } | |
114 | ||
115 | if (x == NULL) { | |
116 | SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE, j); | |
117 | goto end; | |
118 | } | |
119 | ||
120 | ret = SSL_use_certificate(ssl, x); | |
121 | end: | |
222561fe | 122 | X509_free(x); |
ca3a82c3 | 123 | BIO_free(in); |
0f113f3e MC |
124 | return (ret); |
125 | } | |
58964a49 | 126 | #endif |
d02b48c6 | 127 | |
875a644a | 128 | int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len) |
0f113f3e MC |
129 | { |
130 | X509 *x; | |
131 | int ret; | |
132 | ||
133 | x = d2i_X509(NULL, &d, (long)len); | |
134 | if (x == NULL) { | |
135 | SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB); | |
136 | return (0); | |
137 | } | |
138 | ||
139 | ret = SSL_use_certificate(ssl, x); | |
140 | X509_free(x); | |
141 | return (ret); | |
142 | } | |
d02b48c6 | 143 | |
bc36ee62 | 144 | #ifndef OPENSSL_NO_RSA |
6b691a5c | 145 | int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa) |
0f113f3e MC |
146 | { |
147 | EVP_PKEY *pkey; | |
148 | int ret; | |
149 | ||
150 | if (rsa == NULL) { | |
151 | SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER); | |
152 | return (0); | |
153 | } | |
0f113f3e MC |
154 | if ((pkey = EVP_PKEY_new()) == NULL) { |
155 | SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB); | |
156 | return (0); | |
157 | } | |
158 | ||
159 | RSA_up_ref(rsa); | |
5f3d93e4 MC |
160 | if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) { |
161 | RSA_free(rsa); | |
162 | return 0; | |
163 | } | |
0f113f3e MC |
164 | |
165 | ret = ssl_set_pkey(ssl->cert, pkey); | |
166 | EVP_PKEY_free(pkey); | |
167 | return (ret); | |
168 | } | |
d02b48c6 RE |
169 | #endif |
170 | ||
6b691a5c | 171 | static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey) |
0f113f3e MC |
172 | { |
173 | int i; | |
bc71f910 | 174 | i = ssl_cert_type(NULL, pkey); |
0f113f3e MC |
175 | if (i < 0) { |
176 | SSLerr(SSL_F_SSL_SET_PKEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE); | |
177 | return (0); | |
178 | } | |
179 | ||
180 | if (c->pkeys[i].x509 != NULL) { | |
181 | EVP_PKEY *pktmp; | |
8382fd3a | 182 | pktmp = X509_get0_pubkey(c->pkeys[i].x509); |
5f3d93e4 MC |
183 | if (pktmp == NULL) { |
184 | SSLerr(SSL_F_SSL_SET_PKEY, ERR_R_MALLOC_FAILURE); | |
5f3d93e4 MC |
185 | return 0; |
186 | } | |
187 | /* | |
188 | * The return code from EVP_PKEY_copy_parameters is deliberately | |
189 | * ignored. Some EVP_PKEY types cannot do this. | |
190 | */ | |
0f113f3e | 191 | EVP_PKEY_copy_parameters(pktmp, pkey); |
0f113f3e | 192 | ERR_clear_error(); |
dfeab068 | 193 | |
bc36ee62 | 194 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
195 | /* |
196 | * Don't check the public/private key, this is mostly for smart | |
197 | * cards. | |
198 | */ | |
3aeb9348 DSH |
199 | if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA |
200 | && RSA_flags(EVP_PKEY_get0_RSA(pkey)) & RSA_METHOD_FLAG_NO_CHECK); | |
0f113f3e | 201 | else |
58964a49 | 202 | #endif |
0f113f3e MC |
203 | if (!X509_check_private_key(c->pkeys[i].x509, pkey)) { |
204 | X509_free(c->pkeys[i].x509); | |
205 | c->pkeys[i].x509 = NULL; | |
206 | return 0; | |
207 | } | |
208 | } | |
209 | ||
25aaa98a | 210 | EVP_PKEY_free(c->pkeys[i].privatekey); |
3aeb9348 | 211 | EVP_PKEY_up_ref(pkey); |
0f113f3e MC |
212 | c->pkeys[i].privatekey = pkey; |
213 | c->key = &(c->pkeys[i]); | |
0f113f3e MC |
214 | return (1); |
215 | } | |
d02b48c6 | 216 | |
bc36ee62 | 217 | #ifndef OPENSSL_NO_RSA |
0f113f3e | 218 | # ifndef OPENSSL_NO_STDIO |
303c0028 | 219 | int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type) |
0f113f3e MC |
220 | { |
221 | int j, ret = 0; | |
222 | BIO *in; | |
223 | RSA *rsa = NULL; | |
224 | ||
9982cbbb | 225 | in = BIO_new(BIO_s_file()); |
0f113f3e MC |
226 | if (in == NULL) { |
227 | SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB); | |
228 | goto end; | |
229 | } | |
230 | ||
231 | if (BIO_read_filename(in, file) <= 0) { | |
232 | SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB); | |
233 | goto end; | |
234 | } | |
235 | if (type == SSL_FILETYPE_ASN1) { | |
236 | j = ERR_R_ASN1_LIB; | |
237 | rsa = d2i_RSAPrivateKey_bio(in, NULL); | |
238 | } else if (type == SSL_FILETYPE_PEM) { | |
239 | j = ERR_R_PEM_LIB; | |
240 | rsa = PEM_read_bio_RSAPrivateKey(in, NULL, | |
241 | ssl->ctx->default_passwd_callback, | |
242 | ssl-> | |
243 | ctx->default_passwd_callback_userdata); | |
244 | } else { | |
245 | SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE); | |
246 | goto end; | |
247 | } | |
248 | if (rsa == NULL) { | |
249 | SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE, j); | |
250 | goto end; | |
251 | } | |
252 | ret = SSL_use_RSAPrivateKey(ssl, rsa); | |
253 | RSA_free(rsa); | |
254 | end: | |
ca3a82c3 | 255 | BIO_free(in); |
0f113f3e MC |
256 | return (ret); |
257 | } | |
258 | # endif | |
d02b48c6 | 259 | |
693b71fa | 260 | int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len) |
0f113f3e MC |
261 | { |
262 | int ret; | |
263 | const unsigned char *p; | |
264 | RSA *rsa; | |
265 | ||
266 | p = d; | |
267 | if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) { | |
268 | SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB); | |
269 | return (0); | |
270 | } | |
271 | ||
272 | ret = SSL_use_RSAPrivateKey(ssl, rsa); | |
273 | RSA_free(rsa); | |
274 | return (ret); | |
275 | } | |
276 | #endif /* !OPENSSL_NO_RSA */ | |
d02b48c6 | 277 | |
6b691a5c | 278 | int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) |
0f113f3e MC |
279 | { |
280 | int ret; | |
281 | ||
282 | if (pkey == NULL) { | |
283 | SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER); | |
284 | return (0); | |
285 | } | |
0f113f3e MC |
286 | ret = ssl_set_pkey(ssl->cert, pkey); |
287 | return (ret); | |
288 | } | |
d02b48c6 | 289 | |
bc36ee62 | 290 | #ifndef OPENSSL_NO_STDIO |
303c0028 | 291 | int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type) |
0f113f3e MC |
292 | { |
293 | int j, ret = 0; | |
294 | BIO *in; | |
295 | EVP_PKEY *pkey = NULL; | |
296 | ||
9982cbbb | 297 | in = BIO_new(BIO_s_file()); |
0f113f3e MC |
298 | if (in == NULL) { |
299 | SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB); | |
300 | goto end; | |
301 | } | |
302 | ||
303 | if (BIO_read_filename(in, file) <= 0) { | |
304 | SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB); | |
305 | goto end; | |
306 | } | |
307 | if (type == SSL_FILETYPE_PEM) { | |
308 | j = ERR_R_PEM_LIB; | |
309 | pkey = PEM_read_bio_PrivateKey(in, NULL, | |
310 | ssl->ctx->default_passwd_callback, | |
311 | ssl-> | |
312 | ctx->default_passwd_callback_userdata); | |
313 | } else if (type == SSL_FILETYPE_ASN1) { | |
314 | j = ERR_R_ASN1_LIB; | |
315 | pkey = d2i_PrivateKey_bio(in, NULL); | |
316 | } else { | |
317 | SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE); | |
318 | goto end; | |
319 | } | |
320 | if (pkey == NULL) { | |
321 | SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE, j); | |
322 | goto end; | |
323 | } | |
324 | ret = SSL_use_PrivateKey(ssl, pkey); | |
325 | EVP_PKEY_free(pkey); | |
326 | end: | |
ca3a82c3 | 327 | BIO_free(in); |
0f113f3e MC |
328 | return (ret); |
329 | } | |
58964a49 | 330 | #endif |
d02b48c6 | 331 | |
0f113f3e MC |
332 | int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d, |
333 | long len) | |
334 | { | |
335 | int ret; | |
336 | const unsigned char *p; | |
337 | EVP_PKEY *pkey; | |
d02b48c6 | 338 | |
0f113f3e MC |
339 | p = d; |
340 | if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) { | |
341 | SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB); | |
342 | return (0); | |
343 | } | |
d02b48c6 | 344 | |
0f113f3e MC |
345 | ret = SSL_use_PrivateKey(ssl, pkey); |
346 | EVP_PKEY_free(pkey); | |
347 | return (ret); | |
348 | } | |
d02b48c6 | 349 | |
6b691a5c | 350 | int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x) |
0f113f3e MC |
351 | { |
352 | int rv; | |
353 | if (x == NULL) { | |
354 | SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, ERR_R_PASSED_NULL_PARAMETER); | |
355 | return (0); | |
356 | } | |
357 | rv = ssl_security_cert(NULL, ctx, x, 0, 1); | |
358 | if (rv != 1) { | |
359 | SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE, rv); | |
360 | return 0; | |
361 | } | |
0f113f3e MC |
362 | return (ssl_set_cert(ctx->cert, x)); |
363 | } | |
d02b48c6 | 364 | |
6b691a5c | 365 | static int ssl_set_cert(CERT *c, X509 *x) |
0f113f3e MC |
366 | { |
367 | EVP_PKEY *pkey; | |
368 | int i; | |
369 | ||
8382fd3a | 370 | pkey = X509_get0_pubkey(x); |
0f113f3e MC |
371 | if (pkey == NULL) { |
372 | SSLerr(SSL_F_SSL_SET_CERT, SSL_R_X509_LIB); | |
373 | return (0); | |
374 | } | |
375 | ||
376 | i = ssl_cert_type(x, pkey); | |
377 | if (i < 0) { | |
378 | SSLerr(SSL_F_SSL_SET_CERT, SSL_R_UNKNOWN_CERTIFICATE_TYPE); | |
8382fd3a | 379 | return 0; |
0f113f3e MC |
380 | } |
381 | ||
382 | if (c->pkeys[i].privatekey != NULL) { | |
5f3d93e4 MC |
383 | /* |
384 | * The return code from EVP_PKEY_copy_parameters is deliberately | |
385 | * ignored. Some EVP_PKEY types cannot do this. | |
386 | */ | |
0f113f3e MC |
387 | EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey); |
388 | ERR_clear_error(); | |
dfeab068 | 389 | |
bc36ee62 | 390 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
391 | /* |
392 | * Don't check the public/private key, this is mostly for smart | |
393 | * cards. | |
394 | */ | |
3aeb9348 DSH |
395 | if (EVP_PKEY_id(c->pkeys[i].privatekey) == EVP_PKEY_RSA |
396 | && RSA_flags(EVP_PKEY_get0_RSA(c->pkeys[i].privatekey)) & | |
397 | RSA_METHOD_FLAG_NO_CHECK) ; | |
0f113f3e MC |
398 | else |
399 | #endif /* OPENSSL_NO_RSA */ | |
400 | if (!X509_check_private_key(x, c->pkeys[i].privatekey)) { | |
401 | /* | |
402 | * don't fail for a cert/key mismatch, just free current private | |
403 | * key (when switching to a different cert & key, first this | |
404 | * function should be used, then ssl_set_pkey | |
405 | */ | |
406 | EVP_PKEY_free(c->pkeys[i].privatekey); | |
407 | c->pkeys[i].privatekey = NULL; | |
408 | /* clear error queue */ | |
409 | ERR_clear_error(); | |
410 | } | |
411 | } | |
412 | ||
222561fe | 413 | X509_free(c->pkeys[i].x509); |
05f0fb9f | 414 | X509_up_ref(x); |
0f113f3e MC |
415 | c->pkeys[i].x509 = x; |
416 | c->key = &(c->pkeys[i]); | |
417 | ||
8382fd3a | 418 | return 1; |
0f113f3e | 419 | } |
d02b48c6 | 420 | |
bc36ee62 | 421 | #ifndef OPENSSL_NO_STDIO |
303c0028 | 422 | int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type) |
0f113f3e MC |
423 | { |
424 | int j; | |
425 | BIO *in; | |
426 | int ret = 0; | |
427 | X509 *x = NULL; | |
428 | ||
9982cbbb | 429 | in = BIO_new(BIO_s_file()); |
0f113f3e MC |
430 | if (in == NULL) { |
431 | SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_BUF_LIB); | |
432 | goto end; | |
433 | } | |
434 | ||
435 | if (BIO_read_filename(in, file) <= 0) { | |
436 | SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB); | |
437 | goto end; | |
438 | } | |
439 | if (type == SSL_FILETYPE_ASN1) { | |
440 | j = ERR_R_ASN1_LIB; | |
441 | x = d2i_X509_bio(in, NULL); | |
442 | } else if (type == SSL_FILETYPE_PEM) { | |
443 | j = ERR_R_PEM_LIB; | |
444 | x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback, | |
445 | ctx->default_passwd_callback_userdata); | |
446 | } else { | |
447 | SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, SSL_R_BAD_SSL_FILETYPE); | |
448 | goto end; | |
449 | } | |
450 | ||
451 | if (x == NULL) { | |
452 | SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, j); | |
453 | goto end; | |
454 | } | |
455 | ||
456 | ret = SSL_CTX_use_certificate(ctx, x); | |
457 | end: | |
222561fe | 458 | X509_free(x); |
ca3a82c3 | 459 | BIO_free(in); |
0f113f3e MC |
460 | return (ret); |
461 | } | |
58964a49 | 462 | #endif |
d02b48c6 | 463 | |
0f113f3e MC |
464 | int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, |
465 | const unsigned char *d) | |
466 | { | |
467 | X509 *x; | |
468 | int ret; | |
d02b48c6 | 469 | |
0f113f3e MC |
470 | x = d2i_X509(NULL, &d, (long)len); |
471 | if (x == NULL) { | |
472 | SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1, ERR_R_ASN1_LIB); | |
473 | return (0); | |
474 | } | |
d02b48c6 | 475 | |
0f113f3e MC |
476 | ret = SSL_CTX_use_certificate(ctx, x); |
477 | X509_free(x); | |
478 | return (ret); | |
479 | } | |
d02b48c6 | 480 | |
bc36ee62 | 481 | #ifndef OPENSSL_NO_RSA |
6b691a5c | 482 | int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa) |
0f113f3e MC |
483 | { |
484 | int ret; | |
485 | EVP_PKEY *pkey; | |
486 | ||
487 | if (rsa == NULL) { | |
488 | SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER); | |
489 | return (0); | |
490 | } | |
0f113f3e MC |
491 | if ((pkey = EVP_PKEY_new()) == NULL) { |
492 | SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY, ERR_R_EVP_LIB); | |
493 | return (0); | |
494 | } | |
495 | ||
496 | RSA_up_ref(rsa); | |
5f3d93e4 MC |
497 | if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) { |
498 | RSA_free(rsa); | |
499 | return 0; | |
500 | } | |
0f113f3e MC |
501 | |
502 | ret = ssl_set_pkey(ctx->cert, pkey); | |
503 | EVP_PKEY_free(pkey); | |
504 | return (ret); | |
505 | } | |
506 | ||
507 | # ifndef OPENSSL_NO_STDIO | |
303c0028 | 508 | int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type) |
0f113f3e MC |
509 | { |
510 | int j, ret = 0; | |
511 | BIO *in; | |
512 | RSA *rsa = NULL; | |
513 | ||
9982cbbb | 514 | in = BIO_new(BIO_s_file()); |
0f113f3e MC |
515 | if (in == NULL) { |
516 | SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_BUF_LIB); | |
517 | goto end; | |
518 | } | |
519 | ||
520 | if (BIO_read_filename(in, file) <= 0) { | |
521 | SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, ERR_R_SYS_LIB); | |
522 | goto end; | |
523 | } | |
524 | if (type == SSL_FILETYPE_ASN1) { | |
525 | j = ERR_R_ASN1_LIB; | |
526 | rsa = d2i_RSAPrivateKey_bio(in, NULL); | |
527 | } else if (type == SSL_FILETYPE_PEM) { | |
528 | j = ERR_R_PEM_LIB; | |
529 | rsa = PEM_read_bio_RSAPrivateKey(in, NULL, | |
530 | ctx->default_passwd_callback, | |
531 | ctx->default_passwd_callback_userdata); | |
532 | } else { | |
533 | SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE); | |
534 | goto end; | |
535 | } | |
536 | if (rsa == NULL) { | |
537 | SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE, j); | |
538 | goto end; | |
539 | } | |
540 | ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa); | |
541 | RSA_free(rsa); | |
542 | end: | |
ca3a82c3 | 543 | BIO_free(in); |
0f113f3e MC |
544 | return (ret); |
545 | } | |
546 | # endif | |
547 | ||
548 | int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d, | |
549 | long len) | |
550 | { | |
551 | int ret; | |
552 | const unsigned char *p; | |
553 | RSA *rsa; | |
554 | ||
555 | p = d; | |
556 | if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) { | |
557 | SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1, ERR_R_ASN1_LIB); | |
558 | return (0); | |
559 | } | |
560 | ||
561 | ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa); | |
562 | RSA_free(rsa); | |
563 | return (ret); | |
564 | } | |
565 | #endif /* !OPENSSL_NO_RSA */ | |
d02b48c6 | 566 | |
6b691a5c | 567 | int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey) |
0f113f3e MC |
568 | { |
569 | if (pkey == NULL) { | |
570 | SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER); | |
571 | return (0); | |
572 | } | |
0f113f3e MC |
573 | return (ssl_set_pkey(ctx->cert, pkey)); |
574 | } | |
d02b48c6 | 575 | |
bc36ee62 | 576 | #ifndef OPENSSL_NO_STDIO |
303c0028 | 577 | int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type) |
0f113f3e MC |
578 | { |
579 | int j, ret = 0; | |
580 | BIO *in; | |
581 | EVP_PKEY *pkey = NULL; | |
582 | ||
9982cbbb | 583 | in = BIO_new(BIO_s_file()); |
0f113f3e MC |
584 | if (in == NULL) { |
585 | SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_BUF_LIB); | |
586 | goto end; | |
587 | } | |
588 | ||
589 | if (BIO_read_filename(in, file) <= 0) { | |
590 | SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, ERR_R_SYS_LIB); | |
591 | goto end; | |
592 | } | |
593 | if (type == SSL_FILETYPE_PEM) { | |
594 | j = ERR_R_PEM_LIB; | |
595 | pkey = PEM_read_bio_PrivateKey(in, NULL, | |
596 | ctx->default_passwd_callback, | |
597 | ctx->default_passwd_callback_userdata); | |
598 | } else if (type == SSL_FILETYPE_ASN1) { | |
599 | j = ERR_R_ASN1_LIB; | |
600 | pkey = d2i_PrivateKey_bio(in, NULL); | |
601 | } else { | |
602 | SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, SSL_R_BAD_SSL_FILETYPE); | |
603 | goto end; | |
604 | } | |
605 | if (pkey == NULL) { | |
606 | SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE, j); | |
607 | goto end; | |
608 | } | |
609 | ret = SSL_CTX_use_PrivateKey(ctx, pkey); | |
610 | EVP_PKEY_free(pkey); | |
611 | end: | |
ca3a82c3 | 612 | BIO_free(in); |
0f113f3e MC |
613 | return (ret); |
614 | } | |
58964a49 | 615 | #endif |
d02b48c6 | 616 | |
0f113f3e MC |
617 | int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, |
618 | const unsigned char *d, long len) | |
619 | { | |
620 | int ret; | |
621 | const unsigned char *p; | |
622 | EVP_PKEY *pkey; | |
d02b48c6 | 623 | |
0f113f3e MC |
624 | p = d; |
625 | if ((pkey = d2i_PrivateKey(type, NULL, &p, (long)len)) == NULL) { | |
626 | SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1, ERR_R_ASN1_LIB); | |
627 | return (0); | |
628 | } | |
d02b48c6 | 629 | |
0f113f3e MC |
630 | ret = SSL_CTX_use_PrivateKey(ctx, pkey); |
631 | EVP_PKEY_free(pkey); | |
632 | return (ret); | |
633 | } | |
d02b48c6 | 634 | |
bc36ee62 | 635 | #ifndef OPENSSL_NO_STDIO |
0f113f3e MC |
636 | /* |
637 | * Read a file that contains our certificate in "PEM" format, possibly | |
638 | * followed by a sequence of CA certificates that should be sent to the peer | |
639 | * in the Certificate message. | |
b3ca645f | 640 | */ |
fae4772c | 641 | static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file) |
0f113f3e MC |
642 | { |
643 | BIO *in; | |
644 | int ret = 0; | |
645 | X509 *x = NULL; | |
a974e64a MC |
646 | pem_password_cb *passwd_callback; |
647 | void *passwd_callback_userdata; | |
0f113f3e MC |
648 | |
649 | ERR_clear_error(); /* clear error stack for | |
650 | * SSL_CTX_use_certificate() */ | |
651 | ||
a974e64a MC |
652 | if (ctx != NULL) { |
653 | passwd_callback = ctx->default_passwd_callback; | |
654 | passwd_callback_userdata = ctx->default_passwd_callback_userdata; | |
655 | } else { | |
656 | passwd_callback = ssl->default_passwd_callback; | |
657 | passwd_callback_userdata = ssl->default_passwd_callback_userdata; | |
658 | } | |
659 | ||
9982cbbb | 660 | in = BIO_new(BIO_s_file()); |
0f113f3e | 661 | if (in == NULL) { |
fae4772c | 662 | SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_BUF_LIB); |
0f113f3e MC |
663 | goto end; |
664 | } | |
665 | ||
666 | if (BIO_read_filename(in, file) <= 0) { | |
fae4772c | 667 | SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_SYS_LIB); |
0f113f3e MC |
668 | goto end; |
669 | } | |
670 | ||
a974e64a MC |
671 | x = PEM_read_bio_X509_AUX(in, NULL, passwd_callback, |
672 | passwd_callback_userdata); | |
0f113f3e | 673 | if (x == NULL) { |
fae4772c | 674 | SSLerr(SSL_F_USE_CERTIFICATE_CHAIN_FILE, ERR_R_PEM_LIB); |
0f113f3e MC |
675 | goto end; |
676 | } | |
677 | ||
fae4772c DSH |
678 | if (ctx) |
679 | ret = SSL_CTX_use_certificate(ctx, x); | |
680 | else | |
681 | ret = SSL_use_certificate(ssl, x); | |
0f113f3e MC |
682 | |
683 | if (ERR_peek_error() != 0) | |
684 | ret = 0; /* Key/certificate mismatch doesn't imply | |
685 | * ret==0 ... */ | |
686 | if (ret) { | |
687 | /* | |
688 | * If we could set up our certificate, now proceed to the CA | |
689 | * certificates. | |
690 | */ | |
691 | X509 *ca; | |
692 | int r; | |
693 | unsigned long err; | |
694 | ||
fae4772c DSH |
695 | if (ctx) |
696 | r = SSL_CTX_clear_chain_certs(ctx); | |
697 | else | |
698 | r = SSL_clear_chain_certs(ssl); | |
699 | ||
700 | if (r == 0) { | |
69f68237 MC |
701 | ret = 0; |
702 | goto end; | |
703 | } | |
0f113f3e | 704 | |
a974e64a MC |
705 | while ((ca = PEM_read_bio_X509(in, NULL, passwd_callback, |
706 | passwd_callback_userdata)) | |
707 | != NULL) { | |
fae4772c DSH |
708 | if (ctx) |
709 | r = SSL_CTX_add0_chain_cert(ctx, ca); | |
710 | else | |
711 | r = SSL_add0_chain_cert(ssl, ca); | |
712 | /* | |
713 | * Note that we must not free ca if it was successfully added to | |
714 | * the chain (while we must free the main certificate, since its | |
715 | * reference count is increased by SSL_CTX_use_certificate). | |
716 | */ | |
0f113f3e MC |
717 | if (!r) { |
718 | X509_free(ca); | |
719 | ret = 0; | |
720 | goto end; | |
721 | } | |
0f113f3e MC |
722 | } |
723 | /* When the while loop ends, it's usually just EOF. */ | |
724 | err = ERR_peek_last_error(); | |
725 | if (ERR_GET_LIB(err) == ERR_LIB_PEM | |
726 | && ERR_GET_REASON(err) == PEM_R_NO_START_LINE) | |
727 | ERR_clear_error(); | |
728 | else | |
729 | ret = 0; /* some real error */ | |
730 | } | |
731 | ||
732 | end: | |
25aaa98a | 733 | X509_free(x); |
ca3a82c3 | 734 | BIO_free(in); |
0f113f3e MC |
735 | return (ret); |
736 | } | |
fae4772c DSH |
737 | |
738 | int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file) | |
739 | { | |
740 | return use_certificate_chain_file(ctx, NULL, file); | |
741 | } | |
742 | ||
743 | int SSL_use_certificate_chain_file(SSL *ssl, const char *file) | |
744 | { | |
745 | return use_certificate_chain_file(NULL, ssl, file); | |
746 | } | |
b3ca645f | 747 | #endif |
a9e1c50b | 748 | |
a398f821 | 749 | static int serverinfo_find_extension(const unsigned char *serverinfo, |
0f113f3e MC |
750 | size_t serverinfo_length, |
751 | unsigned int extension_type, | |
752 | const unsigned char **extension_data, | |
753 | size_t *extension_length) | |
754 | { | |
755 | *extension_data = NULL; | |
756 | *extension_length = 0; | |
757 | if (serverinfo == NULL || serverinfo_length == 0) | |
fae11ec7 | 758 | return -1; |
0f113f3e MC |
759 | for (;;) { |
760 | unsigned int type = 0; | |
761 | size_t len = 0; | |
762 | ||
763 | /* end of serverinfo */ | |
764 | if (serverinfo_length == 0) | |
fae11ec7 | 765 | return 0; /* Extension not found */ |
0f113f3e MC |
766 | |
767 | /* read 2-byte type field */ | |
768 | if (serverinfo_length < 2) | |
fae11ec7 | 769 | return -1; /* Error */ |
0f113f3e MC |
770 | type = (serverinfo[0] << 8) + serverinfo[1]; |
771 | serverinfo += 2; | |
772 | serverinfo_length -= 2; | |
773 | ||
774 | /* read 2-byte len field */ | |
775 | if (serverinfo_length < 2) | |
fae11ec7 | 776 | return -1; /* Error */ |
0f113f3e MC |
777 | len = (serverinfo[0] << 8) + serverinfo[1]; |
778 | serverinfo += 2; | |
779 | serverinfo_length -= 2; | |
780 | ||
781 | if (len > serverinfo_length) | |
fae11ec7 | 782 | return -1; /* Error */ |
0f113f3e MC |
783 | |
784 | if (type == extension_type) { | |
785 | *extension_data = serverinfo; | |
786 | *extension_length = len; | |
787 | return 1; /* Success */ | |
788 | } | |
789 | ||
790 | serverinfo += len; | |
791 | serverinfo_length -= len; | |
792 | } | |
c6ef15c4 | 793 | /* Unreachable */ |
0f113f3e | 794 | } |
a398f821 | 795 | |
de2a9e38 | 796 | static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type, |
0f113f3e MC |
797 | const unsigned char *in, |
798 | size_t inlen, int *al, void *arg) | |
799 | { | |
0a602875 | 800 | |
0f113f3e MC |
801 | if (inlen != 0) { |
802 | *al = SSL_AD_DECODE_ERROR; | |
803 | return 0; | |
804 | } | |
0a602875 | 805 | |
0f113f3e MC |
806 | return 1; |
807 | } | |
9cd50f73 | 808 | |
de2a9e38 | 809 | static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type, |
0f113f3e MC |
810 | const unsigned char **out, size_t *outlen, |
811 | int *al, void *arg) | |
812 | { | |
813 | const unsigned char *serverinfo = NULL; | |
814 | size_t serverinfo_length = 0; | |
815 | ||
816 | /* Is there serverinfo data for the chosen server cert? */ | |
817 | if ((ssl_get_server_cert_serverinfo(s, &serverinfo, | |
818 | &serverinfo_length)) != 0) { | |
819 | /* Find the relevant extension from the serverinfo */ | |
820 | int retval = serverinfo_find_extension(serverinfo, serverinfo_length, | |
821 | ext_type, out, outlen); | |
fae11ec7 KR |
822 | if (retval == -1) { |
823 | *al = SSL_AD_DECODE_ERROR; | |
824 | return -1; /* Error */ | |
825 | } | |
0f113f3e | 826 | if (retval == 0) |
fae11ec7 | 827 | return 0; /* No extension found, don't send extension */ |
0f113f3e MC |
828 | return 1; /* Send extension */ |
829 | } | |
830 | return -1; /* No serverinfo data found, don't send | |
831 | * extension */ | |
832 | } | |
833 | ||
834 | /* | |
835 | * With a NULL context, this function just checks that the serverinfo data | |
836 | * parses correctly. With a non-NULL context, it registers callbacks for | |
837 | * the included extensions. | |
838 | */ | |
839 | static int serverinfo_process_buffer(const unsigned char *serverinfo, | |
840 | size_t serverinfo_length, SSL_CTX *ctx) | |
841 | { | |
842 | if (serverinfo == NULL || serverinfo_length == 0) | |
843 | return 0; | |
844 | for (;;) { | |
845 | unsigned int ext_type = 0; | |
846 | size_t len = 0; | |
847 | ||
848 | /* end of serverinfo */ | |
849 | if (serverinfo_length == 0) | |
850 | return 1; | |
851 | ||
852 | /* read 2-byte type field */ | |
853 | if (serverinfo_length < 2) | |
854 | return 0; | |
855 | /* FIXME: check for types we understand explicitly? */ | |
856 | ||
857 | /* Register callbacks for extensions */ | |
858 | ext_type = (serverinfo[0] << 8) + serverinfo[1]; | |
859 | if (ctx && !SSL_CTX_add_server_custom_ext(ctx, ext_type, | |
860 | serverinfo_srv_add_cb, | |
861 | NULL, NULL, | |
862 | serverinfo_srv_parse_cb, | |
863 | NULL)) | |
864 | return 0; | |
865 | ||
866 | serverinfo += 2; | |
867 | serverinfo_length -= 2; | |
868 | ||
869 | /* read 2-byte len field */ | |
870 | if (serverinfo_length < 2) | |
871 | return 0; | |
872 | len = (serverinfo[0] << 8) + serverinfo[1]; | |
873 | serverinfo += 2; | |
874 | serverinfo_length -= 2; | |
875 | ||
876 | if (len > serverinfo_length) | |
877 | return 0; | |
878 | ||
879 | serverinfo += len; | |
880 | serverinfo_length -= len; | |
881 | } | |
882 | } | |
a398f821 | 883 | |
a398f821 | 884 | int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo, |
0f113f3e MC |
885 | size_t serverinfo_length) |
886 | { | |
887 | unsigned char *new_serverinfo; | |
888 | ||
889 | if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) { | |
890 | SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_PASSED_NULL_PARAMETER); | |
891 | return 0; | |
892 | } | |
893 | if (!serverinfo_process_buffer(serverinfo, serverinfo_length, NULL)) { | |
894 | SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA); | |
895 | return 0; | |
896 | } | |
0f113f3e MC |
897 | if (ctx->cert->key == NULL) { |
898 | SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_INTERNAL_ERROR); | |
899 | return 0; | |
900 | } | |
901 | new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo, | |
902 | serverinfo_length); | |
903 | if (new_serverinfo == NULL) { | |
904 | SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, ERR_R_MALLOC_FAILURE); | |
905 | return 0; | |
906 | } | |
907 | ctx->cert->key->serverinfo = new_serverinfo; | |
908 | memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length); | |
909 | ctx->cert->key->serverinfo_length = serverinfo_length; | |
910 | ||
911 | /* | |
912 | * Now that the serverinfo is validated and stored, go ahead and | |
913 | * register callbacks. | |
914 | */ | |
915 | if (!serverinfo_process_buffer(serverinfo, serverinfo_length, ctx)) { | |
916 | SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO, SSL_R_INVALID_SERVERINFO_DATA); | |
917 | return 0; | |
918 | } | |
919 | return 1; | |
920 | } | |
921 | ||
a398f821 | 922 | int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file) |
0f113f3e MC |
923 | { |
924 | unsigned char *serverinfo = NULL; | |
925 | size_t serverinfo_length = 0; | |
926 | unsigned char *extension = 0; | |
927 | long extension_length = 0; | |
928 | char *name = NULL; | |
929 | char *header = NULL; | |
930 | char namePrefix[] = "SERVERINFO FOR "; | |
931 | int ret = 0; | |
932 | BIO *bin = NULL; | |
933 | size_t num_extensions = 0; | |
934 | ||
935 | if (ctx == NULL || file == NULL) { | |
936 | SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, | |
937 | ERR_R_PASSED_NULL_PARAMETER); | |
938 | goto end; | |
939 | } | |
940 | ||
9982cbbb | 941 | bin = BIO_new(BIO_s_file()); |
0f113f3e MC |
942 | if (bin == NULL) { |
943 | SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB); | |
944 | goto end; | |
945 | } | |
946 | if (BIO_read_filename(bin, file) <= 0) { | |
947 | SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB); | |
948 | goto end; | |
949 | } | |
950 | ||
951 | for (num_extensions = 0;; num_extensions++) { | |
952 | if (PEM_read_bio(bin, &name, &header, &extension, &extension_length) | |
953 | == 0) { | |
954 | /* | |
955 | * There must be at least one extension in this file | |
956 | */ | |
957 | if (num_extensions == 0) { | |
958 | SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, | |
959 | SSL_R_NO_PEM_EXTENSIONS); | |
960 | goto end; | |
961 | } else /* End of file, we're done */ | |
962 | break; | |
963 | } | |
964 | /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */ | |
965 | if (strlen(name) < strlen(namePrefix)) { | |
966 | SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, | |
967 | SSL_R_PEM_NAME_TOO_SHORT); | |
968 | goto end; | |
969 | } | |
970 | if (strncmp(name, namePrefix, strlen(namePrefix)) != 0) { | |
971 | SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, | |
972 | SSL_R_PEM_NAME_BAD_PREFIX); | |
973 | goto end; | |
974 | } | |
975 | /* | |
976 | * Check that the decoded PEM data is plausible (valid length field) | |
977 | */ | |
978 | if (extension_length < 4 | |
979 | || (extension[2] << 8) + extension[3] != extension_length - 4) { | |
980 | SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA); | |
981 | goto end; | |
982 | } | |
983 | /* Append the decoded extension to the serverinfo buffer */ | |
984 | serverinfo = | |
985 | OPENSSL_realloc(serverinfo, serverinfo_length + extension_length); | |
986 | if (serverinfo == NULL) { | |
987 | SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE); | |
988 | goto end; | |
989 | } | |
990 | memcpy(serverinfo + serverinfo_length, extension, extension_length); | |
991 | serverinfo_length += extension_length; | |
992 | ||
993 | OPENSSL_free(name); | |
994 | name = NULL; | |
995 | OPENSSL_free(header); | |
996 | header = NULL; | |
997 | OPENSSL_free(extension); | |
998 | extension = NULL; | |
999 | } | |
1000 | ||
1001 | ret = SSL_CTX_use_serverinfo(ctx, serverinfo, serverinfo_length); | |
1002 | end: | |
1003 | /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */ | |
1004 | OPENSSL_free(name); | |
1005 | OPENSSL_free(header); | |
1006 | OPENSSL_free(extension); | |
1007 | OPENSSL_free(serverinfo); | |
ca3a82c3 | 1008 | BIO_free(bin); |
0f113f3e MC |
1009 | return ret; |
1010 | } |