PEM: constify PEM_write_ routines
[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
21 #ifndef OPENSSL_NO_RSA
22 static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa);
23 #endif
24 #ifndef OPENSSL_NO_DSA
25 static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa);
26 #endif
27
28 #ifndef OPENSSL_NO_EC
29 static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey);
30 #endif
31
32 IMPLEMENT_PEM_rw(X509_REQ, X509_REQ, PEM_STRING_X509_REQ, X509_REQ)
33
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)
37
38 IMPLEMENT_PEM_rw(NETSCAPE_CERT_SEQUENCE, NETSCAPE_CERT_SEQUENCE,
39                  PEM_STRING_X509, NETSCAPE_CERT_SEQUENCE)
40 #ifndef OPENSSL_NO_RSA
41 /*
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.
46  */
47 static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa)
48 {
49     RSA *rtmp;
50     if (!key)
51         return NULL;
52     rtmp = EVP_PKEY_get1_RSA(key);
53     EVP_PKEY_free(key);
54     if (!rtmp)
55         return NULL;
56     if (rsa) {
57         RSA_free(*rsa);
58         *rsa = rtmp;
59     }
60     return rtmp;
61 }
62
63 RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **rsa, pem_password_cb *cb,
64                                 void *u)
65 {
66     EVP_PKEY *pktmp;
67     pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
68     return pkey_get_rsa(pktmp, rsa);
69 }
70
71 # ifndef OPENSSL_NO_STDIO
72
73 RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **rsa, pem_password_cb *cb, void *u)
74 {
75     EVP_PKEY *pktmp;
76     pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
77     return pkey_get_rsa(pktmp, rsa);
78 }
79
80 # endif
81
82 IMPLEMENT_PEM_write_cb(RSAPrivateKey, RSA, PEM_STRING_RSA, RSAPrivateKey)
83 IMPLEMENT_PEM_rw(RSAPublicKey, RSA, PEM_STRING_RSA_PUBLIC, RSAPublicKey)
84 IMPLEMENT_PEM_rw(RSA_PUBKEY, RSA, PEM_STRING_PUBLIC, RSA_PUBKEY)
85 #endif
86 #ifndef OPENSSL_NO_DSA
87 static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa)
88 {
89     DSA *dtmp;
90     if (!key)
91         return NULL;
92     dtmp = EVP_PKEY_get1_DSA(key);
93     EVP_PKEY_free(key);
94     if (!dtmp)
95         return NULL;
96     if (dsa) {
97         DSA_free(*dsa);
98         *dsa = dtmp;
99     }
100     return dtmp;
101 }
102
103 DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **dsa, pem_password_cb *cb,
104                                 void *u)
105 {
106     EVP_PKEY *pktmp;
107     pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
108     return pkey_get_dsa(pktmp, dsa); /* will free pktmp */
109 }
110
111 IMPLEMENT_PEM_write_cb(DSAPrivateKey, DSA, PEM_STRING_DSA, DSAPrivateKey)
112 IMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY)
113 # ifndef OPENSSL_NO_STDIO
114 DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **dsa, pem_password_cb *cb, void *u)
115 {
116     EVP_PKEY *pktmp;
117     pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
118     return pkey_get_dsa(pktmp, dsa); /* will free pktmp */
119 }
120
121 # endif
122
123 IMPLEMENT_PEM_rw(DSAparams, DSA, PEM_STRING_DSAPARAMS, DSAparams)
124 #endif
125 #ifndef OPENSSL_NO_EC
126 static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey)
127 {
128     EC_KEY *dtmp;
129     if (!key)
130         return NULL;
131     dtmp = EVP_PKEY_get1_EC_KEY(key);
132     EVP_PKEY_free(key);
133     if (!dtmp)
134         return NULL;
135     if (eckey) {
136         EC_KEY_free(*eckey);
137         *eckey = dtmp;
138     }
139     return dtmp;
140 }
141
142 EC_KEY *PEM_read_bio_ECPrivateKey(BIO *bp, EC_KEY **key, pem_password_cb *cb,
143                                   void *u)
144 {
145     EVP_PKEY *pktmp;
146     pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
147     return pkey_get_eckey(pktmp, key); /* will free pktmp */
148 }
149
150 IMPLEMENT_PEM_rw(ECPKParameters, EC_GROUP, PEM_STRING_ECPARAMETERS,
151                  ECPKParameters)
152
153
154 IMPLEMENT_PEM_write_cb(ECPrivateKey, EC_KEY, PEM_STRING_ECPRIVATEKEY,
155                        ECPrivateKey)
156 IMPLEMENT_PEM_rw(EC_PUBKEY, EC_KEY, PEM_STRING_PUBLIC, EC_PUBKEY)
157 # ifndef OPENSSL_NO_STDIO
158 EC_KEY *PEM_read_ECPrivateKey(FILE *fp, EC_KEY **eckey, pem_password_cb *cb,
159                               void *u)
160 {
161     EVP_PKEY *pktmp;
162     pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
163     return pkey_get_eckey(pktmp, eckey); /* will free pktmp */
164 }
165
166 # endif
167
168 #endif
169
170 #ifndef OPENSSL_NO_DH
171
172 IMPLEMENT_PEM_write(DHparams, DH, PEM_STRING_DHPARAMS, DHparams)
173 IMPLEMENT_PEM_write(DHxparams, DH, PEM_STRING_DHXPARAMS, DHxparams)
174 #endif
175 IMPLEMENT_PEM_rw(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY)