1169695Skan/* crypto/asn1/a_dup.c */
2169695Skan/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3169695Skan * All rights reserved.
4169695Skan *
5169695Skan * This package is an SSL implementation written
6169695Skan * by Eric Young (eay@cryptsoft.com).
7169695Skan * The implementation was written so as to conform with Netscapes SSL.
8169695Skan *
9169695Skan * This library is free for commercial and non-commercial use as long as
10169695Skan * the following conditions are aheared to.  The following conditions
11169695Skan * apply to all code found in this distribution, be it the RC4, RSA,
12169695Skan * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13169695Skan * included with this distribution is covered by the same copyright terms
14169695Skan * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15169695Skan *
16169695Skan * Copyright remains Eric Young's, and as such any Copyright notices in
17169695Skan * the code are not to be removed.
18169695Skan * If this package is used in a product, Eric Young should be given attribution
19169695Skan * as the author of the parts of the library used.
20169695Skan * This can be in the form of a textual message at program startup or
21169695Skan * in documentation (online or textual) provided with the package.
22169695Skan *
23169695Skan * Redistribution and use in source and binary forms, with or without
24169695Skan * modification, are permitted provided that the following conditions
25169695Skan * are met:
26169695Skan * 1. Redistributions of source code must retain the copyright
27169695Skan *    notice, this list of conditions and the following disclaimer.
28169695Skan * 2. Redistributions in binary form must reproduce the above copyright
29169695Skan *    notice, this list of conditions and the following disclaimer in the
30169695Skan *    documentation and/or other materials provided with the distribution.
31169695Skan * 3. All advertising materials mentioning features or use of this software
32169695Skan *    must display the following acknowledgement:
33169695Skan *    "This product includes cryptographic software written by
34169695Skan *     Eric Young (eay@cryptsoft.com)"
35169695Skan *    The word 'cryptographic' can be left out if the rouines from the library
36169695Skan *    being used are not cryptographic related :-).
37169695Skan * 4. If you include any Windows specific code (or a derivative thereof) from
38169695Skan *    the apps directory (application code) you must include an acknowledgement:
39169695Skan *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40169695Skan *
41169695Skan * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42169695Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43169695Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44169695Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45169695Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46169695Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47169695Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48169695Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49169695Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50169695Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51169695Skan * SUCH DAMAGE.
52169695Skan *
53169695Skan * The licence and distribution terms for any publically available version or
54169695Skan * derivative of this code cannot be changed.  i.e. this code cannot simply be
55169695Skan * copied and put under another distribution licence
56169695Skan * [including the GNU Public Licence.]
57169695Skan */
58169695Skan
59169695Skan#include <stdio.h>
60169695Skan#include "cryptlib.h"
61169695Skan#include <openssl/asn1.h>
62169695Skan
63169695Skan#ifndef NO_OLD_ASN1
64169695Skan
65169695Skanvoid *ASN1_dup(i2d_of_void *i2d, d2i_of_void *d2i, void *x)
66169695Skan{
67169695Skan    unsigned char *b, *p;
68169695Skan    const unsigned char *p2;
69    int i;
70    char *ret;
71
72    if (x == NULL)
73        return (NULL);
74
75    i = i2d(x, NULL);
76    b = OPENSSL_malloc(i + 10);
77    if (b == NULL) {
78        ASN1err(ASN1_F_ASN1_DUP, ERR_R_MALLOC_FAILURE);
79        return (NULL);
80    }
81    p = b;
82    i = i2d(x, &p);
83    p2 = b;
84    ret = d2i(NULL, &p2, i);
85    OPENSSL_free(b);
86    return (ret);
87}
88
89#endif
90
91/*
92 * ASN1_ITEM version of dup: this follows the model above except we don't
93 * need to allocate the buffer. At some point this could be rewritten to
94 * directly dup the underlying structure instead of doing and encode and
95 * decode.
96 */
97
98void *ASN1_item_dup(const ASN1_ITEM *it, void *x)
99{
100    unsigned char *b = NULL;
101    const unsigned char *p;
102    long i;
103    void *ret;
104
105    if (x == NULL)
106        return (NULL);
107
108    i = ASN1_item_i2d(x, &b, it);
109    if (b == NULL) {
110        ASN1err(ASN1_F_ASN1_ITEM_DUP, ERR_R_MALLOC_FAILURE);
111        return (NULL);
112    }
113    p = b;
114    ret = ASN1_item_d2i(NULL, &p, i, it);
115    OPENSSL_free(b);
116    return (ret);
117}
118