xref: /freebsd/crypto/heimdal/lib/asn1/der_copy.c (revision 1b7487592987c91020063a311a14dc15b6e58075)
1b528cefcSMark Murray /*
2ae771770SStanislav Sedov  * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6ae771770SStanislav Sedov  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7ae771770SStanislav Sedov  *
8b528cefcSMark Murray  * Redistribution and use in source and binary forms, with or without
9b528cefcSMark Murray  * modification, are permitted provided that the following conditions
10b528cefcSMark Murray  * are met:
11b528cefcSMark Murray  *
12b528cefcSMark Murray  * 1. Redistributions of source code must retain the above copyright
13b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer.
14b528cefcSMark Murray  *
15b528cefcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
16b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
17b528cefcSMark Murray  *    documentation and/or other materials provided with the distribution.
18b528cefcSMark Murray  *
19b528cefcSMark Murray  * 3. Neither the name of the Institute nor the names of its contributors
20b528cefcSMark Murray  *    may be used to endorse or promote products derived from this software
21b528cefcSMark Murray  *    without specific prior written permission.
22b528cefcSMark Murray  *
23b528cefcSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24b528cefcSMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25b528cefcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26b528cefcSMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27b528cefcSMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28b528cefcSMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29b528cefcSMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30b528cefcSMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31b528cefcSMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32b528cefcSMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33b528cefcSMark Murray  * SUCH DAMAGE.
34b528cefcSMark Murray  */
35b528cefcSMark Murray 
36b528cefcSMark Murray #include "der_locl.h"
37b528cefcSMark Murray 
38ae771770SStanislav Sedov RCSID("$Id$");
39b528cefcSMark Murray 
40b528cefcSMark Murray int
41c19800e8SDoug Rabson der_copy_general_string (const heim_general_string *from,
42c19800e8SDoug Rabson 			 heim_general_string *to)
43b528cefcSMark Murray {
44bbd80c28SJacques Vidrine     *to = strdup(*from);
45b528cefcSMark Murray     if(*to == NULL)
46b528cefcSMark Murray 	return ENOMEM;
47b528cefcSMark Murray     return 0;
48b528cefcSMark Murray }
49b528cefcSMark Murray 
50b528cefcSMark Murray int
51ae771770SStanislav Sedov der_copy_integer (const int *from, int *to)
52ae771770SStanislav Sedov {
53ae771770SStanislav Sedov     *to = *from;
54ae771770SStanislav Sedov     return 0;
55ae771770SStanislav Sedov }
56ae771770SStanislav Sedov 
57ae771770SStanislav Sedov int
58*1b748759SDimitry Andric der_copy_integer64 (const int64_t *from, int64_t *to)
59*1b748759SDimitry Andric {
60*1b748759SDimitry Andric     *to = *from;
61*1b748759SDimitry Andric     return 0;
62*1b748759SDimitry Andric }
63*1b748759SDimitry Andric 
64*1b748759SDimitry Andric int
65ae771770SStanislav Sedov der_copy_unsigned (const unsigned *from, unsigned *to)
66ae771770SStanislav Sedov {
67ae771770SStanislav Sedov     *to = *from;
68ae771770SStanislav Sedov     return 0;
69ae771770SStanislav Sedov }
70ae771770SStanislav Sedov 
71ae771770SStanislav Sedov int
72*1b748759SDimitry Andric der_copy_unsigned64 (const uint64_t *from, uint64_t *to)
73*1b748759SDimitry Andric {
74*1b748759SDimitry Andric     *to = *from;
75*1b748759SDimitry Andric     return 0;
76*1b748759SDimitry Andric }
77*1b748759SDimitry Andric 
78*1b748759SDimitry Andric int
79ae771770SStanislav Sedov der_copy_generalized_time (const time_t *from, time_t *to)
80ae771770SStanislav Sedov {
81ae771770SStanislav Sedov     *to = *from;
82ae771770SStanislav Sedov     return 0;
83ae771770SStanislav Sedov }
84ae771770SStanislav Sedov 
85ae771770SStanislav Sedov int
86ae771770SStanislav Sedov der_copy_utctime (const time_t *from, time_t *to)
87ae771770SStanislav Sedov {
88ae771770SStanislav Sedov     *to = *from;
89ae771770SStanislav Sedov     return 0;
90ae771770SStanislav Sedov }
91ae771770SStanislav Sedov 
92ae771770SStanislav Sedov int
93c19800e8SDoug Rabson der_copy_utf8string (const heim_utf8_string *from, heim_utf8_string *to)
94c19800e8SDoug Rabson {
95c19800e8SDoug Rabson     return der_copy_general_string(from, to);
96c19800e8SDoug Rabson }
97c19800e8SDoug Rabson 
98c19800e8SDoug Rabson int
99c19800e8SDoug Rabson der_copy_printable_string (const heim_printable_string *from,
100c19800e8SDoug Rabson 		       heim_printable_string *to)
101c19800e8SDoug Rabson {
102ae771770SStanislav Sedov     to->length = from->length;
103ae771770SStanislav Sedov     to->data   = malloc(to->length + 1);
104ae771770SStanislav Sedov     if(to->data == NULL)
105ae771770SStanislav Sedov 	return ENOMEM;
106ae771770SStanislav Sedov     memcpy(to->data, from->data, to->length);
107ae771770SStanislav Sedov     ((char *)to->data)[to->length] = '\0';
108ae771770SStanislav Sedov     return 0;
109c19800e8SDoug Rabson }
110c19800e8SDoug Rabson 
111c19800e8SDoug Rabson int
112ae771770SStanislav Sedov der_copy_ia5_string (const heim_ia5_string *from,
113ae771770SStanislav Sedov 		     heim_ia5_string *to)
114c19800e8SDoug Rabson {
115ae771770SStanislav Sedov     return der_copy_printable_string(from, to);
116c19800e8SDoug Rabson }
117c19800e8SDoug Rabson 
118c19800e8SDoug Rabson int
119c19800e8SDoug Rabson der_copy_bmp_string (const heim_bmp_string *from, heim_bmp_string *to)
120c19800e8SDoug Rabson {
121c19800e8SDoug Rabson     to->length = from->length;
122c19800e8SDoug Rabson     to->data   = malloc(to->length * sizeof(to->data[0]));
123c19800e8SDoug Rabson     if(to->length != 0 && to->data == NULL)
124c19800e8SDoug Rabson 	return ENOMEM;
125c19800e8SDoug Rabson     memcpy(to->data, from->data, to->length * sizeof(to->data[0]));
126c19800e8SDoug Rabson     return 0;
127c19800e8SDoug Rabson }
128c19800e8SDoug Rabson 
129c19800e8SDoug Rabson int
130c19800e8SDoug Rabson der_copy_universal_string (const heim_universal_string *from,
131c19800e8SDoug Rabson 			   heim_universal_string *to)
132c19800e8SDoug Rabson {
133c19800e8SDoug Rabson     to->length = from->length;
134c19800e8SDoug Rabson     to->data   = malloc(to->length * sizeof(to->data[0]));
135c19800e8SDoug Rabson     if(to->length != 0 && to->data == NULL)
136c19800e8SDoug Rabson 	return ENOMEM;
137c19800e8SDoug Rabson     memcpy(to->data, from->data, to->length * sizeof(to->data[0]));
138c19800e8SDoug Rabson     return 0;
139c19800e8SDoug Rabson }
140c19800e8SDoug Rabson 
141c19800e8SDoug Rabson int
142c19800e8SDoug Rabson der_copy_visible_string (const heim_visible_string *from,
143c19800e8SDoug Rabson 			 heim_visible_string *to)
144c19800e8SDoug Rabson {
145c19800e8SDoug Rabson     return der_copy_general_string(from, to);
146c19800e8SDoug Rabson }
147c19800e8SDoug Rabson 
148c19800e8SDoug Rabson int
149c19800e8SDoug Rabson der_copy_octet_string (const heim_octet_string *from, heim_octet_string *to)
150b528cefcSMark Murray {
151b528cefcSMark Murray     to->length = from->length;
152ed549cb0SCy Schubert     if (from->data == NULL) {
153ed549cb0SCy Schubert         to->data = NULL;
154ed549cb0SCy Schubert         return 0;
155ed549cb0SCy Schubert     }
156b528cefcSMark Murray     to->data = malloc(to->length);
157b528cefcSMark Murray     if (to->length != 0 && to->data == NULL)
158b528cefcSMark Murray 	return ENOMEM;
159b528cefcSMark Murray     memcpy(to->data, from->data, to->length);
160b528cefcSMark Murray     return 0;
161b528cefcSMark Murray }
1624137ff4cSJacques Vidrine 
1634137ff4cSJacques Vidrine int
164c19800e8SDoug Rabson der_copy_heim_integer (const heim_integer *from, heim_integer *to)
165c19800e8SDoug Rabson {
166c19800e8SDoug Rabson     to->length = from->length;
167c19800e8SDoug Rabson     to->data   = malloc(to->length);
168c19800e8SDoug Rabson     if(to->length != 0 && to->data == NULL)
169c19800e8SDoug Rabson 	return ENOMEM;
170c19800e8SDoug Rabson     memcpy(to->data, from->data, to->length);
171c19800e8SDoug Rabson     to->negative = from->negative;
172c19800e8SDoug Rabson     return 0;
173c19800e8SDoug Rabson }
174c19800e8SDoug Rabson 
175c19800e8SDoug Rabson int
176c19800e8SDoug Rabson der_copy_oid (const heim_oid *from, heim_oid *to)
1774137ff4cSJacques Vidrine {
1784137ff4cSJacques Vidrine     to->length     = from->length;
1794137ff4cSJacques Vidrine     to->components = malloc(to->length * sizeof(*to->components));
1804137ff4cSJacques Vidrine     if (to->length != 0 && to->components == NULL)
1814137ff4cSJacques Vidrine 	return ENOMEM;
182c19800e8SDoug Rabson     memcpy(to->components, from->components,
183c19800e8SDoug Rabson 	   to->length * sizeof(*to->components));
184c19800e8SDoug Rabson     return 0;
185c19800e8SDoug Rabson }
186c19800e8SDoug Rabson 
187c19800e8SDoug Rabson int
188c19800e8SDoug Rabson der_copy_bit_string (const heim_bit_string *from, heim_bit_string *to)
189c19800e8SDoug Rabson {
190c19800e8SDoug Rabson     size_t len;
191c19800e8SDoug Rabson 
192c19800e8SDoug Rabson     len = (from->length + 7) / 8;
193c19800e8SDoug Rabson     to->length = from->length;
194c19800e8SDoug Rabson     to->data   = malloc(len);
195c19800e8SDoug Rabson     if(len != 0 && to->data == NULL)
196c19800e8SDoug Rabson 	return ENOMEM;
197c19800e8SDoug Rabson     memcpy(to->data, from->data, len);
1984137ff4cSJacques Vidrine     return 0;
1994137ff4cSJacques Vidrine }
200