i2d_pr.c revision 160814
152419Sjulian/* crypto/asn1/i2d_pr.c */
252419Sjulian/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3139823Simp * All rights reserved.
4139823Simp *
5139823Simp * This package is an SSL implementation written
659882Sarchie * by Eric Young (eay@cryptsoft.com).
752419Sjulian * The implementation was written so as to conform with Netscapes SSL.
852419Sjulian *
952419Sjulian * This library is free for commercial and non-commercial use as long as
1052419Sjulian * the following conditions are aheared to.  The following conditions
1152419Sjulian * apply to all code found in this distribution, be it the RC4, RSA,
1252419Sjulian * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1352419Sjulian * included with this distribution is covered by the same copyright terms
1452419Sjulian * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1552419Sjulian *
1652419Sjulian * Copyright remains Eric Young's, and as such any Copyright notices in
1752419Sjulian * the code are not to be removed.
1852419Sjulian * If this package is used in a product, Eric Young should be given attribution
1952419Sjulian * as the author of the parts of the library used.
2052419Sjulian * This can be in the form of a textual message at program startup or
2152419Sjulian * in documentation (online or textual) provided with the package.
2252419Sjulian *
2352419Sjulian * Redistribution and use in source and binary forms, with or without
2452419Sjulian * modification, are permitted provided that the following conditions
2552419Sjulian * are met:
2652419Sjulian * 1. Redistributions of source code must retain the copyright
2752419Sjulian *    notice, this list of conditions and the following disclaimer.
2852419Sjulian * 2. Redistributions in binary form must reproduce the above copyright
2952419Sjulian *    notice, this list of conditions and the following disclaimer in the
3052419Sjulian *    documentation and/or other materials provided with the distribution.
3152419Sjulian * 3. All advertising materials mentioning features or use of this software
3252419Sjulian *    must display the following acknowledgement:
3352419Sjulian *    "This product includes cryptographic software written by
3452419Sjulian *     Eric Young (eay@cryptsoft.com)"
3552419Sjulian *    The word 'cryptographic' can be left out if the rouines from the library
3652419Sjulian *    being used are not cryptographic related :-).
3752419Sjulian * 4. If you include any Windows specific code (or a derivative thereof) from
3866775Sarchie *    the apps directory (application code) you must include an acknowledgement:
3952419Sjulian *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4052419Sjulian *
4152419Sjulian * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4252419Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4352419Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44122481Sru * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45122481Sru * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4652419Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4752419Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4852419Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4966775Sarchie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5052419Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51171688Smav * SUCH DAMAGE.
52171688Smav *
53171688Smav * The licence and distribution terms for any publically available version or
5452639Sarchie * derivative of this code cannot be changed.  i.e. this code cannot simply be
5552639Sarchie * copied and put under another distribution licence
5652639Sarchie * [including the GNU Public Licence.]
5752639Sarchie */
5852639Sarchie
5952639Sarchie#include <stdio.h>
6052639Sarchie#include "cryptlib.h"
6152639Sarchie#include <openssl/bn.h>
6252639Sarchie#include <openssl/evp.h>
6352639Sarchie#include <openssl/objects.h>
6452419Sjulian#ifndef OPENSSL_NO_RSA
6552639Sarchie#include <openssl/rsa.h>
6652639Sarchie#endif
6752639Sarchie#ifndef OPENSSL_NO_DSA
6852639Sarchie#include <openssl/dsa.h>
6952639Sarchie#endif
7052639Sarchie#ifndef OPENSSL_NO_EC
7152639Sarchie#include <openssl/ec.h>
7252639Sarchie#endif
7352639Sarchie
7452639Sarchieint i2d_PrivateKey(EVP_PKEY *a, unsigned char **pp)
7552639Sarchie	{
7652639Sarchie#ifndef OPENSSL_NO_RSA
7759882Sarchie	if (a->type == EVP_PKEY_RSA)
7852419Sjulian		{
7952639Sarchie		return(i2d_RSAPrivateKey(a->pkey.rsa,pp));
8052639Sarchie		}
81165580Sglebius	else
82165580Sglebius#endif
83165580Sglebius#ifndef OPENSSL_NO_DSA
84165580Sglebius	if (a->type == EVP_PKEY_DSA)
85165580Sglebius		{
86165580Sglebius		return(i2d_DSAPrivateKey(a->pkey.dsa,pp));
87165580Sglebius		}
88165580Sglebius#endif
89165580Sglebius#ifndef OPENSSL_NO_EC
90165580Sglebius	if (a->type == EVP_PKEY_EC)
91165580Sglebius		{
92165580Sglebius		return(i2d_ECPrivateKey(a->pkey.ec, pp));
93165580Sglebius		}
94165580Sglebius#endif
9552419Sjulian
9652419Sjulian	ASN1err(ASN1_F_I2D_PRIVATEKEY,ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
9759882Sarchie	return(-1);
9859882Sarchie	}
9966775Sarchie
10052639Sarchie