159191Skris/* crypto/asn1/p8_key.c */
259191Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
359191Skris * All rights reserved.
459191Skris *
559191Skris * This package is an SSL implementation written
659191Skris * by Eric Young (eay@cryptsoft.com).
759191Skris * The implementation was written so as to conform with Netscapes SSL.
8296465Sdelphij *
959191Skris * This library is free for commercial and non-commercial use as long as
1059191Skris * the following conditions are aheared to.  The following conditions
1159191Skris * apply to all code found in this distribution, be it the RC4, RSA,
1259191Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1359191Skris * included with this distribution is covered by the same copyright terms
1459191Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15296465Sdelphij *
1659191Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1759191Skris * the code are not to be removed.
1859191Skris * If this package is used in a product, Eric Young should be given attribution
1959191Skris * as the author of the parts of the library used.
2059191Skris * This can be in the form of a textual message at program startup or
2159191Skris * in documentation (online or textual) provided with the package.
22296465Sdelphij *
2359191Skris * Redistribution and use in source and binary forms, with or without
2459191Skris * modification, are permitted provided that the following conditions
2559191Skris * are met:
2659191Skris * 1. Redistributions of source code must retain the copyright
2759191Skris *    notice, this list of conditions and the following disclaimer.
2859191Skris * 2. Redistributions in binary form must reproduce the above copyright
2959191Skris *    notice, this list of conditions and the following disclaimer in the
3059191Skris *    documentation and/or other materials provided with the distribution.
3159191Skris * 3. All advertising materials mentioning features or use of this software
3259191Skris *    must display the following acknowledgement:
3359191Skris *    "This product includes cryptographic software written by
3459191Skris *     Eric Young (eay@cryptsoft.com)"
3559191Skris *    The word 'cryptographic' can be left out if the rouines from the library
3659191Skris *    being used are not cryptographic related :-).
37296465Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from
3859191Skris *    the apps directory (application code) you must include an acknowledgement:
3959191Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40296465Sdelphij *
4159191Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4259191Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4359191Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4459191Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4559191Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4659191Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4759191Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4859191Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4959191Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5059191Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5159191Skris * SUCH DAMAGE.
52296465Sdelphij *
5359191Skris * The licence and distribution terms for any publically available version or
5459191Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5559191Skris * copied and put under another distribution licence
5659191Skris * [including the GNU Public Licence.]
5759191Skris */
5859191Skris
5959191Skris#include <stdio.h>
6059191Skris#include "cryptlib.h"
6159191Skris#include <openssl/asn1_mac.h>
6259191Skris#include <openssl/objects.h>
6359191Skris
6459191Skrisint i2d_X509_KEY(X509 *a, unsigned char **pp)
65296465Sdelphij{
66296465Sdelphij    M_ASN1_I2D_vars(a);
6759191Skris
68296465Sdelphij    M_ASN1_I2D_len(a->cert_info, i2d_X509_CINF);
69296465Sdelphij    M_ASN1_I2D_len(a->sig_alg, i2d_X509_ALGOR);
70296465Sdelphij    M_ASN1_I2D_len(a->signature, i2d_ASN1_BIT_STRING);
7159191Skris
72296465Sdelphij    M_ASN1_I2D_seq_total();
7359191Skris
74296465Sdelphij    M_ASN1_I2D_put(a->cert_info, i2d_X509_CINF);
75296465Sdelphij    M_ASN1_I2D_put(a->sig_alg, i2d_X509_ALGOR);
76296465Sdelphij    M_ASN1_I2D_put(a->signature, i2d_ASN1_BIT_STRING);
7759191Skris
78296465Sdelphij    M_ASN1_I2D_finish();
79296465Sdelphij}
8059191Skris
8159191SkrisX509 *d2i_X509_KEY(X509 **a, unsigned char **pp, long length)
82296465Sdelphij{
83296465Sdelphij    M_ASN1_D2I_vars(a, X509 *, X509_new);
8459191Skris
85296465Sdelphij    M_ASN1_D2I_Init();
86296465Sdelphij    M_ASN1_D2I_start_sequence();
87296465Sdelphij    M_ASN1_D2I_get(ret->cert_info, d2i_X509_CINF);
88296465Sdelphij    M_ASN1_D2I_get(ret->sig_alg, d2i_X509_ALGOR);
89296465Sdelphij    M_ASN1_D2I_get(ret->signature, d2i_ASN1_BIT_STRING);
90296465Sdelphij    M_ASN1_D2I_Finish(a, X509_free, ASN1_F_D2I_X509);
91296465Sdelphij}
9259191Skris
9359191SkrisX509 *X509_KEY_new(void)
94296465Sdelphij{
95296465Sdelphij    X509_KEY *ret = NULL;
9659191Skris
97296465Sdelphij    M_ASN1_New_OPENSSL_malloc(ret, X509_KEY);
98296465Sdelphij    ret->references = 1;
99296465Sdelphij    ret->type = NID M_ASN1_New(ret->cert_info, X509_CINF_new);
100296465Sdelphij    M_ASN1_New(ret->sig_alg, X509_ALGOR_new);
101296465Sdelphij    M_ASN1_New(ret->signature, ASN1_BIT_STRING_new);
102296465Sdelphij    return (ret);
103296465Sdelphij    M_ASN1_New_Error(ASN1_F_X509_NEW);
104296465Sdelphij}
10559191Skris
10659191Skrisvoid X509_KEY_free(X509 *a)
107296465Sdelphij{
108296465Sdelphij    int i;
10959191Skris
110296465Sdelphij    if (a == NULL)
111296465Sdelphij        return;
11259191Skris
113296465Sdelphij    i = CRYPTO_add_lock(&a->references, -1, CRYPTO_LOCK_X509_KEY);
11459191Skris#ifdef REF_PRINT
115296465Sdelphij    REF_PRINT("X509_KEY", a);
11659191Skris#endif
117296465Sdelphij    if (i > 0)
118296465Sdelphij        return;
11959191Skris#ifdef REF_CHECK
120296465Sdelphij    if (i < 0) {
121296465Sdelphij        fprintf(stderr, "X509_KEY_free, bad reference count\n");
122296465Sdelphij        abort();
123296465Sdelphij    }
12459191Skris#endif
12559191Skris
126296465Sdelphij    X509_CINF_free(a->cert_info);
127296465Sdelphij    X509_ALGOR_free(a->sig_alg);
128296465Sdelphij    ASN1_BIT_STRING_free(a->signature);
129296465Sdelphij    OPENSSL_free(a);
130296465Sdelphij}
131