2
0

pem_pk8.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright 1995-2018 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 "internal/cryptlib.h"
  11. #include <openssl/buffer.h>
  12. #include <openssl/objects.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/x509.h>
  15. #include <openssl/pkcs12.h>
  16. #include <openssl/pem.h>
  17. static int do_pk8pkey(BIO *bp, EVP_PKEY *x, int isder,
  18. int nid, const EVP_CIPHER *enc,
  19. char *kstr, int klen, pem_password_cb *cb, void *u);
  20. #ifndef OPENSSL_NO_STDIO
  21. static int do_pk8pkey_fp(FILE *bp, EVP_PKEY *x, int isder,
  22. int nid, const EVP_CIPHER *enc,
  23. char *kstr, int klen, pem_password_cb *cb, void *u);
  24. #endif
  25. /*
  26. * These functions write a private key in PKCS#8 format: it is a "drop in"
  27. * replacement for PEM_write_bio_PrivateKey() and friends. As usual if 'enc'
  28. * is NULL then it uses the unencrypted private key form. The 'nid' versions
  29. * uses PKCS#5 v1.5 PBE algorithms whereas the others use PKCS#5 v2.0.
  30. */
  31. int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x, int nid,
  32. char *kstr, int klen,
  33. pem_password_cb *cb, void *u)
  34. {
  35. return do_pk8pkey(bp, x, 0, nid, NULL, kstr, klen, cb, u);
  36. }
  37. int PEM_write_bio_PKCS8PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
  38. char *kstr, int klen,
  39. pem_password_cb *cb, void *u)
  40. {
  41. return do_pk8pkey(bp, x, 0, -1, enc, kstr, klen, cb, u);
  42. }
  43. int i2d_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
  44. char *kstr, int klen,
  45. pem_password_cb *cb, void *u)
  46. {
  47. return do_pk8pkey(bp, x, 1, -1, enc, kstr, klen, cb, u);
  48. }
  49. int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, EVP_PKEY *x, int nid,
  50. char *kstr, int klen,
  51. pem_password_cb *cb, void *u)
  52. {
  53. return do_pk8pkey(bp, x, 1, nid, NULL, kstr, klen, cb, u);
  54. }
  55. static int do_pk8pkey(BIO *bp, EVP_PKEY *x, int isder, int nid,
  56. const EVP_CIPHER *enc, char *kstr, int klen,
  57. pem_password_cb *cb, void *u)
  58. {
  59. X509_SIG *p8;
  60. PKCS8_PRIV_KEY_INFO *p8inf;
  61. char buf[PEM_BUFSIZE];
  62. int ret;
  63. if ((p8inf = EVP_PKEY2PKCS8(x)) == NULL) {
  64. PEMerr(PEM_F_DO_PK8PKEY, PEM_R_ERROR_CONVERTING_PRIVATE_KEY);
  65. return 0;
  66. }
  67. if (enc || (nid != -1)) {
  68. if (!kstr) {
  69. if (!cb)
  70. klen = PEM_def_callback(buf, PEM_BUFSIZE, 1, u);
  71. else
  72. klen = cb(buf, PEM_BUFSIZE, 1, u);
  73. if (klen <= 0) {
  74. PEMerr(PEM_F_DO_PK8PKEY, PEM_R_READ_KEY);
  75. PKCS8_PRIV_KEY_INFO_free(p8inf);
  76. return 0;
  77. }
  78. kstr = buf;
  79. }
  80. p8 = PKCS8_encrypt(nid, enc, kstr, klen, NULL, 0, 0, p8inf);
  81. if (kstr == buf)
  82. OPENSSL_cleanse(buf, klen);
  83. PKCS8_PRIV_KEY_INFO_free(p8inf);
  84. if (p8 == NULL)
  85. return 0;
  86. if (isder)
  87. ret = i2d_PKCS8_bio(bp, p8);
  88. else
  89. ret = PEM_write_bio_PKCS8(bp, p8);
  90. X509_SIG_free(p8);
  91. return ret;
  92. } else {
  93. if (isder)
  94. ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf);
  95. else
  96. ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(bp, p8inf);
  97. PKCS8_PRIV_KEY_INFO_free(p8inf);
  98. return ret;
  99. }
  100. }
  101. EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
  102. void *u)
  103. {
  104. PKCS8_PRIV_KEY_INFO *p8inf = NULL;
  105. X509_SIG *p8 = NULL;
  106. int klen;
  107. EVP_PKEY *ret;
  108. char psbuf[PEM_BUFSIZE];
  109. p8 = d2i_PKCS8_bio(bp, NULL);
  110. if (!p8)
  111. return NULL;
  112. if (cb)
  113. klen = cb(psbuf, PEM_BUFSIZE, 0, u);
  114. else
  115. klen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
  116. if (klen < 0) {
  117. PEMerr(PEM_F_D2I_PKCS8PRIVATEKEY_BIO, PEM_R_BAD_PASSWORD_READ);
  118. X509_SIG_free(p8);
  119. return NULL;
  120. }
  121. p8inf = PKCS8_decrypt(p8, psbuf, klen);
  122. X509_SIG_free(p8);
  123. OPENSSL_cleanse(psbuf, klen);
  124. if (!p8inf)
  125. return NULL;
  126. ret = EVP_PKCS82PKEY(p8inf);
  127. PKCS8_PRIV_KEY_INFO_free(p8inf);
  128. if (!ret)
  129. return NULL;
  130. if (x) {
  131. EVP_PKEY_free(*x);
  132. *x = ret;
  133. }
  134. return ret;
  135. }
  136. #ifndef OPENSSL_NO_STDIO
  137. int i2d_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
  138. char *kstr, int klen, pem_password_cb *cb, void *u)
  139. {
  140. return do_pk8pkey_fp(fp, x, 1, -1, enc, kstr, klen, cb, u);
  141. }
  142. int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, EVP_PKEY *x, int nid,
  143. char *kstr, int klen,
  144. pem_password_cb *cb, void *u)
  145. {
  146. return do_pk8pkey_fp(fp, x, 1, nid, NULL, kstr, klen, cb, u);
  147. }
  148. int PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x, int nid,
  149. char *kstr, int klen,
  150. pem_password_cb *cb, void *u)
  151. {
  152. return do_pk8pkey_fp(fp, x, 0, nid, NULL, kstr, klen, cb, u);
  153. }
  154. int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
  155. char *kstr, int klen, pem_password_cb *cb,
  156. void *u)
  157. {
  158. return do_pk8pkey_fp(fp, x, 0, -1, enc, kstr, klen, cb, u);
  159. }
  160. static int do_pk8pkey_fp(FILE *fp, EVP_PKEY *x, int isder, int nid,
  161. const EVP_CIPHER *enc, char *kstr, int klen,
  162. pem_password_cb *cb, void *u)
  163. {
  164. BIO *bp;
  165. int ret;
  166. if ((bp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
  167. PEMerr(PEM_F_DO_PK8PKEY_FP, ERR_R_BUF_LIB);
  168. return 0;
  169. }
  170. ret = do_pk8pkey(bp, x, isder, nid, enc, kstr, klen, cb, u);
  171. BIO_free(bp);
  172. return ret;
  173. }
  174. EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
  175. void *u)
  176. {
  177. BIO *bp;
  178. EVP_PKEY *ret;
  179. if ((bp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
  180. PEMerr(PEM_F_D2I_PKCS8PRIVATEKEY_FP, ERR_R_BUF_LIB);
  181. return NULL;
  182. }
  183. ret = d2i_PKCS8PrivateKey_bio(bp, x, cb, u);
  184. BIO_free(bp);
  185. return ret;
  186. }
  187. #endif
  188. IMPLEMENT_PEM_rw(PKCS8, X509_SIG, PEM_STRING_PKCS8, X509_SIG)
  189. IMPLEMENT_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO, PEM_STRING_PKCS8INF,
  190. PKCS8_PRIV_KEY_INFO)