xref: /freebsd/crypto/heimdal/lib/asn1/der_copy.c (revision ae77177087c655fc883075af4f425b37e032cd05)
1b528cefcSMark Murray /*
2*ae771770SStanislav Sedov  * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6*ae771770SStanislav Sedov  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7*ae771770SStanislav 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 
38*ae771770SStanislav 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
51*ae771770SStanislav Sedov der_copy_integer (const int *from, int *to)
52*ae771770SStanislav Sedov {
53*ae771770SStanislav Sedov     *to = *from;
54*ae771770SStanislav Sedov     return 0;
55*ae771770SStanislav Sedov }
56*ae771770SStanislav Sedov 
57*ae771770SStanislav Sedov int
58*ae771770SStanislav Sedov der_copy_unsigned (const unsigned *from, unsigned *to)
59*ae771770SStanislav Sedov {
60*ae771770SStanislav Sedov     *to = *from;
61*ae771770SStanislav Sedov     return 0;
62*ae771770SStanislav Sedov }
63*ae771770SStanislav Sedov 
64*ae771770SStanislav Sedov int
65*ae771770SStanislav Sedov der_copy_generalized_time (const time_t *from, time_t *to)
66*ae771770SStanislav Sedov {
67*ae771770SStanislav Sedov     *to = *from;
68*ae771770SStanislav Sedov     return 0;
69*ae771770SStanislav Sedov }
70*ae771770SStanislav Sedov 
71*ae771770SStanislav Sedov int
72*ae771770SStanislav Sedov der_copy_utctime (const time_t *from, time_t *to)
73*ae771770SStanislav Sedov {
74*ae771770SStanislav Sedov     *to = *from;
75*ae771770SStanislav Sedov     return 0;
76*ae771770SStanislav Sedov }
77*ae771770SStanislav Sedov 
78*ae771770SStanislav Sedov int
79c19800e8SDoug Rabson der_copy_utf8string (const heim_utf8_string *from, heim_utf8_string *to)
80c19800e8SDoug Rabson {
81c19800e8SDoug Rabson     return der_copy_general_string(from, to);
82c19800e8SDoug Rabson }
83c19800e8SDoug Rabson 
84c19800e8SDoug Rabson int
85c19800e8SDoug Rabson der_copy_printable_string (const heim_printable_string *from,
86c19800e8SDoug Rabson 		       heim_printable_string *to)
87c19800e8SDoug Rabson {
88*ae771770SStanislav Sedov     to->length = from->length;
89*ae771770SStanislav Sedov     to->data   = malloc(to->length + 1);
90*ae771770SStanislav Sedov     if(to->data == NULL)
91*ae771770SStanislav Sedov 	return ENOMEM;
92*ae771770SStanislav Sedov     memcpy(to->data, from->data, to->length);
93*ae771770SStanislav Sedov     ((char *)to->data)[to->length] = '\0';
94*ae771770SStanislav Sedov     return 0;
95c19800e8SDoug Rabson }
96c19800e8SDoug Rabson 
97c19800e8SDoug Rabson int
98*ae771770SStanislav Sedov der_copy_ia5_string (const heim_ia5_string *from,
99*ae771770SStanislav Sedov 		     heim_ia5_string *to)
100c19800e8SDoug Rabson {
101*ae771770SStanislav Sedov     return der_copy_printable_string(from, to);
102c19800e8SDoug Rabson }
103c19800e8SDoug Rabson 
104c19800e8SDoug Rabson int
105c19800e8SDoug Rabson der_copy_bmp_string (const heim_bmp_string *from, heim_bmp_string *to)
106c19800e8SDoug Rabson {
107c19800e8SDoug Rabson     to->length = from->length;
108c19800e8SDoug Rabson     to->data   = malloc(to->length * sizeof(to->data[0]));
109c19800e8SDoug Rabson     if(to->length != 0 && to->data == NULL)
110c19800e8SDoug Rabson 	return ENOMEM;
111c19800e8SDoug Rabson     memcpy(to->data, from->data, to->length * sizeof(to->data[0]));
112c19800e8SDoug Rabson     return 0;
113c19800e8SDoug Rabson }
114c19800e8SDoug Rabson 
115c19800e8SDoug Rabson int
116c19800e8SDoug Rabson der_copy_universal_string (const heim_universal_string *from,
117c19800e8SDoug Rabson 			   heim_universal_string *to)
118c19800e8SDoug Rabson {
119c19800e8SDoug Rabson     to->length = from->length;
120c19800e8SDoug Rabson     to->data   = malloc(to->length * sizeof(to->data[0]));
121c19800e8SDoug Rabson     if(to->length != 0 && to->data == NULL)
122c19800e8SDoug Rabson 	return ENOMEM;
123c19800e8SDoug Rabson     memcpy(to->data, from->data, to->length * sizeof(to->data[0]));
124c19800e8SDoug Rabson     return 0;
125c19800e8SDoug Rabson }
126c19800e8SDoug Rabson 
127c19800e8SDoug Rabson int
128c19800e8SDoug Rabson der_copy_visible_string (const heim_visible_string *from,
129c19800e8SDoug Rabson 			 heim_visible_string *to)
130c19800e8SDoug Rabson {
131c19800e8SDoug Rabson     return der_copy_general_string(from, to);
132c19800e8SDoug Rabson }
133c19800e8SDoug Rabson 
134c19800e8SDoug Rabson int
135c19800e8SDoug Rabson der_copy_octet_string (const heim_octet_string *from, heim_octet_string *to)
136b528cefcSMark Murray {
137b528cefcSMark Murray     to->length = from->length;
138b528cefcSMark Murray     to->data   = malloc(to->length);
139b528cefcSMark Murray     if(to->length != 0 && to->data == NULL)
140b528cefcSMark Murray 	return ENOMEM;
141b528cefcSMark Murray     memcpy(to->data, from->data, to->length);
142b528cefcSMark Murray     return 0;
143b528cefcSMark Murray }
1444137ff4cSJacques Vidrine 
1454137ff4cSJacques Vidrine int
146c19800e8SDoug Rabson der_copy_heim_integer (const heim_integer *from, heim_integer *to)
147c19800e8SDoug Rabson {
148c19800e8SDoug Rabson     to->length = from->length;
149c19800e8SDoug Rabson     to->data   = malloc(to->length);
150c19800e8SDoug Rabson     if(to->length != 0 && to->data == NULL)
151c19800e8SDoug Rabson 	return ENOMEM;
152c19800e8SDoug Rabson     memcpy(to->data, from->data, to->length);
153c19800e8SDoug Rabson     to->negative = from->negative;
154c19800e8SDoug Rabson     return 0;
155c19800e8SDoug Rabson }
156c19800e8SDoug Rabson 
157c19800e8SDoug Rabson int
158c19800e8SDoug Rabson der_copy_oid (const heim_oid *from, heim_oid *to)
1594137ff4cSJacques Vidrine {
1604137ff4cSJacques Vidrine     to->length     = from->length;
1614137ff4cSJacques Vidrine     to->components = malloc(to->length * sizeof(*to->components));
1624137ff4cSJacques Vidrine     if (to->length != 0 && to->components == NULL)
1634137ff4cSJacques Vidrine 	return ENOMEM;
164c19800e8SDoug Rabson     memcpy(to->components, from->components,
165c19800e8SDoug Rabson 	   to->length * sizeof(*to->components));
166c19800e8SDoug Rabson     return 0;
167c19800e8SDoug Rabson }
168c19800e8SDoug Rabson 
169c19800e8SDoug Rabson int
170c19800e8SDoug Rabson der_copy_bit_string (const heim_bit_string *from, heim_bit_string *to)
171c19800e8SDoug Rabson {
172c19800e8SDoug Rabson     size_t len;
173c19800e8SDoug Rabson 
174c19800e8SDoug Rabson     len = (from->length + 7) / 8;
175c19800e8SDoug Rabson     to->length = from->length;
176c19800e8SDoug Rabson     to->data   = malloc(len);
177c19800e8SDoug Rabson     if(len != 0 && to->data == NULL)
178c19800e8SDoug Rabson 	return ENOMEM;
179c19800e8SDoug Rabson     memcpy(to->data, from->data, len);
1804137ff4cSJacques Vidrine     return 0;
1814137ff4cSJacques Vidrine }
182