xref: /minix3/crypto/external/bsd/heimdal/dist/lib/hx509/req.c (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc /*	$NetBSD: req.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 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  * Redistribution and use in source and binary forms, with or without
9*ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10*ebfedea0SLionel Sambuc  * are met:
11*ebfedea0SLionel Sambuc  *
12*ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14*ebfedea0SLionel Sambuc  *
15*ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*ebfedea0SLionel Sambuc  *
19*ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20*ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21*ebfedea0SLionel Sambuc  *    without specific prior written permission.
22*ebfedea0SLionel Sambuc  *
23*ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24*ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27*ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34*ebfedea0SLionel Sambuc  */
35*ebfedea0SLionel Sambuc 
36*ebfedea0SLionel Sambuc #include "hx_locl.h"
37*ebfedea0SLionel Sambuc #include <krb5/pkcs10_asn1.h>
38*ebfedea0SLionel Sambuc 
39*ebfedea0SLionel Sambuc struct hx509_request_data {
40*ebfedea0SLionel Sambuc     hx509_name name;
41*ebfedea0SLionel Sambuc     SubjectPublicKeyInfo key;
42*ebfedea0SLionel Sambuc     ExtKeyUsage eku;
43*ebfedea0SLionel Sambuc     GeneralNames san;
44*ebfedea0SLionel Sambuc };
45*ebfedea0SLionel Sambuc 
46*ebfedea0SLionel Sambuc /*
47*ebfedea0SLionel Sambuc  *
48*ebfedea0SLionel Sambuc  */
49*ebfedea0SLionel Sambuc 
50*ebfedea0SLionel Sambuc int
hx509_request_init(hx509_context context,hx509_request * req)51*ebfedea0SLionel Sambuc hx509_request_init(hx509_context context, hx509_request *req)
52*ebfedea0SLionel Sambuc {
53*ebfedea0SLionel Sambuc     *req = calloc(1, sizeof(**req));
54*ebfedea0SLionel Sambuc     if (*req == NULL)
55*ebfedea0SLionel Sambuc 	return ENOMEM;
56*ebfedea0SLionel Sambuc 
57*ebfedea0SLionel Sambuc     return 0;
58*ebfedea0SLionel Sambuc }
59*ebfedea0SLionel Sambuc 
60*ebfedea0SLionel Sambuc void
hx509_request_free(hx509_request * req)61*ebfedea0SLionel Sambuc hx509_request_free(hx509_request *req)
62*ebfedea0SLionel Sambuc {
63*ebfedea0SLionel Sambuc     if ((*req)->name)
64*ebfedea0SLionel Sambuc 	hx509_name_free(&(*req)->name);
65*ebfedea0SLionel Sambuc     free_SubjectPublicKeyInfo(&(*req)->key);
66*ebfedea0SLionel Sambuc     free_ExtKeyUsage(&(*req)->eku);
67*ebfedea0SLionel Sambuc     free_GeneralNames(&(*req)->san);
68*ebfedea0SLionel Sambuc     memset(*req, 0, sizeof(**req));
69*ebfedea0SLionel Sambuc     free(*req);
70*ebfedea0SLionel Sambuc     *req = NULL;
71*ebfedea0SLionel Sambuc }
72*ebfedea0SLionel Sambuc 
73*ebfedea0SLionel Sambuc int
hx509_request_set_name(hx509_context context,hx509_request req,hx509_name name)74*ebfedea0SLionel Sambuc hx509_request_set_name(hx509_context context,
75*ebfedea0SLionel Sambuc 			hx509_request req,
76*ebfedea0SLionel Sambuc 			hx509_name name)
77*ebfedea0SLionel Sambuc {
78*ebfedea0SLionel Sambuc     if (req->name)
79*ebfedea0SLionel Sambuc 	hx509_name_free(&req->name);
80*ebfedea0SLionel Sambuc     if (name) {
81*ebfedea0SLionel Sambuc 	int ret = hx509_name_copy(context, name, &req->name);
82*ebfedea0SLionel Sambuc 	if (ret)
83*ebfedea0SLionel Sambuc 	    return ret;
84*ebfedea0SLionel Sambuc     }
85*ebfedea0SLionel Sambuc     return 0;
86*ebfedea0SLionel Sambuc }
87*ebfedea0SLionel Sambuc 
88*ebfedea0SLionel Sambuc int
hx509_request_get_name(hx509_context context,hx509_request req,hx509_name * name)89*ebfedea0SLionel Sambuc hx509_request_get_name(hx509_context context,
90*ebfedea0SLionel Sambuc 			hx509_request req,
91*ebfedea0SLionel Sambuc 			hx509_name *name)
92*ebfedea0SLionel Sambuc {
93*ebfedea0SLionel Sambuc     if (req->name == NULL) {
94*ebfedea0SLionel Sambuc 	hx509_set_error_string(context, 0, EINVAL, "Request have no name");
95*ebfedea0SLionel Sambuc 	return EINVAL;
96*ebfedea0SLionel Sambuc     }
97*ebfedea0SLionel Sambuc     return hx509_name_copy(context, req->name, name);
98*ebfedea0SLionel Sambuc }
99*ebfedea0SLionel Sambuc 
100*ebfedea0SLionel Sambuc int
hx509_request_set_SubjectPublicKeyInfo(hx509_context context,hx509_request req,const SubjectPublicKeyInfo * key)101*ebfedea0SLionel Sambuc hx509_request_set_SubjectPublicKeyInfo(hx509_context context,
102*ebfedea0SLionel Sambuc 					hx509_request req,
103*ebfedea0SLionel Sambuc 					const SubjectPublicKeyInfo *key)
104*ebfedea0SLionel Sambuc {
105*ebfedea0SLionel Sambuc     free_SubjectPublicKeyInfo(&req->key);
106*ebfedea0SLionel Sambuc     return copy_SubjectPublicKeyInfo(key, &req->key);
107*ebfedea0SLionel Sambuc }
108*ebfedea0SLionel Sambuc 
109*ebfedea0SLionel Sambuc int
hx509_request_get_SubjectPublicKeyInfo(hx509_context context,hx509_request req,SubjectPublicKeyInfo * key)110*ebfedea0SLionel Sambuc hx509_request_get_SubjectPublicKeyInfo(hx509_context context,
111*ebfedea0SLionel Sambuc 					hx509_request req,
112*ebfedea0SLionel Sambuc 					SubjectPublicKeyInfo *key)
113*ebfedea0SLionel Sambuc {
114*ebfedea0SLionel Sambuc     return copy_SubjectPublicKeyInfo(&req->key, key);
115*ebfedea0SLionel Sambuc }
116*ebfedea0SLionel Sambuc 
117*ebfedea0SLionel Sambuc int
_hx509_request_add_eku(hx509_context context,hx509_request req,const heim_oid * oid)118*ebfedea0SLionel Sambuc _hx509_request_add_eku(hx509_context context,
119*ebfedea0SLionel Sambuc 		       hx509_request req,
120*ebfedea0SLionel Sambuc 		       const heim_oid *oid)
121*ebfedea0SLionel Sambuc {
122*ebfedea0SLionel Sambuc     void *val;
123*ebfedea0SLionel Sambuc     int ret;
124*ebfedea0SLionel Sambuc 
125*ebfedea0SLionel Sambuc     val = realloc(req->eku.val, sizeof(req->eku.val[0]) * (req->eku.len + 1));
126*ebfedea0SLionel Sambuc     if (val == NULL)
127*ebfedea0SLionel Sambuc 	return ENOMEM;
128*ebfedea0SLionel Sambuc     req->eku.val = val;
129*ebfedea0SLionel Sambuc 
130*ebfedea0SLionel Sambuc     ret = der_copy_oid(oid, &req->eku.val[req->eku.len]);
131*ebfedea0SLionel Sambuc     if (ret)
132*ebfedea0SLionel Sambuc 	return ret;
133*ebfedea0SLionel Sambuc 
134*ebfedea0SLionel Sambuc     req->eku.len += 1;
135*ebfedea0SLionel Sambuc 
136*ebfedea0SLionel Sambuc     return 0;
137*ebfedea0SLionel Sambuc }
138*ebfedea0SLionel Sambuc 
139*ebfedea0SLionel Sambuc int
_hx509_request_add_dns_name(hx509_context context,hx509_request req,const char * hostname)140*ebfedea0SLionel Sambuc _hx509_request_add_dns_name(hx509_context context,
141*ebfedea0SLionel Sambuc 			    hx509_request req,
142*ebfedea0SLionel Sambuc 			    const char *hostname)
143*ebfedea0SLionel Sambuc {
144*ebfedea0SLionel Sambuc     GeneralName name;
145*ebfedea0SLionel Sambuc 
146*ebfedea0SLionel Sambuc     memset(&name, 0, sizeof(name));
147*ebfedea0SLionel Sambuc     name.element = choice_GeneralName_dNSName;
148*ebfedea0SLionel Sambuc     name.u.dNSName.data = rk_UNCONST(hostname);
149*ebfedea0SLionel Sambuc     name.u.dNSName.length = strlen(hostname);
150*ebfedea0SLionel Sambuc 
151*ebfedea0SLionel Sambuc     return add_GeneralNames(&req->san, &name);
152*ebfedea0SLionel Sambuc }
153*ebfedea0SLionel Sambuc 
154*ebfedea0SLionel Sambuc int
_hx509_request_add_email(hx509_context context,hx509_request req,const char * email)155*ebfedea0SLionel Sambuc _hx509_request_add_email(hx509_context context,
156*ebfedea0SLionel Sambuc 			 hx509_request req,
157*ebfedea0SLionel Sambuc 			 const char *email)
158*ebfedea0SLionel Sambuc {
159*ebfedea0SLionel Sambuc     GeneralName name;
160*ebfedea0SLionel Sambuc 
161*ebfedea0SLionel Sambuc     memset(&name, 0, sizeof(name));
162*ebfedea0SLionel Sambuc     name.element = choice_GeneralName_rfc822Name;
163*ebfedea0SLionel Sambuc     name.u.dNSName.data = rk_UNCONST(email);
164*ebfedea0SLionel Sambuc     name.u.dNSName.length = strlen(email);
165*ebfedea0SLionel Sambuc 
166*ebfedea0SLionel Sambuc     return add_GeneralNames(&req->san, &name);
167*ebfedea0SLionel Sambuc }
168*ebfedea0SLionel Sambuc 
169*ebfedea0SLionel Sambuc 
170*ebfedea0SLionel Sambuc 
171*ebfedea0SLionel Sambuc int
_hx509_request_to_pkcs10(hx509_context context,const hx509_request req,const hx509_private_key signer,heim_octet_string * request)172*ebfedea0SLionel Sambuc _hx509_request_to_pkcs10(hx509_context context,
173*ebfedea0SLionel Sambuc 			 const hx509_request req,
174*ebfedea0SLionel Sambuc 			 const hx509_private_key signer,
175*ebfedea0SLionel Sambuc 			 heim_octet_string *request)
176*ebfedea0SLionel Sambuc {
177*ebfedea0SLionel Sambuc     CertificationRequest r;
178*ebfedea0SLionel Sambuc     heim_octet_string data, os;
179*ebfedea0SLionel Sambuc     int ret;
180*ebfedea0SLionel Sambuc     size_t size;
181*ebfedea0SLionel Sambuc 
182*ebfedea0SLionel Sambuc     if (req->name == NULL) {
183*ebfedea0SLionel Sambuc 	hx509_set_error_string(context, 0, EINVAL,
184*ebfedea0SLionel Sambuc 			       "PKCS10 needs to have a subject");
185*ebfedea0SLionel Sambuc 	return EINVAL;
186*ebfedea0SLionel Sambuc     }
187*ebfedea0SLionel Sambuc 
188*ebfedea0SLionel Sambuc     memset(&r, 0, sizeof(r));
189*ebfedea0SLionel Sambuc     memset(request, 0, sizeof(*request));
190*ebfedea0SLionel Sambuc 
191*ebfedea0SLionel Sambuc     r.certificationRequestInfo.version = pkcs10_v1;
192*ebfedea0SLionel Sambuc 
193*ebfedea0SLionel Sambuc     ret = copy_Name(&req->name->der_name,
194*ebfedea0SLionel Sambuc 		    &r.certificationRequestInfo.subject);
195*ebfedea0SLionel Sambuc     if (ret)
196*ebfedea0SLionel Sambuc 	goto out;
197*ebfedea0SLionel Sambuc     ret = copy_SubjectPublicKeyInfo(&req->key,
198*ebfedea0SLionel Sambuc 				    &r.certificationRequestInfo.subjectPKInfo);
199*ebfedea0SLionel Sambuc     if (ret)
200*ebfedea0SLionel Sambuc 	goto out;
201*ebfedea0SLionel Sambuc     r.certificationRequestInfo.attributes =
202*ebfedea0SLionel Sambuc 	calloc(1, sizeof(*r.certificationRequestInfo.attributes));
203*ebfedea0SLionel Sambuc     if (r.certificationRequestInfo.attributes == NULL) {
204*ebfedea0SLionel Sambuc 	ret = ENOMEM;
205*ebfedea0SLionel Sambuc 	goto out;
206*ebfedea0SLionel Sambuc     }
207*ebfedea0SLionel Sambuc 
208*ebfedea0SLionel Sambuc     ASN1_MALLOC_ENCODE(CertificationRequestInfo, data.data, data.length,
209*ebfedea0SLionel Sambuc 		       &r.certificationRequestInfo, &size, ret);
210*ebfedea0SLionel Sambuc     if (ret)
211*ebfedea0SLionel Sambuc 	goto out;
212*ebfedea0SLionel Sambuc     if (data.length != size)
213*ebfedea0SLionel Sambuc 	abort();
214*ebfedea0SLionel Sambuc 
215*ebfedea0SLionel Sambuc     ret = _hx509_create_signature(context,
216*ebfedea0SLionel Sambuc 				  signer,
217*ebfedea0SLionel Sambuc 				  _hx509_crypto_default_sig_alg,
218*ebfedea0SLionel Sambuc 				  &data,
219*ebfedea0SLionel Sambuc 				  &r.signatureAlgorithm,
220*ebfedea0SLionel Sambuc 				  &os);
221*ebfedea0SLionel Sambuc     free(data.data);
222*ebfedea0SLionel Sambuc     if (ret)
223*ebfedea0SLionel Sambuc 	goto out;
224*ebfedea0SLionel Sambuc     r.signature.data = os.data;
225*ebfedea0SLionel Sambuc     r.signature.length = os.length * 8;
226*ebfedea0SLionel Sambuc 
227*ebfedea0SLionel Sambuc     ASN1_MALLOC_ENCODE(CertificationRequest, data.data, data.length,
228*ebfedea0SLionel Sambuc 		       &r, &size, ret);
229*ebfedea0SLionel Sambuc     if (ret)
230*ebfedea0SLionel Sambuc 	goto out;
231*ebfedea0SLionel Sambuc     if (data.length != size)
232*ebfedea0SLionel Sambuc 	abort();
233*ebfedea0SLionel Sambuc 
234*ebfedea0SLionel Sambuc     *request = data;
235*ebfedea0SLionel Sambuc 
236*ebfedea0SLionel Sambuc out:
237*ebfedea0SLionel Sambuc     free_CertificationRequest(&r);
238*ebfedea0SLionel Sambuc 
239*ebfedea0SLionel Sambuc     return ret;
240*ebfedea0SLionel Sambuc }
241*ebfedea0SLionel Sambuc 
242*ebfedea0SLionel Sambuc int
_hx509_request_parse(hx509_context context,const char * path,hx509_request * req)243*ebfedea0SLionel Sambuc _hx509_request_parse(hx509_context context,
244*ebfedea0SLionel Sambuc 		     const char *path,
245*ebfedea0SLionel Sambuc 		     hx509_request *req)
246*ebfedea0SLionel Sambuc {
247*ebfedea0SLionel Sambuc     CertificationRequest r;
248*ebfedea0SLionel Sambuc     CertificationRequestInfo *rinfo;
249*ebfedea0SLionel Sambuc     hx509_name subject;
250*ebfedea0SLionel Sambuc     size_t len, size;
251*ebfedea0SLionel Sambuc     void *p;
252*ebfedea0SLionel Sambuc     int ret;
253*ebfedea0SLionel Sambuc 
254*ebfedea0SLionel Sambuc     if (strncmp(path, "PKCS10:", 7) != 0) {
255*ebfedea0SLionel Sambuc 	hx509_set_error_string(context, 0, HX509_UNSUPPORTED_OPERATION,
256*ebfedea0SLionel Sambuc 			       "unsupport type in %s", path);
257*ebfedea0SLionel Sambuc 	return HX509_UNSUPPORTED_OPERATION;
258*ebfedea0SLionel Sambuc     }
259*ebfedea0SLionel Sambuc     path += 7;
260*ebfedea0SLionel Sambuc 
261*ebfedea0SLionel Sambuc     /* XXX PEM request */
262*ebfedea0SLionel Sambuc 
263*ebfedea0SLionel Sambuc     ret = rk_undumpdata(path, &p, &len);
264*ebfedea0SLionel Sambuc     if (ret) {
265*ebfedea0SLionel Sambuc 	hx509_set_error_string(context, 0, ret, "Failed to map file %s", path);
266*ebfedea0SLionel Sambuc 	return ret;
267*ebfedea0SLionel Sambuc     }
268*ebfedea0SLionel Sambuc 
269*ebfedea0SLionel Sambuc     ret = decode_CertificationRequest(p, len, &r, &size);
270*ebfedea0SLionel Sambuc     rk_xfree(p);
271*ebfedea0SLionel Sambuc     if (ret) {
272*ebfedea0SLionel Sambuc 	hx509_set_error_string(context, 0, ret, "Failed to decode %s", path);
273*ebfedea0SLionel Sambuc 	return ret;
274*ebfedea0SLionel Sambuc     }
275*ebfedea0SLionel Sambuc 
276*ebfedea0SLionel Sambuc     ret = hx509_request_init(context, req);
277*ebfedea0SLionel Sambuc     if (ret) {
278*ebfedea0SLionel Sambuc 	free_CertificationRequest(&r);
279*ebfedea0SLionel Sambuc 	return ret;
280*ebfedea0SLionel Sambuc     }
281*ebfedea0SLionel Sambuc 
282*ebfedea0SLionel Sambuc     rinfo = &r.certificationRequestInfo;
283*ebfedea0SLionel Sambuc 
284*ebfedea0SLionel Sambuc     ret = hx509_request_set_SubjectPublicKeyInfo(context, *req,
285*ebfedea0SLionel Sambuc 						  &rinfo->subjectPKInfo);
286*ebfedea0SLionel Sambuc     if (ret) {
287*ebfedea0SLionel Sambuc 	free_CertificationRequest(&r);
288*ebfedea0SLionel Sambuc 	hx509_request_free(req);
289*ebfedea0SLionel Sambuc 	return ret;
290*ebfedea0SLionel Sambuc     }
291*ebfedea0SLionel Sambuc 
292*ebfedea0SLionel Sambuc     ret = _hx509_name_from_Name(&rinfo->subject, &subject);
293*ebfedea0SLionel Sambuc     if (ret) {
294*ebfedea0SLionel Sambuc 	free_CertificationRequest(&r);
295*ebfedea0SLionel Sambuc 	hx509_request_free(req);
296*ebfedea0SLionel Sambuc 	return ret;
297*ebfedea0SLionel Sambuc     }
298*ebfedea0SLionel Sambuc     ret = hx509_request_set_name(context, *req, subject);
299*ebfedea0SLionel Sambuc     hx509_name_free(&subject);
300*ebfedea0SLionel Sambuc     free_CertificationRequest(&r);
301*ebfedea0SLionel Sambuc     if (ret) {
302*ebfedea0SLionel Sambuc 	hx509_request_free(req);
303*ebfedea0SLionel Sambuc 	return ret;
304*ebfedea0SLionel Sambuc     }
305*ebfedea0SLionel Sambuc 
306*ebfedea0SLionel Sambuc     return 0;
307*ebfedea0SLionel Sambuc }
308*ebfedea0SLionel Sambuc 
309*ebfedea0SLionel Sambuc 
310*ebfedea0SLionel Sambuc int
_hx509_request_print(hx509_context context,hx509_request req,FILE * f)311*ebfedea0SLionel Sambuc _hx509_request_print(hx509_context context, hx509_request req, FILE *f)
312*ebfedea0SLionel Sambuc {
313*ebfedea0SLionel Sambuc     int ret;
314*ebfedea0SLionel Sambuc 
315*ebfedea0SLionel Sambuc     if (req->name) {
316*ebfedea0SLionel Sambuc 	char *subject;
317*ebfedea0SLionel Sambuc 	ret = hx509_name_to_string(req->name, &subject);
318*ebfedea0SLionel Sambuc 	if (ret) {
319*ebfedea0SLionel Sambuc 	    hx509_set_error_string(context, 0, ret, "Failed to print name");
320*ebfedea0SLionel Sambuc 	    return ret;
321*ebfedea0SLionel Sambuc 	}
322*ebfedea0SLionel Sambuc         fprintf(f, "name: %s\n", subject);
323*ebfedea0SLionel Sambuc 	free(subject);
324*ebfedea0SLionel Sambuc     }
325*ebfedea0SLionel Sambuc 
326*ebfedea0SLionel Sambuc     return 0;
327*ebfedea0SLionel Sambuc }
328*ebfedea0SLionel Sambuc 
329