pem_all.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. /*
  10. * DSA 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/bio.h>
  17. #include <openssl/evp.h>
  18. #include <openssl/x509.h>
  19. #include <openssl/pkcs7.h>
  20. #include <openssl/pem.h>
  21. #include <openssl/rsa.h>
  22. #include <openssl/dsa.h>
  23. #include <openssl/dh.h>
  24. #include "pem_local.h"
  25. static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa);
  26. #ifndef OPENSSL_NO_DSA
  27. static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa);
  28. #endif
  29. #ifndef OPENSSL_NO_EC
  30. static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey);
  31. #endif
  32. IMPLEMENT_PEM_rw(X509_REQ, X509_REQ, PEM_STRING_X509_REQ, X509_REQ)
  33. IMPLEMENT_PEM_write(X509_REQ_NEW, X509_REQ, PEM_STRING_X509_REQ_OLD, X509_REQ)
  34. IMPLEMENT_PEM_rw(X509_CRL, X509_CRL, PEM_STRING_X509_CRL, X509_CRL)
  35. IMPLEMENT_PEM_rw(X509_PUBKEY, X509_PUBKEY, PEM_STRING_PUBLIC, X509_PUBKEY)
  36. IMPLEMENT_PEM_rw(PKCS7, PKCS7, PEM_STRING_PKCS7, PKCS7)
  37. IMPLEMENT_PEM_rw(NETSCAPE_CERT_SEQUENCE, NETSCAPE_CERT_SEQUENCE,
  38. PEM_STRING_X509, NETSCAPE_CERT_SEQUENCE)
  39. #ifndef OPENSSL_NO_DEPRECATED_3_0
  40. /*
  41. * We treat RSA or DSA private keys as a special case. For private keys we
  42. * read in an EVP_PKEY structure with PEM_read_bio_PrivateKey() and extract
  43. * the relevant private key: this means can handle "traditional" and PKCS#8
  44. * formats transparently.
  45. */
  46. static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa)
  47. {
  48. RSA *rtmp;
  49. if (!key)
  50. return NULL;
  51. rtmp = EVP_PKEY_get1_RSA(key);
  52. EVP_PKEY_free(key);
  53. if (!rtmp)
  54. return NULL;
  55. if (rsa) {
  56. RSA_free(*rsa);
  57. *rsa = rtmp;
  58. }
  59. return rtmp;
  60. }
  61. RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **rsa, pem_password_cb *cb,
  62. void *u)
  63. {
  64. EVP_PKEY *pktmp;
  65. pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
  66. return pkey_get_rsa(pktmp, rsa);
  67. }
  68. # ifndef OPENSSL_NO_STDIO
  69. RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **rsa, pem_password_cb *cb, void *u)
  70. {
  71. EVP_PKEY *pktmp;
  72. pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
  73. return pkey_get_rsa(pktmp, rsa);
  74. }
  75. # endif
  76. IMPLEMENT_PEM_write_cb(RSAPrivateKey, RSA, PEM_STRING_RSA, RSAPrivateKey)
  77. IMPLEMENT_PEM_rw(RSAPublicKey, RSA, PEM_STRING_RSA_PUBLIC, RSAPublicKey)
  78. IMPLEMENT_PEM_rw(RSA_PUBKEY, RSA, PEM_STRING_PUBLIC, RSA_PUBKEY)
  79. #endif
  80. #ifndef OPENSSL_NO_DSA
  81. static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa)
  82. {
  83. DSA *dtmp;
  84. if (!key)
  85. return NULL;
  86. dtmp = EVP_PKEY_get1_DSA(key);
  87. EVP_PKEY_free(key);
  88. if (!dtmp)
  89. return NULL;
  90. if (dsa) {
  91. DSA_free(*dsa);
  92. *dsa = dtmp;
  93. }
  94. return dtmp;
  95. }
  96. DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **dsa, pem_password_cb *cb,
  97. void *u)
  98. {
  99. EVP_PKEY *pktmp;
  100. pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
  101. return pkey_get_dsa(pktmp, dsa); /* will free pktmp */
  102. }
  103. IMPLEMENT_PEM_write_cb(DSAPrivateKey, DSA, PEM_STRING_DSA, DSAPrivateKey)
  104. IMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY)
  105. # ifndef OPENSSL_NO_STDIO
  106. DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **dsa, pem_password_cb *cb, void *u)
  107. {
  108. EVP_PKEY *pktmp;
  109. pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
  110. return pkey_get_dsa(pktmp, dsa); /* will free pktmp */
  111. }
  112. # endif
  113. IMPLEMENT_PEM_rw(DSAparams, DSA, PEM_STRING_DSAPARAMS, DSAparams)
  114. #endif
  115. #ifndef OPENSSL_NO_DEPRECATED_3_0
  116. # ifndef OPENSSL_NO_EC
  117. static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey)
  118. {
  119. EC_KEY *dtmp;
  120. if (!key)
  121. return NULL;
  122. dtmp = EVP_PKEY_get1_EC_KEY(key);
  123. EVP_PKEY_free(key);
  124. if (!dtmp)
  125. return NULL;
  126. if (eckey) {
  127. EC_KEY_free(*eckey);
  128. *eckey = dtmp;
  129. }
  130. return dtmp;
  131. }
  132. EC_KEY *PEM_read_bio_ECPrivateKey(BIO *bp, EC_KEY **key, pem_password_cb *cb,
  133. void *u)
  134. {
  135. EVP_PKEY *pktmp;
  136. pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
  137. return pkey_get_eckey(pktmp, key); /* will free pktmp */
  138. }
  139. IMPLEMENT_PEM_rw(ECPKParameters, EC_GROUP, PEM_STRING_ECPARAMETERS,
  140. ECPKParameters)
  141. IMPLEMENT_PEM_write_cb(ECPrivateKey, EC_KEY, PEM_STRING_ECPRIVATEKEY,
  142. ECPrivateKey)
  143. IMPLEMENT_PEM_rw(EC_PUBKEY, EC_KEY, PEM_STRING_PUBLIC, EC_PUBKEY)
  144. # ifndef OPENSSL_NO_STDIO
  145. EC_KEY *PEM_read_ECPrivateKey(FILE *fp, EC_KEY **eckey, pem_password_cb *cb,
  146. void *u)
  147. {
  148. EVP_PKEY *pktmp;
  149. pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
  150. return pkey_get_eckey(pktmp, eckey); /* will free pktmp */
  151. }
  152. # endif
  153. # endif /* !OPENSSL_NO_EC */
  154. #endif /* !OPENSSL_NO_DEPRECATED_3_0 */
  155. #ifndef OPENSSL_NO_DH
  156. IMPLEMENT_PEM_write(DHparams, DH, PEM_STRING_DHPARAMS, DHparams)
  157. IMPLEMENT_PEM_write(DHxparams, DH, PEM_STRING_DHXPARAMS, DHxparams)
  158. /* Transparently read in PKCS#3 or X9.42 DH parameters */
  159. DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u)
  160. {
  161. char *nm = NULL;
  162. const unsigned char *p = NULL;
  163. unsigned char *data = NULL;
  164. long len;
  165. DH *ret = NULL;
  166. if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_DHPARAMS, bp, cb, u))
  167. return NULL;
  168. p = data;
  169. if (strcmp(nm, PEM_STRING_DHXPARAMS) == 0)
  170. ret = d2i_DHxparams(x, &p, len);
  171. else
  172. ret = d2i_DHparams(x, &p, len);
  173. if (ret == NULL)
  174. ERR_raise(ERR_LIB_PEM, ERR_R_ASN1_LIB);
  175. OPENSSL_free(nm);
  176. OPENSSL_free(data);
  177. return ret;
  178. }
  179. # ifndef OPENSSL_NO_STDIO
  180. DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u)
  181. {
  182. BIO *b;
  183. DH *ret;
  184. if ((b = BIO_new(BIO_s_file())) == NULL) {
  185. ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
  186. return 0;
  187. }
  188. BIO_set_fp(b, fp, BIO_NOCLOSE);
  189. ret = PEM_read_bio_DHparams(b, x, cb, u);
  190. BIO_free(b);
  191. return ret;
  192. }
  193. # endif
  194. #endif
  195. IMPLEMENT_PEM_provided_write(PUBKEY, EVP_PKEY, pkey, PEM_STRING_PUBLIC, PUBKEY)