ssl_rsa.c 31 KB

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