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