1*ebfedea0SLionel Sambuc /* $NetBSD: peer.c,v 1.1.1.1 2011/04/13 18:15:12 elric Exp $ */
2*ebfedea0SLionel Sambuc
3*ebfedea0SLionel Sambuc /*
4*ebfedea0SLionel Sambuc * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
5*ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6*ebfedea0SLionel Sambuc * All rights reserved.
7*ebfedea0SLionel Sambuc *
8*ebfedea0SLionel Sambuc * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
9*ebfedea0SLionel Sambuc *
10*ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
11*ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
12*ebfedea0SLionel Sambuc * are met:
13*ebfedea0SLionel Sambuc *
14*ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
15*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
16*ebfedea0SLionel Sambuc *
17*ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
18*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
19*ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
20*ebfedea0SLionel Sambuc *
21*ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
22*ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
23*ebfedea0SLionel Sambuc * without specific prior written permission.
24*ebfedea0SLionel Sambuc *
25*ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26*ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27*ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28*ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29*ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30*ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31*ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32*ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33*ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34*ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35*ebfedea0SLionel Sambuc * SUCH DAMAGE.
36*ebfedea0SLionel Sambuc */
37*ebfedea0SLionel Sambuc
38*ebfedea0SLionel Sambuc #include "hx_locl.h"
39*ebfedea0SLionel Sambuc
40*ebfedea0SLionel Sambuc /**
41*ebfedea0SLionel Sambuc * @page page_peer Hx509 crypto selecting functions
42*ebfedea0SLionel Sambuc *
43*ebfedea0SLionel Sambuc * Peer info structures are used togeter with hx509_crypto_select() to
44*ebfedea0SLionel Sambuc * select the best avaible crypto algorithm to use.
45*ebfedea0SLionel Sambuc *
46*ebfedea0SLionel Sambuc * See the library functions here: @ref hx509_peer
47*ebfedea0SLionel Sambuc */
48*ebfedea0SLionel Sambuc
49*ebfedea0SLionel Sambuc /**
50*ebfedea0SLionel Sambuc * Allocate a new peer info structure an init it to default values.
51*ebfedea0SLionel Sambuc *
52*ebfedea0SLionel Sambuc * @param context A hx509 context.
53*ebfedea0SLionel Sambuc * @param peer return an allocated peer, free with hx509_peer_info_free().
54*ebfedea0SLionel Sambuc *
55*ebfedea0SLionel Sambuc * @return An hx509 error code, see hx509_get_error_string().
56*ebfedea0SLionel Sambuc *
57*ebfedea0SLionel Sambuc * @ingroup hx509_peer
58*ebfedea0SLionel Sambuc */
59*ebfedea0SLionel Sambuc
60*ebfedea0SLionel Sambuc int
hx509_peer_info_alloc(hx509_context context,hx509_peer_info * peer)61*ebfedea0SLionel Sambuc hx509_peer_info_alloc(hx509_context context, hx509_peer_info *peer)
62*ebfedea0SLionel Sambuc {
63*ebfedea0SLionel Sambuc *peer = calloc(1, sizeof(**peer));
64*ebfedea0SLionel Sambuc if (*peer == NULL) {
65*ebfedea0SLionel Sambuc hx509_set_error_string(context, 0, ENOMEM, "out of memory");
66*ebfedea0SLionel Sambuc return ENOMEM;
67*ebfedea0SLionel Sambuc }
68*ebfedea0SLionel Sambuc return 0;
69*ebfedea0SLionel Sambuc }
70*ebfedea0SLionel Sambuc
71*ebfedea0SLionel Sambuc
72*ebfedea0SLionel Sambuc static void
free_cms_alg(hx509_peer_info peer)73*ebfedea0SLionel Sambuc free_cms_alg(hx509_peer_info peer)
74*ebfedea0SLionel Sambuc {
75*ebfedea0SLionel Sambuc if (peer->val) {
76*ebfedea0SLionel Sambuc size_t i;
77*ebfedea0SLionel Sambuc for (i = 0; i < peer->len; i++)
78*ebfedea0SLionel Sambuc free_AlgorithmIdentifier(&peer->val[i]);
79*ebfedea0SLionel Sambuc free(peer->val);
80*ebfedea0SLionel Sambuc peer->val = NULL;
81*ebfedea0SLionel Sambuc peer->len = 0;
82*ebfedea0SLionel Sambuc }
83*ebfedea0SLionel Sambuc }
84*ebfedea0SLionel Sambuc
85*ebfedea0SLionel Sambuc /**
86*ebfedea0SLionel Sambuc * Free a peer info structure.
87*ebfedea0SLionel Sambuc *
88*ebfedea0SLionel Sambuc * @param peer peer info to be freed.
89*ebfedea0SLionel Sambuc *
90*ebfedea0SLionel Sambuc * @ingroup hx509_peer
91*ebfedea0SLionel Sambuc */
92*ebfedea0SLionel Sambuc
93*ebfedea0SLionel Sambuc void
hx509_peer_info_free(hx509_peer_info peer)94*ebfedea0SLionel Sambuc hx509_peer_info_free(hx509_peer_info peer)
95*ebfedea0SLionel Sambuc {
96*ebfedea0SLionel Sambuc if (peer == NULL)
97*ebfedea0SLionel Sambuc return;
98*ebfedea0SLionel Sambuc if (peer->cert)
99*ebfedea0SLionel Sambuc hx509_cert_free(peer->cert);
100*ebfedea0SLionel Sambuc free_cms_alg(peer);
101*ebfedea0SLionel Sambuc memset(peer, 0, sizeof(*peer));
102*ebfedea0SLionel Sambuc free(peer);
103*ebfedea0SLionel Sambuc }
104*ebfedea0SLionel Sambuc
105*ebfedea0SLionel Sambuc /**
106*ebfedea0SLionel Sambuc * Set the certificate that remote peer is using.
107*ebfedea0SLionel Sambuc *
108*ebfedea0SLionel Sambuc * @param peer peer info to update
109*ebfedea0SLionel Sambuc * @param cert cerificate of the remote peer.
110*ebfedea0SLionel Sambuc *
111*ebfedea0SLionel Sambuc * @return An hx509 error code, see hx509_get_error_string().
112*ebfedea0SLionel Sambuc *
113*ebfedea0SLionel Sambuc * @ingroup hx509_peer
114*ebfedea0SLionel Sambuc */
115*ebfedea0SLionel Sambuc
116*ebfedea0SLionel Sambuc int
hx509_peer_info_set_cert(hx509_peer_info peer,hx509_cert cert)117*ebfedea0SLionel Sambuc hx509_peer_info_set_cert(hx509_peer_info peer,
118*ebfedea0SLionel Sambuc hx509_cert cert)
119*ebfedea0SLionel Sambuc {
120*ebfedea0SLionel Sambuc if (peer->cert)
121*ebfedea0SLionel Sambuc hx509_cert_free(peer->cert);
122*ebfedea0SLionel Sambuc peer->cert = hx509_cert_ref(cert);
123*ebfedea0SLionel Sambuc return 0;
124*ebfedea0SLionel Sambuc }
125*ebfedea0SLionel Sambuc
126*ebfedea0SLionel Sambuc /**
127*ebfedea0SLionel Sambuc * Add an additional algorithm that the peer supports.
128*ebfedea0SLionel Sambuc *
129*ebfedea0SLionel Sambuc * @param context A hx509 context.
130*ebfedea0SLionel Sambuc * @param peer the peer to set the new algorithms for
131*ebfedea0SLionel Sambuc * @param val an AlgorithmsIdentier to add
132*ebfedea0SLionel Sambuc *
133*ebfedea0SLionel Sambuc * @return An hx509 error code, see hx509_get_error_string().
134*ebfedea0SLionel Sambuc *
135*ebfedea0SLionel Sambuc * @ingroup hx509_peer
136*ebfedea0SLionel Sambuc */
137*ebfedea0SLionel Sambuc
138*ebfedea0SLionel Sambuc int
hx509_peer_info_add_cms_alg(hx509_context context,hx509_peer_info peer,const AlgorithmIdentifier * val)139*ebfedea0SLionel Sambuc hx509_peer_info_add_cms_alg(hx509_context context,
140*ebfedea0SLionel Sambuc hx509_peer_info peer,
141*ebfedea0SLionel Sambuc const AlgorithmIdentifier *val)
142*ebfedea0SLionel Sambuc {
143*ebfedea0SLionel Sambuc void *ptr;
144*ebfedea0SLionel Sambuc int ret;
145*ebfedea0SLionel Sambuc
146*ebfedea0SLionel Sambuc ptr = realloc(peer->val, sizeof(peer->val[0]) * (peer->len + 1));
147*ebfedea0SLionel Sambuc if (ptr == NULL) {
148*ebfedea0SLionel Sambuc hx509_set_error_string(context, 0, ENOMEM, "out of memory");
149*ebfedea0SLionel Sambuc return ENOMEM;
150*ebfedea0SLionel Sambuc }
151*ebfedea0SLionel Sambuc peer->val = ptr;
152*ebfedea0SLionel Sambuc ret = copy_AlgorithmIdentifier(val, &peer->val[peer->len]);
153*ebfedea0SLionel Sambuc if (ret == 0)
154*ebfedea0SLionel Sambuc peer->len += 1;
155*ebfedea0SLionel Sambuc else
156*ebfedea0SLionel Sambuc hx509_set_error_string(context, 0, ret, "out of memory");
157*ebfedea0SLionel Sambuc return ret;
158*ebfedea0SLionel Sambuc }
159*ebfedea0SLionel Sambuc
160*ebfedea0SLionel Sambuc /**
161*ebfedea0SLionel Sambuc * Set the algorithms that the peer supports.
162*ebfedea0SLionel Sambuc *
163*ebfedea0SLionel Sambuc * @param context A hx509 context.
164*ebfedea0SLionel Sambuc * @param peer the peer to set the new algorithms for
165*ebfedea0SLionel Sambuc * @param val array of supported AlgorithmsIdentiers
166*ebfedea0SLionel Sambuc * @param len length of array val.
167*ebfedea0SLionel Sambuc *
168*ebfedea0SLionel Sambuc * @return An hx509 error code, see hx509_get_error_string().
169*ebfedea0SLionel Sambuc *
170*ebfedea0SLionel Sambuc * @ingroup hx509_peer
171*ebfedea0SLionel Sambuc */
172*ebfedea0SLionel Sambuc
173*ebfedea0SLionel Sambuc int
hx509_peer_info_set_cms_algs(hx509_context context,hx509_peer_info peer,const AlgorithmIdentifier * val,size_t len)174*ebfedea0SLionel Sambuc hx509_peer_info_set_cms_algs(hx509_context context,
175*ebfedea0SLionel Sambuc hx509_peer_info peer,
176*ebfedea0SLionel Sambuc const AlgorithmIdentifier *val,
177*ebfedea0SLionel Sambuc size_t len)
178*ebfedea0SLionel Sambuc {
179*ebfedea0SLionel Sambuc size_t i;
180*ebfedea0SLionel Sambuc
181*ebfedea0SLionel Sambuc free_cms_alg(peer);
182*ebfedea0SLionel Sambuc
183*ebfedea0SLionel Sambuc peer->val = calloc(len, sizeof(*peer->val));
184*ebfedea0SLionel Sambuc if (peer->val == NULL) {
185*ebfedea0SLionel Sambuc peer->len = 0;
186*ebfedea0SLionel Sambuc hx509_set_error_string(context, 0, ENOMEM, "out of memory");
187*ebfedea0SLionel Sambuc return ENOMEM;
188*ebfedea0SLionel Sambuc }
189*ebfedea0SLionel Sambuc peer->len = len;
190*ebfedea0SLionel Sambuc for (i = 0; i < len; i++) {
191*ebfedea0SLionel Sambuc int ret;
192*ebfedea0SLionel Sambuc ret = copy_AlgorithmIdentifier(&val[i], &peer->val[i]);
193*ebfedea0SLionel Sambuc if (ret) {
194*ebfedea0SLionel Sambuc hx509_clear_error_string(context);
195*ebfedea0SLionel Sambuc free_cms_alg(peer);
196*ebfedea0SLionel Sambuc return ret;
197*ebfedea0SLionel Sambuc }
198*ebfedea0SLionel Sambuc }
199*ebfedea0SLionel Sambuc return 0;
200*ebfedea0SLionel Sambuc }
201*ebfedea0SLionel Sambuc
202*ebfedea0SLionel Sambuc #if 0
203*ebfedea0SLionel Sambuc
204*ebfedea0SLionel Sambuc /*
205*ebfedea0SLionel Sambuc * S/MIME
206*ebfedea0SLionel Sambuc */
207*ebfedea0SLionel Sambuc
208*ebfedea0SLionel Sambuc int
209*ebfedea0SLionel Sambuc hx509_peer_info_parse_smime(hx509_peer_info peer,
210*ebfedea0SLionel Sambuc const heim_octet_string *data)
211*ebfedea0SLionel Sambuc {
212*ebfedea0SLionel Sambuc return 0;
213*ebfedea0SLionel Sambuc }
214*ebfedea0SLionel Sambuc
215*ebfedea0SLionel Sambuc int
216*ebfedea0SLionel Sambuc hx509_peer_info_unparse_smime(hx509_peer_info peer,
217*ebfedea0SLionel Sambuc heim_octet_string *data)
218*ebfedea0SLionel Sambuc {
219*ebfedea0SLionel Sambuc return 0;
220*ebfedea0SLionel Sambuc }
221*ebfedea0SLionel Sambuc
222*ebfedea0SLionel Sambuc /*
223*ebfedea0SLionel Sambuc * For storing hx509_peer_info to be able to cache them.
224*ebfedea0SLionel Sambuc */
225*ebfedea0SLionel Sambuc
226*ebfedea0SLionel Sambuc int
227*ebfedea0SLionel Sambuc hx509_peer_info_parse(hx509_peer_info peer,
228*ebfedea0SLionel Sambuc const heim_octet_string *data)
229*ebfedea0SLionel Sambuc {
230*ebfedea0SLionel Sambuc return 0;
231*ebfedea0SLionel Sambuc }
232*ebfedea0SLionel Sambuc
233*ebfedea0SLionel Sambuc int
234*ebfedea0SLionel Sambuc hx509_peer_info_unparse(hx509_peer_info peer,
235*ebfedea0SLionel Sambuc heim_octet_string *data)
236*ebfedea0SLionel Sambuc {
237*ebfedea0SLionel Sambuc return 0;
238*ebfedea0SLionel Sambuc }
239*ebfedea0SLionel Sambuc #endif
240