i2d_pu.c revision 296465
1189753Simp/* crypto/asn1/i2d_pu.c */
2189753Simp/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3189753Simp * All rights reserved.
4189753Simp *
5189753Simp * This package is an SSL implementation written
6189753Simp * by Eric Young (eay@cryptsoft.com).
7189753Simp * The implementation was written so as to conform with Netscapes SSL.
8189753Simp *
9189753Simp * This library is free for commercial and non-commercial use as long as
10189753Simp * the following conditions are aheared to.  The following conditions
11189753Simp * apply to all code found in this distribution, be it the RC4, RSA,
12189753Simp * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13189753Simp * included with this distribution is covered by the same copyright terms
14189753Simp * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15189753Simp *
16189753Simp * Copyright remains Eric Young's, and as such any Copyright notices in
17189753Simp * the code are not to be removed.
18189753Simp * If this package is used in a product, Eric Young should be given attribution
19189753Simp * as the author of the parts of the library used.
20189753Simp * This can be in the form of a textual message at program startup or
21189753Simp * in documentation (online or textual) provided with the package.
22189753Simp *
23189753Simp * Redistribution and use in source and binary forms, with or without
24189753Simp * modification, are permitted provided that the following conditions
25189753Simp * are met:
26189753Simp * 1. Redistributions of source code must retain the copyright
27189753Simp *    notice, this list of conditions and the following disclaimer.
28189753Simp * 2. Redistributions in binary form must reproduce the above copyright
29189753Simp *    notice, this list of conditions and the following disclaimer in the
30189753Simp *    documentation and/or other materials provided with the distribution.
31189753Simp * 3. All advertising materials mentioning features or use of this software
32189753Simp *    must display the following acknowledgement:
33189753Simp *    "This product includes cryptographic software written by
34189753Simp *     Eric Young (eay@cryptsoft.com)"
35189753Simp *    The word 'cryptographic' can be left out if the rouines from the library
36189753Simp *    being used are not cryptographic related :-).
37189753Simp * 4. If you include any Windows specific code (or a derivative thereof) from
38189753Simp *    the apps directory (application code) you must include an acknowledgement:
39189753Simp *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40189753Simp *
41189753Simp * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42189753Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43189753Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44189753Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45189753Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46189753Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47189753Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48189753Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49189753Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50189753Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51189753Simp * SUCH DAMAGE.
52189753Simp *
53189753Simp * The licence and distribution terms for any publically available version or
54189753Simp * derivative of this code cannot be changed.  i.e. this code cannot simply be
55189753Simp * copied and put under another distribution licence
56189753Simp * [including the GNU Public Licence.]
57189753Simp */
58189753Simp
59189753Simp#include <stdio.h>
60189753Simp#include "cryptlib.h"
61189753Simp#include <openssl/bn.h>
62189753Simp#include <openssl/evp.h>
63189753Simp#include <openssl/objects.h>
64189753Simp#ifndef OPENSSL_NO_RSA
65189753Simp# include <openssl/rsa.h>
66189753Simp#endif
67#ifndef OPENSSL_NO_DSA
68# include <openssl/dsa.h>
69#endif
70#ifndef OPENSSL_NO_EC
71# include <openssl/ec.h>
72#endif
73
74int i2d_PublicKey(EVP_PKEY *a, unsigned char **pp)
75{
76    switch (a->type) {
77#ifndef OPENSSL_NO_RSA
78    case EVP_PKEY_RSA:
79        return (i2d_RSAPublicKey(a->pkey.rsa, pp));
80#endif
81#ifndef OPENSSL_NO_DSA
82    case EVP_PKEY_DSA:
83        return (i2d_DSAPublicKey(a->pkey.dsa, pp));
84#endif
85#ifndef OPENSSL_NO_EC
86    case EVP_PKEY_EC:
87        return (i2o_ECPublicKey(a->pkey.ec, pp));
88#endif
89    default:
90        ASN1err(ASN1_F_I2D_PUBLICKEY, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
91        return (-1);
92    }
93}
94