]>
Commit | Line | Data |
---|---|---|
02e4fbed | 1 | /* ocsp_srv.c */ |
0f113f3e MC |
2 | /* |
3 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project | |
4 | * 2001. | |
02e4fbed DSH |
5 | */ |
6 | /* ==================================================================== | |
7 | * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. | |
8 | * | |
9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | |
12 | * | |
13 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 14 | * notice, this list of conditions and the following disclaimer. |
02e4fbed DSH |
15 | * |
16 | * 2. Redistributions in binary form must reproduce the above copyright | |
17 | * notice, this list of conditions and the following disclaimer in | |
18 | * the documentation and/or other materials provided with the | |
19 | * distribution. | |
20 | * | |
21 | * 3. All advertising materials mentioning features or use of this | |
22 | * software must display the following acknowledgment: | |
23 | * "This product includes software developed by the OpenSSL Project | |
24 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | |
25 | * | |
26 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
27 | * endorse or promote products derived from this software without | |
28 | * prior written permission. For written permission, please contact | |
29 | * openssl-core@openssl.org. | |
30 | * | |
31 | * 5. Products derived from this software may not be called "OpenSSL" | |
32 | * nor may "OpenSSL" appear in their names without prior written | |
33 | * permission of the OpenSSL Project. | |
34 | * | |
35 | * 6. Redistributions of any form whatsoever must retain the following | |
36 | * acknowledgment: | |
37 | * "This product includes software developed by the OpenSSL Project | |
38 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | |
39 | * | |
40 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
41 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
43 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
44 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
45 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
46 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
47 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
49 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
50 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
51 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
52 | * ==================================================================== | |
53 | * | |
54 | * This product includes cryptographic software written by Eric Young | |
55 | * (eay@cryptsoft.com). This product includes software written by Tim | |
56 | * Hudson (tjh@cryptsoft.com). | |
57 | * | |
58 | */ | |
59 | ||
60 | #include <stdio.h> | |
61 | #include <cryptlib.h> | |
62 | #include <openssl/objects.h> | |
63 | #include <openssl/rand.h> | |
64 | #include <openssl/x509.h> | |
65 | #include <openssl/pem.h> | |
66 | #include <openssl/x509v3.h> | |
67 | #include <openssl/ocsp.h> | |
6ef869d7 | 68 | #include "ocsp_lcl.h" |
02e4fbed | 69 | |
0f113f3e MC |
70 | /* |
71 | * Utility functions related to sending OCSP responses and extracting | |
02e4fbed DSH |
72 | * relevant information from the request. |
73 | */ | |
74 | ||
75 | int OCSP_request_onereq_count(OCSP_REQUEST *req) | |
0f113f3e MC |
76 | { |
77 | return sk_OCSP_ONEREQ_num(req->tbsRequest->requestList); | |
78 | } | |
02e4fbed DSH |
79 | |
80 | OCSP_ONEREQ *OCSP_request_onereq_get0(OCSP_REQUEST *req, int i) | |
0f113f3e MC |
81 | { |
82 | return sk_OCSP_ONEREQ_value(req->tbsRequest->requestList, i); | |
83 | } | |
02e4fbed DSH |
84 | |
85 | OCSP_CERTID *OCSP_onereq_get0_id(OCSP_ONEREQ *one) | |
0f113f3e MC |
86 | { |
87 | return one->reqCert; | |
88 | } | |
02e4fbed DSH |
89 | |
90 | int OCSP_id_get0_info(ASN1_OCTET_STRING **piNameHash, ASN1_OBJECT **pmd, | |
0f113f3e MC |
91 | ASN1_OCTET_STRING **pikeyHash, |
92 | ASN1_INTEGER **pserial, OCSP_CERTID *cid) | |
93 | { | |
94 | if (!cid) | |
95 | return 0; | |
96 | if (pmd) | |
97 | *pmd = cid->hashAlgorithm->algorithm; | |
98 | if (piNameHash) | |
99 | *piNameHash = cid->issuerNameHash; | |
100 | if (pikeyHash) | |
101 | *pikeyHash = cid->issuerKeyHash; | |
102 | if (pserial) | |
103 | *pserial = cid->serialNumber; | |
104 | return 1; | |
105 | } | |
02e4fbed | 106 | |
fafc7f98 | 107 | int OCSP_request_is_signed(OCSP_REQUEST *req) |
0f113f3e MC |
108 | { |
109 | if (req->optionalSignature) | |
110 | return 1; | |
111 | return 0; | |
112 | } | |
fafc7f98 | 113 | |
02e4fbed DSH |
114 | /* Create an OCSP response and encode an optional basic response */ |
115 | OCSP_RESPONSE *OCSP_response_create(int status, OCSP_BASICRESP *bs) | |
0f113f3e MC |
116 | { |
117 | OCSP_RESPONSE *rsp = NULL; | |
118 | ||
119 | if (!(rsp = OCSP_RESPONSE_new())) | |
120 | goto err; | |
121 | if (!(ASN1_ENUMERATED_set(rsp->responseStatus, status))) | |
122 | goto err; | |
123 | if (!bs) | |
124 | return rsp; | |
125 | if (!(rsp->responseBytes = OCSP_RESPBYTES_new())) | |
126 | goto err; | |
127 | rsp->responseBytes->responseType = OBJ_nid2obj(NID_id_pkix_OCSP_basic); | |
128 | if (!ASN1_item_pack | |
129 | (bs, ASN1_ITEM_rptr(OCSP_BASICRESP), &rsp->responseBytes->response)) | |
130 | goto err; | |
131 | return rsp; | |
132 | err: | |
133 | if (rsp) | |
134 | OCSP_RESPONSE_free(rsp); | |
135 | return NULL; | |
136 | } | |
02e4fbed DSH |
137 | |
138 | OCSP_SINGLERESP *OCSP_basic_add1_status(OCSP_BASICRESP *rsp, | |
0f113f3e MC |
139 | OCSP_CERTID *cid, |
140 | int status, int reason, | |
141 | ASN1_TIME *revtime, | |
142 | ASN1_TIME *thisupd, | |
143 | ASN1_TIME *nextupd) | |
144 | { | |
145 | OCSP_SINGLERESP *single = NULL; | |
146 | OCSP_CERTSTATUS *cs; | |
147 | OCSP_REVOKEDINFO *ri; | |
148 | ||
149 | if (!rsp->tbsResponseData->responses && | |
150 | !(rsp->tbsResponseData->responses = sk_OCSP_SINGLERESP_new_null())) | |
151 | goto err; | |
152 | ||
153 | if (!(single = OCSP_SINGLERESP_new())) | |
154 | goto err; | |
155 | ||
156 | if (!ASN1_TIME_to_generalizedtime(thisupd, &single->thisUpdate)) | |
157 | goto err; | |
158 | if (nextupd && | |
159 | !ASN1_TIME_to_generalizedtime(nextupd, &single->nextUpdate)) | |
160 | goto err; | |
161 | ||
162 | OCSP_CERTID_free(single->certId); | |
163 | ||
164 | if (!(single->certId = OCSP_CERTID_dup(cid))) | |
165 | goto err; | |
166 | ||
167 | cs = single->certStatus; | |
168 | switch (cs->type = status) { | |
169 | case V_OCSP_CERTSTATUS_REVOKED: | |
170 | if (!revtime) { | |
171 | OCSPerr(OCSP_F_OCSP_BASIC_ADD1_STATUS, OCSP_R_NO_REVOKED_TIME); | |
172 | goto err; | |
173 | } | |
174 | if (!(cs->value.revoked = ri = OCSP_REVOKEDINFO_new())) | |
175 | goto err; | |
176 | if (!ASN1_TIME_to_generalizedtime(revtime, &ri->revocationTime)) | |
177 | goto err; | |
178 | if (reason != OCSP_REVOKED_STATUS_NOSTATUS) { | |
179 | if (!(ri->revocationReason = ASN1_ENUMERATED_new())) | |
180 | goto err; | |
181 | if (!(ASN1_ENUMERATED_set(ri->revocationReason, reason))) | |
182 | goto err; | |
183 | } | |
184 | break; | |
185 | ||
186 | case V_OCSP_CERTSTATUS_GOOD: | |
187 | cs->value.good = ASN1_NULL_new(); | |
188 | break; | |
189 | ||
190 | case V_OCSP_CERTSTATUS_UNKNOWN: | |
191 | cs->value.unknown = ASN1_NULL_new(); | |
192 | break; | |
193 | ||
194 | default: | |
195 | goto err; | |
196 | ||
197 | } | |
198 | if (!(sk_OCSP_SINGLERESP_push(rsp->tbsResponseData->responses, single))) | |
199 | goto err; | |
200 | return single; | |
201 | err: | |
202 | OCSP_SINGLERESP_free(single); | |
203 | return NULL; | |
204 | } | |
02e4fbed DSH |
205 | |
206 | /* Add a certificate to an OCSP request */ | |
207 | ||
208 | int OCSP_basic_add1_cert(OCSP_BASICRESP *resp, X509 *cert) | |
0f113f3e MC |
209 | { |
210 | if (!resp->certs && !(resp->certs = sk_X509_new_null())) | |
211 | return 0; | |
212 | ||
213 | if (!sk_X509_push(resp->certs, cert)) | |
214 | return 0; | |
215 | CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509); | |
216 | return 1; | |
217 | } | |
218 | ||
219 | int OCSP_basic_sign(OCSP_BASICRESP *brsp, | |
220 | X509 *signer, EVP_PKEY *key, const EVP_MD *dgst, | |
221 | STACK_OF(X509) *certs, unsigned long flags) | |
222 | { | |
223 | int i; | |
224 | OCSP_RESPID *rid; | |
225 | ||
226 | if (!X509_check_private_key(signer, key)) { | |
227 | OCSPerr(OCSP_F_OCSP_BASIC_SIGN, | |
228 | OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); | |
229 | goto err; | |
230 | } | |
231 | ||
232 | if (!(flags & OCSP_NOCERTS)) { | |
233 | if (!OCSP_basic_add1_cert(brsp, signer)) | |
234 | goto err; | |
235 | for (i = 0; i < sk_X509_num(certs); i++) { | |
236 | X509 *tmpcert = sk_X509_value(certs, i); | |
237 | if (!OCSP_basic_add1_cert(brsp, tmpcert)) | |
238 | goto err; | |
239 | } | |
240 | } | |
241 | ||
242 | rid = brsp->tbsResponseData->responderId; | |
243 | if (flags & OCSP_RESPID_KEY) { | |
244 | unsigned char md[SHA_DIGEST_LENGTH]; | |
245 | X509_pubkey_digest(signer, EVP_sha1(), md, NULL); | |
246 | if (!(rid->value.byKey = ASN1_OCTET_STRING_new())) | |
247 | goto err; | |
248 | if (!(ASN1_OCTET_STRING_set(rid->value.byKey, md, SHA_DIGEST_LENGTH))) | |
249 | goto err; | |
250 | rid->type = V_OCSP_RESPID_KEY; | |
251 | } else { | |
252 | if (!X509_NAME_set(&rid->value.byName, X509_get_subject_name(signer))) | |
253 | goto err; | |
254 | rid->type = V_OCSP_RESPID_NAME; | |
255 | } | |
256 | ||
257 | if (!(flags & OCSP_NOTIME) && | |
258 | !X509_gmtime_adj(brsp->tbsResponseData->producedAt, 0)) | |
259 | goto err; | |
260 | ||
261 | /* | |
262 | * Right now, I think that not doing double hashing is the right thing. | |
263 | * -- Richard Levitte | |
264 | */ | |
265 | ||
266 | if (!OCSP_BASICRESP_sign(brsp, key, dgst, 0)) | |
267 | goto err; | |
268 | ||
269 | return 1; | |
270 | err: | |
271 | return 0; | |
272 | } |