2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 * DSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.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"
28 #ifndef OPENSSL_NO_RSA
29 static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa);
31 #ifndef OPENSSL_NO_DSA
32 static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa);
36 static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey);
39 IMPLEMENT_PEM_rw(X509_REQ, X509_REQ, PEM_STRING_X509_REQ, X509_REQ)
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)
46 IMPLEMENT_PEM_rw(NETSCAPE_CERT_SEQUENCE, NETSCAPE_CERT_SEQUENCE,
47 PEM_STRING_X509, NETSCAPE_CERT_SEQUENCE)
48 #ifndef OPENSSL_NO_RSA
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.
55 static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa)
60 rtmp = EVP_PKEY_get1_RSA(key);
71 RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **rsa, pem_password_cb *cb,
75 pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
76 return pkey_get_rsa(pktmp, rsa);
79 # ifndef OPENSSL_NO_STDIO
81 RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **rsa, pem_password_cb *cb, void *u)
84 pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
85 return pkey_get_rsa(pktmp, rsa);
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)
94 #ifndef OPENSSL_NO_DSA
95 static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa)
100 dtmp = EVP_PKEY_get1_DSA(key);
111 DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **dsa, pem_password_cb *cb,
115 pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
116 return pkey_get_dsa(pktmp, dsa); /* will free pktmp */
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)
125 pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
126 return pkey_get_dsa(pktmp, dsa); /* will free pktmp */
131 IMPLEMENT_PEM_rw(DSAparams, DSA, PEM_STRING_DSAPARAMS, DSAparams)
133 #ifndef OPENSSL_NO_EC
134 static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey)
139 dtmp = EVP_PKEY_get1_EC_KEY(key);
150 EC_KEY *PEM_read_bio_ECPrivateKey(BIO *bp, EC_KEY **key, pem_password_cb *cb,
154 pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
155 return pkey_get_eckey(pktmp, key); /* will free pktmp */
158 IMPLEMENT_PEM_rw(ECPKParameters, EC_GROUP, PEM_STRING_ECPARAMETERS,
162 IMPLEMENT_PEM_write_cb(ECPrivateKey, EC_KEY, PEM_STRING_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,
170 pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
171 return pkey_get_eckey(pktmp, eckey); /* will free pktmp */
178 #ifndef OPENSSL_NO_DH
180 IMPLEMENT_PEM_write(DHparams, DH, PEM_STRING_DHPARAMS, DHparams)
181 IMPLEMENT_PEM_write(DHxparams, DH, PEM_STRING_DHXPARAMS, DHxparams)
183 IMPLEMENT_PEM_provided_write(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY)
184 IMPLEMENT_PEM_read(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY)