r23456: Update Samba4 to current lorikeet-heimdal.
[sfrench/samba-autobuild/.git] / source4 / heimdal / lib / hx509 / req.c
1 /*
2  * Copyright (c) 2006 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright 
14  *    notice, this list of conditions and the following disclaimer in the 
15  *    documentation and/or other materials provided with the distribution. 
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors 
18  *    may be used to endorse or promote products derived from this software 
19  *    without specific prior written permission. 
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
31  * SUCH DAMAGE. 
32  */
33
34 #include "hx_locl.h"
35 #include <pkcs10_asn1.h>
36 RCSID("$Id: req.c 20934 2007-06-06 15:30:02Z lha $");
37
38 struct hx509_request_data {
39     hx509_name name;
40     SubjectPublicKeyInfo key;
41     ExtKeyUsage eku;
42     GeneralNames san;
43 };
44
45 /*
46  *
47  */
48
49 int
50 _hx509_request_init(hx509_context context, hx509_request *req)
51 {
52     *req = calloc(1, sizeof(**req));
53     if (*req == NULL)
54         return ENOMEM;
55
56     return 0;
57 }
58
59 void
60 _hx509_request_free(hx509_request *req)
61 {
62     if ((*req)->name)
63         hx509_name_free(&(*req)->name);
64     free_SubjectPublicKeyInfo(&(*req)->key);
65     free_ExtKeyUsage(&(*req)->eku);
66     free_GeneralNames(&(*req)->san);
67     memset(*req, 0, sizeof(**req));
68     free(*req);
69     *req = NULL;
70 }
71
72 int
73 _hx509_request_set_name(hx509_context context,
74                         hx509_request req,
75                         hx509_name name)
76 {
77     if (req->name)
78         hx509_name_free(&req->name);
79     if (name) {
80         int ret = hx509_name_copy(context, name, &req->name);
81         if (ret)
82             return ret;
83     }
84     return 0;
85 }
86
87 int
88 _hx509_request_set_SubjectPublicKeyInfo(hx509_context context,
89                                         hx509_request req,
90                                         const SubjectPublicKeyInfo *key)
91 {
92     free_SubjectPublicKeyInfo(&req->key);
93     return copy_SubjectPublicKeyInfo(key, &req->key);
94 }
95
96 int
97 _hx509_request_add_eku(hx509_context context,
98                        hx509_request req,
99                        const heim_oid *oid)
100 {
101     void *val;
102     int ret;
103
104     val = realloc(req->eku.val, sizeof(req->eku.val[0]) * (req->eku.len + 1));
105     if (val == NULL)
106         return ENOMEM;
107     req->eku.val = val;
108
109     ret = der_copy_oid(oid, &req->eku.val[req->eku.len]);
110     if (ret)
111         return ret;
112
113     req->eku.len += 1;
114
115     return 0;
116 }
117
118 int
119 _hx509_request_add_dns_name(hx509_context context,
120                             hx509_request req,
121                             const char *hostname)
122 {
123     GeneralName name;
124
125     memset(&name, 0, sizeof(name));
126     name.element = choice_GeneralName_dNSName;
127     name.u.dNSName = rk_UNCONST(hostname);
128
129     return add_GeneralNames(&req->san, &name);
130 }
131
132 int
133 _hx509_request_add_email(hx509_context context,
134                          hx509_request req,
135                          const char *email)
136 {
137     GeneralName name;
138
139     memset(&name, 0, sizeof(name));
140     name.element = choice_GeneralName_rfc822Name;
141     name.u.dNSName = rk_UNCONST(email);
142
143     return add_GeneralNames(&req->san, &name);
144 }
145
146
147
148 int
149 _hx509_request_to_pkcs10(hx509_context context,
150                          const hx509_request req,
151                          const hx509_private_key signer,
152                          heim_octet_string *request)
153 {
154     CertificationRequest r;
155     heim_octet_string data, os;
156     int ret;
157     size_t size;
158
159     if (req->name == NULL) {
160         hx509_set_error_string(context, 0, EINVAL,
161                                "PKCS10 needs to have a subject");
162         return EINVAL;
163     }
164
165     memset(&r, 0, sizeof(r));
166     memset(request, 0, sizeof(*request));
167
168     r.certificationRequestInfo.version = pkcs10_v1;
169
170     ret = copy_Name(&req->name->der_name,
171                     &r.certificationRequestInfo.subject);
172     if (ret)
173         goto out;
174     ret = copy_SubjectPublicKeyInfo(&req->key,
175                                     &r.certificationRequestInfo.subjectPKInfo);
176     if (ret)
177         goto out;
178     r.certificationRequestInfo.attributes = 
179         calloc(1, sizeof(*r.certificationRequestInfo.attributes));
180     if (r.certificationRequestInfo.attributes == NULL) {
181         ret = ENOMEM;
182         goto out;
183     }
184
185     ASN1_MALLOC_ENCODE(CertificationRequestInfo, data.data, data.length, 
186                        &r.certificationRequestInfo, &size, ret);
187     if (ret)
188         goto out;
189     if (data.length != size)
190         abort();
191
192     ret = _hx509_create_signature(context,
193                                   signer,
194                                   _hx509_crypto_default_sig_alg,
195                                   &data,
196                                   &r.signatureAlgorithm,
197                                   &os);
198     free(data.data);
199     if (ret)
200         goto out;
201     r.signature.data = os.data;
202     r.signature.length = os.length * 8;
203
204     ASN1_MALLOC_ENCODE(CertificationRequest, data.data, data.length,
205                        &r, &size, ret);
206     if (ret)
207         goto out;
208     if (data.length != size)
209         abort();
210
211     *request = data;
212
213 out:
214     free_CertificationRequest(&r);
215
216     return ret;
217 }