155714Skris/* crypto/pem/pem_all.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8296465Sdelphij *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15296465Sdelphij *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22296465Sdelphij *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37296465Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40296465Sdelphij *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52296465Sdelphij *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
58160814Ssimon/* ====================================================================
59160814Ssimon * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
60160814Ssimon *
61160814Ssimon * Redistribution and use in source and binary forms, with or without
62160814Ssimon * modification, are permitted provided that the following conditions
63160814Ssimon * are met:
64160814Ssimon *
65160814Ssimon * 1. Redistributions of source code must retain the above copyright
66296465Sdelphij *    notice, this list of conditions and the following disclaimer.
67160814Ssimon *
68160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright
69160814Ssimon *    notice, this list of conditions and the following disclaimer in
70160814Ssimon *    the documentation and/or other materials provided with the
71160814Ssimon *    distribution.
72160814Ssimon *
73160814Ssimon * 3. All advertising materials mentioning features or use of this
74160814Ssimon *    software must display the following acknowledgment:
75160814Ssimon *    "This product includes software developed by the OpenSSL Project
76160814Ssimon *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77160814Ssimon *
78160814Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79160814Ssimon *    endorse or promote products derived from this software without
80160814Ssimon *    prior written permission. For written permission, please contact
81160814Ssimon *    openssl-core@openssl.org.
82160814Ssimon *
83160814Ssimon * 5. Products derived from this software may not be called "OpenSSL"
84160814Ssimon *    nor may "OpenSSL" appear in their names without prior written
85160814Ssimon *    permission of the OpenSSL Project.
86160814Ssimon *
87160814Ssimon * 6. Redistributions of any form whatsoever must retain the following
88160814Ssimon *    acknowledgment:
89160814Ssimon *    "This product includes software developed by the OpenSSL Project
90160814Ssimon *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91160814Ssimon *
92160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93160814Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95160814Ssimon * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96160814Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97160814Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99160814Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101160814Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102160814Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103160814Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE.
104160814Ssimon * ====================================================================
105160814Ssimon *
106160814Ssimon * This product includes cryptographic software written by Eric Young
107160814Ssimon * (eay@cryptsoft.com).  This product includes software written by Tim
108160814Ssimon * Hudson (tjh@cryptsoft.com).
109160814Ssimon *
110160814Ssimon */
11155714Skris
11255714Skris#include <stdio.h>
11355714Skris#undef SSLEAY_MACROS
11455714Skris#include "cryptlib.h"
11555714Skris#include <openssl/bio.h>
11655714Skris#include <openssl/evp.h>
11755714Skris#include <openssl/x509.h>
11855714Skris#include <openssl/pkcs7.h>
11955714Skris#include <openssl/pem.h>
120160814Ssimon#ifndef OPENSSL_NO_RSA
121296465Sdelphij# include <openssl/rsa.h>
122160814Ssimon#endif
123160814Ssimon#ifndef OPENSSL_NO_DSA
124296465Sdelphij# include <openssl/dsa.h>
125160814Ssimon#endif
126160814Ssimon#ifndef OPENSSL_NO_DH
127296465Sdelphij# include <openssl/dh.h>
128160814Ssimon#endif
12955714Skris
130109998Smarkm#ifndef OPENSSL_NO_RSA
13159191Skrisstatic RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa);
13259191Skris#endif
133109998Smarkm#ifndef OPENSSL_NO_DSA
13459191Skrisstatic DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa);
13559191Skris#endif
13659191Skris
137160814Ssimon#ifndef OPENSSL_NO_EC
138160814Ssimonstatic EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey);
139160814Ssimon#endif
140160814Ssimon
14155714SkrisIMPLEMENT_PEM_rw(X509_REQ, X509_REQ, PEM_STRING_X509_REQ, X509_REQ)
14255714Skris
14359191SkrisIMPLEMENT_PEM_write(X509_REQ_NEW, X509_REQ, PEM_STRING_X509_REQ_OLD, X509_REQ)
14455714SkrisIMPLEMENT_PEM_rw(X509_CRL, X509_CRL, PEM_STRING_X509_CRL, X509_CRL)
14555714SkrisIMPLEMENT_PEM_rw(PKCS7, PKCS7, PEM_STRING_PKCS7, PKCS7)
14655714Skris
14755714SkrisIMPLEMENT_PEM_rw(NETSCAPE_CERT_SEQUENCE, NETSCAPE_CERT_SEQUENCE,
148296465Sdelphij                 PEM_STRING_X509, NETSCAPE_CERT_SEQUENCE)
149109998Smarkm#ifndef OPENSSL_NO_RSA
150296465Sdelphij/*
151296465Sdelphij * We treat RSA or DSA private keys as a special case. For private keys we
152296465Sdelphij * read in an EVP_PKEY structure with PEM_read_bio_PrivateKey() and extract
153296465Sdelphij * the relevant private key: this means can handle "traditional" and PKCS#8
154296465Sdelphij * formats transparently.
15559191Skris */
15659191Skrisstatic RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa)
15759191Skris{
158296465Sdelphij    RSA *rtmp;
159296465Sdelphij    if (!key)
160296465Sdelphij        return NULL;
161296465Sdelphij    rtmp = EVP_PKEY_get1_RSA(key);
162296465Sdelphij    EVP_PKEY_free(key);
163296465Sdelphij    if (!rtmp)
164296465Sdelphij        return NULL;
165296465Sdelphij    if (rsa) {
166296465Sdelphij        RSA_free(*rsa);
167296465Sdelphij        *rsa = rtmp;
168296465Sdelphij    }
169296465Sdelphij    return rtmp;
17059191Skris}
17159191Skris
17259191SkrisRSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **rsa, pem_password_cb *cb,
173296465Sdelphij                                void *u)
17459191Skris{
175296465Sdelphij    EVP_PKEY *pktmp;
176296465Sdelphij    pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
177296465Sdelphij    return pkey_get_rsa(pktmp, rsa);
17859191Skris}
17959191Skris
180296465Sdelphij# ifndef OPENSSL_NO_FP_API
18159191Skris
182296465SdelphijRSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **rsa, pem_password_cb *cb, void *u)
18359191Skris{
184296465Sdelphij    EVP_PKEY *pktmp;
185296465Sdelphij    pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
186296465Sdelphij    return pkey_get_rsa(pktmp, rsa);
18759191Skris}
18859191Skris
189296465Sdelphij# endif
19059191Skris
191296465Sdelphij# ifdef OPENSSL_FIPS
192194206Ssimon
193194206Ssimonint PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER *enc,
194296465Sdelphij                                unsigned char *kstr, int klen,
195296465Sdelphij                                pem_password_cb *cb, void *u)
196194206Ssimon{
197296465Sdelphij    EVP_PKEY *k;
198296465Sdelphij    int ret;
199296465Sdelphij    k = EVP_PKEY_new();
200296465Sdelphij    if (!k)
201296465Sdelphij        return 0;
202296465Sdelphij    EVP_PKEY_set1_RSA(k, x);
203194206Ssimon
204296465Sdelphij    ret = PEM_write_bio_PrivateKey(bp, k, enc, kstr, klen, cb, u);
205296465Sdelphij    EVP_PKEY_free(k);
206296465Sdelphij    return ret;
207194206Ssimon}
208194206Ssimon
209296465Sdelphij#  ifndef OPENSSL_NO_FP_API
210194206Ssimonint PEM_write_RSAPrivateKey(FILE *fp, RSA *x, const EVP_CIPHER *enc,
211296465Sdelphij                            unsigned char *kstr, int klen,
212296465Sdelphij                            pem_password_cb *cb, void *u)
213194206Ssimon{
214296465Sdelphij    EVP_PKEY *k;
215296465Sdelphij    int ret;
216296465Sdelphij    k = EVP_PKEY_new();
217296465Sdelphij    if (!k)
218296465Sdelphij        return 0;
219194206Ssimon
220296465Sdelphij    EVP_PKEY_set1_RSA(k, x);
221194206Ssimon
222296465Sdelphij    ret = PEM_write_PrivateKey(fp, k, enc, kstr, klen, cb, u);
223296465Sdelphij    EVP_PKEY_free(k);
224296465Sdelphij    return ret;
225194206Ssimon}
226296465Sdelphij#  endif
227194206Ssimon
228296465Sdelphij# else
229194206Ssimon
230296465SdelphijIMPLEMENT_PEM_write_cb_const(RSAPrivateKey, RSA, PEM_STRING_RSA,
231296465Sdelphij                             RSAPrivateKey)
232296465Sdelphij# endif
233296465SdelphijIMPLEMENT_PEM_rw_const(RSAPublicKey, RSA, PEM_STRING_RSA_PUBLIC,
234296465Sdelphij                       RSAPublicKey) IMPLEMENT_PEM_rw(RSA_PUBKEY, RSA,
235296465Sdelphij                                                      PEM_STRING_PUBLIC,
236296465Sdelphij                                                      RSA_PUBKEY)
237194206Ssimon#endif
238109998Smarkm#ifndef OPENSSL_NO_DSA
23959191Skrisstatic DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa)
24059191Skris{
241296465Sdelphij    DSA *dtmp;
242296465Sdelphij    if (!key)
243296465Sdelphij        return NULL;
244296465Sdelphij    dtmp = EVP_PKEY_get1_DSA(key);
245296465Sdelphij    EVP_PKEY_free(key);
246296465Sdelphij    if (!dtmp)
247296465Sdelphij        return NULL;
248296465Sdelphij    if (dsa) {
249296465Sdelphij        DSA_free(*dsa);
250296465Sdelphij        *dsa = dtmp;
251296465Sdelphij    }
252296465Sdelphij    return dtmp;
25359191Skris}
25455714Skris
25559191SkrisDSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **dsa, pem_password_cb *cb,
256296465Sdelphij                                void *u)
25759191Skris{
258296465Sdelphij    EVP_PKEY *pktmp;
259296465Sdelphij    pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
260296465Sdelphij    return pkey_get_dsa(pktmp, dsa);
26159191Skris}
26259191Skris
263296465Sdelphij# ifdef OPENSSL_FIPS
264194206Ssimon
265194206Ssimonint PEM_write_bio_DSAPrivateKey(BIO *bp, DSA *x, const EVP_CIPHER *enc,
266296465Sdelphij                                unsigned char *kstr, int klen,
267296465Sdelphij                                pem_password_cb *cb, void *u)
268194206Ssimon{
269296465Sdelphij    EVP_PKEY *k;
270296465Sdelphij    int ret;
271296465Sdelphij    k = EVP_PKEY_new();
272296465Sdelphij    if (!k)
273296465Sdelphij        return 0;
274296465Sdelphij    EVP_PKEY_set1_DSA(k, x);
275194206Ssimon
276296465Sdelphij    ret = PEM_write_bio_PrivateKey(bp, k, enc, kstr, klen, cb, u);
277296465Sdelphij    EVP_PKEY_free(k);
278296465Sdelphij    return ret;
279194206Ssimon}
280194206Ssimon
281296465Sdelphij#  ifndef OPENSSL_NO_FP_API
282194206Ssimonint PEM_write_DSAPrivateKey(FILE *fp, DSA *x, const EVP_CIPHER *enc,
283296465Sdelphij                            unsigned char *kstr, int klen,
284296465Sdelphij                            pem_password_cb *cb, void *u)
285194206Ssimon{
286296465Sdelphij    EVP_PKEY *k;
287296465Sdelphij    int ret;
288296465Sdelphij    k = EVP_PKEY_new();
289296465Sdelphij    if (!k)
290296465Sdelphij        return 0;
291296465Sdelphij    EVP_PKEY_set1_DSA(k, x);
292296465Sdelphij    ret = PEM_write_PrivateKey(fp, k, enc, kstr, klen, cb, u);
293296465Sdelphij    EVP_PKEY_free(k);
294296465Sdelphij    return ret;
295194206Ssimon}
296296465Sdelphij#  endif
297194206Ssimon
298296465Sdelphij# else
299194206Ssimon
300296465SdelphijIMPLEMENT_PEM_write_cb_const(DSAPrivateKey, DSA, PEM_STRING_DSA,
301296465Sdelphij                             DSAPrivateKey)
302296465Sdelphij# endif
303296465Sdelphij    IMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY)
304296465Sdelphij# ifndef OPENSSL_NO_FP_API
305296465SdelphijDSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **dsa, pem_password_cb *cb, void *u)
306142425Snectar{
307296465Sdelphij    EVP_PKEY *pktmp;
308296465Sdelphij    pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
309296465Sdelphij    return pkey_get_dsa(pktmp, dsa);
310160814Ssimon}
311142425Snectar
312296465Sdelphij# endif
313160814Ssimon
314160814SsimonIMPLEMENT_PEM_rw_const(DSAparams, DSA, PEM_STRING_DSAPARAMS, DSAparams)
315160814Ssimon#endif
316160814Ssimon#ifndef OPENSSL_NO_EC
317160814Ssimonstatic EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey)
318160814Ssimon{
319296465Sdelphij    EC_KEY *dtmp;
320296465Sdelphij    if (!key)
321296465Sdelphij        return NULL;
322296465Sdelphij    dtmp = EVP_PKEY_get1_EC_KEY(key);
323296465Sdelphij    EVP_PKEY_free(key);
324296465Sdelphij    if (!dtmp)
325296465Sdelphij        return NULL;
326296465Sdelphij    if (eckey) {
327296465Sdelphij        EC_KEY_free(*eckey);
328296465Sdelphij        *eckey = dtmp;
329296465Sdelphij    }
330296465Sdelphij    return dtmp;
331142425Snectar}
332142425Snectar
333160814SsimonEC_KEY *PEM_read_bio_ECPrivateKey(BIO *bp, EC_KEY **key, pem_password_cb *cb,
334296465Sdelphij                                  void *u)
335142425Snectar{
336296465Sdelphij    EVP_PKEY *pktmp;
337296465Sdelphij    pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);
338296465Sdelphij    return pkey_get_eckey(pktmp, key);
339142425Snectar}
340142425Snectar
341296465SdelphijIMPLEMENT_PEM_rw_const(ECPKParameters, EC_GROUP, PEM_STRING_ECPARAMETERS,
342296465Sdelphij                       ECPKParameters)
343296465Sdelphij# ifdef OPENSSL_FIPS
344194206Ssimonint PEM_write_bio_ECPrivateKey(BIO *bp, EC_KEY *x, const EVP_CIPHER *enc,
345296465Sdelphij                               unsigned char *kstr, int klen,
346296465Sdelphij                               pem_password_cb *cb, void *u)
347194206Ssimon{
348296465Sdelphij    EVP_PKEY *k;
349296465Sdelphij    int ret;
350296465Sdelphij    k = EVP_PKEY_new();
351296465Sdelphij    if (!k)
352296465Sdelphij        return 0;
353296465Sdelphij    EVP_PKEY_set1_EC_KEY(k, x);
354194206Ssimon
355296465Sdelphij    ret = PEM_write_bio_PrivateKey(bp, k, enc, kstr, klen, cb, u);
356296465Sdelphij    EVP_PKEY_free(k);
357296465Sdelphij    return ret;
358194206Ssimon}
359194206Ssimon
360296465Sdelphij#  ifndef OPENSSL_NO_FP_API
361194206Ssimonint PEM_write_ECPrivateKey(FILE *fp, EC_KEY *x, const EVP_CIPHER *enc,
362296465Sdelphij                           unsigned char *kstr, int klen,
363296465Sdelphij                           pem_password_cb *cb, void *u)
364194206Ssimon{
365296465Sdelphij    EVP_PKEY *k;
366296465Sdelphij    int ret;
367296465Sdelphij    k = EVP_PKEY_new();
368296465Sdelphij    if (!k)
369296465Sdelphij        return 0;
370296465Sdelphij    EVP_PKEY_set1_EC_KEY(k, x);
371296465Sdelphij    ret = PEM_write_PrivateKey(fp, k, enc, kstr, klen, cb, u);
372296465Sdelphij    EVP_PKEY_free(k);
373296465Sdelphij    return ret;
374194206Ssimon}
375296465Sdelphij#  endif
376194206Ssimon
377296465Sdelphij# else
378296465Sdelphij    IMPLEMENT_PEM_write_cb(ECPrivateKey, EC_KEY, PEM_STRING_ECPRIVATEKEY,
379296465Sdelphij                       ECPrivateKey)
380296465Sdelphij# endif
381160814SsimonIMPLEMENT_PEM_rw(EC_PUBKEY, EC_KEY, PEM_STRING_PUBLIC, EC_PUBKEY)
382296465Sdelphij# ifndef OPENSSL_NO_FP_API
383160814SsimonEC_KEY *PEM_read_ECPrivateKey(FILE *fp, EC_KEY **eckey, pem_password_cb *cb,
384296465Sdelphij                              void *u)
38559191Skris{
386296465Sdelphij    EVP_PKEY *pktmp;
387296465Sdelphij    pktmp = PEM_read_PrivateKey(fp, NULL, cb, u);
388296465Sdelphij    return pkey_get_eckey(pktmp, eckey);
38959191Skris}
39059191Skris
391296465Sdelphij# endif
39259191Skris
39355714Skris#endif
39455714Skris
395109998Smarkm#ifndef OPENSSL_NO_DH
39655714Skris
397160814SsimonIMPLEMENT_PEM_rw_const(DHparams, DH, PEM_STRING_DHPARAMS, DHparams)
39855714Skris#endif
399296465Sdelphij/*-
400296465Sdelphij * The PrivateKey case is not that straightforward.
40155714Skris *   IMPLEMENT_PEM_rw_cb(PrivateKey, EVP_PKEY, PEM_STRING_EVP_PKEY, PrivateKey)
40255714Skris * does not work, RSA and DSA keys have specific strings.
40355714Skris * (When reading, parameter PEM_STRING_EVP_PKEY is a wildcard for anything
40455714Skris * appropriate.)
40555714Skris */
406194206Ssimon#ifdef OPENSSL_FIPS
407194206Ssimonstatic const char *pkey_str(EVP_PKEY *x)
408296465Sdelphij{
409296465Sdelphij    switch (x->type) {
410296465Sdelphij    case EVP_PKEY_RSA:
411296465Sdelphij        return PEM_STRING_RSA;
412194206Ssimon
413296465Sdelphij    case EVP_PKEY_DSA:
414296465Sdelphij        return PEM_STRING_DSA;
415194206Ssimon
416296465Sdelphij    case EVP_PKEY_EC:
417296465Sdelphij        return PEM_STRING_ECPRIVATEKEY;
418194206Ssimon
419296465Sdelphij    default:
420296465Sdelphij        return NULL;
421296465Sdelphij    }
422296465Sdelphij}
423194206Ssimon
424194206Ssimonint PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
425296465Sdelphij                             unsigned char *kstr, int klen,
426296465Sdelphij                             pem_password_cb *cb, void *u)
427296465Sdelphij{
428296465Sdelphij    if (FIPS_mode())
429296465Sdelphij        return PEM_write_bio_PKCS8PrivateKey(bp, x, enc,
430296465Sdelphij                                             (char *)kstr, klen, cb, u);
431296465Sdelphij    else
432296465Sdelphij        return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
433296465Sdelphij                                  pkey_str(x), bp, (char *)x, enc, kstr, klen,
434296465Sdelphij                                  cb, u);
435296465Sdelphij}
436194206Ssimon
437296465Sdelphij# ifndef OPENSSL_NO_FP_API
438194206Ssimonint PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
439296465Sdelphij                         unsigned char *kstr, int klen,
440296465Sdelphij                         pem_password_cb *cb, void *u)
441296465Sdelphij{
442296465Sdelphij    if (FIPS_mode())
443296465Sdelphij        return PEM_write_PKCS8PrivateKey(fp, x, enc,
444296465Sdelphij                                         (char *)kstr, klen, cb, u);
445296465Sdelphij    else
446296465Sdelphij        return PEM_ASN1_write((i2d_of_void *)i2d_PrivateKey,
447296465Sdelphij                              pkey_str(x), fp, (char *)x, enc, kstr, klen, cb,
448296465Sdelphij                              u);
449296465Sdelphij}
450296465Sdelphij# endif
451194206Ssimon
452194206Ssimon#else
453296465SdelphijIMPLEMENT_PEM_write_cb(PrivateKey, EVP_PKEY,
454296465Sdelphij                       ((x->type ==
455296465Sdelphij                         EVP_PKEY_DSA) ? PEM_STRING_DSA : (x->type ==
456296465Sdelphij                                                           EVP_PKEY_RSA) ?
457296465Sdelphij                        PEM_STRING_RSA : PEM_STRING_ECPRIVATEKEY), PrivateKey)
458194206Ssimon#endif
459296465Sdelphij    IMPLEMENT_PEM_rw(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY)
460