ssl_rsa.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "ssl_local.h"
  11. #include "internal/packet.h"
  12. #include <openssl/bio.h>
  13. #include <openssl/objects.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/x509.h>
  16. #include <openssl/x509v3.h>
  17. #include <openssl/pem.h>
  18. static int ssl_set_cert(CERT *c, X509 *x509);
  19. static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
  20. #define SYNTHV1CONTEXT (SSL_EXT_TLS1_2_AND_BELOW_ONLY \
  21. | SSL_EXT_CLIENT_HELLO \
  22. | SSL_EXT_TLS1_2_SERVER_HELLO \
  23. | SSL_EXT_IGNORE_ON_RESUMPTION)
  24. int SSL_use_certificate(SSL *ssl, X509 *x)
  25. {
  26. int rv;
  27. if (x == NULL) {
  28. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
  29. return 0;
  30. }
  31. rv = ssl_security_cert(ssl, NULL, x, 0, 1);
  32. if (rv != 1) {
  33. ERR_raise(ERR_LIB_SSL, rv);
  34. return 0;
  35. }
  36. return ssl_set_cert(ssl->cert, x);
  37. }
  38. int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
  39. {
  40. int j;
  41. BIO *in;
  42. int ret = 0;
  43. X509 *cert = NULL, *x = NULL;
  44. in = BIO_new(BIO_s_file());
  45. if (in == NULL) {
  46. ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
  47. goto end;
  48. }
  49. if (BIO_read_filename(in, file) <= 0) {
  50. ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
  51. goto end;
  52. }
  53. if (type != SSL_FILETYPE_ASN1 && type != SSL_FILETYPE_PEM) {
  54. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
  55. goto end;
  56. }
  57. x = X509_new_ex(ssl->ctx->libctx, ssl->ctx->propq);
  58. if (x == NULL) {
  59. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  60. goto end;
  61. }
  62. if (type == SSL_FILETYPE_ASN1) {
  63. j = ERR_R_ASN1_LIB;
  64. cert = d2i_X509_bio(in, &x);
  65. } else if (type == SSL_FILETYPE_PEM) {
  66. j = ERR_R_PEM_LIB;
  67. cert = PEM_read_bio_X509(in, &x, ssl->default_passwd_callback,
  68. ssl->default_passwd_callback_userdata);
  69. } else {
  70. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
  71. goto end;
  72. }
  73. if (cert == NULL) {
  74. ERR_raise(ERR_LIB_SSL, j);
  75. goto end;
  76. }
  77. ret = SSL_use_certificate(ssl, x);
  78. end:
  79. X509_free(x);
  80. BIO_free(in);
  81. return ret;
  82. }
  83. int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
  84. {
  85. X509 *x;
  86. int ret;
  87. x = X509_new_ex(ssl->ctx->libctx, ssl->ctx->propq);
  88. if (x == NULL) {
  89. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  90. return 0;
  91. }
  92. if (d2i_X509(&x, &d, (long)len)== NULL) {
  93. X509_free(x);
  94. ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
  95. return 0;
  96. }
  97. ret = SSL_use_certificate(ssl, x);
  98. X509_free(x);
  99. return ret;
  100. }
  101. static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
  102. {
  103. size_t i;
  104. if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
  105. ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  106. return 0;
  107. }
  108. if (c->pkeys[i].x509 != NULL
  109. && !X509_check_private_key(c->pkeys[i].x509, pkey))
  110. return 0;
  111. EVP_PKEY_free(c->pkeys[i].privatekey);
  112. EVP_PKEY_up_ref(pkey);
  113. c->pkeys[i].privatekey = pkey;
  114. c->key = &c->pkeys[i];
  115. return 1;
  116. }
  117. int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
  118. {
  119. int ret;
  120. if (pkey == NULL) {
  121. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
  122. return 0;
  123. }
  124. ret = ssl_set_pkey(ssl->cert, pkey);
  125. return ret;
  126. }
  127. int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
  128. {
  129. int j, ret = 0;
  130. BIO *in;
  131. EVP_PKEY *pkey = NULL;
  132. in = BIO_new(BIO_s_file());
  133. if (in == NULL) {
  134. ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
  135. goto end;
  136. }
  137. if (BIO_read_filename(in, file) <= 0) {
  138. ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
  139. goto end;
  140. }
  141. if (type == SSL_FILETYPE_PEM) {
  142. j = ERR_R_PEM_LIB;
  143. pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
  144. ssl->default_passwd_callback,
  145. ssl->default_passwd_callback_userdata,
  146. ssl->ctx->libctx,
  147. ssl->ctx->propq);
  148. } else if (type == SSL_FILETYPE_ASN1) {
  149. j = ERR_R_ASN1_LIB;
  150. pkey = d2i_PrivateKey_ex_bio(in, NULL, ssl->ctx->libctx,
  151. ssl->ctx->propq);
  152. } else {
  153. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
  154. goto end;
  155. }
  156. if (pkey == NULL) {
  157. ERR_raise(ERR_LIB_SSL, j);
  158. goto end;
  159. }
  160. ret = SSL_use_PrivateKey(ssl, pkey);
  161. EVP_PKEY_free(pkey);
  162. end:
  163. BIO_free(in);
  164. return ret;
  165. }
  166. int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
  167. long len)
  168. {
  169. int ret;
  170. const unsigned char *p;
  171. EVP_PKEY *pkey;
  172. p = d;
  173. if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ssl->ctx->libctx,
  174. ssl->ctx->propq)) == NULL) {
  175. ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
  176. return 0;
  177. }
  178. ret = SSL_use_PrivateKey(ssl, pkey);
  179. EVP_PKEY_free(pkey);
  180. return ret;
  181. }
  182. int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
  183. {
  184. int rv;
  185. if (x == NULL) {
  186. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
  187. return 0;
  188. }
  189. rv = ssl_security_cert(NULL, ctx, x, 0, 1);
  190. if (rv != 1) {
  191. ERR_raise(ERR_LIB_SSL, rv);
  192. return 0;
  193. }
  194. return ssl_set_cert(ctx->cert, x);
  195. }
  196. static int ssl_set_cert(CERT *c, X509 *x)
  197. {
  198. EVP_PKEY *pkey;
  199. size_t i;
  200. pkey = X509_get0_pubkey(x);
  201. if (pkey == NULL) {
  202. ERR_raise(ERR_LIB_SSL, SSL_R_X509_LIB);
  203. return 0;
  204. }
  205. if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
  206. ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  207. return 0;
  208. }
  209. if (i == SSL_PKEY_ECC && !EVP_PKEY_can_sign(pkey)) {
  210. ERR_raise(ERR_LIB_SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
  211. return 0;
  212. }
  213. if (c->pkeys[i].privatekey != NULL) {
  214. /*
  215. * The return code from EVP_PKEY_copy_parameters is deliberately
  216. * ignored. Some EVP_PKEY types cannot do this.
  217. */
  218. EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
  219. ERR_clear_error();
  220. if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
  221. /*
  222. * don't fail for a cert/key mismatch, just free current private
  223. * key (when switching to a different cert & key, first this
  224. * function should be used, then ssl_set_pkey
  225. */
  226. EVP_PKEY_free(c->pkeys[i].privatekey);
  227. c->pkeys[i].privatekey = NULL;
  228. /* clear error queue */
  229. ERR_clear_error();
  230. }
  231. }
  232. X509_free(c->pkeys[i].x509);
  233. X509_up_ref(x);
  234. c->pkeys[i].x509 = x;
  235. c->key = &(c->pkeys[i]);
  236. return 1;
  237. }
  238. int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
  239. {
  240. int j = SSL_R_BAD_VALUE;
  241. BIO *in;
  242. int ret = 0;
  243. X509 *x = NULL, *cert = NULL;
  244. in = BIO_new(BIO_s_file());
  245. if (in == NULL) {
  246. ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
  247. goto end;
  248. }
  249. if (BIO_read_filename(in, file) <= 0) {
  250. ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
  251. goto end;
  252. }
  253. if (type != SSL_FILETYPE_ASN1 && type != SSL_FILETYPE_PEM) {
  254. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
  255. goto end;
  256. }
  257. x = X509_new_ex(ctx->libctx, ctx->propq);
  258. if (x == NULL) {
  259. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  260. goto end;
  261. }
  262. if (type == SSL_FILETYPE_ASN1) {
  263. j = ERR_R_ASN1_LIB;
  264. cert = d2i_X509_bio(in, &x);
  265. } else if (type == SSL_FILETYPE_PEM) {
  266. j = ERR_R_PEM_LIB;
  267. cert = PEM_read_bio_X509(in, &x, ctx->default_passwd_callback,
  268. ctx->default_passwd_callback_userdata);
  269. }
  270. if (cert == NULL) {
  271. ERR_raise(ERR_LIB_SSL, j);
  272. goto end;
  273. }
  274. ret = SSL_CTX_use_certificate(ctx, x);
  275. end:
  276. X509_free(x);
  277. BIO_free(in);
  278. return ret;
  279. }
  280. int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
  281. {
  282. X509 *x;
  283. int ret;
  284. x = X509_new_ex(ctx->libctx, ctx->propq);
  285. if (x == NULL) {
  286. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  287. return 0;
  288. }
  289. if (d2i_X509(&x, &d, (long)len) == NULL) {
  290. X509_free(x);
  291. ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
  292. return 0;
  293. }
  294. ret = SSL_CTX_use_certificate(ctx, x);
  295. X509_free(x);
  296. return ret;
  297. }
  298. int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
  299. {
  300. if (pkey == NULL) {
  301. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
  302. return 0;
  303. }
  304. return ssl_set_pkey(ctx->cert, pkey);
  305. }
  306. int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
  307. {
  308. int j, ret = 0;
  309. BIO *in;
  310. EVP_PKEY *pkey = NULL;
  311. in = BIO_new(BIO_s_file());
  312. if (in == NULL) {
  313. ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
  314. goto end;
  315. }
  316. if (BIO_read_filename(in, file) <= 0) {
  317. ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
  318. goto end;
  319. }
  320. if (type == SSL_FILETYPE_PEM) {
  321. j = ERR_R_PEM_LIB;
  322. pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
  323. ctx->default_passwd_callback,
  324. ctx->default_passwd_callback_userdata,
  325. ctx->libctx, ctx->propq);
  326. } else if (type == SSL_FILETYPE_ASN1) {
  327. j = ERR_R_ASN1_LIB;
  328. pkey = d2i_PrivateKey_ex_bio(in, NULL, ctx->libctx, ctx->propq);
  329. } else {
  330. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
  331. goto end;
  332. }
  333. if (pkey == NULL) {
  334. ERR_raise(ERR_LIB_SSL, j);
  335. goto end;
  336. }
  337. ret = SSL_CTX_use_PrivateKey(ctx, pkey);
  338. EVP_PKEY_free(pkey);
  339. end:
  340. BIO_free(in);
  341. return ret;
  342. }
  343. int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
  344. const unsigned char *d, long len)
  345. {
  346. int ret;
  347. const unsigned char *p;
  348. EVP_PKEY *pkey;
  349. p = d;
  350. if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ctx->libctx,
  351. ctx->propq)) == NULL) {
  352. ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
  353. return 0;
  354. }
  355. ret = SSL_CTX_use_PrivateKey(ctx, pkey);
  356. EVP_PKEY_free(pkey);
  357. return ret;
  358. }
  359. /*
  360. * Read a file that contains our certificate in "PEM" format, possibly
  361. * followed by a sequence of CA certificates that should be sent to the peer
  362. * in the Certificate message.
  363. */
  364. static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
  365. {
  366. BIO *in;
  367. int ret = 0;
  368. X509 *x = NULL;
  369. pem_password_cb *passwd_callback;
  370. void *passwd_callback_userdata;
  371. SSL_CTX *real_ctx = (ssl == NULL) ? ctx : ssl->ctx;
  372. if (ctx == NULL && ssl == NULL)
  373. return 0;
  374. ERR_clear_error(); /* clear error stack for
  375. * SSL_CTX_use_certificate() */
  376. if (ctx != NULL) {
  377. passwd_callback = ctx->default_passwd_callback;
  378. passwd_callback_userdata = ctx->default_passwd_callback_userdata;
  379. } else {
  380. passwd_callback = ssl->default_passwd_callback;
  381. passwd_callback_userdata = ssl->default_passwd_callback_userdata;
  382. }
  383. in = BIO_new(BIO_s_file());
  384. if (in == NULL) {
  385. ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
  386. goto end;
  387. }
  388. if (BIO_read_filename(in, file) <= 0) {
  389. ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
  390. goto end;
  391. }
  392. x = X509_new_ex(real_ctx->libctx, real_ctx->propq);
  393. if (x == NULL) {
  394. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  395. goto end;
  396. }
  397. if (PEM_read_bio_X509_AUX(in, &x, passwd_callback,
  398. passwd_callback_userdata) == NULL) {
  399. ERR_raise(ERR_LIB_SSL, ERR_R_PEM_LIB);
  400. goto end;
  401. }
  402. if (ctx)
  403. ret = SSL_CTX_use_certificate(ctx, x);
  404. else
  405. ret = SSL_use_certificate(ssl, x);
  406. if (ERR_peek_error() != 0)
  407. ret = 0; /* Key/certificate mismatch doesn't imply
  408. * ret==0 ... */
  409. if (ret) {
  410. /*
  411. * If we could set up our certificate, now proceed to the CA
  412. * certificates.
  413. */
  414. X509 *ca;
  415. int r;
  416. unsigned long err;
  417. if (ctx)
  418. r = SSL_CTX_clear_chain_certs(ctx);
  419. else
  420. r = SSL_clear_chain_certs(ssl);
  421. if (r == 0) {
  422. ret = 0;
  423. goto end;
  424. }
  425. while (1) {
  426. ca = X509_new_ex(real_ctx->libctx, real_ctx->propq);
  427. if (ca == NULL) {
  428. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  429. goto end;
  430. }
  431. if (PEM_read_bio_X509(in, &ca, passwd_callback,
  432. passwd_callback_userdata) != NULL) {
  433. if (ctx)
  434. r = SSL_CTX_add0_chain_cert(ctx, ca);
  435. else
  436. r = SSL_add0_chain_cert(ssl, ca);
  437. /*
  438. * Note that we must not free ca if it was successfully added to
  439. * the chain (while we must free the main certificate, since its
  440. * reference count is increased by SSL_CTX_use_certificate).
  441. */
  442. if (!r) {
  443. X509_free(ca);
  444. ret = 0;
  445. goto end;
  446. }
  447. } else {
  448. X509_free(ca);
  449. break;
  450. }
  451. }
  452. /* When the while loop ends, it's usually just EOF. */
  453. err = ERR_peek_last_error();
  454. if (ERR_GET_LIB(err) == ERR_LIB_PEM
  455. && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
  456. ERR_clear_error();
  457. else
  458. ret = 0; /* some real error */
  459. }
  460. end:
  461. X509_free(x);
  462. BIO_free(in);
  463. return ret;
  464. }
  465. int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
  466. {
  467. return use_certificate_chain_file(ctx, NULL, file);
  468. }
  469. int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
  470. {
  471. return use_certificate_chain_file(NULL, ssl, file);
  472. }
  473. static int serverinfo_find_extension(const unsigned char *serverinfo,
  474. size_t serverinfo_length,
  475. unsigned int extension_type,
  476. const unsigned char **extension_data,
  477. size_t *extension_length)
  478. {
  479. PACKET pkt, data;
  480. *extension_data = NULL;
  481. *extension_length = 0;
  482. if (serverinfo == NULL || serverinfo_length == 0)
  483. return -1;
  484. if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
  485. return -1;
  486. for (;;) {
  487. unsigned int type = 0;
  488. unsigned long context = 0;
  489. /* end of serverinfo */
  490. if (PACKET_remaining(&pkt) == 0)
  491. return 0; /* Extension not found */
  492. if (!PACKET_get_net_4(&pkt, &context)
  493. || !PACKET_get_net_2(&pkt, &type)
  494. || !PACKET_get_length_prefixed_2(&pkt, &data))
  495. return -1;
  496. if (type == extension_type) {
  497. *extension_data = PACKET_data(&data);
  498. *extension_length = PACKET_remaining(&data);;
  499. return 1; /* Success */
  500. }
  501. }
  502. /* Unreachable */
  503. }
  504. static int serverinfoex_srv_parse_cb(SSL *s, unsigned int ext_type,
  505. unsigned int context,
  506. const unsigned char *in,
  507. size_t inlen, X509 *x, size_t chainidx,
  508. int *al, void *arg)
  509. {
  510. if (inlen != 0) {
  511. *al = SSL_AD_DECODE_ERROR;
  512. return 0;
  513. }
  514. return 1;
  515. }
  516. static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
  517. const unsigned char *in,
  518. size_t inlen, int *al, void *arg)
  519. {
  520. return serverinfoex_srv_parse_cb(s, ext_type, 0, in, inlen, NULL, 0, al,
  521. arg);
  522. }
  523. static int serverinfoex_srv_add_cb(SSL *s, unsigned int ext_type,
  524. unsigned int context,
  525. const unsigned char **out,
  526. size_t *outlen, X509 *x, size_t chainidx,
  527. int *al, void *arg)
  528. {
  529. const unsigned char *serverinfo = NULL;
  530. size_t serverinfo_length = 0;
  531. /* We only support extensions for the first Certificate */
  532. if ((context & SSL_EXT_TLS1_3_CERTIFICATE) != 0 && chainidx > 0)
  533. return 0;
  534. /* Is there serverinfo data for the chosen server cert? */
  535. if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
  536. &serverinfo_length)) != 0) {
  537. /* Find the relevant extension from the serverinfo */
  538. int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
  539. ext_type, out, outlen);
  540. if (retval == -1) {
  541. *al = SSL_AD_INTERNAL_ERROR;
  542. return -1; /* Error */
  543. }
  544. if (retval == 0)
  545. return 0; /* No extension found, don't send extension */
  546. return 1; /* Send extension */
  547. }
  548. return 0; /* No serverinfo data found, don't send
  549. * extension */
  550. }
  551. static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
  552. const unsigned char **out, size_t *outlen,
  553. int *al, void *arg)
  554. {
  555. return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
  556. arg);
  557. }
  558. /*
  559. * With a NULL context, this function just checks that the serverinfo data
  560. * parses correctly. With a non-NULL context, it registers callbacks for
  561. * the included extensions.
  562. */
  563. static int serverinfo_process_buffer(unsigned int version,
  564. const unsigned char *serverinfo,
  565. size_t serverinfo_length, SSL_CTX *ctx)
  566. {
  567. PACKET pkt;
  568. if (serverinfo == NULL || serverinfo_length == 0)
  569. return 0;
  570. if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2)
  571. return 0;
  572. if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
  573. return 0;
  574. while (PACKET_remaining(&pkt)) {
  575. unsigned long context = 0;
  576. unsigned int ext_type = 0;
  577. PACKET data;
  578. if ((version == SSL_SERVERINFOV2 && !PACKET_get_net_4(&pkt, &context))
  579. || !PACKET_get_net_2(&pkt, &ext_type)
  580. || !PACKET_get_length_prefixed_2(&pkt, &data))
  581. return 0;
  582. if (ctx == NULL)
  583. continue;
  584. /*
  585. * The old style custom extensions API could be set separately for
  586. * server/client, i.e. you could set one custom extension for a client,
  587. * and *for the same extension in the same SSL_CTX* you could set a
  588. * custom extension for the server as well. It seems quite weird to be
  589. * setting a custom extension for both client and server in a single
  590. * SSL_CTX - but theoretically possible. This isn't possible in the
  591. * new API. Therefore, if we have V1 serverinfo we use the old API. We
  592. * also use the old API even if we have V2 serverinfo but the context
  593. * looks like an old style <= TLSv1.2 extension.
  594. */
  595. if (version == SSL_SERVERINFOV1 || context == SYNTHV1CONTEXT) {
  596. if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
  597. serverinfo_srv_add_cb,
  598. NULL, NULL,
  599. serverinfo_srv_parse_cb,
  600. NULL))
  601. return 0;
  602. } else {
  603. if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
  604. serverinfoex_srv_add_cb,
  605. NULL, NULL,
  606. serverinfoex_srv_parse_cb,
  607. NULL))
  608. return 0;
  609. }
  610. }
  611. return 1;
  612. }
  613. int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
  614. const unsigned char *serverinfo,
  615. size_t serverinfo_length)
  616. {
  617. unsigned char *new_serverinfo;
  618. if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
  619. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
  620. return 0;
  621. }
  622. if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
  623. NULL)) {
  624. ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_SERVERINFO_DATA);
  625. return 0;
  626. }
  627. if (ctx->cert->key == NULL) {
  628. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  629. return 0;
  630. }
  631. new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
  632. serverinfo_length);
  633. if (new_serverinfo == NULL) {
  634. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  635. return 0;
  636. }
  637. ctx->cert->key->serverinfo = new_serverinfo;
  638. memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
  639. ctx->cert->key->serverinfo_length = serverinfo_length;
  640. /*
  641. * Now that the serverinfo is validated and stored, go ahead and
  642. * register callbacks.
  643. */
  644. if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
  645. ctx)) {
  646. ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_SERVERINFO_DATA);
  647. return 0;
  648. }
  649. return 1;
  650. }
  651. int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
  652. size_t serverinfo_length)
  653. {
  654. return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo,
  655. serverinfo_length);
  656. }
  657. int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
  658. {
  659. unsigned char *serverinfo = NULL;
  660. unsigned char *tmp;
  661. size_t serverinfo_length = 0;
  662. unsigned char *extension = 0;
  663. long extension_length = 0;
  664. char *name = NULL;
  665. char *header = NULL;
  666. static const char namePrefix1[] = "SERVERINFO FOR ";
  667. static const char namePrefix2[] = "SERVERINFOV2 FOR ";
  668. unsigned int name_len;
  669. int ret = 0;
  670. BIO *bin = NULL;
  671. size_t num_extensions = 0, contextoff = 0;
  672. if (ctx == NULL || file == NULL) {
  673. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
  674. goto end;
  675. }
  676. bin = BIO_new(BIO_s_file());
  677. if (bin == NULL) {
  678. ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
  679. goto end;
  680. }
  681. if (BIO_read_filename(bin, file) <= 0) {
  682. ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
  683. goto end;
  684. }
  685. for (num_extensions = 0;; num_extensions++) {
  686. unsigned int version;
  687. if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
  688. == 0) {
  689. /*
  690. * There must be at least one extension in this file
  691. */
  692. if (num_extensions == 0) {
  693. ERR_raise(ERR_LIB_SSL, SSL_R_NO_PEM_EXTENSIONS);
  694. goto end;
  695. } else /* End of file, we're done */
  696. break;
  697. }
  698. /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
  699. name_len = strlen(name);
  700. if (name_len < sizeof(namePrefix1) - 1) {
  701. ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_TOO_SHORT);
  702. goto end;
  703. }
  704. if (strncmp(name, namePrefix1, sizeof(namePrefix1) - 1) == 0) {
  705. version = SSL_SERVERINFOV1;
  706. } else {
  707. if (name_len < sizeof(namePrefix2) - 1) {
  708. ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_TOO_SHORT);
  709. goto end;
  710. }
  711. if (strncmp(name, namePrefix2, sizeof(namePrefix2) - 1) != 0) {
  712. ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_BAD_PREFIX);
  713. goto end;
  714. }
  715. version = SSL_SERVERINFOV2;
  716. }
  717. /*
  718. * Check that the decoded PEM data is plausible (valid length field)
  719. */
  720. if (version == SSL_SERVERINFOV1) {
  721. /* 4 byte header: 2 bytes type, 2 bytes len */
  722. if (extension_length < 4
  723. || (extension[2] << 8) + extension[3]
  724. != extension_length - 4) {
  725. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA);
  726. goto end;
  727. }
  728. /*
  729. * File does not have a context value so we must take account of
  730. * this later.
  731. */
  732. contextoff = 4;
  733. } else {
  734. /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
  735. if (extension_length < 8
  736. || (extension[6] << 8) + extension[7]
  737. != extension_length - 8) {
  738. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA);
  739. goto end;
  740. }
  741. }
  742. /* Append the decoded extension to the serverinfo buffer */
  743. tmp = OPENSSL_realloc(serverinfo, serverinfo_length + extension_length
  744. + contextoff);
  745. if (tmp == NULL) {
  746. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  747. goto end;
  748. }
  749. serverinfo = tmp;
  750. if (contextoff > 0) {
  751. unsigned char *sinfo = serverinfo + serverinfo_length;
  752. /* We know this only uses the last 2 bytes */
  753. sinfo[0] = 0;
  754. sinfo[1] = 0;
  755. sinfo[2] = (SYNTHV1CONTEXT >> 8) & 0xff;
  756. sinfo[3] = SYNTHV1CONTEXT & 0xff;
  757. }
  758. memcpy(serverinfo + serverinfo_length + contextoff,
  759. extension, extension_length);
  760. serverinfo_length += extension_length + contextoff;
  761. OPENSSL_free(name);
  762. name = NULL;
  763. OPENSSL_free(header);
  764. header = NULL;
  765. OPENSSL_free(extension);
  766. extension = NULL;
  767. }
  768. ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, serverinfo,
  769. serverinfo_length);
  770. end:
  771. /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
  772. OPENSSL_free(name);
  773. OPENSSL_free(header);
  774. OPENSSL_free(extension);
  775. OPENSSL_free(serverinfo);
  776. BIO_free(bin);
  777. return ret;
  778. }
  779. static int ssl_set_cert_and_key(SSL *ssl, SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
  780. STACK_OF(X509) *chain, int override)
  781. {
  782. int ret = 0;
  783. size_t i;
  784. int j;
  785. int rv;
  786. CERT *c = ssl != NULL ? ssl->cert : ctx->cert;
  787. STACK_OF(X509) *dup_chain = NULL;
  788. EVP_PKEY *pubkey = NULL;
  789. /* Do all security checks before anything else */
  790. rv = ssl_security_cert(ssl, ctx, x509, 0, 1);
  791. if (rv != 1) {
  792. ERR_raise(ERR_LIB_SSL, rv);
  793. goto out;
  794. }
  795. for (j = 0; j < sk_X509_num(chain); j++) {
  796. rv = ssl_security_cert(ssl, ctx, sk_X509_value(chain, j), 0, 0);
  797. if (rv != 1) {
  798. ERR_raise(ERR_LIB_SSL, rv);
  799. goto out;
  800. }
  801. }
  802. pubkey = X509_get_pubkey(x509); /* bumps reference */
  803. if (pubkey == NULL)
  804. goto out;
  805. if (privatekey == NULL) {
  806. privatekey = pubkey;
  807. } else {
  808. /* For RSA, which has no parameters, missing returns 0 */
  809. if (EVP_PKEY_missing_parameters(privatekey)) {
  810. if (EVP_PKEY_missing_parameters(pubkey)) {
  811. /* nobody has parameters? - error */
  812. ERR_raise(ERR_LIB_SSL, SSL_R_MISSING_PARAMETERS);
  813. goto out;
  814. } else {
  815. /* copy to privatekey from pubkey */
  816. EVP_PKEY_copy_parameters(privatekey, pubkey);
  817. }
  818. } else if (EVP_PKEY_missing_parameters(pubkey)) {
  819. /* copy to pubkey from privatekey */
  820. EVP_PKEY_copy_parameters(pubkey, privatekey);
  821. } /* else both have parameters */
  822. /* check that key <-> cert match */
  823. if (EVP_PKEY_eq(pubkey, privatekey) != 1) {
  824. ERR_raise(ERR_LIB_SSL, SSL_R_PRIVATE_KEY_MISMATCH);
  825. goto out;
  826. }
  827. }
  828. if (ssl_cert_lookup_by_pkey(pubkey, &i) == NULL) {
  829. ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  830. goto out;
  831. }
  832. if (!override && (c->pkeys[i].x509 != NULL
  833. || c->pkeys[i].privatekey != NULL
  834. || c->pkeys[i].chain != NULL)) {
  835. /* No override, and something already there */
  836. ERR_raise(ERR_LIB_SSL, SSL_R_NOT_REPLACING_CERTIFICATE);
  837. goto out;
  838. }
  839. if (chain != NULL) {
  840. dup_chain = X509_chain_up_ref(chain);
  841. if (dup_chain == NULL) {
  842. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  843. goto out;
  844. }
  845. }
  846. sk_X509_pop_free(c->pkeys[i].chain, X509_free);
  847. c->pkeys[i].chain = dup_chain;
  848. X509_free(c->pkeys[i].x509);
  849. X509_up_ref(x509);
  850. c->pkeys[i].x509 = x509;
  851. EVP_PKEY_free(c->pkeys[i].privatekey);
  852. EVP_PKEY_up_ref(privatekey);
  853. c->pkeys[i].privatekey = privatekey;
  854. c->key = &(c->pkeys[i]);
  855. ret = 1;
  856. out:
  857. EVP_PKEY_free(pubkey);
  858. return ret;
  859. }
  860. int SSL_use_cert_and_key(SSL *ssl, X509 *x509, EVP_PKEY *privatekey,
  861. STACK_OF(X509) *chain, int override)
  862. {
  863. return ssl_set_cert_and_key(ssl, NULL, x509, privatekey, chain, override);
  864. }
  865. int SSL_CTX_use_cert_and_key(SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
  866. STACK_OF(X509) *chain, int override)
  867. {
  868. return ssl_set_cert_and_key(NULL, ctx, x509, privatekey, chain, override);
  869. }