1 // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <stdio.h>
16
17 #include <openssl/bio.h>
18 #include <openssl/dh.h>
19 #include <openssl/dsa.h>
20 #include <openssl/evp.h>
21 #include <openssl/pem.h>
22 #include <openssl/pkcs7.h>
23 #include <openssl/rsa.h>
24 #include <openssl/x509.h>
25
26 static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa);
27 static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa);
28 static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey);
29
IMPLEMENT_PEM_rw(X509_REQ,X509_REQ,PEM_STRING_X509_REQ,X509_REQ)30 IMPLEMENT_PEM_rw(X509_REQ, X509_REQ, PEM_STRING_X509_REQ, X509_REQ)
31
32 IMPLEMENT_PEM_write(X509_REQ_NEW, X509_REQ, PEM_STRING_X509_REQ_OLD, X509_REQ)
33 IMPLEMENT_PEM_rw(X509_CRL, X509_CRL, PEM_STRING_X509_CRL, X509_CRL)
34 IMPLEMENT_PEM_rw(PKCS7, PKCS7, PEM_STRING_PKCS7, PKCS7)
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 static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa) {
41 RSA *rtmp;
42 if (!key) {
43 return NULL;
44 }
45 rtmp = EVP_PKEY_get1_RSA(key);
46 EVP_PKEY_free(key);
47 if (!rtmp) {
48 return NULL;
49 }
50 if (rsa) {
51 RSA_free(*rsa);
52 *rsa = rtmp;
53 }
54 return rtmp;
55 }
56
PEM_read_bio_RSAPrivateKey(BIO * bp,RSA ** rsa,pem_password_cb * cb,void * u)57 RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **rsa, pem_password_cb *cb,
58 void *u) {
59 EVP_PKEY *pktmp;
60 pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
61 return pkey_get_rsa(pktmp, rsa);
62 }
63
PEM_read_RSAPrivateKey(FILE * fp,RSA ** rsa,pem_password_cb * cb,void * u)64 RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **rsa, pem_password_cb *cb, void *u) {
65 EVP_PKEY *pktmp;
66 pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
67 return pkey_get_rsa(pktmp, rsa);
68 }
69
IMPLEMENT_PEM_write_cb_const(RSAPrivateKey,RSA,PEM_STRING_RSA,RSAPrivateKey)70 IMPLEMENT_PEM_write_cb_const(RSAPrivateKey, RSA, PEM_STRING_RSA, RSAPrivateKey)
71
72
73 IMPLEMENT_PEM_rw_const(RSAPublicKey, RSA, PEM_STRING_RSA_PUBLIC, RSAPublicKey)
74 IMPLEMENT_PEM_rw(RSA_PUBKEY, RSA, PEM_STRING_PUBLIC, RSA_PUBKEY)
75 #ifndef OPENSSL_NO_DSA
76 static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa) {
77 DSA *dtmp;
78 if (!key) {
79 return NULL;
80 }
81 dtmp = EVP_PKEY_get1_DSA(key);
82 EVP_PKEY_free(key);
83 if (!dtmp) {
84 return NULL;
85 }
86 if (dsa) {
87 DSA_free(*dsa);
88 *dsa = dtmp;
89 }
90 return dtmp;
91 }
92
PEM_read_bio_DSAPrivateKey(BIO * bp,DSA ** dsa,pem_password_cb * cb,void * u)93 DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **dsa, pem_password_cb *cb,
94 void *u) {
95 EVP_PKEY *pktmp;
96 pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
97 return pkey_get_dsa(pktmp, dsa); // will free pktmp
98 }
99
IMPLEMENT_PEM_write_cb_const(DSAPrivateKey,DSA,PEM_STRING_DSA,DSAPrivateKey)100 IMPLEMENT_PEM_write_cb_const(DSAPrivateKey, DSA, PEM_STRING_DSA, DSAPrivateKey)
101
102 IMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY)
103 DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **dsa, pem_password_cb *cb, void *u) {
104 EVP_PKEY *pktmp;
105 pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
106 return pkey_get_dsa(pktmp, dsa); // will free pktmp
107 }
108
IMPLEMENT_PEM_rw_const(DSAparams,DSA,PEM_STRING_DSAPARAMS,DSAparams)109 IMPLEMENT_PEM_rw_const(DSAparams, DSA, PEM_STRING_DSAPARAMS, DSAparams)
110 #endif
111 static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey) {
112 EC_KEY *dtmp;
113 if (!key) {
114 return NULL;
115 }
116 dtmp = EVP_PKEY_get1_EC_KEY(key);
117 EVP_PKEY_free(key);
118 if (!dtmp) {
119 return NULL;
120 }
121 if (eckey) {
122 EC_KEY_free(*eckey);
123 *eckey = dtmp;
124 }
125 return dtmp;
126 }
127
PEM_read_bio_ECPrivateKey(BIO * bp,EC_KEY ** key,pem_password_cb * cb,void * u)128 EC_KEY *PEM_read_bio_ECPrivateKey(BIO *bp, EC_KEY **key, pem_password_cb *cb,
129 void *u) {
130 EVP_PKEY *pktmp;
131 pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
132 return pkey_get_eckey(pktmp, key); // will free pktmp
133 }
134
IMPLEMENT_PEM_write_cb(ECPrivateKey,EC_KEY,PEM_STRING_ECPRIVATEKEY,ECPrivateKey)135 IMPLEMENT_PEM_write_cb(ECPrivateKey, EC_KEY, PEM_STRING_ECPRIVATEKEY,
136 ECPrivateKey)
137
138 IMPLEMENT_PEM_rw(EC_PUBKEY, EC_KEY, PEM_STRING_PUBLIC, EC_PUBKEY)
139 EC_KEY *PEM_read_ECPrivateKey(FILE *fp, EC_KEY **eckey, pem_password_cb *cb,
140 void *u) {
141 EVP_PKEY *pktmp;
142 pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
143 return pkey_get_eckey(pktmp, eckey); // will free pktmp
144 }
145
146
147 IMPLEMENT_PEM_rw_const(DHparams, DH, PEM_STRING_DHPARAMS, DHparams)
148
149 IMPLEMENT_PEM_rw(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY)
150