2
0

ssl_rsa.c 33 KB

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