pem_all.c revision 109998
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. 855714Skris * 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). 1555714Skris * 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. 2255714Skris * 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 :-). 3755714Skris * 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)" 4055714Skris * 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. 5255714Skris * 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 */ 5855714Skris 5955714Skris#include <stdio.h> 6055714Skris#undef SSLEAY_MACROS 6155714Skris#include "cryptlib.h" 6255714Skris#include <openssl/bio.h> 6355714Skris#include <openssl/evp.h> 6455714Skris#include <openssl/x509.h> 6555714Skris#include <openssl/pkcs7.h> 6655714Skris#include <openssl/pem.h> 6755714Skris 68109998Smarkm#ifndef OPENSSL_NO_RSA 6959191Skrisstatic RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa); 7059191Skris#endif 71109998Smarkm#ifndef OPENSSL_NO_DSA 7259191Skrisstatic DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa); 7359191Skris#endif 7459191Skris 7555714SkrisIMPLEMENT_PEM_rw(X509_REQ, X509_REQ, PEM_STRING_X509_REQ, X509_REQ) 7655714Skris 7759191SkrisIMPLEMENT_PEM_write(X509_REQ_NEW, X509_REQ, PEM_STRING_X509_REQ_OLD, X509_REQ) 7859191Skris 7955714SkrisIMPLEMENT_PEM_rw(X509_CRL, X509_CRL, PEM_STRING_X509_CRL, X509_CRL) 8055714Skris 8155714SkrisIMPLEMENT_PEM_rw(PKCS7, PKCS7, PEM_STRING_PKCS7, PKCS7) 8255714Skris 8355714SkrisIMPLEMENT_PEM_rw(NETSCAPE_CERT_SEQUENCE, NETSCAPE_CERT_SEQUENCE, 8455714Skris PEM_STRING_X509, NETSCAPE_CERT_SEQUENCE) 8555714Skris 8655714Skris 87109998Smarkm#ifndef OPENSSL_NO_RSA 8855714Skris 8959191Skris/* We treat RSA or DSA private keys as a special case. 9059191Skris * 9159191Skris * For private keys we read in an EVP_PKEY structure with 9259191Skris * PEM_read_bio_PrivateKey() and extract the relevant private 9359191Skris * key: this means can handle "traditional" and PKCS#8 formats 9459191Skris * transparently. 9559191Skris */ 9655714Skris 9759191Skrisstatic RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa) 9859191Skris{ 9959191Skris RSA *rtmp; 10059191Skris if(!key) return NULL; 10159191Skris rtmp = EVP_PKEY_get1_RSA(key); 10259191Skris EVP_PKEY_free(key); 10359191Skris if(!rtmp) return NULL; 10459191Skris if(rsa) { 10559191Skris RSA_free(*rsa); 10659191Skris *rsa = rtmp; 10759191Skris } 10859191Skris return rtmp; 10959191Skris} 11059191Skris 11159191SkrisRSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **rsa, pem_password_cb *cb, 11259191Skris void *u) 11359191Skris{ 11459191Skris EVP_PKEY *pktmp; 11559191Skris pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u); 11659191Skris return pkey_get_rsa(pktmp, rsa); 11759191Skris} 11859191Skris 119109998Smarkm#ifndef OPENSSL_NO_FP_API 12059191Skris 12159191SkrisRSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **rsa, pem_password_cb *cb, 12259191Skris void *u) 12359191Skris{ 12459191Skris EVP_PKEY *pktmp; 12559191Skris pktmp = PEM_read_PrivateKey(fp, NULL, cb, u); 12659191Skris return pkey_get_rsa(pktmp, rsa); 12759191Skris} 12859191Skris 12959191Skris#endif 13059191Skris 13159191SkrisIMPLEMENT_PEM_write_cb(RSAPrivateKey, RSA, PEM_STRING_RSA, RSAPrivateKey) 13255714SkrisIMPLEMENT_PEM_rw(RSAPublicKey, RSA, PEM_STRING_RSA_PUBLIC, RSAPublicKey) 13359191SkrisIMPLEMENT_PEM_rw(RSA_PUBKEY, RSA, PEM_STRING_PUBLIC, RSA_PUBKEY) 13455714Skris 13555714Skris#endif 13655714Skris 137109998Smarkm#ifndef OPENSSL_NO_DSA 13855714Skris 13959191Skrisstatic DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa) 14059191Skris{ 14159191Skris DSA *dtmp; 14259191Skris if(!key) return NULL; 14359191Skris dtmp = EVP_PKEY_get1_DSA(key); 14459191Skris EVP_PKEY_free(key); 14559191Skris if(!dtmp) return NULL; 14659191Skris if(dsa) { 14759191Skris DSA_free(*dsa); 14859191Skris *dsa = dtmp; 14959191Skris } 15059191Skris return dtmp; 15159191Skris} 15255714Skris 15359191SkrisDSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **dsa, pem_password_cb *cb, 15459191Skris void *u) 15559191Skris{ 15659191Skris EVP_PKEY *pktmp; 15759191Skris pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u); 15859191Skris return pkey_get_dsa(pktmp, dsa); 15959191Skris} 16059191Skris 16159191SkrisIMPLEMENT_PEM_write_cb(DSAPrivateKey, DSA, PEM_STRING_DSA, DSAPrivateKey) 16259191SkrisIMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY) 16359191Skris 164109998Smarkm#ifndef OPENSSL_NO_FP_API 16559191Skris 16659191SkrisDSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **dsa, pem_password_cb *cb, 16759191Skris void *u) 16859191Skris{ 16959191Skris EVP_PKEY *pktmp; 17059191Skris pktmp = PEM_read_PrivateKey(fp, NULL, cb, u); 17159191Skris return pkey_get_dsa(pktmp, dsa); 17259191Skris} 17359191Skris 17459191Skris#endif 17559191Skris 17655714SkrisIMPLEMENT_PEM_rw(DSAparams, DSA, PEM_STRING_DSAPARAMS, DSAparams) 17755714Skris 17855714Skris#endif 17955714Skris 180109998Smarkm#ifndef OPENSSL_NO_DH 18155714Skris 18255714SkrisIMPLEMENT_PEM_rw(DHparams, DH, PEM_STRING_DHPARAMS, DHparams) 18355714Skris 18455714Skris#endif 18555714Skris 18655714Skris 18755714Skris/* The PrivateKey case is not that straightforward. 18855714Skris * IMPLEMENT_PEM_rw_cb(PrivateKey, EVP_PKEY, PEM_STRING_EVP_PKEY, PrivateKey) 18955714Skris * does not work, RSA and DSA keys have specific strings. 19055714Skris * (When reading, parameter PEM_STRING_EVP_PKEY is a wildcard for anything 19155714Skris * appropriate.) 19255714Skris */ 19355714SkrisIMPLEMENT_PEM_write_cb(PrivateKey, EVP_PKEY, ((x->type == EVP_PKEY_DSA)?PEM_STRING_DSA:PEM_STRING_RSA), PrivateKey) 19459191Skris 19559191SkrisIMPLEMENT_PEM_rw(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY) 196109998Smarkm 197