pem_all.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright 1995-2020 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/bio.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/x509.h>
  14. #include <openssl/pkcs7.h>
  15. #include <openssl/pem.h>
  16. #include <openssl/rsa.h>
  17. #include <openssl/dsa.h>
  18. #include <openssl/dh.h>
  19. #ifndef OPENSSL_NO_RSA
  20. static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa);
  21. #endif
  22. #ifndef OPENSSL_NO_DSA
  23. static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa);
  24. #endif
  25. #ifndef OPENSSL_NO_EC
  26. static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey);
  27. #endif
  28. IMPLEMENT_PEM_rw(X509_REQ, X509_REQ, PEM_STRING_X509_REQ, X509_REQ)
  29. IMPLEMENT_PEM_write(X509_REQ_NEW, X509_REQ, PEM_STRING_X509_REQ_OLD, X509_REQ)
  30. IMPLEMENT_PEM_rw(X509_CRL, X509_CRL, PEM_STRING_X509_CRL, X509_CRL)
  31. IMPLEMENT_PEM_rw(PKCS7, PKCS7, PEM_STRING_PKCS7, PKCS7)
  32. IMPLEMENT_PEM_rw(NETSCAPE_CERT_SEQUENCE, NETSCAPE_CERT_SEQUENCE,
  33. PEM_STRING_X509, NETSCAPE_CERT_SEQUENCE)
  34. #ifndef OPENSSL_NO_RSA
  35. /*
  36. * We treat RSA or DSA private keys as a special case. For private keys we
  37. * read in an EVP_PKEY structure with PEM_read_bio_PrivateKey() and extract
  38. * the relevant private key: this means can handle "traditional" and PKCS#8
  39. * formats transparently.
  40. */
  41. static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa)
  42. {
  43. RSA *rtmp;
  44. if (!key)
  45. return NULL;
  46. rtmp = EVP_PKEY_get1_RSA(key);
  47. EVP_PKEY_free(key);
  48. if (!rtmp)
  49. return NULL;
  50. if (rsa) {
  51. RSA_free(*rsa);
  52. *rsa = rtmp;
  53. }
  54. return rtmp;
  55. }
  56. RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **rsa, pem_password_cb *cb,
  57. void *u)
  58. {
  59. EVP_PKEY *pktmp;
  60. pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
  61. return pkey_get_rsa(pktmp, rsa);
  62. }
  63. # ifndef OPENSSL_NO_STDIO
  64. RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **rsa, pem_password_cb *cb, void *u)
  65. {
  66. EVP_PKEY *pktmp;
  67. pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
  68. return pkey_get_rsa(pktmp, rsa);
  69. }
  70. # endif
  71. IMPLEMENT_PEM_write_cb_const(RSAPrivateKey, RSA, PEM_STRING_RSA,
  72. RSAPrivateKey)
  73. IMPLEMENT_PEM_rw_const(RSAPublicKey, RSA, PEM_STRING_RSA_PUBLIC,
  74. RSAPublicKey)
  75. IMPLEMENT_PEM_rw(RSA_PUBKEY, RSA, PEM_STRING_PUBLIC, RSA_PUBKEY)
  76. #endif
  77. #ifndef OPENSSL_NO_DSA
  78. static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa)
  79. {
  80. DSA *dtmp;
  81. if (!key)
  82. return NULL;
  83. dtmp = EVP_PKEY_get1_DSA(key);
  84. EVP_PKEY_free(key);
  85. if (!dtmp)
  86. return NULL;
  87. if (dsa) {
  88. DSA_free(*dsa);
  89. *dsa = dtmp;
  90. }
  91. return dtmp;
  92. }
  93. DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **dsa, pem_password_cb *cb,
  94. void *u)
  95. {
  96. EVP_PKEY *pktmp;
  97. pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
  98. return pkey_get_dsa(pktmp, dsa); /* will free pktmp */
  99. }
  100. IMPLEMENT_PEM_write_cb_const(DSAPrivateKey, DSA, PEM_STRING_DSA,
  101. DSAPrivateKey)
  102. IMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY)
  103. # ifndef OPENSSL_NO_STDIO
  104. DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **dsa, pem_password_cb *cb, void *u)
  105. {
  106. EVP_PKEY *pktmp;
  107. pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
  108. return pkey_get_dsa(pktmp, dsa); /* will free pktmp */
  109. }
  110. # endif
  111. IMPLEMENT_PEM_rw_const(DSAparams, DSA, PEM_STRING_DSAPARAMS, DSAparams)
  112. #endif
  113. #ifndef OPENSSL_NO_EC
  114. static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey)
  115. {
  116. EC_KEY *dtmp;
  117. if (!key)
  118. return NULL;
  119. dtmp = EVP_PKEY_get1_EC_KEY(key);
  120. EVP_PKEY_free(key);
  121. if (!dtmp)
  122. return NULL;
  123. if (eckey) {
  124. EC_KEY_free(*eckey);
  125. *eckey = dtmp;
  126. }
  127. return dtmp;
  128. }
  129. EC_KEY *PEM_read_bio_ECPrivateKey(BIO *bp, EC_KEY **key, pem_password_cb *cb,
  130. void *u)
  131. {
  132. EVP_PKEY *pktmp;
  133. pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
  134. return pkey_get_eckey(pktmp, key); /* will free pktmp */
  135. }
  136. IMPLEMENT_PEM_rw_const(ECPKParameters, EC_GROUP, PEM_STRING_ECPARAMETERS,
  137. ECPKParameters)
  138. IMPLEMENT_PEM_write_cb(ECPrivateKey, EC_KEY, PEM_STRING_ECPRIVATEKEY,
  139. ECPrivateKey)
  140. IMPLEMENT_PEM_rw(EC_PUBKEY, EC_KEY, PEM_STRING_PUBLIC, EC_PUBKEY)
  141. # ifndef OPENSSL_NO_STDIO
  142. EC_KEY *PEM_read_ECPrivateKey(FILE *fp, EC_KEY **eckey, pem_password_cb *cb,
  143. void *u)
  144. {
  145. EVP_PKEY *pktmp;
  146. pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
  147. return pkey_get_eckey(pktmp, eckey); /* will free pktmp */
  148. }
  149. # endif
  150. #endif
  151. #ifndef OPENSSL_NO_DH
  152. IMPLEMENT_PEM_write_const(DHparams, DH, PEM_STRING_DHPARAMS, DHparams)
  153. IMPLEMENT_PEM_write_const(DHxparams, DH, PEM_STRING_DHXPARAMS, DHxparams)
  154. #endif
  155. IMPLEMENT_PEM_rw(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY)