VirtualBox

source: vbox/trunk/src/libs/openssl-1.1.1f/crypto/pem/pem_all.c@ 83531

Last change on this file since 83531 was 83531, checked in by vboxsync, 5 years ago

setting svn:sync-process=export for openssl-1.1.1f, all files except tests

File size: 4.9 KB
Line 
1/*
2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (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
22static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa);
23#endif
24#ifndef OPENSSL_NO_DSA
25static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa);
26#endif
27
28#ifndef OPENSSL_NO_EC
29static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey);
30#endif
31
32IMPLEMENT_PEM_rw(X509_REQ, X509_REQ, PEM_STRING_X509_REQ, X509_REQ)
33
34IMPLEMENT_PEM_write(X509_REQ_NEW, X509_REQ, PEM_STRING_X509_REQ_OLD, X509_REQ)
35IMPLEMENT_PEM_rw(X509_CRL, X509_CRL, PEM_STRING_X509_CRL, X509_CRL)
36IMPLEMENT_PEM_rw(PKCS7, PKCS7, PEM_STRING_PKCS7, PKCS7)
37
38IMPLEMENT_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 */
47static 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
63RSA *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
73RSA *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
82IMPLEMENT_PEM_write_cb_const(RSAPrivateKey, RSA, PEM_STRING_RSA,
83 RSAPrivateKey)
84
85
86IMPLEMENT_PEM_rw_const(RSAPublicKey, RSA, PEM_STRING_RSA_PUBLIC,
87 RSAPublicKey)
88IMPLEMENT_PEM_rw(RSA_PUBKEY, RSA, PEM_STRING_PUBLIC, RSA_PUBKEY)
89#endif
90#ifndef OPENSSL_NO_DSA
91static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa)
92{
93 DSA *dtmp;
94 if (!key)
95 return NULL;
96 dtmp = EVP_PKEY_get1_DSA(key);
97 EVP_PKEY_free(key);
98 if (!dtmp)
99 return NULL;
100 if (dsa) {
101 DSA_free(*dsa);
102 *dsa = dtmp;
103 }
104 return dtmp;
105}
106
107DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **dsa, pem_password_cb *cb,
108 void *u)
109{
110 EVP_PKEY *pktmp;
111 pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
112 return pkey_get_dsa(pktmp, dsa); /* will free pktmp */
113}
114
115IMPLEMENT_PEM_write_cb_const(DSAPrivateKey, DSA, PEM_STRING_DSA,
116 DSAPrivateKey)
117IMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY)
118# ifndef OPENSSL_NO_STDIO
119DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **dsa, pem_password_cb *cb, void *u)
120{
121 EVP_PKEY *pktmp;
122 pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
123 return pkey_get_dsa(pktmp, dsa); /* will free pktmp */
124}
125
126# endif
127
128IMPLEMENT_PEM_rw_const(DSAparams, DSA, PEM_STRING_DSAPARAMS, DSAparams)
129#endif
130#ifndef OPENSSL_NO_EC
131static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey)
132{
133 EC_KEY *dtmp;
134 if (!key)
135 return NULL;
136 dtmp = EVP_PKEY_get1_EC_KEY(key);
137 EVP_PKEY_free(key);
138 if (!dtmp)
139 return NULL;
140 if (eckey) {
141 EC_KEY_free(*eckey);
142 *eckey = dtmp;
143 }
144 return dtmp;
145}
146
147EC_KEY *PEM_read_bio_ECPrivateKey(BIO *bp, EC_KEY **key, pem_password_cb *cb,
148 void *u)
149{
150 EVP_PKEY *pktmp;
151 pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
152 return pkey_get_eckey(pktmp, key); /* will free pktmp */
153}
154
155IMPLEMENT_PEM_rw_const(ECPKParameters, EC_GROUP, PEM_STRING_ECPARAMETERS,
156 ECPKParameters)
157
158
159IMPLEMENT_PEM_write_cb(ECPrivateKey, EC_KEY, PEM_STRING_ECPRIVATEKEY,
160 ECPrivateKey)
161IMPLEMENT_PEM_rw(EC_PUBKEY, EC_KEY, PEM_STRING_PUBLIC, EC_PUBKEY)
162# ifndef OPENSSL_NO_STDIO
163EC_KEY *PEM_read_ECPrivateKey(FILE *fp, EC_KEY **eckey, pem_password_cb *cb,
164 void *u)
165{
166 EVP_PKEY *pktmp;
167 pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
168 return pkey_get_eckey(pktmp, eckey); /* will free pktmp */
169}
170
171# endif
172
173#endif
174
175#ifndef OPENSSL_NO_DH
176
177IMPLEMENT_PEM_write_const(DHparams, DH, PEM_STRING_DHPARAMS, DHparams)
178IMPLEMENT_PEM_write_const(DHxparams, DH, PEM_STRING_DHXPARAMS, DHxparams)
179#endif
180IMPLEMENT_PEM_rw(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY)
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette