pem_pk8.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright 1995-2021 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, "pkcs8", 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. if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0) {
  86. ret = 1;
  87. if (enc != NULL) {
  88. ret = 0;
  89. if (OSSL_ENCODER_CTX_set_cipher(ctx, EVP_CIPHER_name(enc), NULL)) {
  90. const unsigned char *ukstr = (const unsigned char *)kstr;
  91. /*
  92. * Try to pass the passphrase if one was given, or the
  93. * passphrase callback if one was given. If none of them
  94. * are given and that's wrong, we rely on the _to_bio()
  95. * call to generate errors.
  96. */
  97. ret = 1;
  98. if (kstr != NULL
  99. && !OSSL_ENCODER_CTX_set_passphrase(ctx, ukstr, klen))
  100. ret = 0;
  101. else if (cb != NULL
  102. && !OSSL_ENCODER_CTX_set_pem_password_cb(ctx, cb, u))
  103. ret = 0;
  104. }
  105. }
  106. ret = ret && OSSL_ENCODER_to_bio(ctx, bp);
  107. } else {
  108. X509_SIG *p8;
  109. PKCS8_PRIV_KEY_INFO *p8inf;
  110. char buf[PEM_BUFSIZE];
  111. ret = 0;
  112. if ((p8inf = EVP_PKEY2PKCS8(x)) == NULL) {
  113. ERR_raise(ERR_LIB_PEM, PEM_R_ERROR_CONVERTING_PRIVATE_KEY);
  114. goto legacy_end;
  115. }
  116. if (enc || (nid != -1)) {
  117. if (kstr == NULL) {
  118. klen = cb(buf, PEM_BUFSIZE, 1, u);
  119. if (klen <= 0) {
  120. ERR_raise(ERR_LIB_PEM, PEM_R_READ_KEY);
  121. goto legacy_end;
  122. }
  123. kstr = buf;
  124. }
  125. p8 = PKCS8_encrypt(nid, enc, kstr, klen, NULL, 0, 0, p8inf);
  126. if (kstr == buf)
  127. OPENSSL_cleanse(buf, klen);
  128. if (p8 == NULL)
  129. goto legacy_end;
  130. if (isder)
  131. ret = i2d_PKCS8_bio(bp, p8);
  132. else
  133. ret = PEM_write_bio_PKCS8(bp, p8);
  134. X509_SIG_free(p8);
  135. } else {
  136. if (isder)
  137. ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf);
  138. else
  139. ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(bp, p8inf);
  140. }
  141. legacy_end:
  142. PKCS8_PRIV_KEY_INFO_free(p8inf);
  143. }
  144. OSSL_ENCODER_CTX_free(ctx);
  145. return ret;
  146. }
  147. EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
  148. void *u)
  149. {
  150. PKCS8_PRIV_KEY_INFO *p8inf = NULL;
  151. X509_SIG *p8 = NULL;
  152. int klen;
  153. EVP_PKEY *ret;
  154. char psbuf[PEM_BUFSIZE];
  155. p8 = d2i_PKCS8_bio(bp, NULL);
  156. if (p8 == NULL)
  157. return NULL;
  158. if (cb != NULL)
  159. klen = cb(psbuf, PEM_BUFSIZE, 0, u);
  160. else
  161. klen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
  162. if (klen < 0) {
  163. ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ);
  164. X509_SIG_free(p8);
  165. return NULL;
  166. }
  167. p8inf = PKCS8_decrypt(p8, psbuf, klen);
  168. X509_SIG_free(p8);
  169. OPENSSL_cleanse(psbuf, klen);
  170. if (p8inf == NULL)
  171. return NULL;
  172. ret = EVP_PKCS82PKEY(p8inf);
  173. PKCS8_PRIV_KEY_INFO_free(p8inf);
  174. if (!ret)
  175. return NULL;
  176. if (x != NULL) {
  177. EVP_PKEY_free(*x);
  178. *x = ret;
  179. }
  180. return ret;
  181. }
  182. #ifndef OPENSSL_NO_STDIO
  183. int i2d_PKCS8PrivateKey_fp(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
  184. const char *kstr, int klen,
  185. pem_password_cb *cb, void *u)
  186. {
  187. return do_pk8pkey_fp(fp, x, 1, -1, enc, kstr, klen, cb, u, NULL);
  188. }
  189. int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, const EVP_PKEY *x, int nid,
  190. const char *kstr, int klen,
  191. pem_password_cb *cb, void *u)
  192. {
  193. return do_pk8pkey_fp(fp, x, 1, nid, NULL, kstr, klen, cb, u, NULL);
  194. }
  195. int PEM_write_PKCS8PrivateKey_nid(FILE *fp, const EVP_PKEY *x, int nid,
  196. const char *kstr, int klen,
  197. pem_password_cb *cb, void *u)
  198. {
  199. return do_pk8pkey_fp(fp, x, 0, nid, NULL, kstr, klen, cb, u, NULL);
  200. }
  201. int PEM_write_PKCS8PrivateKey(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
  202. const char *kstr, int klen,
  203. pem_password_cb *cb, void *u)
  204. {
  205. return do_pk8pkey_fp(fp, x, 0, -1, enc, kstr, klen, cb, u, NULL);
  206. }
  207. static int do_pk8pkey_fp(FILE *fp, const EVP_PKEY *x, int isder, int nid,
  208. const EVP_CIPHER *enc, const char *kstr, int klen,
  209. pem_password_cb *cb, void *u, const char *propq)
  210. {
  211. BIO *bp;
  212. int ret;
  213. if ((bp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
  214. ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
  215. return 0;
  216. }
  217. ret = do_pk8pkey(bp, x, isder, nid, enc, kstr, klen, cb, u, propq);
  218. BIO_free(bp);
  219. return ret;
  220. }
  221. EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
  222. void *u)
  223. {
  224. BIO *bp;
  225. EVP_PKEY *ret;
  226. if ((bp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
  227. ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
  228. return NULL;
  229. }
  230. ret = d2i_PKCS8PrivateKey_bio(bp, x, cb, u);
  231. BIO_free(bp);
  232. return ret;
  233. }
  234. #endif
  235. IMPLEMENT_PEM_rw(PKCS8, X509_SIG, PEM_STRING_PKCS8, X509_SIG)
  236. IMPLEMENT_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO, PEM_STRING_PKCS8INF,
  237. PKCS8_PRIV_KEY_INFO)