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