x_all.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /*
  2. * Copyright 1995-2020 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 "crypto/x509.h"
  21. #include <openssl/http.h>
  22. #include <openssl/rsa.h>
  23. #include <openssl/dsa.h>
  24. #include <openssl/x509v3.h>
  25. #include "crypto/asn1.h"
  26. static void clean_id_ctx(EVP_MD_CTX *ctx)
  27. {
  28. EVP_PKEY_CTX *pctx = EVP_MD_CTX_pkey_ctx(ctx);
  29. EVP_PKEY_CTX_free(pctx);
  30. EVP_MD_CTX_free(ctx);
  31. }
  32. static EVP_MD_CTX *make_id_ctx(EVP_PKEY *r, ASN1_OCTET_STRING *id,
  33. OPENSSL_CTX *libctx, const char *propq)
  34. {
  35. EVP_MD_CTX *ctx = NULL;
  36. EVP_PKEY_CTX *pctx = NULL;
  37. if ((ctx = EVP_MD_CTX_new()) == NULL
  38. || (pctx = EVP_PKEY_CTX_new_from_pkey(libctx, r, propq)) == NULL) {
  39. X509err(0, ERR_R_MALLOC_FAILURE);
  40. goto error;
  41. }
  42. #ifndef OPENSSL_NO_EC
  43. if (id != NULL) {
  44. if (EVP_PKEY_CTX_set1_id(pctx, id->data, id->length) <= 0) {
  45. X509err(0, ERR_R_MALLOC_FAILURE);
  46. goto error;
  47. }
  48. }
  49. #endif
  50. EVP_MD_CTX_set_pkey_ctx(ctx, pctx);
  51. return ctx;
  52. error:
  53. EVP_PKEY_CTX_free(pctx);
  54. EVP_MD_CTX_free(ctx);
  55. return NULL;
  56. }
  57. int X509_verify_ex(X509 *a, EVP_PKEY *r, OPENSSL_CTX *libctx, const char *propq)
  58. {
  59. int rv = 0;
  60. EVP_MD_CTX *ctx = NULL;
  61. ASN1_OCTET_STRING *id = NULL;
  62. if (X509_ALGOR_cmp(&a->sig_alg, &a->cert_info.signature))
  63. return 0;
  64. id = a->distinguishing_id;
  65. if ((ctx = make_id_ctx(r, id, libctx, propq)) != NULL) {
  66. rv = ASN1_item_verify_ctx(ASN1_ITEM_rptr(X509_CINF), &a->sig_alg,
  67. &a->signature, &a->cert_info, ctx);
  68. clean_id_ctx(ctx);
  69. }
  70. return rv;
  71. }
  72. int X509_verify(X509 *a, EVP_PKEY *r)
  73. {
  74. return X509_verify_ex(a, r, NULL, NULL);
  75. }
  76. int X509_REQ_verify_ex(X509_REQ *a, EVP_PKEY *r, OPENSSL_CTX *libctx,
  77. const char *propq)
  78. {
  79. int rv = 0;
  80. EVP_MD_CTX *ctx = NULL;
  81. ASN1_OCTET_STRING *id = NULL;
  82. id = a->distinguishing_id;
  83. if ((ctx = make_id_ctx(r, id, libctx, propq)) != NULL) {
  84. rv = ASN1_item_verify_ctx(ASN1_ITEM_rptr(X509_REQ_INFO), &a->sig_alg,
  85. a->signature, &a->req_info, ctx);
  86. clean_id_ctx(ctx);
  87. }
  88. return rv;
  89. }
  90. int X509_REQ_verify(X509_REQ *a, EVP_PKEY *r)
  91. {
  92. return X509_REQ_verify_ex(a, r, NULL, NULL);
  93. }
  94. int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *a, EVP_PKEY *r)
  95. {
  96. return (ASN1_item_verify(ASN1_ITEM_rptr(NETSCAPE_SPKAC),
  97. &a->sig_algor, a->signature, a->spkac, r));
  98. }
  99. int X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md)
  100. {
  101. x->cert_info.enc.modified = 1;
  102. return (ASN1_item_sign(ASN1_ITEM_rptr(X509_CINF), &x->cert_info.signature,
  103. &x->sig_alg, &x->signature, &x->cert_info, pkey,
  104. md));
  105. }
  106. int X509_sign_ctx(X509 *x, EVP_MD_CTX *ctx)
  107. {
  108. x->cert_info.enc.modified = 1;
  109. return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CINF),
  110. &x->cert_info.signature,
  111. &x->sig_alg, &x->signature, &x->cert_info, ctx);
  112. }
  113. #if !defined(OPENSSL_NO_SOCK)
  114. static ASN1_VALUE *simple_get_asn1(const char *url, BIO *bio, BIO *rbio,
  115. int timeout, const ASN1_ITEM *it)
  116. {
  117. return OSSL_HTTP_get_asn1(url, NULL, NULL /* no proxy used */, bio,
  118. rbio, NULL /* no callback for SSL/TLS */, NULL,
  119. NULL /* headers */, 1024 /* maxline */,
  120. 0 /* max_resp_len */, timeout,
  121. NULL /* expected_content_type */, it);
  122. }
  123. X509 *X509_load_http(const char *url, BIO *bio, BIO *rbio, int timeout)
  124. {
  125. return (X509 *)simple_get_asn1(url, bio, rbio, timeout,
  126. ASN1_ITEM_rptr(X509));
  127. }
  128. #endif
  129. int X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md)
  130. {
  131. return (ASN1_item_sign(ASN1_ITEM_rptr(X509_REQ_INFO), &x->sig_alg, NULL,
  132. x->signature, &x->req_info, pkey, md));
  133. }
  134. int X509_REQ_sign_ctx(X509_REQ *x, EVP_MD_CTX *ctx)
  135. {
  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. x->crl.enc.modified = 1;
  143. return (ASN1_item_sign(ASN1_ITEM_rptr(X509_CRL_INFO), &x->crl.sig_alg,
  144. &x->sig_alg, &x->signature, &x->crl, pkey, md));
  145. }
  146. int X509_CRL_sign_ctx(X509_CRL *x, EVP_MD_CTX *ctx)
  147. {
  148. x->crl.enc.modified = 1;
  149. return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CRL_INFO),
  150. &x->crl.sig_alg, &x->sig_alg, &x->signature,
  151. &x->crl, ctx);
  152. }
  153. #if !defined(OPENSSL_NO_SOCK)
  154. X509_CRL *X509_CRL_load_http(const char *url, BIO *bio, BIO *rbio, int timeout)
  155. {
  156. return (X509_CRL *)simple_get_asn1(url, bio, rbio, timeout,
  157. ASN1_ITEM_rptr(X509_CRL));
  158. }
  159. #endif
  160. int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, EVP_PKEY *pkey, const EVP_MD *md)
  161. {
  162. return (ASN1_item_sign(ASN1_ITEM_rptr(NETSCAPE_SPKAC), &x->sig_algor, NULL,
  163. x->signature, x->spkac, pkey, md));
  164. }
  165. #ifndef OPENSSL_NO_STDIO
  166. X509 *d2i_X509_fp(FILE *fp, X509 **x509)
  167. {
  168. return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509), fp, x509);
  169. }
  170. int i2d_X509_fp(FILE *fp, const X509 *x509)
  171. {
  172. return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509), fp, x509);
  173. }
  174. #endif
  175. X509 *d2i_X509_bio(BIO *bp, X509 **x509)
  176. {
  177. return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509), bp, x509);
  178. }
  179. int i2d_X509_bio(BIO *bp, const X509 *x509)
  180. {
  181. return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509), bp, x509);
  182. }
  183. #ifndef OPENSSL_NO_STDIO
  184. X509_CRL *d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl)
  185. {
  186. return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl);
  187. }
  188. int i2d_X509_CRL_fp(FILE *fp, const X509_CRL *crl)
  189. {
  190. return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl);
  191. }
  192. #endif
  193. X509_CRL *d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl)
  194. {
  195. return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl);
  196. }
  197. int i2d_X509_CRL_bio(BIO *bp, const X509_CRL *crl)
  198. {
  199. return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl);
  200. }
  201. #ifndef OPENSSL_NO_STDIO
  202. PKCS7 *d2i_PKCS7_fp(FILE *fp, PKCS7 **p7)
  203. {
  204. return ASN1_item_d2i_fp(ASN1_ITEM_rptr(PKCS7), fp, p7);
  205. }
  206. int i2d_PKCS7_fp(FILE *fp, const PKCS7 *p7)
  207. {
  208. return ASN1_item_i2d_fp(ASN1_ITEM_rptr(PKCS7), fp, p7);
  209. }
  210. #endif
  211. PKCS7 *d2i_PKCS7_bio(BIO *bp, PKCS7 **p7)
  212. {
  213. return ASN1_item_d2i_bio(ASN1_ITEM_rptr(PKCS7), bp, p7);
  214. }
  215. int i2d_PKCS7_bio(BIO *bp, const PKCS7 *p7)
  216. {
  217. return ASN1_item_i2d_bio(ASN1_ITEM_rptr(PKCS7), bp, p7);
  218. }
  219. #ifndef OPENSSL_NO_STDIO
  220. X509_REQ *d2i_X509_REQ_fp(FILE *fp, X509_REQ **req)
  221. {
  222. return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_REQ), fp, req);
  223. }
  224. int i2d_X509_REQ_fp(FILE *fp, const X509_REQ *req)
  225. {
  226. return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_REQ), fp, req);
  227. }
  228. #endif
  229. X509_REQ *d2i_X509_REQ_bio(BIO *bp, X509_REQ **req)
  230. {
  231. return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_REQ), bp, req);
  232. }
  233. int i2d_X509_REQ_bio(BIO *bp, const X509_REQ *req)
  234. {
  235. return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_REQ), bp, req);
  236. }
  237. #ifndef OPENSSL_NO_RSA
  238. # ifndef OPENSSL_NO_STDIO
  239. RSA *d2i_RSAPrivateKey_fp(FILE *fp, RSA **rsa)
  240. {
  241. return ASN1_item_d2i_fp(ASN1_ITEM_rptr(RSAPrivateKey), fp, rsa);
  242. }
  243. int i2d_RSAPrivateKey_fp(FILE *fp, const RSA *rsa)
  244. {
  245. return ASN1_item_i2d_fp(ASN1_ITEM_rptr(RSAPrivateKey), fp, rsa);
  246. }
  247. RSA *d2i_RSAPublicKey_fp(FILE *fp, RSA **rsa)
  248. {
  249. return ASN1_item_d2i_fp(ASN1_ITEM_rptr(RSAPublicKey), fp, rsa);
  250. }
  251. RSA *d2i_RSA_PUBKEY_fp(FILE *fp, RSA **rsa)
  252. {
  253. return ASN1_d2i_fp((void *(*)(void))
  254. RSA_new, (D2I_OF(void)) d2i_RSA_PUBKEY, fp,
  255. (void **)rsa);
  256. }
  257. int i2d_RSAPublicKey_fp(FILE *fp, const RSA *rsa)
  258. {
  259. return ASN1_item_i2d_fp(ASN1_ITEM_rptr(RSAPublicKey), fp, rsa);
  260. }
  261. int i2d_RSA_PUBKEY_fp(FILE *fp, const RSA *rsa)
  262. {
  263. return ASN1_i2d_fp((I2D_OF(void))i2d_RSA_PUBKEY, fp, rsa);
  264. }
  265. # endif
  266. RSA *d2i_RSAPrivateKey_bio(BIO *bp, RSA **rsa)
  267. {
  268. return ASN1_item_d2i_bio(ASN1_ITEM_rptr(RSAPrivateKey), bp, rsa);
  269. }
  270. int i2d_RSAPrivateKey_bio(BIO *bp, const RSA *rsa)
  271. {
  272. return ASN1_item_i2d_bio(ASN1_ITEM_rptr(RSAPrivateKey), bp, rsa);
  273. }
  274. RSA *d2i_RSAPublicKey_bio(BIO *bp, RSA **rsa)
  275. {
  276. return ASN1_item_d2i_bio(ASN1_ITEM_rptr(RSAPublicKey), bp, rsa);
  277. }
  278. RSA *d2i_RSA_PUBKEY_bio(BIO *bp, RSA **rsa)
  279. {
  280. return ASN1_d2i_bio_of(RSA, RSA_new, d2i_RSA_PUBKEY, bp, rsa);
  281. }
  282. int i2d_RSAPublicKey_bio(BIO *bp, const RSA *rsa)
  283. {
  284. return ASN1_item_i2d_bio(ASN1_ITEM_rptr(RSAPublicKey), bp, rsa);
  285. }
  286. int i2d_RSA_PUBKEY_bio(BIO *bp, const RSA *rsa)
  287. {
  288. return ASN1_i2d_bio_of(RSA, i2d_RSA_PUBKEY, bp, rsa);
  289. }
  290. #endif
  291. #ifndef OPENSSL_NO_DSA
  292. # ifndef OPENSSL_NO_STDIO
  293. DSA *d2i_DSAPrivateKey_fp(FILE *fp, DSA **dsa)
  294. {
  295. return ASN1_d2i_fp_of(DSA, DSA_new, d2i_DSAPrivateKey, fp, dsa);
  296. }
  297. int i2d_DSAPrivateKey_fp(FILE *fp, const DSA *dsa)
  298. {
  299. return ASN1_i2d_fp_of(DSA, i2d_DSAPrivateKey, fp, dsa);
  300. }
  301. DSA *d2i_DSA_PUBKEY_fp(FILE *fp, DSA **dsa)
  302. {
  303. return ASN1_d2i_fp_of(DSA, DSA_new, d2i_DSA_PUBKEY, fp, dsa);
  304. }
  305. int i2d_DSA_PUBKEY_fp(FILE *fp, const DSA *dsa)
  306. {
  307. return ASN1_i2d_fp_of(DSA, i2d_DSA_PUBKEY, fp, dsa);
  308. }
  309. # endif
  310. DSA *d2i_DSAPrivateKey_bio(BIO *bp, DSA **dsa)
  311. {
  312. return ASN1_d2i_bio_of(DSA, DSA_new, d2i_DSAPrivateKey, bp, dsa);
  313. }
  314. int i2d_DSAPrivateKey_bio(BIO *bp, const DSA *dsa)
  315. {
  316. return ASN1_i2d_bio_of(DSA, i2d_DSAPrivateKey, bp, dsa);
  317. }
  318. DSA *d2i_DSA_PUBKEY_bio(BIO *bp, DSA **dsa)
  319. {
  320. return ASN1_d2i_bio_of(DSA, DSA_new, d2i_DSA_PUBKEY, bp, dsa);
  321. }
  322. int i2d_DSA_PUBKEY_bio(BIO *bp, const DSA *dsa)
  323. {
  324. return ASN1_i2d_bio_of(DSA, i2d_DSA_PUBKEY, bp, dsa);
  325. }
  326. #endif
  327. #ifndef OPENSSL_NO_EC
  328. # ifndef OPENSSL_NO_STDIO
  329. EC_KEY *d2i_EC_PUBKEY_fp(FILE *fp, EC_KEY **eckey)
  330. {
  331. return ASN1_d2i_fp_of(EC_KEY, EC_KEY_new, d2i_EC_PUBKEY, fp, eckey);
  332. }
  333. int i2d_EC_PUBKEY_fp(FILE *fp, const EC_KEY *eckey)
  334. {
  335. return ASN1_i2d_fp_of(EC_KEY, i2d_EC_PUBKEY, fp, eckey);
  336. }
  337. EC_KEY *d2i_ECPrivateKey_fp(FILE *fp, EC_KEY **eckey)
  338. {
  339. return ASN1_d2i_fp_of(EC_KEY, EC_KEY_new, d2i_ECPrivateKey, fp, eckey);
  340. }
  341. int i2d_ECPrivateKey_fp(FILE *fp, const EC_KEY *eckey)
  342. {
  343. return ASN1_i2d_fp_of(EC_KEY, i2d_ECPrivateKey, fp, eckey);
  344. }
  345. # endif
  346. EC_KEY *d2i_EC_PUBKEY_bio(BIO *bp, EC_KEY **eckey)
  347. {
  348. return ASN1_d2i_bio_of(EC_KEY, EC_KEY_new, d2i_EC_PUBKEY, bp, eckey);
  349. }
  350. int i2d_EC_PUBKEY_bio(BIO *bp, const EC_KEY *ecdsa)
  351. {
  352. return ASN1_i2d_bio_of(EC_KEY, i2d_EC_PUBKEY, bp, ecdsa);
  353. }
  354. EC_KEY *d2i_ECPrivateKey_bio(BIO *bp, EC_KEY **eckey)
  355. {
  356. return ASN1_d2i_bio_of(EC_KEY, EC_KEY_new, d2i_ECPrivateKey, bp, eckey);
  357. }
  358. int i2d_ECPrivateKey_bio(BIO *bp, const EC_KEY *eckey)
  359. {
  360. return ASN1_i2d_bio_of(EC_KEY, i2d_ECPrivateKey, bp, eckey);
  361. }
  362. #endif
  363. int X509_pubkey_digest(const X509 *data, const EVP_MD *type,
  364. unsigned char *md, unsigned int *len)
  365. {
  366. ASN1_BIT_STRING *key;
  367. key = X509_get0_pubkey_bitstr(data);
  368. if (!key)
  369. return 0;
  370. return EVP_Digest(key->data, key->length, md, len, type, NULL);
  371. }
  372. int X509_digest(const X509 *data, const EVP_MD *type, unsigned char *md,
  373. unsigned int *len)
  374. {
  375. if (type == EVP_sha1() && (data->ex_flags & EXFLAG_SET) != 0
  376. && (data->ex_flags & EXFLAG_INVALID) == 0) {
  377. /* Asking for SHA1 and we already computed it. */
  378. if (len != NULL)
  379. *len = sizeof(data->sha1_hash);
  380. memcpy(md, data->sha1_hash, sizeof(data->sha1_hash));
  381. return 1;
  382. }
  383. return (ASN1_item_digest
  384. (ASN1_ITEM_rptr(X509), type, (char *)data, md, len));
  385. }
  386. /* calculate cert digest using the same hash algorithm as in its signature */
  387. ASN1_OCTET_STRING *X509_digest_sig(const X509 *cert)
  388. {
  389. unsigned int len;
  390. unsigned char hash[EVP_MAX_MD_SIZE];
  391. int md_NID;
  392. const EVP_MD *md = NULL;
  393. ASN1_OCTET_STRING *new = NULL;
  394. if (cert == NULL) {
  395. X509err(0, ERR_R_PASSED_NULL_PARAMETER);
  396. return NULL;
  397. }
  398. if (!OBJ_find_sigid_algs(X509_get_signature_nid(cert), &md_NID, NULL)
  399. || (md = EVP_get_digestbynid(md_NID)) == NULL) {
  400. CMPerr(0, X509_R_UNSUPPORTED_ALGORITHM);
  401. return NULL;
  402. }
  403. if (!X509_digest(cert, md, hash, &len)
  404. || (new = ASN1_OCTET_STRING_new()) == NULL)
  405. return NULL;
  406. if (!(ASN1_OCTET_STRING_set(new, hash, len))) {
  407. ASN1_OCTET_STRING_free(new);
  408. return NULL;
  409. }
  410. return new;
  411. }
  412. int X509_CRL_digest(const X509_CRL *data, const EVP_MD *type,
  413. unsigned char *md, unsigned int *len)
  414. {
  415. if (type == EVP_sha1() && (data->flags & EXFLAG_SET) != 0
  416. && (data->flags & EXFLAG_INVALID) == 0) {
  417. /* Asking for SHA1; always computed in CRL d2i. */
  418. if (len != NULL)
  419. *len = sizeof(data->sha1_hash);
  420. memcpy(md, data->sha1_hash, sizeof(data->sha1_hash));
  421. return 1;
  422. }
  423. return (ASN1_item_digest
  424. (ASN1_ITEM_rptr(X509_CRL), type, (char *)data, md, len));
  425. }
  426. int X509_REQ_digest(const X509_REQ *data, const EVP_MD *type,
  427. unsigned char *md, unsigned int *len)
  428. {
  429. return (ASN1_item_digest
  430. (ASN1_ITEM_rptr(X509_REQ), type, (char *)data, md, len));
  431. }
  432. int X509_NAME_digest(const X509_NAME *data, const EVP_MD *type,
  433. unsigned char *md, unsigned int *len)
  434. {
  435. return (ASN1_item_digest
  436. (ASN1_ITEM_rptr(X509_NAME), type, (char *)data, md, len));
  437. }
  438. int PKCS7_ISSUER_AND_SERIAL_digest(PKCS7_ISSUER_AND_SERIAL *data,
  439. const EVP_MD *type, unsigned char *md,
  440. unsigned int *len)
  441. {
  442. return (ASN1_item_digest(ASN1_ITEM_rptr(PKCS7_ISSUER_AND_SERIAL), type,
  443. (char *)data, md, len));
  444. }
  445. #ifndef OPENSSL_NO_STDIO
  446. X509_SIG *d2i_PKCS8_fp(FILE *fp, X509_SIG **p8)
  447. {
  448. return ASN1_d2i_fp_of(X509_SIG, X509_SIG_new, d2i_X509_SIG, fp, p8);
  449. }
  450. int i2d_PKCS8_fp(FILE *fp, const X509_SIG *p8)
  451. {
  452. return ASN1_i2d_fp_of(X509_SIG, i2d_X509_SIG, fp, p8);
  453. }
  454. #endif
  455. X509_SIG *d2i_PKCS8_bio(BIO *bp, X509_SIG **p8)
  456. {
  457. return ASN1_d2i_bio_of(X509_SIG, X509_SIG_new, d2i_X509_SIG, bp, p8);
  458. }
  459. int i2d_PKCS8_bio(BIO *bp, const X509_SIG *p8)
  460. {
  461. return ASN1_i2d_bio_of(X509_SIG, i2d_X509_SIG, bp, p8);
  462. }
  463. #ifndef OPENSSL_NO_STDIO
  464. X509_PUBKEY *d2i_X509_PUBKEY_fp(FILE *fp, X509_PUBKEY **xpk)
  465. {
  466. return ASN1_d2i_fp_of(X509_PUBKEY, X509_PUBKEY_new, d2i_X509_PUBKEY,
  467. fp, xpk);
  468. }
  469. int i2d_X509_PUBKEY_fp(FILE *fp, const X509_PUBKEY *xpk)
  470. {
  471. return ASN1_i2d_fp_of(X509_PUBKEY, i2d_X509_PUBKEY, fp, xpk);
  472. }
  473. #endif
  474. X509_PUBKEY *d2i_X509_PUBKEY_bio(BIO *bp, X509_PUBKEY **xpk)
  475. {
  476. return ASN1_d2i_bio_of(X509_PUBKEY, X509_PUBKEY_new, d2i_X509_PUBKEY,
  477. bp, xpk);
  478. }
  479. int i2d_X509_PUBKEY_bio(BIO *bp, const X509_PUBKEY *xpk)
  480. {
  481. return ASN1_i2d_bio_of(X509_PUBKEY, i2d_X509_PUBKEY, bp, xpk);
  482. }
  483. #ifndef OPENSSL_NO_STDIO
  484. PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_fp(FILE *fp,
  485. PKCS8_PRIV_KEY_INFO **p8inf)
  486. {
  487. return ASN1_d2i_fp_of(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_new,
  488. d2i_PKCS8_PRIV_KEY_INFO, fp, p8inf);
  489. }
  490. int i2d_PKCS8_PRIV_KEY_INFO_fp(FILE *fp, const PKCS8_PRIV_KEY_INFO *p8inf)
  491. {
  492. return ASN1_i2d_fp_of(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO, fp,
  493. p8inf);
  494. }
  495. int i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, const EVP_PKEY *key)
  496. {
  497. PKCS8_PRIV_KEY_INFO *p8inf;
  498. int ret;
  499. p8inf = EVP_PKEY2PKCS8(key);
  500. if (p8inf == NULL)
  501. return 0;
  502. ret = i2d_PKCS8_PRIV_KEY_INFO_fp(fp, p8inf);
  503. PKCS8_PRIV_KEY_INFO_free(p8inf);
  504. return ret;
  505. }
  506. int i2d_PrivateKey_fp(FILE *fp, const EVP_PKEY *pkey)
  507. {
  508. return ASN1_i2d_fp_of(EVP_PKEY, i2d_PrivateKey, fp, pkey);
  509. }
  510. EVP_PKEY *d2i_PrivateKey_fp(FILE *fp, EVP_PKEY **a)
  511. {
  512. return ASN1_d2i_fp_of(EVP_PKEY, EVP_PKEY_new, d2i_AutoPrivateKey, fp, a);
  513. }
  514. EVP_PKEY *d2i_PrivateKey_ex_fp(FILE *fp, EVP_PKEY **a, OPENSSL_CTX *libctx,
  515. const char *propq)
  516. {
  517. BIO *b;
  518. void *ret;
  519. if ((b = BIO_new(BIO_s_file())) == NULL) {
  520. X509err(0, ERR_R_BUF_LIB);
  521. return NULL;
  522. }
  523. BIO_set_fp(b, fp, BIO_NOCLOSE);
  524. ret = d2i_PrivateKey_ex_bio(b, a, libctx, propq);
  525. BIO_free(b);
  526. return ret;
  527. }
  528. int i2d_PUBKEY_fp(FILE *fp, const EVP_PKEY *pkey)
  529. {
  530. return ASN1_i2d_fp_of(EVP_PKEY, i2d_PUBKEY, fp, pkey);
  531. }
  532. EVP_PKEY *d2i_PUBKEY_fp(FILE *fp, EVP_PKEY **a)
  533. {
  534. return ASN1_d2i_fp_of(EVP_PKEY, EVP_PKEY_new, d2i_PUBKEY, fp, a);
  535. }
  536. #endif
  537. PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_bio(BIO *bp,
  538. PKCS8_PRIV_KEY_INFO **p8inf)
  539. {
  540. return ASN1_d2i_bio_of(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_new,
  541. d2i_PKCS8_PRIV_KEY_INFO, bp, p8inf);
  542. }
  543. int i2d_PKCS8_PRIV_KEY_INFO_bio(BIO *bp, const PKCS8_PRIV_KEY_INFO *p8inf)
  544. {
  545. return ASN1_i2d_bio_of(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO, bp,
  546. p8inf);
  547. }
  548. int i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, const EVP_PKEY *key)
  549. {
  550. PKCS8_PRIV_KEY_INFO *p8inf;
  551. int ret;
  552. p8inf = EVP_PKEY2PKCS8(key);
  553. if (p8inf == NULL)
  554. return 0;
  555. ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf);
  556. PKCS8_PRIV_KEY_INFO_free(p8inf);
  557. return ret;
  558. }
  559. int i2d_PrivateKey_bio(BIO *bp, const EVP_PKEY *pkey)
  560. {
  561. return ASN1_i2d_bio_of(EVP_PKEY, i2d_PrivateKey, bp, pkey);
  562. }
  563. EVP_PKEY *d2i_PrivateKey_bio(BIO *bp, EVP_PKEY **a)
  564. {
  565. return ASN1_d2i_bio_of(EVP_PKEY, EVP_PKEY_new, d2i_AutoPrivateKey, bp, a);
  566. }
  567. EVP_PKEY *d2i_PrivateKey_ex_bio(BIO *bp, EVP_PKEY **a, OPENSSL_CTX *libctx,
  568. const char *propq)
  569. {
  570. BUF_MEM *b = NULL;
  571. const unsigned char *p;
  572. void *ret = NULL;
  573. int len;
  574. len = asn1_d2i_read_bio(bp, &b);
  575. if (len < 0)
  576. goto err;
  577. p = (unsigned char *)b->data;
  578. ret = d2i_AutoPrivateKey_ex(a, &p, len, libctx, propq);
  579. err:
  580. BUF_MEM_free(b);
  581. return ret;
  582. }
  583. int i2d_PUBKEY_bio(BIO *bp, const EVP_PKEY *pkey)
  584. {
  585. return ASN1_i2d_bio_of(EVP_PKEY, i2d_PUBKEY, bp, pkey);
  586. }
  587. EVP_PKEY *d2i_PUBKEY_bio(BIO *bp, EVP_PKEY **a)
  588. {
  589. return ASN1_d2i_bio_of(EVP_PKEY, EVP_PKEY_new, d2i_PUBKEY, bp, a);
  590. }