Convert all {NAME}err() in crypto/ to their corresponding ERR_raise() call
[openssl.git] / crypto / pem / pem_all.c
1 /*
2  * Copyright 1995-2020 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 /*
11  * DSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include "internal/cryptlib.h"
18 #include <openssl/bio.h>
19 #include <openssl/evp.h>
20 #include <openssl/x509.h>
21 #include <openssl/pkcs7.h>
22 #include <openssl/pem.h>
23 #include <openssl/rsa.h>
24 #include <openssl/dsa.h>
25 #include <openssl/dh.h>
26 #include "pem_local.h"
27
28 #ifndef OPENSSL_NO_RSA
29 static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa);
30 #endif
31 #ifndef OPENSSL_NO_DSA
32 static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa);
33 #endif
34
35 #ifndef OPENSSL_NO_EC
36 static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey);
37 #endif
38
39 IMPLEMENT_PEM_rw(X509_REQ, X509_REQ, PEM_STRING_X509_REQ, X509_REQ)
40
41 IMPLEMENT_PEM_write(X509_REQ_NEW, X509_REQ, PEM_STRING_X509_REQ_OLD, X509_REQ)
42 IMPLEMENT_PEM_rw(X509_CRL, X509_CRL, PEM_STRING_X509_CRL, X509_CRL)
43 IMPLEMENT_PEM_rw(X509_PUBKEY, X509_PUBKEY, PEM_STRING_PUBLIC, X509_PUBKEY)
44 IMPLEMENT_PEM_rw(PKCS7, PKCS7, PEM_STRING_PKCS7, PKCS7)
45
46 IMPLEMENT_PEM_rw(NETSCAPE_CERT_SEQUENCE, NETSCAPE_CERT_SEQUENCE,
47                  PEM_STRING_X509, NETSCAPE_CERT_SEQUENCE)
48 #ifndef OPENSSL_NO_RSA
49 /*
50  * We treat RSA or DSA private keys as a special case. For private keys we
51  * read in an EVP_PKEY structure with PEM_read_bio_PrivateKey() and extract
52  * the relevant private key: this means can handle "traditional" and PKCS#8
53  * formats transparently.
54  */
55 static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa)
56 {
57     RSA *rtmp;
58     if (!key)
59         return NULL;
60     rtmp = EVP_PKEY_get1_RSA(key);
61     EVP_PKEY_free(key);
62     if (!rtmp)
63         return NULL;
64     if (rsa) {
65         RSA_free(*rsa);
66         *rsa = rtmp;
67     }
68     return rtmp;
69 }
70
71 RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **rsa, pem_password_cb *cb,
72                                 void *u)
73 {
74     EVP_PKEY *pktmp;
75     pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
76     return pkey_get_rsa(pktmp, rsa);
77 }
78
79 # ifndef OPENSSL_NO_STDIO
80
81 RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **rsa, pem_password_cb *cb, void *u)
82 {
83     EVP_PKEY *pktmp;
84     pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
85     return pkey_get_rsa(pktmp, rsa);
86 }
87
88 # endif
89
90 IMPLEMENT_PEM_write_cb(RSAPrivateKey, RSA, PEM_STRING_RSA, RSAPrivateKey)
91 IMPLEMENT_PEM_rw(RSAPublicKey, RSA, PEM_STRING_RSA_PUBLIC, RSAPublicKey)
92 IMPLEMENT_PEM_rw(RSA_PUBKEY, RSA, PEM_STRING_PUBLIC, RSA_PUBKEY)
93 #endif
94 #ifndef OPENSSL_NO_DSA
95 static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa)
96 {
97     DSA *dtmp;
98     if (!key)
99         return NULL;
100     dtmp = EVP_PKEY_get1_DSA(key);
101     EVP_PKEY_free(key);
102     if (!dtmp)
103         return NULL;
104     if (dsa) {
105         DSA_free(*dsa);
106         *dsa = dtmp;
107     }
108     return dtmp;
109 }
110
111 DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **dsa, pem_password_cb *cb,
112                                 void *u)
113 {
114     EVP_PKEY *pktmp;
115     pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
116     return pkey_get_dsa(pktmp, dsa); /* will free pktmp */
117 }
118
119 IMPLEMENT_PEM_write_cb(DSAPrivateKey, DSA, PEM_STRING_DSA, DSAPrivateKey)
120 IMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY)
121 # ifndef OPENSSL_NO_STDIO
122 DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **dsa, pem_password_cb *cb, void *u)
123 {
124     EVP_PKEY *pktmp;
125     pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
126     return pkey_get_dsa(pktmp, dsa); /* will free pktmp */
127 }
128
129 # endif
130
131 IMPLEMENT_PEM_rw(DSAparams, DSA, PEM_STRING_DSAPARAMS, DSAparams)
132 #endif
133 #ifndef OPENSSL_NO_EC
134 static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey)
135 {
136     EC_KEY *dtmp;
137     if (!key)
138         return NULL;
139     dtmp = EVP_PKEY_get1_EC_KEY(key);
140     EVP_PKEY_free(key);
141     if (!dtmp)
142         return NULL;
143     if (eckey) {
144         EC_KEY_free(*eckey);
145         *eckey = dtmp;
146     }
147     return dtmp;
148 }
149
150 EC_KEY *PEM_read_bio_ECPrivateKey(BIO *bp, EC_KEY **key, pem_password_cb *cb,
151                                   void *u)
152 {
153     EVP_PKEY *pktmp;
154     pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
155     return pkey_get_eckey(pktmp, key); /* will free pktmp */
156 }
157
158 IMPLEMENT_PEM_rw(ECPKParameters, EC_GROUP, PEM_STRING_ECPARAMETERS,
159                  ECPKParameters)
160
161
162 IMPLEMENT_PEM_write_cb(ECPrivateKey, EC_KEY, PEM_STRING_ECPRIVATEKEY,
163                        ECPrivateKey)
164 IMPLEMENT_PEM_rw(EC_PUBKEY, EC_KEY, PEM_STRING_PUBLIC, EC_PUBKEY)
165 # ifndef OPENSSL_NO_STDIO
166 EC_KEY *PEM_read_ECPrivateKey(FILE *fp, EC_KEY **eckey, pem_password_cb *cb,
167                               void *u)
168 {
169     EVP_PKEY *pktmp;
170     pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
171     return pkey_get_eckey(pktmp, eckey); /* will free pktmp */
172 }
173
174 # endif
175
176 #endif
177
178 #ifndef OPENSSL_NO_DH
179
180 IMPLEMENT_PEM_write(DHparams, DH, PEM_STRING_DHPARAMS, DHparams)
181 IMPLEMENT_PEM_write(DHxparams, DH, PEM_STRING_DHXPARAMS, DHxparams)
182
183 /* Transparently read in PKCS#3 or X9.42 DH parameters */
184
185 DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u)
186 {
187     char *nm = NULL;
188     const unsigned char *p = NULL;
189     unsigned char *data = NULL;
190     long len;
191     DH *ret = NULL;
192
193     if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_DHPARAMS, bp, cb, u))
194         return NULL;
195     p = data;
196
197     if (strcmp(nm, PEM_STRING_DHXPARAMS) == 0)
198         ret = d2i_DHxparams(x, &p, len);
199     else
200         ret = d2i_DHparams(x, &p, len);
201
202     if (ret == NULL)
203         ERR_raise(ERR_LIB_PEM, ERR_R_ASN1_LIB);
204     OPENSSL_free(nm);
205     OPENSSL_free(data);
206     return ret;
207 }
208
209 # ifndef OPENSSL_NO_STDIO
210 DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u)
211 {
212     BIO *b;
213     DH *ret;
214
215     if ((b = BIO_new(BIO_s_file())) == NULL) {
216         ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
217         return 0;
218     }
219     BIO_set_fp(b, fp, BIO_NOCLOSE);
220     ret = PEM_read_bio_DHparams(b, x, cb, u);
221     BIO_free(b);
222     return ret;
223 }
224 # endif
225
226 #endif
227 IMPLEMENT_PEM_provided_write(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY)