xref: /freebsd/crypto/heimdal/lib/asn1/extra.c (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 /*
2  * Copyright (c) 2003 - 2005 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the Institute nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include "der_locl.h"
37 #include "heim_asn1.h"
38 
39 RCSID("$Id$");
40 
41 int
42 encode_heim_any(unsigned char *p, size_t len,
43 		const heim_any *data, size_t *size)
44 {
45     return der_put_octet_string (p, len, data, size);
46 }
47 
48 int
49 decode_heim_any(const unsigned char *p, size_t len,
50 		heim_any *data, size_t *size)
51 {
52     size_t len_len, length, l;
53     Der_class thisclass;
54     Der_type thistype;
55     unsigned int thistag;
56     int e;
57 
58     memset(data, 0, sizeof(*data));
59 
60     e = der_get_tag (p, len, &thisclass, &thistype, &thistag, &l);
61     if (e) return e;
62     if (l > len)
63 	return ASN1_OVERFLOW;
64     e = der_get_length(p + l, len - l, &length, &len_len);
65     if (e) return e;
66     if (length == ASN1_INDEFINITE) {
67         if (len < len_len + l)
68 	    return ASN1_OVERFLOW;
69 	length = len - (len_len + l);
70     } else {
71 	if (len < length + len_len + l)
72 	    return ASN1_OVERFLOW;
73     }
74 
75     data->data = malloc(length + len_len + l);
76     if (data->data == NULL)
77 	return ENOMEM;
78     data->length = length + len_len + l;
79     memcpy(data->data, p, length + len_len + l);
80 
81     if (size)
82 	*size = length + len_len + l;
83 
84     return 0;
85 }
86 
87 void
88 free_heim_any(heim_any *data)
89 {
90     der_free_octet_string(data);
91 }
92 
93 size_t
94 length_heim_any(const heim_any *data)
95 {
96     return data->length;
97 }
98 
99 int
100 copy_heim_any(const heim_any *from, heim_any *to)
101 {
102     return der_copy_octet_string(from, to);
103 }
104 
105 int
106 encode_heim_any_set(unsigned char *p, size_t len,
107 		    const heim_any_set *data, size_t *size)
108 {
109     return der_put_octet_string (p, len, data, size);
110 }
111 
112 int
113 decode_heim_any_set(const unsigned char *p, size_t len,
114 		heim_any_set *data, size_t *size)
115 {
116     return der_get_octet_string(p, len, data, size);
117 }
118 
119 void
120 free_heim_any_set(heim_any_set *data)
121 {
122     der_free_octet_string(data);
123 }
124 
125 size_t
126 length_heim_any_set(const heim_any *data)
127 {
128     return data->length;
129 }
130 
131 int
132 copy_heim_any_set(const heim_any_set *from, heim_any_set *to)
133 {
134     return der_copy_octet_string(from, to);
135 }
136 
137 int
138 heim_any_cmp(const heim_any_set *p, const heim_any_set *q)
139 {
140     return der_heim_octet_string_cmp(p, q);
141 }
142