pem_all.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright 1995-2016 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/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) IMPLEMENT_PEM_rw(RSA_PUBKEY, RSA,
  75. PEM_STRING_PUBLIC,
  76. RSA_PUBKEY)
  77. #endif
  78. #ifndef OPENSSL_NO_DSA
  79. static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa)
  80. {
  81. DSA *dtmp;
  82. if (!key)
  83. return NULL;
  84. dtmp = EVP_PKEY_get1_DSA(key);
  85. EVP_PKEY_free(key);
  86. if (!dtmp)
  87. return NULL;
  88. if (dsa) {
  89. DSA_free(*dsa);
  90. *dsa = dtmp;
  91. }
  92. return dtmp;
  93. }
  94. DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **dsa, pem_password_cb *cb,
  95. void *u)
  96. {
  97. EVP_PKEY *pktmp;
  98. pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
  99. return pkey_get_dsa(pktmp, dsa); /* will free pktmp */
  100. }
  101. IMPLEMENT_PEM_write_cb_const(DSAPrivateKey, DSA, PEM_STRING_DSA,
  102. DSAPrivateKey)
  103. IMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY)
  104. # ifndef OPENSSL_NO_STDIO
  105. DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **dsa, pem_password_cb *cb, void *u)
  106. {
  107. EVP_PKEY *pktmp;
  108. pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
  109. return pkey_get_dsa(pktmp, dsa); /* will free pktmp */
  110. }
  111. # endif
  112. IMPLEMENT_PEM_rw_const(DSAparams, DSA, PEM_STRING_DSAPARAMS, DSAparams)
  113. #endif
  114. #ifndef OPENSSL_NO_EC
  115. static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey)
  116. {
  117. EC_KEY *dtmp;
  118. if (!key)
  119. return NULL;
  120. dtmp = EVP_PKEY_get1_EC_KEY(key);
  121. EVP_PKEY_free(key);
  122. if (!dtmp)
  123. return NULL;
  124. if (eckey) {
  125. EC_KEY_free(*eckey);
  126. *eckey = dtmp;
  127. }
  128. return dtmp;
  129. }
  130. EC_KEY *PEM_read_bio_ECPrivateKey(BIO *bp, EC_KEY **key, pem_password_cb *cb,
  131. void *u)
  132. {
  133. EVP_PKEY *pktmp;
  134. pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
  135. return pkey_get_eckey(pktmp, key); /* will free pktmp */
  136. }
  137. IMPLEMENT_PEM_rw_const(ECPKParameters, EC_GROUP, PEM_STRING_ECPARAMETERS,
  138. ECPKParameters)
  139. IMPLEMENT_PEM_write_cb(ECPrivateKey, EC_KEY, PEM_STRING_ECPRIVATEKEY,
  140. ECPrivateKey)
  141. IMPLEMENT_PEM_rw(EC_PUBKEY, EC_KEY, PEM_STRING_PUBLIC, EC_PUBKEY)
  142. # ifndef OPENSSL_NO_STDIO
  143. EC_KEY *PEM_read_ECPrivateKey(FILE *fp, EC_KEY **eckey, pem_password_cb *cb,
  144. void *u)
  145. {
  146. EVP_PKEY *pktmp;
  147. pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
  148. return pkey_get_eckey(pktmp, eckey); /* will free pktmp */
  149. }
  150. # endif
  151. #endif
  152. #ifndef OPENSSL_NO_DH
  153. IMPLEMENT_PEM_write_const(DHparams, DH, PEM_STRING_DHPARAMS, DHparams)
  154. IMPLEMENT_PEM_write_const(DHxparams, DH, PEM_STRING_DHXPARAMS, DHxparams)
  155. #endif
  156. IMPLEMENT_PEM_rw(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY)