You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

584 lines
16 KiB

  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. #include <openssl/ssl.h>
  111. #include <errno.h>
  112. #include <string.h>
  113. #include <openssl/asn1.h>
  114. #include <openssl/bio.h>
  115. #include <openssl/err.h>
  116. #include <openssl/mem.h>
  117. #include <openssl/pem.h>
  118. #include <openssl/stack.h>
  119. #include <openssl/x509.h>
  120. #include "internal.h"
  121. static int xname_cmp(const X509_NAME **a, const X509_NAME **b) {
  122. return X509_NAME_cmp(*a, *b);
  123. }
  124. // TODO(davidben): Is there any reason this doesn't call
  125. // |SSL_add_file_cert_subjects_to_stack|?
  126. STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file) {
  127. BIO *in;
  128. X509 *x = NULL;
  129. X509_NAME *xn = NULL;
  130. STACK_OF(X509_NAME) *ret = NULL, *sk;
  131. sk = sk_X509_NAME_new(xname_cmp);
  132. in = BIO_new(BIO_s_file());
  133. if (sk == NULL || in == NULL) {
  134. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  135. goto err;
  136. }
  137. if (!BIO_read_filename(in, file)) {
  138. goto err;
  139. }
  140. for (;;) {
  141. if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) {
  142. break;
  143. }
  144. if (ret == NULL) {
  145. ret = sk_X509_NAME_new_null();
  146. if (ret == NULL) {
  147. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  148. goto err;
  149. }
  150. }
  151. xn = X509_get_subject_name(x);
  152. if (xn == NULL) {
  153. goto err;
  154. }
  155. // Check for duplicates.
  156. if (sk_X509_NAME_find(sk, NULL, xn)) {
  157. continue;
  158. }
  159. xn = X509_NAME_dup(xn);
  160. if (xn == NULL ||
  161. !sk_X509_NAME_push(sk /* non-owning */, xn) ||
  162. !sk_X509_NAME_push(ret /* owning */, xn)) {
  163. X509_NAME_free(xn);
  164. goto err;
  165. }
  166. }
  167. if (0) {
  168. err:
  169. sk_X509_NAME_pop_free(ret, X509_NAME_free);
  170. ret = NULL;
  171. }
  172. sk_X509_NAME_free(sk);
  173. BIO_free(in);
  174. X509_free(x);
  175. if (ret != NULL) {
  176. ERR_clear_error();
  177. }
  178. return ret;
  179. }
  180. int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
  181. const char *file) {
  182. BIO *in;
  183. X509 *x = NULL;
  184. X509_NAME *xn = NULL;
  185. int ret = 0;
  186. int (*oldcmp)(const X509_NAME **a, const X509_NAME **b);
  187. oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_cmp);
  188. in = BIO_new(BIO_s_file());
  189. if (in == NULL) {
  190. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  191. goto err;
  192. }
  193. if (!BIO_read_filename(in, file)) {
  194. goto err;
  195. }
  196. for (;;) {
  197. if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) {
  198. break;
  199. }
  200. xn = X509_get_subject_name(x);
  201. if (xn == NULL) {
  202. goto err;
  203. }
  204. // Check for duplicates.
  205. if (sk_X509_NAME_find(stack, NULL, xn)) {
  206. continue;
  207. }
  208. xn = X509_NAME_dup(xn);
  209. if (xn == NULL ||
  210. !sk_X509_NAME_push(stack, xn)) {
  211. X509_NAME_free(xn);
  212. goto err;
  213. }
  214. }
  215. ERR_clear_error();
  216. ret = 1;
  217. err:
  218. BIO_free(in);
  219. X509_free(x);
  220. (void) sk_X509_NAME_set_cmp_func(stack, oldcmp);
  221. return ret;
  222. }
  223. int SSL_use_certificate_file(SSL *ssl, const char *file, int type) {
  224. int reason_code;
  225. BIO *in;
  226. int ret = 0;
  227. X509 *x = NULL;
  228. in = BIO_new(BIO_s_file());
  229. if (in == NULL) {
  230. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  231. goto end;
  232. }
  233. if (BIO_read_filename(in, file) <= 0) {
  234. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  235. goto end;
  236. }
  237. if (type == SSL_FILETYPE_ASN1) {
  238. reason_code = ERR_R_ASN1_LIB;
  239. x = d2i_X509_bio(in, NULL);
  240. } else if (type == SSL_FILETYPE_PEM) {
  241. reason_code = ERR_R_PEM_LIB;
  242. x = PEM_read_bio_X509(in, NULL, ssl->ctx->default_passwd_callback,
  243. ssl->ctx->default_passwd_callback_userdata);
  244. } else {
  245. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE);
  246. goto end;
  247. }
  248. if (x == NULL) {
  249. OPENSSL_PUT_ERROR(SSL, reason_code);
  250. goto end;
  251. }
  252. ret = SSL_use_certificate(ssl, x);
  253. end:
  254. X509_free(x);
  255. BIO_free(in);
  256. return ret;
  257. }
  258. int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type) {
  259. int reason_code, ret = 0;
  260. BIO *in;
  261. RSA *rsa = NULL;
  262. in = BIO_new(BIO_s_file());
  263. if (in == NULL) {
  264. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  265. goto end;
  266. }
  267. if (BIO_read_filename(in, file) <= 0) {
  268. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  269. goto end;
  270. }
  271. if (type == SSL_FILETYPE_ASN1) {
  272. reason_code = ERR_R_ASN1_LIB;
  273. rsa = d2i_RSAPrivateKey_bio(in, NULL);
  274. } else if (type == SSL_FILETYPE_PEM) {
  275. reason_code = ERR_R_PEM_LIB;
  276. rsa =
  277. PEM_read_bio_RSAPrivateKey(in, NULL, ssl->ctx->default_passwd_callback,
  278. ssl->ctx->default_passwd_callback_userdata);
  279. } else {
  280. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE);
  281. goto end;
  282. }
  283. if (rsa == NULL) {
  284. OPENSSL_PUT_ERROR(SSL, reason_code);
  285. goto end;
  286. }
  287. ret = SSL_use_RSAPrivateKey(ssl, rsa);
  288. RSA_free(rsa);
  289. end:
  290. BIO_free(in);
  291. return ret;
  292. }
  293. int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type) {
  294. int reason_code, ret = 0;
  295. BIO *in;
  296. EVP_PKEY *pkey = NULL;
  297. in = BIO_new(BIO_s_file());
  298. if (in == NULL) {
  299. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  300. goto end;
  301. }
  302. if (BIO_read_filename(in, file) <= 0) {
  303. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  304. goto end;
  305. }
  306. if (type == SSL_FILETYPE_PEM) {
  307. reason_code = ERR_R_PEM_LIB;
  308. pkey = PEM_read_bio_PrivateKey(in, NULL, ssl->ctx->default_passwd_callback,
  309. ssl->ctx->default_passwd_callback_userdata);
  310. } else if (type == SSL_FILETYPE_ASN1) {
  311. reason_code = ERR_R_ASN1_LIB;
  312. pkey = d2i_PrivateKey_bio(in, NULL);
  313. } else {
  314. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE);
  315. goto end;
  316. }
  317. if (pkey == NULL) {
  318. OPENSSL_PUT_ERROR(SSL, reason_code);
  319. goto end;
  320. }
  321. ret = SSL_use_PrivateKey(ssl, pkey);
  322. EVP_PKEY_free(pkey);
  323. end:
  324. BIO_free(in);
  325. return ret;
  326. }
  327. int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type) {
  328. int reason_code;
  329. BIO *in;
  330. int ret = 0;
  331. X509 *x = NULL;
  332. in = BIO_new(BIO_s_file());
  333. if (in == NULL) {
  334. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  335. goto end;
  336. }
  337. if (BIO_read_filename(in, file) <= 0) {
  338. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  339. goto end;
  340. }
  341. if (type == SSL_FILETYPE_ASN1) {
  342. reason_code = ERR_R_ASN1_LIB;
  343. x = d2i_X509_bio(in, NULL);
  344. } else if (type == SSL_FILETYPE_PEM) {
  345. reason_code = ERR_R_PEM_LIB;
  346. x = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
  347. ctx->default_passwd_callback_userdata);
  348. } else {
  349. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE);
  350. goto end;
  351. }
  352. if (x == NULL) {
  353. OPENSSL_PUT_ERROR(SSL, reason_code);
  354. goto end;
  355. }
  356. ret = SSL_CTX_use_certificate(ctx, x);
  357. end:
  358. X509_free(x);
  359. BIO_free(in);
  360. return ret;
  361. }
  362. int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type) {
  363. int reason_code, ret = 0;
  364. BIO *in;
  365. RSA *rsa = NULL;
  366. in = BIO_new(BIO_s_file());
  367. if (in == NULL) {
  368. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  369. goto end;
  370. }
  371. if (BIO_read_filename(in, file) <= 0) {
  372. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  373. goto end;
  374. }
  375. if (type == SSL_FILETYPE_ASN1) {
  376. reason_code = ERR_R_ASN1_LIB;
  377. rsa = d2i_RSAPrivateKey_bio(in, NULL);
  378. } else if (type == SSL_FILETYPE_PEM) {
  379. reason_code = ERR_R_PEM_LIB;
  380. rsa = PEM_read_bio_RSAPrivateKey(in, NULL, ctx->default_passwd_callback,
  381. ctx->default_passwd_callback_userdata);
  382. } else {
  383. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE);
  384. goto end;
  385. }
  386. if (rsa == NULL) {
  387. OPENSSL_PUT_ERROR(SSL, reason_code);
  388. goto end;
  389. }
  390. ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
  391. RSA_free(rsa);
  392. end:
  393. BIO_free(in);
  394. return ret;
  395. }
  396. int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type) {
  397. int reason_code, ret = 0;
  398. BIO *in;
  399. EVP_PKEY *pkey = NULL;
  400. in = BIO_new(BIO_s_file());
  401. if (in == NULL) {
  402. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  403. goto end;
  404. }
  405. if (BIO_read_filename(in, file) <= 0) {
  406. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  407. goto end;
  408. }
  409. if (type == SSL_FILETYPE_PEM) {
  410. reason_code = ERR_R_PEM_LIB;
  411. pkey = PEM_read_bio_PrivateKey(in, NULL, ctx->default_passwd_callback,
  412. ctx->default_passwd_callback_userdata);
  413. } else if (type == SSL_FILETYPE_ASN1) {
  414. reason_code = ERR_R_ASN1_LIB;
  415. pkey = d2i_PrivateKey_bio(in, NULL);
  416. } else {
  417. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SSL_FILETYPE);
  418. goto end;
  419. }
  420. if (pkey == NULL) {
  421. OPENSSL_PUT_ERROR(SSL, reason_code);
  422. goto end;
  423. }
  424. ret = SSL_CTX_use_PrivateKey(ctx, pkey);
  425. EVP_PKEY_free(pkey);
  426. end:
  427. BIO_free(in);
  428. return ret;
  429. }
  430. // Read a file that contains our certificate in "PEM" format, possibly followed
  431. // by a sequence of CA certificates that should be sent to the peer in the
  432. // Certificate message.
  433. int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file) {
  434. BIO *in;
  435. int ret = 0;
  436. X509 *x = NULL;
  437. ERR_clear_error(); // clear error stack for SSL_CTX_use_certificate()
  438. in = BIO_new(BIO_s_file());
  439. if (in == NULL) {
  440. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  441. goto end;
  442. }
  443. if (BIO_read_filename(in, file) <= 0) {
  444. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  445. goto end;
  446. }
  447. x = PEM_read_bio_X509_AUX(in, NULL, ctx->default_passwd_callback,
  448. ctx->default_passwd_callback_userdata);
  449. if (x == NULL) {
  450. OPENSSL_PUT_ERROR(SSL, ERR_R_PEM_LIB);
  451. goto end;
  452. }
  453. ret = SSL_CTX_use_certificate(ctx, x);
  454. if (ERR_peek_error() != 0) {
  455. ret = 0; // Key/certificate mismatch doesn't imply ret==0 ...
  456. }
  457. if (ret) {
  458. // If we could set up our certificate, now proceed to the CA
  459. // certificates.
  460. X509 *ca;
  461. int r;
  462. uint32_t err;
  463. SSL_CTX_clear_chain_certs(ctx);
  464. while ((ca = PEM_read_bio_X509(in, NULL, ctx->default_passwd_callback,
  465. ctx->default_passwd_callback_userdata)) !=
  466. NULL) {
  467. r = SSL_CTX_add0_chain_cert(ctx, ca);
  468. if (!r) {
  469. X509_free(ca);
  470. ret = 0;
  471. goto end;
  472. }
  473. // Note that we must not free r if it was successfully added to the chain
  474. // (while we must free the main certificate, since its reference count is
  475. // increased by SSL_CTX_use_certificate).
  476. }
  477. // When the while loop ends, it's usually just EOF.
  478. err = ERR_peek_last_error();
  479. if (ERR_GET_LIB(err) == ERR_LIB_PEM &&
  480. ERR_GET_REASON(err) == PEM_R_NO_START_LINE) {
  481. ERR_clear_error();
  482. } else {
  483. ret = 0; // some real error
  484. }
  485. }
  486. end:
  487. X509_free(x);
  488. BIO_free(in);
  489. return ret;
  490. }
  491. void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
  492. ctx->default_passwd_callback = cb;
  493. }
  494. pem_password_cb *SSL_CTX_get_default_passwd_cb(const SSL_CTX *ctx) {
  495. return ctx->default_passwd_callback;
  496. }
  497. void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *data) {
  498. ctx->default_passwd_callback_userdata = data;
  499. }
  500. void *SSL_CTX_get_default_passwd_cb_userdata(const SSL_CTX *ctx) {
  501. return ctx->default_passwd_callback_userdata;
  502. }