xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/hx509/peer.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1*d3273b5bSchristos /*	$NetBSD: peer.c,v 1.2 2017/01/28 21:31:48 christos Exp $	*/
2ca1c9b0cSelric 
3ca1c9b0cSelric /*
4ca1c9b0cSelric  * Copyright (c) 2006 - 2007 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 "hx_locl.h"
39ca1c9b0cSelric 
40ca1c9b0cSelric /**
41ca1c9b0cSelric  * @page page_peer Hx509 crypto selecting functions
42ca1c9b0cSelric  *
43ca1c9b0cSelric  * Peer info structures are used togeter with hx509_crypto_select() to
44ca1c9b0cSelric  * select the best avaible crypto algorithm to use.
45ca1c9b0cSelric  *
46ca1c9b0cSelric  * See the library functions here: @ref hx509_peer
47ca1c9b0cSelric  */
48ca1c9b0cSelric 
49ca1c9b0cSelric /**
50ca1c9b0cSelric  * Allocate a new peer info structure an init it to default values.
51ca1c9b0cSelric  *
52ca1c9b0cSelric  * @param context A hx509 context.
53ca1c9b0cSelric  * @param peer return an allocated peer, free with hx509_peer_info_free().
54ca1c9b0cSelric  *
55ca1c9b0cSelric  * @return An hx509 error code, see hx509_get_error_string().
56ca1c9b0cSelric  *
57ca1c9b0cSelric  * @ingroup hx509_peer
58ca1c9b0cSelric  */
59ca1c9b0cSelric 
60ca1c9b0cSelric int
hx509_peer_info_alloc(hx509_context context,hx509_peer_info * peer)61ca1c9b0cSelric hx509_peer_info_alloc(hx509_context context, hx509_peer_info *peer)
62ca1c9b0cSelric {
63ca1c9b0cSelric     *peer = calloc(1, sizeof(**peer));
64ca1c9b0cSelric     if (*peer == NULL) {
65ca1c9b0cSelric 	hx509_set_error_string(context, 0, ENOMEM, "out of memory");
66ca1c9b0cSelric 	return ENOMEM;
67ca1c9b0cSelric     }
68ca1c9b0cSelric     return 0;
69ca1c9b0cSelric }
70ca1c9b0cSelric 
71ca1c9b0cSelric 
72ca1c9b0cSelric static void
free_cms_alg(hx509_peer_info peer)73ca1c9b0cSelric free_cms_alg(hx509_peer_info peer)
74ca1c9b0cSelric {
75ca1c9b0cSelric     if (peer->val) {
76ca1c9b0cSelric 	size_t i;
77ca1c9b0cSelric 	for (i = 0; i < peer->len; i++)
78ca1c9b0cSelric 	    free_AlgorithmIdentifier(&peer->val[i]);
79ca1c9b0cSelric 	free(peer->val);
80ca1c9b0cSelric 	peer->val = NULL;
81ca1c9b0cSelric 	peer->len = 0;
82ca1c9b0cSelric     }
83ca1c9b0cSelric }
84ca1c9b0cSelric 
85ca1c9b0cSelric /**
86ca1c9b0cSelric  * Free a peer info structure.
87ca1c9b0cSelric  *
88ca1c9b0cSelric  * @param peer peer info to be freed.
89ca1c9b0cSelric  *
90ca1c9b0cSelric  * @ingroup hx509_peer
91ca1c9b0cSelric  */
92ca1c9b0cSelric 
93ca1c9b0cSelric void
hx509_peer_info_free(hx509_peer_info peer)94ca1c9b0cSelric hx509_peer_info_free(hx509_peer_info peer)
95ca1c9b0cSelric {
96ca1c9b0cSelric     if (peer == NULL)
97ca1c9b0cSelric 	return;
98ca1c9b0cSelric     if (peer->cert)
99ca1c9b0cSelric 	hx509_cert_free(peer->cert);
100ca1c9b0cSelric     free_cms_alg(peer);
101ca1c9b0cSelric     memset(peer, 0, sizeof(*peer));
102ca1c9b0cSelric     free(peer);
103ca1c9b0cSelric }
104ca1c9b0cSelric 
105ca1c9b0cSelric /**
106ca1c9b0cSelric  * Set the certificate that remote peer is using.
107ca1c9b0cSelric  *
108ca1c9b0cSelric  * @param peer peer info to update
109ca1c9b0cSelric  * @param cert cerificate of the remote peer.
110ca1c9b0cSelric  *
111ca1c9b0cSelric  * @return An hx509 error code, see hx509_get_error_string().
112ca1c9b0cSelric  *
113ca1c9b0cSelric  * @ingroup hx509_peer
114ca1c9b0cSelric  */
115ca1c9b0cSelric 
116ca1c9b0cSelric int
hx509_peer_info_set_cert(hx509_peer_info peer,hx509_cert cert)117ca1c9b0cSelric hx509_peer_info_set_cert(hx509_peer_info peer,
118ca1c9b0cSelric 			 hx509_cert cert)
119ca1c9b0cSelric {
120ca1c9b0cSelric     if (peer->cert)
121ca1c9b0cSelric 	hx509_cert_free(peer->cert);
122ca1c9b0cSelric     peer->cert = hx509_cert_ref(cert);
123ca1c9b0cSelric     return 0;
124ca1c9b0cSelric }
125ca1c9b0cSelric 
126ca1c9b0cSelric /**
127ca1c9b0cSelric  * Add an additional algorithm that the peer supports.
128ca1c9b0cSelric  *
129ca1c9b0cSelric  * @param context A hx509 context.
130ca1c9b0cSelric  * @param peer the peer to set the new algorithms for
131ca1c9b0cSelric  * @param val an AlgorithmsIdentier to add
132ca1c9b0cSelric  *
133ca1c9b0cSelric  * @return An hx509 error code, see hx509_get_error_string().
134ca1c9b0cSelric  *
135ca1c9b0cSelric  * @ingroup hx509_peer
136ca1c9b0cSelric  */
137ca1c9b0cSelric 
138ca1c9b0cSelric int
hx509_peer_info_add_cms_alg(hx509_context context,hx509_peer_info peer,const AlgorithmIdentifier * val)139ca1c9b0cSelric hx509_peer_info_add_cms_alg(hx509_context context,
140ca1c9b0cSelric 			    hx509_peer_info peer,
141ca1c9b0cSelric 			    const AlgorithmIdentifier *val)
142ca1c9b0cSelric {
143ca1c9b0cSelric     void *ptr;
144ca1c9b0cSelric     int ret;
145ca1c9b0cSelric 
146ca1c9b0cSelric     ptr = realloc(peer->val, sizeof(peer->val[0]) * (peer->len + 1));
147ca1c9b0cSelric     if (ptr == NULL) {
148ca1c9b0cSelric 	hx509_set_error_string(context, 0, ENOMEM, "out of memory");
149ca1c9b0cSelric 	return ENOMEM;
150ca1c9b0cSelric     }
151ca1c9b0cSelric     peer->val = ptr;
152ca1c9b0cSelric     ret = copy_AlgorithmIdentifier(val, &peer->val[peer->len]);
153ca1c9b0cSelric     if (ret == 0)
154ca1c9b0cSelric 	peer->len += 1;
155ca1c9b0cSelric     else
156ca1c9b0cSelric 	hx509_set_error_string(context, 0, ret, "out of memory");
157ca1c9b0cSelric     return ret;
158ca1c9b0cSelric }
159ca1c9b0cSelric 
160ca1c9b0cSelric /**
161ca1c9b0cSelric  * Set the algorithms that the peer supports.
162ca1c9b0cSelric  *
163ca1c9b0cSelric  * @param context A hx509 context.
164ca1c9b0cSelric  * @param peer the peer to set the new algorithms for
165ca1c9b0cSelric  * @param val array of supported AlgorithmsIdentiers
166ca1c9b0cSelric  * @param len length of array val.
167ca1c9b0cSelric  *
168ca1c9b0cSelric  * @return An hx509 error code, see hx509_get_error_string().
169ca1c9b0cSelric  *
170ca1c9b0cSelric  * @ingroup hx509_peer
171ca1c9b0cSelric  */
172ca1c9b0cSelric 
173ca1c9b0cSelric int
hx509_peer_info_set_cms_algs(hx509_context context,hx509_peer_info peer,const AlgorithmIdentifier * val,size_t len)174ca1c9b0cSelric hx509_peer_info_set_cms_algs(hx509_context context,
175ca1c9b0cSelric 			     hx509_peer_info peer,
176ca1c9b0cSelric 			     const AlgorithmIdentifier *val,
177ca1c9b0cSelric 			     size_t len)
178ca1c9b0cSelric {
179ca1c9b0cSelric     size_t i;
180ca1c9b0cSelric 
181ca1c9b0cSelric     free_cms_alg(peer);
182ca1c9b0cSelric 
183ca1c9b0cSelric     peer->val = calloc(len, sizeof(*peer->val));
184ca1c9b0cSelric     if (peer->val == NULL) {
185ca1c9b0cSelric 	peer->len = 0;
186ca1c9b0cSelric 	hx509_set_error_string(context, 0, ENOMEM, "out of memory");
187ca1c9b0cSelric 	return ENOMEM;
188ca1c9b0cSelric     }
189ca1c9b0cSelric     peer->len = len;
190ca1c9b0cSelric     for (i = 0; i < len; i++) {
191ca1c9b0cSelric 	int ret;
192ca1c9b0cSelric 	ret = copy_AlgorithmIdentifier(&val[i], &peer->val[i]);
193ca1c9b0cSelric 	if (ret) {
194ca1c9b0cSelric 	    hx509_clear_error_string(context);
195ca1c9b0cSelric 	    free_cms_alg(peer);
196ca1c9b0cSelric 	    return ret;
197ca1c9b0cSelric 	}
198ca1c9b0cSelric     }
199ca1c9b0cSelric     return 0;
200ca1c9b0cSelric }
201ca1c9b0cSelric 
202ca1c9b0cSelric #if 0
203ca1c9b0cSelric 
204ca1c9b0cSelric /*
205ca1c9b0cSelric  * S/MIME
206ca1c9b0cSelric  */
207ca1c9b0cSelric 
208ca1c9b0cSelric int
209ca1c9b0cSelric hx509_peer_info_parse_smime(hx509_peer_info peer,
210ca1c9b0cSelric 			    const heim_octet_string *data)
211ca1c9b0cSelric {
212ca1c9b0cSelric     return 0;
213ca1c9b0cSelric }
214ca1c9b0cSelric 
215ca1c9b0cSelric int
216ca1c9b0cSelric hx509_peer_info_unparse_smime(hx509_peer_info peer,
217ca1c9b0cSelric 			      heim_octet_string *data)
218ca1c9b0cSelric {
219ca1c9b0cSelric     return 0;
220ca1c9b0cSelric }
221ca1c9b0cSelric 
222ca1c9b0cSelric /*
223ca1c9b0cSelric  * For storing hx509_peer_info to be able to cache them.
224ca1c9b0cSelric  */
225ca1c9b0cSelric 
226ca1c9b0cSelric int
227ca1c9b0cSelric hx509_peer_info_parse(hx509_peer_info peer,
228ca1c9b0cSelric 		      const heim_octet_string *data)
229ca1c9b0cSelric {
230ca1c9b0cSelric     return 0;
231ca1c9b0cSelric }
232ca1c9b0cSelric 
233ca1c9b0cSelric int
234ca1c9b0cSelric hx509_peer_info_unparse(hx509_peer_info peer,
235ca1c9b0cSelric 			heim_octet_string *data)
236ca1c9b0cSelric {
237ca1c9b0cSelric     return 0;
238ca1c9b0cSelric }
239ca1c9b0cSelric #endif
240