x_all.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. /*
  10. * Low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #include <openssl/buffer.h>
  17. #include <openssl/asn1.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/x509.h>
  20. #include <openssl/x509_acert.h>
  21. #include <openssl/http.h>
  22. #include <openssl/rsa.h>
  23. #include <openssl/dsa.h>
  24. #include <openssl/x509v3.h>
  25. #include "internal/asn1.h"
  26. #include "crypto/pkcs7.h"
  27. #include "crypto/x509.h"
  28. #include "crypto/x509_acert.h"
  29. #include "crypto/rsa.h"
  30. int X509_verify(X509 *a, EVP_PKEY *r)
  31. {
  32. if (X509_ALGOR_cmp(&a->sig_alg, &a->cert_info.signature) != 0)
  33. return 0;
  34. return ASN1_item_verify_ex(ASN1_ITEM_rptr(X509_CINF), &a->sig_alg,
  35. &a->signature, &a->cert_info,
  36. a->distinguishing_id, r, a->libctx, a->propq);
  37. }
  38. int X509_REQ_verify_ex(X509_REQ *a, EVP_PKEY *r, OSSL_LIB_CTX *libctx,
  39. const char *propq)
  40. {
  41. return ASN1_item_verify_ex(ASN1_ITEM_rptr(X509_REQ_INFO), &a->sig_alg,
  42. a->signature, &a->req_info, a->distinguishing_id,
  43. r, libctx, propq);
  44. }
  45. int X509_REQ_verify(X509_REQ *a, EVP_PKEY *r)
  46. {
  47. return X509_REQ_verify_ex(a, r, NULL, NULL);
  48. }
  49. int X509_ACERT_verify(X509_ACERT *a, EVP_PKEY *r)
  50. {
  51. if (X509_ALGOR_cmp(&a->sig_alg, &a->acinfo->signature) != 0)
  52. return 0;
  53. return ASN1_item_verify_ex(ASN1_ITEM_rptr(X509_ACERT_INFO), &a->sig_alg,
  54. &a->signature, a->acinfo,
  55. NULL, r, NULL, NULL);
  56. }
  57. int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *a, EVP_PKEY *r)
  58. {
  59. return ASN1_item_verify(ASN1_ITEM_rptr(NETSCAPE_SPKAC),
  60. &a->sig_algor, a->signature, a->spkac, r);
  61. }
  62. int X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md)
  63. {
  64. if (x == NULL) {
  65. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  66. return 0;
  67. }
  68. if (sk_X509_EXTENSION_num(X509_get0_extensions(x)) > 0
  69. && !X509_set_version(x, X509_VERSION_3))
  70. return 0;
  71. /*
  72. * Setting the modified flag before signing it. This makes the cached
  73. * encoding to be ignored, so even if the certificate fields have changed,
  74. * they are signed correctly.
  75. * The X509_sign_ctx, X509_REQ_sign{,_ctx}, X509_CRL_sign{,_ctx} functions
  76. * which exist below are the same.
  77. */
  78. x->cert_info.enc.modified = 1;
  79. return ASN1_item_sign_ex(ASN1_ITEM_rptr(X509_CINF), &x->cert_info.signature,
  80. &x->sig_alg, &x->signature, &x->cert_info, NULL,
  81. pkey, md, x->libctx, x->propq);
  82. }
  83. int X509_sign_ctx(X509 *x, EVP_MD_CTX *ctx)
  84. {
  85. if (x == NULL) {
  86. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  87. return 0;
  88. }
  89. if (sk_X509_EXTENSION_num(X509_get0_extensions(x)) > 0
  90. && !X509_set_version(x, X509_VERSION_3))
  91. return 0;
  92. x->cert_info.enc.modified = 1;
  93. return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CINF),
  94. &x->cert_info.signature,
  95. &x->sig_alg, &x->signature, &x->cert_info, ctx);
  96. }
  97. static ASN1_VALUE *simple_get_asn1(const char *url, BIO *bio, BIO *rbio,
  98. int timeout, const ASN1_ITEM *it)
  99. {
  100. #ifndef OPENSSL_NO_HTTP
  101. BIO *mem = OSSL_HTTP_get(url, NULL /* proxy */, NULL /* no_proxy */,
  102. bio, rbio, NULL /* cb */, NULL /* arg */,
  103. 1024 /* buf_size */, NULL /* headers */,
  104. NULL /* expected_ct */, 1 /* expect_asn1 */,
  105. OSSL_HTTP_DEFAULT_MAX_RESP_LEN, timeout);
  106. ASN1_VALUE *res = ASN1_item_d2i_bio(it, mem, NULL);
  107. BIO_free(mem);
  108. return res;
  109. #else
  110. return 0;
  111. #endif
  112. }
  113. X509 *X509_load_http(const char *url, BIO *bio, BIO *rbio, int timeout)
  114. {
  115. return (X509 *)simple_get_asn1(url, bio, rbio, timeout,
  116. ASN1_ITEM_rptr(X509));
  117. }
  118. int X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md)
  119. {
  120. if (x == NULL) {
  121. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  122. return 0;
  123. }
  124. x->req_info.enc.modified = 1;
  125. return ASN1_item_sign_ex(ASN1_ITEM_rptr(X509_REQ_INFO), &x->sig_alg, NULL,
  126. x->signature, &x->req_info, NULL,
  127. pkey, md, x->libctx, x->propq);
  128. }
  129. int X509_REQ_sign_ctx(X509_REQ *x, EVP_MD_CTX *ctx)
  130. {
  131. if (x == NULL) {
  132. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  133. return 0;
  134. }
  135. x->req_info.enc.modified = 1;
  136. return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_REQ_INFO),
  137. &x->sig_alg, NULL, x->signature, &x->req_info,
  138. ctx);
  139. }
  140. int X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md)
  141. {
  142. if (x == NULL) {
  143. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  144. return 0;
  145. }
  146. x->crl.enc.modified = 1;
  147. return ASN1_item_sign_ex(ASN1_ITEM_rptr(X509_CRL_INFO), &x->crl.sig_alg,
  148. &x->sig_alg, &x->signature, &x->crl, NULL,
  149. pkey, md, x->libctx, x->propq);
  150. }
  151. int X509_CRL_sign_ctx(X509_CRL *x, EVP_MD_CTX *ctx)
  152. {
  153. if (x == NULL) {
  154. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  155. return 0;
  156. }
  157. x->crl.enc.modified = 1;
  158. return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CRL_INFO),
  159. &x->crl.sig_alg, &x->sig_alg, &x->signature,
  160. &x->crl, ctx);
  161. }
  162. X509_CRL *X509_CRL_load_http(const char *url, BIO *bio, BIO *rbio, int timeout)
  163. {
  164. return (X509_CRL *)simple_get_asn1(url, bio, rbio, timeout,
  165. ASN1_ITEM_rptr(X509_CRL));
  166. }
  167. int X509_ACERT_sign(X509_ACERT *x, EVP_PKEY *pkey, const EVP_MD *md)
  168. {
  169. return ASN1_item_sign_ex(ASN1_ITEM_rptr(X509_ACERT_INFO), &x->sig_alg,
  170. &x->acinfo->signature,
  171. &x->signature, x->acinfo, NULL,
  172. pkey, md, NULL, NULL);
  173. }
  174. int X509_ACERT_sign_ctx(X509_ACERT *x, EVP_MD_CTX *ctx)
  175. {
  176. return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_ACERT_INFO),
  177. &x->sig_alg, &x->acinfo->signature, &x->signature,
  178. &x->acinfo, ctx);
  179. }
  180. int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, EVP_PKEY *pkey, const EVP_MD *md)
  181. {
  182. return
  183. ASN1_item_sign_ex(ASN1_ITEM_rptr(NETSCAPE_SPKAC), &x->sig_algor, NULL,
  184. x->signature, x->spkac, NULL, pkey, md, NULL, NULL);
  185. }
  186. #ifndef OPENSSL_NO_STDIO
  187. X509 *d2i_X509_fp(FILE *fp, X509 **x509)
  188. {
  189. return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509), fp, x509);
  190. }
  191. int i2d_X509_fp(FILE *fp, const X509 *x509)
  192. {
  193. return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509), fp, x509);
  194. }
  195. #endif
  196. X509 *d2i_X509_bio(BIO *bp, X509 **x509)
  197. {
  198. return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509), bp, x509);
  199. }
  200. int i2d_X509_bio(BIO *bp, const X509 *x509)
  201. {
  202. return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509), bp, x509);
  203. }
  204. #ifndef OPENSSL_NO_STDIO
  205. X509_CRL *d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl)
  206. {
  207. return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl);
  208. }
  209. int i2d_X509_CRL_fp(FILE *fp, const X509_CRL *crl)
  210. {
  211. return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl);
  212. }
  213. #endif
  214. X509_CRL *d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl)
  215. {
  216. return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl);
  217. }
  218. int i2d_X509_CRL_bio(BIO *bp, const X509_CRL *crl)
  219. {
  220. return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl);
  221. }
  222. #ifndef OPENSSL_NO_STDIO
  223. PKCS7 *d2i_PKCS7_fp(FILE *fp, PKCS7 **p7)
  224. {
  225. PKCS7 *ret;
  226. OSSL_LIB_CTX *libctx = NULL;
  227. const char *propq = NULL;
  228. if (p7 != NULL && *p7 != NULL) {
  229. libctx = (*p7)->ctx.libctx;
  230. propq = (*p7)->ctx.propq;
  231. }
  232. ret = ASN1_item_d2i_fp_ex(ASN1_ITEM_rptr(PKCS7), fp, p7, libctx, propq);
  233. if (ret != NULL)
  234. ossl_pkcs7_resolve_libctx(ret);
  235. return ret;
  236. }
  237. int i2d_PKCS7_fp(FILE *fp, const PKCS7 *p7)
  238. {
  239. return ASN1_item_i2d_fp(ASN1_ITEM_rptr(PKCS7), fp, p7);
  240. }
  241. #endif
  242. PKCS7 *d2i_PKCS7_bio(BIO *bp, PKCS7 **p7)
  243. {
  244. PKCS7 *ret;
  245. OSSL_LIB_CTX *libctx = NULL;
  246. const char *propq = NULL;
  247. if (p7 != NULL && *p7 != NULL) {
  248. libctx = (*p7)->ctx.libctx;
  249. propq = (*p7)->ctx.propq;
  250. }
  251. ret = ASN1_item_d2i_bio_ex(ASN1_ITEM_rptr(PKCS7), bp, p7, libctx, propq);
  252. if (ret != NULL)
  253. ossl_pkcs7_resolve_libctx(ret);
  254. return ret;
  255. }
  256. int i2d_PKCS7_bio(BIO *bp, const PKCS7 *p7)
  257. {
  258. return ASN1_item_i2d_bio(ASN1_ITEM_rptr(PKCS7), bp, p7);
  259. }
  260. #ifndef OPENSSL_NO_STDIO
  261. X509_REQ *d2i_X509_REQ_fp(FILE *fp, X509_REQ **req)
  262. {
  263. return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_REQ), fp, req);
  264. }
  265. int i2d_X509_REQ_fp(FILE *fp, const X509_REQ *req)
  266. {
  267. return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_REQ), fp, req);
  268. }
  269. #endif
  270. X509_REQ *d2i_X509_REQ_bio(BIO *bp, X509_REQ **req)
  271. {
  272. OSSL_LIB_CTX *libctx = NULL;
  273. const char *propq = NULL;
  274. if (req != NULL && *req != NULL) {
  275. libctx = (*req)->libctx;
  276. propq = (*req)->propq;
  277. }
  278. return
  279. ASN1_item_d2i_bio_ex(ASN1_ITEM_rptr(X509_REQ), bp, req, libctx, propq);
  280. }
  281. int i2d_X509_REQ_bio(BIO *bp, const X509_REQ *req)
  282. {
  283. return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_REQ), bp, req);
  284. }
  285. #ifndef OPENSSL_NO_STDIO
  286. RSA *d2i_RSAPrivateKey_fp(FILE *fp, RSA **rsa)
  287. {
  288. return ASN1_item_d2i_fp(ASN1_ITEM_rptr(RSAPrivateKey), fp, rsa);
  289. }
  290. int i2d_RSAPrivateKey_fp(FILE *fp, const RSA *rsa)
  291. {
  292. return ASN1_item_i2d_fp(ASN1_ITEM_rptr(RSAPrivateKey), fp, rsa);
  293. }
  294. RSA *d2i_RSAPublicKey_fp(FILE *fp, RSA **rsa)
  295. {
  296. return ASN1_item_d2i_fp(ASN1_ITEM_rptr(RSAPublicKey), fp, rsa);
  297. }
  298. RSA *d2i_RSA_PUBKEY_fp(FILE *fp, RSA **rsa)
  299. {
  300. return ASN1_d2i_fp((void *(*)(void))
  301. RSA_new, (D2I_OF(void)) d2i_RSA_PUBKEY, fp,
  302. (void **)rsa);
  303. }
  304. int i2d_RSAPublicKey_fp(FILE *fp, const RSA *rsa)
  305. {
  306. return ASN1_item_i2d_fp(ASN1_ITEM_rptr(RSAPublicKey), fp, rsa);
  307. }
  308. int i2d_RSA_PUBKEY_fp(FILE *fp, const RSA *rsa)
  309. {
  310. return ASN1_i2d_fp((I2D_OF(void))i2d_RSA_PUBKEY, fp, rsa);
  311. }
  312. #endif
  313. RSA *d2i_RSAPrivateKey_bio(BIO *bp, RSA **rsa)
  314. {
  315. return ASN1_item_d2i_bio(ASN1_ITEM_rptr(RSAPrivateKey), bp, rsa);
  316. }
  317. int i2d_RSAPrivateKey_bio(BIO *bp, const RSA *rsa)
  318. {
  319. return ASN1_item_i2d_bio(ASN1_ITEM_rptr(RSAPrivateKey), bp, rsa);
  320. }
  321. RSA *d2i_RSAPublicKey_bio(BIO *bp, RSA **rsa)
  322. {
  323. return ASN1_item_d2i_bio(ASN1_ITEM_rptr(RSAPublicKey), bp, rsa);
  324. }
  325. RSA *d2i_RSA_PUBKEY_bio(BIO *bp, RSA **rsa)
  326. {
  327. return ASN1_d2i_bio_of(RSA, RSA_new, d2i_RSA_PUBKEY, bp, rsa);
  328. }
  329. int i2d_RSAPublicKey_bio(BIO *bp, const RSA *rsa)
  330. {
  331. return ASN1_item_i2d_bio(ASN1_ITEM_rptr(RSAPublicKey), bp, rsa);
  332. }
  333. int i2d_RSA_PUBKEY_bio(BIO *bp, const RSA *rsa)
  334. {
  335. return ASN1_i2d_bio_of(RSA, i2d_RSA_PUBKEY, bp, rsa);
  336. }
  337. #ifndef OPENSSL_NO_DSA
  338. # ifndef OPENSSL_NO_STDIO
  339. DSA *d2i_DSAPrivateKey_fp(FILE *fp, DSA **dsa)
  340. {
  341. return ASN1_d2i_fp_of(DSA, DSA_new, d2i_DSAPrivateKey, fp, dsa);
  342. }
  343. int i2d_DSAPrivateKey_fp(FILE *fp, const DSA *dsa)
  344. {
  345. return ASN1_i2d_fp_of(DSA, i2d_DSAPrivateKey, fp, dsa);
  346. }
  347. DSA *d2i_DSA_PUBKEY_fp(FILE *fp, DSA **dsa)
  348. {
  349. return ASN1_d2i_fp_of(DSA, DSA_new, d2i_DSA_PUBKEY, fp, dsa);
  350. }
  351. int i2d_DSA_PUBKEY_fp(FILE *fp, const DSA *dsa)
  352. {
  353. return ASN1_i2d_fp_of(DSA, i2d_DSA_PUBKEY, fp, dsa);
  354. }
  355. # endif
  356. DSA *d2i_DSAPrivateKey_bio(BIO *bp, DSA **dsa)
  357. {
  358. return ASN1_d2i_bio_of(DSA, DSA_new, d2i_DSAPrivateKey, bp, dsa);
  359. }
  360. int i2d_DSAPrivateKey_bio(BIO *bp, const DSA *dsa)
  361. {
  362. return ASN1_i2d_bio_of(DSA, i2d_DSAPrivateKey, bp, dsa);
  363. }
  364. DSA *d2i_DSA_PUBKEY_bio(BIO *bp, DSA **dsa)
  365. {
  366. return ASN1_d2i_bio_of(DSA, DSA_new, d2i_DSA_PUBKEY, bp, dsa);
  367. }
  368. int i2d_DSA_PUBKEY_bio(BIO *bp, const DSA *dsa)
  369. {
  370. return ASN1_i2d_bio_of(DSA, i2d_DSA_PUBKEY, bp, dsa);
  371. }
  372. #endif
  373. #ifndef OPENSSL_NO_EC
  374. # ifndef OPENSSL_NO_STDIO
  375. EC_KEY *d2i_EC_PUBKEY_fp(FILE *fp, EC_KEY **eckey)
  376. {
  377. return ASN1_d2i_fp_of(EC_KEY, EC_KEY_new, d2i_EC_PUBKEY, fp, eckey);
  378. }
  379. int i2d_EC_PUBKEY_fp(FILE *fp, const EC_KEY *eckey)
  380. {
  381. return ASN1_i2d_fp_of(EC_KEY, i2d_EC_PUBKEY, fp, eckey);
  382. }
  383. EC_KEY *d2i_ECPrivateKey_fp(FILE *fp, EC_KEY **eckey)
  384. {
  385. return ASN1_d2i_fp_of(EC_KEY, EC_KEY_new, d2i_ECPrivateKey, fp, eckey);
  386. }
  387. int i2d_ECPrivateKey_fp(FILE *fp, const EC_KEY *eckey)
  388. {
  389. return ASN1_i2d_fp_of(EC_KEY, i2d_ECPrivateKey, fp, eckey);
  390. }
  391. # endif
  392. EC_KEY *d2i_EC_PUBKEY_bio(BIO *bp, EC_KEY **eckey)
  393. {
  394. return ASN1_d2i_bio_of(EC_KEY, EC_KEY_new, d2i_EC_PUBKEY, bp, eckey);
  395. }
  396. int i2d_EC_PUBKEY_bio(BIO *bp, const EC_KEY *ecdsa)
  397. {
  398. return ASN1_i2d_bio_of(EC_KEY, i2d_EC_PUBKEY, bp, ecdsa);
  399. }
  400. EC_KEY *d2i_ECPrivateKey_bio(BIO *bp, EC_KEY **eckey)
  401. {
  402. return ASN1_d2i_bio_of(EC_KEY, EC_KEY_new, d2i_ECPrivateKey, bp, eckey);
  403. }
  404. int i2d_ECPrivateKey_bio(BIO *bp, const EC_KEY *eckey)
  405. {
  406. return ASN1_i2d_bio_of(EC_KEY, i2d_ECPrivateKey, bp, eckey);
  407. }
  408. #endif
  409. int X509_pubkey_digest(const X509 *data, const EVP_MD *type,
  410. unsigned char *md, unsigned int *len)
  411. {
  412. ASN1_BIT_STRING *key = X509_get0_pubkey_bitstr(data);
  413. if (key == NULL)
  414. return 0;
  415. return EVP_Digest(key->data, key->length, md, len, type, NULL);
  416. }
  417. int X509_digest(const X509 *cert, const EVP_MD *md, unsigned char *data,
  418. unsigned int *len)
  419. {
  420. if (EVP_MD_is_a(md, SN_sha1) && (cert->ex_flags & EXFLAG_SET) != 0
  421. && (cert->ex_flags & EXFLAG_NO_FINGERPRINT) == 0) {
  422. /* Asking for SHA1 and we already computed it. */
  423. if (len != NULL)
  424. *len = sizeof(cert->sha1_hash);
  425. memcpy(data, cert->sha1_hash, sizeof(cert->sha1_hash));
  426. return 1;
  427. }
  428. return ossl_asn1_item_digest_ex(ASN1_ITEM_rptr(X509), md, (char *)cert,
  429. data, len, cert->libctx, cert->propq);
  430. }
  431. /* calculate cert digest using the same hash algorithm as in its signature */
  432. ASN1_OCTET_STRING *X509_digest_sig(const X509 *cert,
  433. EVP_MD **md_used, int *md_is_fallback)
  434. {
  435. unsigned int len;
  436. unsigned char hash[EVP_MAX_MD_SIZE];
  437. int mdnid, pknid;
  438. EVP_MD *md = NULL;
  439. const char *md_name;
  440. ASN1_OCTET_STRING *new;
  441. if (md_used != NULL)
  442. *md_used = NULL;
  443. if (md_is_fallback != NULL)
  444. *md_is_fallback = 0;
  445. if (cert == NULL) {
  446. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  447. return NULL;
  448. }
  449. if (!OBJ_find_sigid_algs(X509_get_signature_nid(cert), &mdnid, &pknid)) {
  450. ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_SIGID_ALGS);
  451. return NULL;
  452. }
  453. if (mdnid == NID_undef) {
  454. if (pknid == EVP_PKEY_RSA_PSS) {
  455. RSA_PSS_PARAMS *pss = ossl_rsa_pss_decode(&cert->sig_alg);
  456. const EVP_MD *mgf1md, *mmd = NULL;
  457. int saltlen, trailerfield;
  458. if (pss == NULL
  459. || !ossl_rsa_pss_get_param_unverified(pss, &mmd, &mgf1md,
  460. &saltlen,
  461. &trailerfield)
  462. || mmd == NULL) {
  463. RSA_PSS_PARAMS_free(pss);
  464. ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
  465. return NULL;
  466. }
  467. RSA_PSS_PARAMS_free(pss);
  468. /* Fetch explicitly and do not fallback */
  469. if ((md = EVP_MD_fetch(cert->libctx, EVP_MD_get0_name(mmd),
  470. cert->propq)) == NULL)
  471. /* Error code from fetch is sufficient */
  472. return NULL;
  473. } else if (pknid != NID_undef) {
  474. /* A known algorithm, but without a digest */
  475. switch (pknid) {
  476. case NID_ED25519: /* Follow CMS default given in RFC8419 */
  477. md_name = "SHA512";
  478. break;
  479. case NID_ED448: /* Follow CMS default given in RFC8419 */
  480. md_name = "SHAKE256";
  481. break;
  482. default: /* Fall back to SHA-256 */
  483. md_name = "SHA256";
  484. break;
  485. }
  486. if ((md = EVP_MD_fetch(cert->libctx, md_name,
  487. cert->propq)) == NULL)
  488. return NULL;
  489. if (md_is_fallback != NULL)
  490. *md_is_fallback = 1;
  491. } else {
  492. /* A completely unknown algorithm */
  493. ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
  494. return NULL;
  495. }
  496. } else if ((md = EVP_MD_fetch(cert->libctx, OBJ_nid2sn(mdnid),
  497. cert->propq)) == NULL
  498. && (md = (EVP_MD *)EVP_get_digestbynid(mdnid)) == NULL) {
  499. ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
  500. return NULL;
  501. }
  502. if (!X509_digest(cert, md, hash, &len)
  503. || (new = ASN1_OCTET_STRING_new()) == NULL)
  504. goto err;
  505. if (ASN1_OCTET_STRING_set(new, hash, len)) {
  506. if (md_used != NULL)
  507. *md_used = md;
  508. else
  509. EVP_MD_free(md);
  510. return new;
  511. }
  512. ASN1_OCTET_STRING_free(new);
  513. err:
  514. EVP_MD_free(md);
  515. return NULL;
  516. }
  517. int X509_CRL_digest(const X509_CRL *data, const EVP_MD *type,
  518. unsigned char *md, unsigned int *len)
  519. {
  520. if (type == NULL) {
  521. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  522. return 0;
  523. }
  524. if (EVP_MD_is_a(type, SN_sha1)
  525. && (data->flags & EXFLAG_SET) != 0
  526. && (data->flags & EXFLAG_NO_FINGERPRINT) == 0) {
  527. /* Asking for SHA1; always computed in CRL d2i. */
  528. if (len != NULL)
  529. *len = sizeof(data->sha1_hash);
  530. memcpy(md, data->sha1_hash, sizeof(data->sha1_hash));
  531. return 1;
  532. }
  533. return
  534. ossl_asn1_item_digest_ex(ASN1_ITEM_rptr(X509_CRL), type, (char *)data,
  535. md, len, data->libctx, data->propq);
  536. }
  537. int X509_REQ_digest(const X509_REQ *data, const EVP_MD *type,
  538. unsigned char *md, unsigned int *len)
  539. {
  540. return
  541. ossl_asn1_item_digest_ex(ASN1_ITEM_rptr(X509_REQ), type, (char *)data,
  542. md, len, data->libctx, data->propq);
  543. }
  544. int X509_NAME_digest(const X509_NAME *data, const EVP_MD *type,
  545. unsigned char *md, unsigned int *len)
  546. {
  547. return ASN1_item_digest(ASN1_ITEM_rptr(X509_NAME), type, (char *)data,
  548. md, len);
  549. }
  550. int PKCS7_ISSUER_AND_SERIAL_digest(PKCS7_ISSUER_AND_SERIAL *data,
  551. const EVP_MD *type, unsigned char *md,
  552. unsigned int *len)
  553. {
  554. return ASN1_item_digest(ASN1_ITEM_rptr(PKCS7_ISSUER_AND_SERIAL), type,
  555. (char *)data, md, len);
  556. }
  557. #ifndef OPENSSL_NO_STDIO
  558. X509_SIG *d2i_PKCS8_fp(FILE *fp, X509_SIG **p8)
  559. {
  560. return ASN1_d2i_fp_of(X509_SIG, X509_SIG_new, d2i_X509_SIG, fp, p8);
  561. }
  562. int i2d_PKCS8_fp(FILE *fp, const X509_SIG *p8)
  563. {
  564. return ASN1_i2d_fp_of(X509_SIG, i2d_X509_SIG, fp, p8);
  565. }
  566. #endif
  567. X509_SIG *d2i_PKCS8_bio(BIO *bp, X509_SIG **p8)
  568. {
  569. return ASN1_d2i_bio_of(X509_SIG, X509_SIG_new, d2i_X509_SIG, bp, p8);
  570. }
  571. int i2d_PKCS8_bio(BIO *bp, const X509_SIG *p8)
  572. {
  573. return ASN1_i2d_bio_of(X509_SIG, i2d_X509_SIG, bp, p8);
  574. }
  575. #ifndef OPENSSL_NO_STDIO
  576. X509_PUBKEY *d2i_X509_PUBKEY_fp(FILE *fp, X509_PUBKEY **xpk)
  577. {
  578. return ASN1_d2i_fp_of(X509_PUBKEY, X509_PUBKEY_new, d2i_X509_PUBKEY,
  579. fp, xpk);
  580. }
  581. int i2d_X509_PUBKEY_fp(FILE *fp, const X509_PUBKEY *xpk)
  582. {
  583. return ASN1_i2d_fp_of(X509_PUBKEY, i2d_X509_PUBKEY, fp, xpk);
  584. }
  585. #endif
  586. X509_PUBKEY *d2i_X509_PUBKEY_bio(BIO *bp, X509_PUBKEY **xpk)
  587. {
  588. return ASN1_d2i_bio_of(X509_PUBKEY, X509_PUBKEY_new, d2i_X509_PUBKEY,
  589. bp, xpk);
  590. }
  591. int i2d_X509_PUBKEY_bio(BIO *bp, const X509_PUBKEY *xpk)
  592. {
  593. return ASN1_i2d_bio_of(X509_PUBKEY, i2d_X509_PUBKEY, bp, xpk);
  594. }
  595. #ifndef OPENSSL_NO_STDIO
  596. PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_fp(FILE *fp,
  597. PKCS8_PRIV_KEY_INFO **p8inf)
  598. {
  599. return ASN1_d2i_fp_of(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_new,
  600. d2i_PKCS8_PRIV_KEY_INFO, fp, p8inf);
  601. }
  602. int i2d_PKCS8_PRIV_KEY_INFO_fp(FILE *fp, const PKCS8_PRIV_KEY_INFO *p8inf)
  603. {
  604. return ASN1_i2d_fp_of(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO, fp,
  605. p8inf);
  606. }
  607. int i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, const EVP_PKEY *key)
  608. {
  609. PKCS8_PRIV_KEY_INFO *p8inf;
  610. int ret;
  611. p8inf = EVP_PKEY2PKCS8(key);
  612. if (p8inf == NULL)
  613. return 0;
  614. ret = i2d_PKCS8_PRIV_KEY_INFO_fp(fp, p8inf);
  615. PKCS8_PRIV_KEY_INFO_free(p8inf);
  616. return ret;
  617. }
  618. int i2d_PrivateKey_fp(FILE *fp, const EVP_PKEY *pkey)
  619. {
  620. return ASN1_i2d_fp_of(EVP_PKEY, i2d_PrivateKey, fp, pkey);
  621. }
  622. EVP_PKEY *d2i_PrivateKey_fp(FILE *fp, EVP_PKEY **a)
  623. {
  624. return ASN1_d2i_fp_of(EVP_PKEY, EVP_PKEY_new, d2i_AutoPrivateKey, fp, a);
  625. }
  626. EVP_PKEY *d2i_PrivateKey_ex_fp(FILE *fp, EVP_PKEY **a, OSSL_LIB_CTX *libctx,
  627. const char *propq)
  628. {
  629. BIO *b;
  630. void *ret;
  631. if ((b = BIO_new(BIO_s_file())) == NULL) {
  632. ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB);
  633. return NULL;
  634. }
  635. BIO_set_fp(b, fp, BIO_NOCLOSE);
  636. ret = d2i_PrivateKey_ex_bio(b, a, libctx, propq);
  637. BIO_free(b);
  638. return ret;
  639. }
  640. int i2d_PUBKEY_fp(FILE *fp, const EVP_PKEY *pkey)
  641. {
  642. return ASN1_i2d_fp_of(EVP_PKEY, i2d_PUBKEY, fp, pkey);
  643. }
  644. EVP_PKEY *d2i_PUBKEY_ex_fp(FILE *fp, EVP_PKEY **a, OSSL_LIB_CTX *libctx,
  645. const char *propq)
  646. {
  647. BIO *b;
  648. void *ret;
  649. if ((b = BIO_new(BIO_s_file())) == NULL) {
  650. ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB);
  651. return NULL;
  652. }
  653. BIO_set_fp(b, fp, BIO_NOCLOSE);
  654. ret = d2i_PUBKEY_ex_bio(b, a, libctx, propq);
  655. BIO_free(b);
  656. return ret;
  657. }
  658. EVP_PKEY *d2i_PUBKEY_fp(FILE *fp, EVP_PKEY **a)
  659. {
  660. return ASN1_d2i_fp_of(EVP_PKEY, EVP_PKEY_new, d2i_PUBKEY, fp, a);
  661. }
  662. #endif
  663. PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_bio(BIO *bp,
  664. PKCS8_PRIV_KEY_INFO **p8inf)
  665. {
  666. return ASN1_d2i_bio_of(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_new,
  667. d2i_PKCS8_PRIV_KEY_INFO, bp, p8inf);
  668. }
  669. int i2d_PKCS8_PRIV_KEY_INFO_bio(BIO *bp, const PKCS8_PRIV_KEY_INFO *p8inf)
  670. {
  671. return ASN1_i2d_bio_of(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO, bp,
  672. p8inf);
  673. }
  674. int i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, const EVP_PKEY *key)
  675. {
  676. PKCS8_PRIV_KEY_INFO *p8inf;
  677. int ret;
  678. p8inf = EVP_PKEY2PKCS8(key);
  679. if (p8inf == NULL)
  680. return 0;
  681. ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf);
  682. PKCS8_PRIV_KEY_INFO_free(p8inf);
  683. return ret;
  684. }
  685. int i2d_PrivateKey_bio(BIO *bp, const EVP_PKEY *pkey)
  686. {
  687. return ASN1_i2d_bio_of(EVP_PKEY, i2d_PrivateKey, bp, pkey);
  688. }
  689. EVP_PKEY *d2i_PrivateKey_bio(BIO *bp, EVP_PKEY **a)
  690. {
  691. return ASN1_d2i_bio_of(EVP_PKEY, EVP_PKEY_new, d2i_AutoPrivateKey, bp, a);
  692. }
  693. EVP_PKEY *d2i_PrivateKey_ex_bio(BIO *bp, EVP_PKEY **a, OSSL_LIB_CTX *libctx,
  694. const char *propq)
  695. {
  696. BUF_MEM *b = NULL;
  697. const unsigned char *p;
  698. void *ret = NULL;
  699. int len;
  700. len = asn1_d2i_read_bio(bp, &b);
  701. if (len < 0)
  702. goto err;
  703. p = (unsigned char *)b->data;
  704. ret = d2i_AutoPrivateKey_ex(a, &p, len, libctx, propq);
  705. err:
  706. BUF_MEM_free(b);
  707. return ret;
  708. }
  709. int i2d_PUBKEY_bio(BIO *bp, const EVP_PKEY *pkey)
  710. {
  711. return ASN1_i2d_bio_of(EVP_PKEY, i2d_PUBKEY, bp, pkey);
  712. }
  713. EVP_PKEY *d2i_PUBKEY_ex_bio(BIO *bp, EVP_PKEY **a, OSSL_LIB_CTX *libctx,
  714. const char *propq)
  715. {
  716. BUF_MEM *b = NULL;
  717. const unsigned char *p;
  718. void *ret = NULL;
  719. int len;
  720. len = asn1_d2i_read_bio(bp, &b);
  721. if (len < 0)
  722. goto err;
  723. p = (unsigned char *)b->data;
  724. ret = d2i_PUBKEY_ex(a, &p, len, libctx, propq);
  725. err:
  726. BUF_MEM_free(b);
  727. return ret;
  728. }
  729. EVP_PKEY *d2i_PUBKEY_bio(BIO *bp, EVP_PKEY **a)
  730. {
  731. return ASN1_d2i_bio_of(EVP_PKEY, EVP_PKEY_new, d2i_PUBKEY, bp, a);
  732. }
  733. #ifndef OPENSSL_NO_STDIO
  734. X509_ACERT *d2i_X509_ACERT_fp(FILE *fp, X509_ACERT **acert)
  735. {
  736. return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_ACERT), fp, acert);
  737. }
  738. int i2d_X509_ACERT_fp(FILE *fp, const X509_ACERT *acert)
  739. {
  740. return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_ACERT), fp, acert);
  741. }
  742. #endif
  743. X509_ACERT *d2i_X509_ACERT_bio(BIO *bp, X509_ACERT **acert)
  744. {
  745. return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_ACERT), bp, acert);
  746. }
  747. int i2d_X509_ACERT_bio(BIO *bp, const X509_ACERT *acert)
  748. {
  749. return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_ACERT), bp, acert);
  750. }