xref: /freebsd/crypto/heimdal/lib/asn1/der_copy.c (revision c19800e8cd5640693f36f2040db4ab5e8d738146)
1b528cefcSMark Murray /*
2c19800e8SDoug Rabson  * Copyright (c) 1997 - 2006 Kungliga Tekniska H�gskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6b528cefcSMark Murray  * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray  * modification, are permitted provided that the following conditions
8b528cefcSMark Murray  * are met:
9b528cefcSMark Murray  *
10b528cefcSMark Murray  * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray  *
13b528cefcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray  *    documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray  *
17b528cefcSMark Murray  * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray  *    may be used to endorse or promote products derived from this software
19b528cefcSMark Murray  *    without specific prior written permission.
20b528cefcSMark Murray  *
21b528cefcSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray  * SUCH DAMAGE.
32b528cefcSMark Murray  */
33b528cefcSMark Murray 
34b528cefcSMark Murray #include "der_locl.h"
35b528cefcSMark Murray 
36c19800e8SDoug Rabson RCSID("$Id: der_copy.c 19539 2006-12-28 17:15:05Z lha $");
37b528cefcSMark Murray 
38b528cefcSMark Murray int
39c19800e8SDoug Rabson der_copy_general_string (const heim_general_string *from,
40c19800e8SDoug Rabson 			 heim_general_string *to)
41b528cefcSMark Murray {
42bbd80c28SJacques Vidrine     *to = strdup(*from);
43b528cefcSMark Murray     if(*to == NULL)
44b528cefcSMark Murray 	return ENOMEM;
45b528cefcSMark Murray     return 0;
46b528cefcSMark Murray }
47b528cefcSMark Murray 
48b528cefcSMark Murray int
49c19800e8SDoug Rabson der_copy_utf8string (const heim_utf8_string *from, heim_utf8_string *to)
50c19800e8SDoug Rabson {
51c19800e8SDoug Rabson     return der_copy_general_string(from, to);
52c19800e8SDoug Rabson }
53c19800e8SDoug Rabson 
54c19800e8SDoug Rabson int
55c19800e8SDoug Rabson der_copy_printable_string (const heim_printable_string *from,
56c19800e8SDoug Rabson 		       heim_printable_string *to)
57c19800e8SDoug Rabson {
58c19800e8SDoug Rabson     return der_copy_general_string(from, to);
59c19800e8SDoug Rabson }
60c19800e8SDoug Rabson 
61c19800e8SDoug Rabson int
62c19800e8SDoug Rabson der_copy_ia5_string (const heim_printable_string *from,
63c19800e8SDoug Rabson 		     heim_printable_string *to)
64c19800e8SDoug Rabson {
65c19800e8SDoug Rabson     return der_copy_general_string(from, to);
66c19800e8SDoug Rabson }
67c19800e8SDoug Rabson 
68c19800e8SDoug Rabson int
69c19800e8SDoug Rabson der_copy_bmp_string (const heim_bmp_string *from, heim_bmp_string *to)
70c19800e8SDoug Rabson {
71c19800e8SDoug Rabson     to->length = from->length;
72c19800e8SDoug Rabson     to->data   = malloc(to->length * sizeof(to->data[0]));
73c19800e8SDoug Rabson     if(to->length != 0 && to->data == NULL)
74c19800e8SDoug Rabson 	return ENOMEM;
75c19800e8SDoug Rabson     memcpy(to->data, from->data, to->length * sizeof(to->data[0]));
76c19800e8SDoug Rabson     return 0;
77c19800e8SDoug Rabson }
78c19800e8SDoug Rabson 
79c19800e8SDoug Rabson int
80c19800e8SDoug Rabson der_copy_universal_string (const heim_universal_string *from,
81c19800e8SDoug Rabson 			   heim_universal_string *to)
82c19800e8SDoug Rabson {
83c19800e8SDoug Rabson     to->length = from->length;
84c19800e8SDoug Rabson     to->data   = malloc(to->length * sizeof(to->data[0]));
85c19800e8SDoug Rabson     if(to->length != 0 && to->data == NULL)
86c19800e8SDoug Rabson 	return ENOMEM;
87c19800e8SDoug Rabson     memcpy(to->data, from->data, to->length * sizeof(to->data[0]));
88c19800e8SDoug Rabson     return 0;
89c19800e8SDoug Rabson }
90c19800e8SDoug Rabson 
91c19800e8SDoug Rabson int
92c19800e8SDoug Rabson der_copy_visible_string (const heim_visible_string *from,
93c19800e8SDoug Rabson 			 heim_visible_string *to)
94c19800e8SDoug Rabson {
95c19800e8SDoug Rabson     return der_copy_general_string(from, to);
96c19800e8SDoug Rabson }
97c19800e8SDoug Rabson 
98c19800e8SDoug Rabson int
99c19800e8SDoug Rabson der_copy_octet_string (const heim_octet_string *from, heim_octet_string *to)
100b528cefcSMark Murray {
101b528cefcSMark Murray     to->length = from->length;
102b528cefcSMark Murray     to->data   = malloc(to->length);
103b528cefcSMark Murray     if(to->length != 0 && to->data == NULL)
104b528cefcSMark Murray 	return ENOMEM;
105b528cefcSMark Murray     memcpy(to->data, from->data, to->length);
106b528cefcSMark Murray     return 0;
107b528cefcSMark Murray }
1084137ff4cSJacques Vidrine 
1094137ff4cSJacques Vidrine int
110c19800e8SDoug Rabson der_copy_heim_integer (const heim_integer *from, heim_integer *to)
111c19800e8SDoug Rabson {
112c19800e8SDoug Rabson     to->length = from->length;
113c19800e8SDoug Rabson     to->data   = malloc(to->length);
114c19800e8SDoug Rabson     if(to->length != 0 && to->data == NULL)
115c19800e8SDoug Rabson 	return ENOMEM;
116c19800e8SDoug Rabson     memcpy(to->data, from->data, to->length);
117c19800e8SDoug Rabson     to->negative = from->negative;
118c19800e8SDoug Rabson     return 0;
119c19800e8SDoug Rabson }
120c19800e8SDoug Rabson 
121c19800e8SDoug Rabson int
122c19800e8SDoug Rabson der_copy_oid (const heim_oid *from, heim_oid *to)
1234137ff4cSJacques Vidrine {
1244137ff4cSJacques Vidrine     to->length     = from->length;
1254137ff4cSJacques Vidrine     to->components = malloc(to->length * sizeof(*to->components));
1264137ff4cSJacques Vidrine     if (to->length != 0 && to->components == NULL)
1274137ff4cSJacques Vidrine 	return ENOMEM;
128c19800e8SDoug Rabson     memcpy(to->components, from->components,
129c19800e8SDoug Rabson 	   to->length * sizeof(*to->components));
130c19800e8SDoug Rabson     return 0;
131c19800e8SDoug Rabson }
132c19800e8SDoug Rabson 
133c19800e8SDoug Rabson int
134c19800e8SDoug Rabson der_copy_bit_string (const heim_bit_string *from, heim_bit_string *to)
135c19800e8SDoug Rabson {
136c19800e8SDoug Rabson     size_t len;
137c19800e8SDoug Rabson 
138c19800e8SDoug Rabson     len = (from->length + 7) / 8;
139c19800e8SDoug Rabson     to->length = from->length;
140c19800e8SDoug Rabson     to->data   = malloc(len);
141c19800e8SDoug Rabson     if(len != 0 && to->data == NULL)
142c19800e8SDoug Rabson 	return ENOMEM;
143c19800e8SDoug Rabson     memcpy(to->data, from->data, len);
1444137ff4cSJacques Vidrine     return 0;
1454137ff4cSJacques Vidrine }
146