2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/bio.h>
13 #include <openssl/evp.h>
14 #include <openssl/x509.h>
15 #include <openssl/pkcs7.h>
16 #include <openssl/pem.h>
17 #include <openssl/rsa.h>
18 #include <openssl/dsa.h>
19 #include <openssl/dh.h>
21 #ifndef OPENSSL_NO_RSA
22 static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa);
24 #ifndef OPENSSL_NO_DSA
25 static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa);
29 static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey);
32 IMPLEMENT_PEM_rw(X509_REQ, X509_REQ, PEM_STRING_X509_REQ, X509_REQ)
34 IMPLEMENT_PEM_write(X509_REQ_NEW, X509_REQ, PEM_STRING_X509_REQ_OLD, X509_REQ)
35 IMPLEMENT_PEM_rw(X509_CRL, X509_CRL, PEM_STRING_X509_CRL, X509_CRL)
36 IMPLEMENT_PEM_rw(PKCS7, PKCS7, PEM_STRING_PKCS7, PKCS7)
38 IMPLEMENT_PEM_rw(NETSCAPE_CERT_SEQUENCE, NETSCAPE_CERT_SEQUENCE,
39 PEM_STRING_X509, NETSCAPE_CERT_SEQUENCE)
40 #ifndef OPENSSL_NO_RSA
42 * We treat RSA or DSA private keys as a special case. For private keys we
43 * read in an EVP_PKEY structure with PEM_read_bio_PrivateKey() and extract
44 * the relevant private key: this means can handle "traditional" and PKCS#8
45 * formats transparently.
47 static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa)
52 rtmp = EVP_PKEY_get1_RSA(key);
63 RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **rsa, pem_password_cb *cb,
67 pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
68 return pkey_get_rsa(pktmp, rsa);
71 # ifndef OPENSSL_NO_STDIO
73 RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **rsa, pem_password_cb *cb, void *u)
76 pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
77 return pkey_get_rsa(pktmp, rsa);
82 IMPLEMENT_PEM_write_cb_const(RSAPrivateKey, RSA, PEM_STRING_RSA,
86 IMPLEMENT_PEM_rw_const(RSAPublicKey, RSA, PEM_STRING_RSA_PUBLIC,
87 RSAPublicKey) IMPLEMENT_PEM_rw(RSA_PUBKEY, RSA,
91 #ifndef OPENSSL_NO_DSA
92 static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa)
97 dtmp = EVP_PKEY_get1_DSA(key);
108 DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **dsa, pem_password_cb *cb,
112 pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
113 return pkey_get_dsa(pktmp, dsa); /* will free pktmp */
116 IMPLEMENT_PEM_write_cb_const(DSAPrivateKey, DSA, PEM_STRING_DSA,
118 IMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY)
119 # ifndef OPENSSL_NO_STDIO
120 DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **dsa, pem_password_cb *cb, void *u)
123 pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
124 return pkey_get_dsa(pktmp, dsa); /* will free pktmp */
129 IMPLEMENT_PEM_rw_const(DSAparams, DSA, PEM_STRING_DSAPARAMS, DSAparams)
131 #ifndef OPENSSL_NO_EC
132 static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey)
137 dtmp = EVP_PKEY_get1_EC_KEY(key);
148 EC_KEY *PEM_read_bio_ECPrivateKey(BIO *bp, EC_KEY **key, pem_password_cb *cb,
152 pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
153 return pkey_get_eckey(pktmp, key); /* will free pktmp */
156 IMPLEMENT_PEM_rw_const(ECPKParameters, EC_GROUP, PEM_STRING_ECPARAMETERS,
160 IMPLEMENT_PEM_write_cb(ECPrivateKey, EC_KEY, PEM_STRING_ECPRIVATEKEY,
162 IMPLEMENT_PEM_rw(EC_PUBKEY, EC_KEY, PEM_STRING_PUBLIC, EC_PUBKEY)
163 # ifndef OPENSSL_NO_STDIO
164 EC_KEY *PEM_read_ECPrivateKey(FILE *fp, EC_KEY **eckey, pem_password_cb *cb,
168 pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
169 return pkey_get_eckey(pktmp, eckey); /* will free pktmp */
176 #ifndef OPENSSL_NO_DH
178 IMPLEMENT_PEM_write_const(DHparams, DH, PEM_STRING_DHPARAMS, DHparams)
179 IMPLEMENT_PEM_write_const(DHxparams, DH, PEM_STRING_DHXPARAMS, DHxparams)
181 IMPLEMENT_PEM_rw(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY)