pem_pk8.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/core_dispatch.h>
  12. #include <openssl/buffer.h>
  13. #include <openssl/objects.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/x509.h>
  16. #include <openssl/pkcs12.h>
  17. #include <openssl/pem.h>
  18. #include <openssl/encoder.h>
  19. static int do_pk8pkey(BIO *bp, const EVP_PKEY *x, int isder,
  20. int nid, const EVP_CIPHER *enc,
  21. const char *kstr, int klen,
  22. pem_password_cb *cb, void *u,
  23. const char *propq);
  24. #ifndef OPENSSL_NO_STDIO
  25. static int do_pk8pkey_fp(FILE *bp, const EVP_PKEY *x, int isder,
  26. int nid, const EVP_CIPHER *enc,
  27. const char *kstr, int klen,
  28. pem_password_cb *cb, void *u,
  29. const char *propq);
  30. #endif
  31. /*
  32. * These functions write a private key in PKCS#8 format: it is a "drop in"
  33. * replacement for PEM_write_bio_PrivateKey() and friends. As usual if 'enc'
  34. * is NULL then it uses the unencrypted private key form. The 'nid' versions
  35. * uses PKCS#5 v1.5 PBE algorithms whereas the others use PKCS#5 v2.0.
  36. */
  37. int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, const EVP_PKEY *x, int nid,
  38. const char *kstr, int klen,
  39. pem_password_cb *cb, void *u)
  40. {
  41. return do_pk8pkey(bp, x, 0, nid, NULL, kstr, klen, cb, u, NULL);
  42. }
  43. int PEM_write_bio_PKCS8PrivateKey(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc,
  44. const char *kstr, int klen,
  45. pem_password_cb *cb, void *u)
  46. {
  47. return do_pk8pkey(bp, x, 0, -1, enc, kstr, klen, cb, u, NULL);
  48. }
  49. int i2d_PKCS8PrivateKey_bio(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc,
  50. const char *kstr, int klen,
  51. pem_password_cb *cb, void *u)
  52. {
  53. return do_pk8pkey(bp, x, 1, -1, enc, kstr, klen, cb, u, NULL);
  54. }
  55. int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, const EVP_PKEY *x, int nid,
  56. const char *kstr, int klen,
  57. pem_password_cb *cb, void *u)
  58. {
  59. return do_pk8pkey(bp, x, 1, nid, NULL, kstr, klen, cb, u, NULL);
  60. }
  61. static int do_pk8pkey(BIO *bp, const EVP_PKEY *x, int isder, int nid,
  62. const EVP_CIPHER *enc, const char *kstr, int klen,
  63. pem_password_cb *cb, void *u, const char *propq)
  64. {
  65. int ret = 0;
  66. const char *outtype = isder ? "DER" : "PEM";
  67. OSSL_ENCODER_CTX *ctx =
  68. OSSL_ENCODER_CTX_new_for_pkey(x, OSSL_KEYMGMT_SELECT_ALL,
  69. outtype, "PrivateKeyInfo", propq);
  70. if (ctx == NULL)
  71. return 0;
  72. /*
  73. * If no keystring or callback is set, OpenSSL traditionally uses the
  74. * user's cb argument as a password string, or if that's NULL, it falls
  75. * back on PEM_def_callback().
  76. */
  77. if (kstr == NULL && cb == NULL) {
  78. if (u != NULL) {
  79. kstr = u;
  80. klen = strlen(u);
  81. } else {
  82. cb = PEM_def_callback;
  83. }
  84. }
  85. /*
  86. * NOTE: There is no attempt to do a EVP_CIPHER_fetch() using the nid,
  87. * since the nid is a PBE algorithm which can't be fetched currently.
  88. * (e.g. NID_pbe_WithSHA1And2_Key_TripleDES_CBC). Just use the legacy
  89. * path if the NID is passed.
  90. */
  91. if (nid == -1 && OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0) {
  92. ret = 1;
  93. if (enc != NULL) {
  94. ret = 0;
  95. if (OSSL_ENCODER_CTX_set_cipher(ctx, EVP_CIPHER_get0_name(enc),
  96. NULL)) {
  97. const unsigned char *ukstr = (const unsigned char *)kstr;
  98. /*
  99. * Try to pass the passphrase if one was given, or the
  100. * passphrase callback if one was given. If none of them
  101. * are given and that's wrong, we rely on the _to_bio()
  102. * call to generate errors.
  103. */
  104. ret = 1;
  105. if (kstr != NULL
  106. && !OSSL_ENCODER_CTX_set_passphrase(ctx, ukstr, klen))
  107. ret = 0;
  108. else if (cb != NULL
  109. && !OSSL_ENCODER_CTX_set_pem_password_cb(ctx, cb, u))
  110. ret = 0;
  111. }
  112. }
  113. ret = ret && OSSL_ENCODER_to_bio(ctx, bp);
  114. } else {
  115. X509_SIG *p8;
  116. PKCS8_PRIV_KEY_INFO *p8inf;
  117. char buf[PEM_BUFSIZE];
  118. ret = 0;
  119. if ((p8inf = EVP_PKEY2PKCS8(x)) == NULL) {
  120. ERR_raise(ERR_LIB_PEM, PEM_R_ERROR_CONVERTING_PRIVATE_KEY);
  121. goto legacy_end;
  122. }
  123. if (enc || (nid != -1)) {
  124. if (kstr == NULL) {
  125. klen = cb(buf, PEM_BUFSIZE, 1, u);
  126. if (klen < 0) {
  127. ERR_raise(ERR_LIB_PEM, PEM_R_READ_KEY);
  128. goto legacy_end;
  129. }
  130. kstr = buf;
  131. }
  132. p8 = PKCS8_encrypt(nid, enc, kstr, klen, NULL, 0, 0, p8inf);
  133. if (kstr == buf)
  134. OPENSSL_cleanse(buf, klen);
  135. if (p8 == NULL)
  136. goto legacy_end;
  137. if (isder)
  138. ret = i2d_PKCS8_bio(bp, p8);
  139. else
  140. ret = PEM_write_bio_PKCS8(bp, p8);
  141. X509_SIG_free(p8);
  142. } else {
  143. if (isder)
  144. ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf);
  145. else
  146. ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(bp, p8inf);
  147. }
  148. legacy_end:
  149. PKCS8_PRIV_KEY_INFO_free(p8inf);
  150. }
  151. OSSL_ENCODER_CTX_free(ctx);
  152. return ret;
  153. }
  154. EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
  155. void *u)
  156. {
  157. PKCS8_PRIV_KEY_INFO *p8inf = NULL;
  158. X509_SIG *p8 = NULL;
  159. int klen;
  160. EVP_PKEY *ret;
  161. char psbuf[PEM_BUFSIZE];
  162. p8 = d2i_PKCS8_bio(bp, NULL);
  163. if (p8 == NULL)
  164. return NULL;
  165. if (cb != NULL)
  166. klen = cb(psbuf, PEM_BUFSIZE, 0, u);
  167. else
  168. klen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
  169. if (klen < 0) {
  170. ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ);
  171. X509_SIG_free(p8);
  172. return NULL;
  173. }
  174. p8inf = PKCS8_decrypt(p8, psbuf, klen);
  175. X509_SIG_free(p8);
  176. OPENSSL_cleanse(psbuf, klen);
  177. if (p8inf == NULL)
  178. return NULL;
  179. ret = EVP_PKCS82PKEY(p8inf);
  180. PKCS8_PRIV_KEY_INFO_free(p8inf);
  181. if (!ret)
  182. return NULL;
  183. if (x != NULL) {
  184. EVP_PKEY_free(*x);
  185. *x = ret;
  186. }
  187. return ret;
  188. }
  189. #ifndef OPENSSL_NO_STDIO
  190. int i2d_PKCS8PrivateKey_fp(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
  191. const char *kstr, int klen,
  192. pem_password_cb *cb, void *u)
  193. {
  194. return do_pk8pkey_fp(fp, x, 1, -1, enc, kstr, klen, cb, u, NULL);
  195. }
  196. int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, const EVP_PKEY *x, int nid,
  197. const char *kstr, int klen,
  198. pem_password_cb *cb, void *u)
  199. {
  200. return do_pk8pkey_fp(fp, x, 1, nid, NULL, kstr, klen, cb, u, NULL);
  201. }
  202. int PEM_write_PKCS8PrivateKey_nid(FILE *fp, const EVP_PKEY *x, int nid,
  203. const char *kstr, int klen,
  204. pem_password_cb *cb, void *u)
  205. {
  206. return do_pk8pkey_fp(fp, x, 0, nid, NULL, kstr, klen, cb, u, NULL);
  207. }
  208. int PEM_write_PKCS8PrivateKey(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
  209. const char *kstr, int klen,
  210. pem_password_cb *cb, void *u)
  211. {
  212. return do_pk8pkey_fp(fp, x, 0, -1, enc, kstr, klen, cb, u, NULL);
  213. }
  214. static int do_pk8pkey_fp(FILE *fp, const EVP_PKEY *x, int isder, int nid,
  215. const EVP_CIPHER *enc, const char *kstr, int klen,
  216. pem_password_cb *cb, void *u, const char *propq)
  217. {
  218. BIO *bp;
  219. int ret;
  220. if ((bp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
  221. ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
  222. return 0;
  223. }
  224. ret = do_pk8pkey(bp, x, isder, nid, enc, kstr, klen, cb, u, propq);
  225. BIO_free(bp);
  226. return ret;
  227. }
  228. EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
  229. void *u)
  230. {
  231. BIO *bp;
  232. EVP_PKEY *ret;
  233. if ((bp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
  234. ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
  235. return NULL;
  236. }
  237. ret = d2i_PKCS8PrivateKey_bio(bp, x, cb, u);
  238. BIO_free(bp);
  239. return ret;
  240. }
  241. #endif
  242. IMPLEMENT_PEM_rw(PKCS8, X509_SIG, PEM_STRING_PKCS8, X509_SIG)
  243. IMPLEMENT_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO, PEM_STRING_PKCS8INF,
  244. PKCS8_PRIV_KEY_INFO)