ssl_rsa.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. /*
  2. * Copyright 1995-2022 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 size_t extension_contextoff(unsigned int version)
  638. {
  639. return version == SSL_SERVERINFOV1 ? 4 : 0;
  640. }
  641. static size_t extension_append_length(unsigned int version, size_t extension_length)
  642. {
  643. return extension_length + extension_contextoff(version);
  644. }
  645. static void extension_append(unsigned int version,
  646. const unsigned char *extension,
  647. const size_t extension_length,
  648. unsigned char *serverinfo)
  649. {
  650. const size_t contextoff = extension_contextoff(version);
  651. if (contextoff > 0) {
  652. /* We know this only uses the last 2 bytes */
  653. serverinfo[0] = 0;
  654. serverinfo[1] = 0;
  655. serverinfo[2] = (SYNTHV1CONTEXT >> 8) & 0xff;
  656. serverinfo[3] = SYNTHV1CONTEXT & 0xff;
  657. }
  658. memcpy(serverinfo + contextoff, extension, extension_length);
  659. }
  660. static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
  661. const unsigned char *in,
  662. size_t inlen, int *al, void *arg)
  663. {
  664. return serverinfoex_srv_parse_cb(s, ext_type, 0, in, inlen, NULL, 0, al,
  665. arg);
  666. }
  667. static int serverinfoex_srv_add_cb(SSL *s, unsigned int ext_type,
  668. unsigned int context,
  669. const unsigned char **out,
  670. size_t *outlen, X509 *x, size_t chainidx,
  671. int *al, void *arg)
  672. {
  673. const unsigned char *serverinfo = NULL;
  674. size_t serverinfo_length = 0;
  675. /* We only support extensions for the first Certificate */
  676. if ((context & SSL_EXT_TLS1_3_CERTIFICATE) != 0 && chainidx > 0)
  677. return 0;
  678. /* Is there serverinfo data for the chosen server cert? */
  679. if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
  680. &serverinfo_length)) != 0) {
  681. /* Find the relevant extension from the serverinfo */
  682. int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
  683. ext_type, out, outlen);
  684. if (retval == -1) {
  685. *al = SSL_AD_INTERNAL_ERROR;
  686. return -1; /* Error */
  687. }
  688. if (retval == 0)
  689. return 0; /* No extension found, don't send extension */
  690. return 1; /* Send extension */
  691. }
  692. return 0; /* No serverinfo data found, don't send
  693. * extension */
  694. }
  695. static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
  696. const unsigned char **out, size_t *outlen,
  697. int *al, void *arg)
  698. {
  699. return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
  700. arg);
  701. }
  702. /*
  703. * With a NULL context, this function just checks that the serverinfo data
  704. * parses correctly. With a non-NULL context, it registers callbacks for
  705. * the included extensions.
  706. */
  707. static int serverinfo_process_buffer(unsigned int version,
  708. const unsigned char *serverinfo,
  709. size_t serverinfo_length, SSL_CTX *ctx)
  710. {
  711. PACKET pkt;
  712. if (serverinfo == NULL || serverinfo_length == 0)
  713. return 0;
  714. if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2)
  715. return 0;
  716. if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
  717. return 0;
  718. while (PACKET_remaining(&pkt)) {
  719. unsigned long context = 0;
  720. unsigned int ext_type = 0;
  721. PACKET data;
  722. if ((version == SSL_SERVERINFOV2 && !PACKET_get_net_4(&pkt, &context))
  723. || !PACKET_get_net_2(&pkt, &ext_type)
  724. || !PACKET_get_length_prefixed_2(&pkt, &data))
  725. return 0;
  726. if (ctx == NULL)
  727. continue;
  728. /*
  729. * The old style custom extensions API could be set separately for
  730. * server/client, i.e. you could set one custom extension for a client,
  731. * and *for the same extension in the same SSL_CTX* you could set a
  732. * custom extension for the server as well. It seems quite weird to be
  733. * setting a custom extension for both client and server in a single
  734. * SSL_CTX - but theoretically possible. This isn't possible in the
  735. * new API. Therefore, if we have V1 serverinfo we use the old API. We
  736. * also use the old API even if we have V2 serverinfo but the context
  737. * looks like an old style <= TLSv1.2 extension.
  738. */
  739. if (version == SSL_SERVERINFOV1 || context == SYNTHV1CONTEXT) {
  740. if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
  741. serverinfo_srv_add_cb,
  742. NULL, NULL,
  743. serverinfo_srv_parse_cb,
  744. NULL))
  745. return 0;
  746. } else {
  747. if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
  748. serverinfoex_srv_add_cb,
  749. NULL, NULL,
  750. serverinfoex_srv_parse_cb,
  751. NULL))
  752. return 0;
  753. }
  754. }
  755. return 1;
  756. }
  757. int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
  758. const unsigned char *serverinfo,
  759. size_t serverinfo_length)
  760. {
  761. unsigned char *new_serverinfo = NULL;
  762. if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
  763. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_PASSED_NULL_PARAMETER);
  764. return 0;
  765. }
  766. if (version == SSL_SERVERINFOV1) {
  767. /*
  768. * Convert serverinfo version v1 to v2 and call yourself recursively
  769. * over the converted serverinfo.
  770. */
  771. const size_t sinfo_length = extension_append_length(SSL_SERVERINFOV1,
  772. serverinfo_length);
  773. unsigned char *sinfo;
  774. int ret;
  775. sinfo = OPENSSL_malloc(sinfo_length);
  776. if (sinfo == NULL) {
  777. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_MALLOC_FAILURE);
  778. return 0;
  779. }
  780. extension_append(SSL_SERVERINFOV1, serverinfo, serverinfo_length, sinfo);
  781. ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, sinfo,
  782. sinfo_length);
  783. OPENSSL_free(sinfo);
  784. return ret;
  785. }
  786. if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
  787. NULL)) {
  788. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
  789. return 0;
  790. }
  791. if (ctx->cert->key == NULL) {
  792. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_INTERNAL_ERROR);
  793. return 0;
  794. }
  795. new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
  796. serverinfo_length);
  797. if (new_serverinfo == NULL) {
  798. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, ERR_R_MALLOC_FAILURE);
  799. return 0;
  800. }
  801. ctx->cert->key->serverinfo = new_serverinfo;
  802. memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
  803. ctx->cert->key->serverinfo_length = serverinfo_length;
  804. /*
  805. * Now that the serverinfo is validated and stored, go ahead and
  806. * register callbacks.
  807. */
  808. if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
  809. ctx)) {
  810. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_EX, SSL_R_INVALID_SERVERINFO_DATA);
  811. return 0;
  812. }
  813. return 1;
  814. }
  815. int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
  816. size_t serverinfo_length)
  817. {
  818. return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo,
  819. serverinfo_length);
  820. }
  821. int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
  822. {
  823. unsigned char *serverinfo = NULL;
  824. unsigned char *tmp;
  825. size_t serverinfo_length = 0;
  826. unsigned char *extension = 0;
  827. long extension_length = 0;
  828. char *name = NULL;
  829. char *header = NULL;
  830. char namePrefix1[] = "SERVERINFO FOR ";
  831. char namePrefix2[] = "SERVERINFOV2 FOR ";
  832. int ret = 0;
  833. BIO *bin = NULL;
  834. size_t num_extensions = 0;
  835. if (ctx == NULL || file == NULL) {
  836. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_PASSED_NULL_PARAMETER);
  837. goto end;
  838. }
  839. bin = BIO_new(BIO_s_file());
  840. if (bin == NULL) {
  841. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_BUF_LIB);
  842. goto end;
  843. }
  844. if (BIO_read_filename(bin, file) <= 0) {
  845. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_SYS_LIB);
  846. goto end;
  847. }
  848. for (num_extensions = 0;; num_extensions++) {
  849. unsigned int version;
  850. size_t append_length;
  851. if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
  852. == 0) {
  853. /*
  854. * There must be at least one extension in this file
  855. */
  856. if (num_extensions == 0) {
  857. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
  858. SSL_R_NO_PEM_EXTENSIONS);
  859. goto end;
  860. } else /* End of file, we're done */
  861. break;
  862. }
  863. /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
  864. if (strlen(name) < strlen(namePrefix1)) {
  865. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_PEM_NAME_TOO_SHORT);
  866. goto end;
  867. }
  868. if (strncmp(name, namePrefix1, strlen(namePrefix1)) == 0) {
  869. version = SSL_SERVERINFOV1;
  870. } else {
  871. if (strlen(name) < strlen(namePrefix2)) {
  872. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
  873. SSL_R_PEM_NAME_TOO_SHORT);
  874. goto end;
  875. }
  876. if (strncmp(name, namePrefix2, strlen(namePrefix2)) != 0) {
  877. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE,
  878. SSL_R_PEM_NAME_BAD_PREFIX);
  879. goto end;
  880. }
  881. version = SSL_SERVERINFOV2;
  882. }
  883. /*
  884. * Check that the decoded PEM data is plausible (valid length field)
  885. */
  886. if (version == SSL_SERVERINFOV1) {
  887. /* 4 byte header: 2 bytes type, 2 bytes len */
  888. if (extension_length < 4
  889. || (extension[2] << 8) + extension[3]
  890. != extension_length - 4) {
  891. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
  892. goto end;
  893. }
  894. } else {
  895. /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
  896. if (extension_length < 8
  897. || (extension[6] << 8) + extension[7]
  898. != extension_length - 8) {
  899. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, SSL_R_BAD_DATA);
  900. goto end;
  901. }
  902. }
  903. /* Append the decoded extension to the serverinfo buffer */
  904. append_length = extension_append_length(version, extension_length);
  905. tmp = OPENSSL_realloc(serverinfo, serverinfo_length + append_length);
  906. if (tmp == NULL) {
  907. SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE, ERR_R_MALLOC_FAILURE);
  908. goto end;
  909. }
  910. serverinfo = tmp;
  911. extension_append(version, extension, extension_length,
  912. serverinfo + serverinfo_length);
  913. serverinfo_length += append_length;
  914. OPENSSL_free(name);
  915. name = NULL;
  916. OPENSSL_free(header);
  917. header = NULL;
  918. OPENSSL_free(extension);
  919. extension = NULL;
  920. }
  921. ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, serverinfo,
  922. serverinfo_length);
  923. end:
  924. /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
  925. OPENSSL_free(name);
  926. OPENSSL_free(header);
  927. OPENSSL_free(extension);
  928. OPENSSL_free(serverinfo);
  929. BIO_free(bin);
  930. return ret;
  931. }
  932. static int ssl_set_cert_and_key(SSL *ssl, SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
  933. STACK_OF(X509) *chain, int override)
  934. {
  935. int ret = 0;
  936. size_t i;
  937. int j;
  938. int rv;
  939. CERT *c = ssl != NULL ? ssl->cert : ctx->cert;
  940. STACK_OF(X509) *dup_chain = NULL;
  941. EVP_PKEY *pubkey = NULL;
  942. /* Do all security checks before anything else */
  943. rv = ssl_security_cert(ssl, ctx, x509, 0, 1);
  944. if (rv != 1) {
  945. SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, rv);
  946. goto out;
  947. }
  948. for (j = 0; j < sk_X509_num(chain); j++) {
  949. rv = ssl_security_cert(ssl, ctx, sk_X509_value(chain, j), 0, 0);
  950. if (rv != 1) {
  951. SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, rv);
  952. goto out;
  953. }
  954. }
  955. pubkey = X509_get_pubkey(x509); /* bumps reference */
  956. if (pubkey == NULL)
  957. goto out;
  958. if (privatekey == NULL) {
  959. privatekey = pubkey;
  960. } else {
  961. /* For RSA, which has no parameters, missing returns 0 */
  962. if (EVP_PKEY_missing_parameters(privatekey)) {
  963. if (EVP_PKEY_missing_parameters(pubkey)) {
  964. /* nobody has parameters? - error */
  965. SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_MISSING_PARAMETERS);
  966. goto out;
  967. } else {
  968. /* copy to privatekey from pubkey */
  969. EVP_PKEY_copy_parameters(privatekey, pubkey);
  970. }
  971. } else if (EVP_PKEY_missing_parameters(pubkey)) {
  972. /* copy to pubkey from privatekey */
  973. EVP_PKEY_copy_parameters(pubkey, privatekey);
  974. } /* else both have parameters */
  975. /* check that key <-> cert match */
  976. if (EVP_PKEY_cmp(pubkey, privatekey) != 1) {
  977. SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_PRIVATE_KEY_MISMATCH);
  978. goto out;
  979. }
  980. }
  981. if (ssl_cert_lookup_by_pkey(pubkey, &i) == NULL) {
  982. SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  983. goto out;
  984. }
  985. if (!override && (c->pkeys[i].x509 != NULL
  986. || c->pkeys[i].privatekey != NULL
  987. || c->pkeys[i].chain != NULL)) {
  988. /* No override, and something already there */
  989. SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, SSL_R_NOT_REPLACING_CERTIFICATE);
  990. goto out;
  991. }
  992. if (chain != NULL) {
  993. dup_chain = X509_chain_up_ref(chain);
  994. if (dup_chain == NULL) {
  995. SSLerr(SSL_F_SSL_SET_CERT_AND_KEY, ERR_R_MALLOC_FAILURE);
  996. goto out;
  997. }
  998. }
  999. sk_X509_pop_free(c->pkeys[i].chain, X509_free);
  1000. c->pkeys[i].chain = dup_chain;
  1001. X509_free(c->pkeys[i].x509);
  1002. X509_up_ref(x509);
  1003. c->pkeys[i].x509 = x509;
  1004. EVP_PKEY_free(c->pkeys[i].privatekey);
  1005. EVP_PKEY_up_ref(privatekey);
  1006. c->pkeys[i].privatekey = privatekey;
  1007. c->key = &(c->pkeys[i]);
  1008. ret = 1;
  1009. out:
  1010. EVP_PKEY_free(pubkey);
  1011. return ret;
  1012. }
  1013. int SSL_use_cert_and_key(SSL *ssl, X509 *x509, EVP_PKEY *privatekey,
  1014. STACK_OF(X509) *chain, int override)
  1015. {
  1016. return ssl_set_cert_and_key(ssl, NULL, x509, privatekey, chain, override);
  1017. }
  1018. int SSL_CTX_use_cert_and_key(SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
  1019. STACK_OF(X509) *chain, int override)
  1020. {
  1021. return ssl_set_cert_and_key(NULL, ctx, x509, privatekey, chain, override);
  1022. }