2 * Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
7 * History: This file was transfered to Richard Levitte from CertCo by Kathy
8 * Weinhold in mid-spring 2000 to be included in OpenSSL or released as a
12 /* ====================================================================
13 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in
24 * the documentation and/or other materials provided with the
27 * 3. All advertising materials mentioning features or use of this
28 * software must display the following acknowledgment:
29 * "This product includes software developed by the OpenSSL Project
30 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
32 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
33 * endorse or promote products derived from this software without
34 * prior written permission. For written permission, please contact
35 * openssl-core@openssl.org.
37 * 5. Products derived from this software may not be called "OpenSSL"
38 * nor may "OpenSSL" appear in their names without prior written
39 * permission of the OpenSSL Project.
41 * 6. Redistributions of any form whatsoever must retain the following
43 * "This product includes software developed by the OpenSSL Project
44 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
46 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
47 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
50 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
51 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
53 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
55 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
57 * OF THE POSSIBILITY OF SUCH DAMAGE.
58 * ====================================================================
60 * This product includes cryptographic software written by Eric Young
61 * (eay@cryptsoft.com). This product includes software written by Tim
62 * Hudson (tjh@cryptsoft.com).
68 #include "internal/cryptlib.h"
69 #include <openssl/objects.h>
70 #include <openssl/rand.h>
71 #include <openssl/x509.h>
72 #include <openssl/pem.h>
73 #include <openssl/x509v3.h>
74 #include <openssl/ocsp.h>
78 * Utility functions related to sending OCSP requests and extracting relevant
79 * information from the response.
83 * Add an OCSP_CERTID to an OCSP request. Return new OCSP_ONEREQ pointer:
84 * useful if we want to add extensions.
87 OCSP_ONEREQ
*OCSP_request_add0_id(OCSP_REQUEST
*req
, OCSP_CERTID
*cid
)
89 OCSP_ONEREQ
*one
= NULL
;
91 if ((one
= OCSP_ONEREQ_new()) == NULL
)
93 OCSP_CERTID_free(one
->reqCert
);
95 if (req
&& !sk_OCSP_ONEREQ_push(req
->tbsRequest
.requestList
, one
))
99 OCSP_ONEREQ_free(one
);
103 /* Set requestorName from an X509_NAME structure */
105 int OCSP_request_set1_name(OCSP_REQUEST
*req
, X509_NAME
*nm
)
109 gen
= GENERAL_NAME_new();
112 if (!X509_NAME_set(&gen
->d
.directoryName
, nm
)) {
113 GENERAL_NAME_free(gen
);
116 gen
->type
= GEN_DIRNAME
;
117 GENERAL_NAME_free(req
->tbsRequest
.requestorName
);
118 req
->tbsRequest
.requestorName
= gen
;
122 /* Add a certificate to an OCSP request */
124 int OCSP_request_add1_cert(OCSP_REQUEST
*req
, X509
*cert
)
127 if (req
->optionalSignature
== NULL
)
128 req
->optionalSignature
= OCSP_SIGNATURE_new();
129 sig
= req
->optionalSignature
;
134 if (sig
->certs
== NULL
135 && (sig
->certs
= sk_X509_new_null()) == NULL
)
138 if (!sk_X509_push(sig
->certs
, cert
))
145 * Sign an OCSP request set the requestorName to the subjec name of an
146 * optional signers certificate and include one or more optional certificates
147 * in the request. Behaves like PKCS7_sign().
150 int OCSP_request_sign(OCSP_REQUEST
*req
,
154 STACK_OF(X509
) *certs
, unsigned long flags
)
159 if (!OCSP_request_set1_name(req
, X509_get_subject_name(signer
)))
162 if ((req
->optionalSignature
= OCSP_SIGNATURE_new()) == NULL
)
165 if (!X509_check_private_key(signer
, key
)) {
166 OCSPerr(OCSP_F_OCSP_REQUEST_SIGN
,
167 OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE
);
170 if (!OCSP_REQUEST_sign(req
, key
, dgst
))
174 if (!(flags
& OCSP_NOCERTS
)) {
175 if (!OCSP_request_add1_cert(req
, signer
))
177 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
178 x
= sk_X509_value(certs
, i
);
179 if (!OCSP_request_add1_cert(req
, x
))
186 OCSP_SIGNATURE_free(req
->optionalSignature
);
187 req
->optionalSignature
= NULL
;
191 /* Get response status */
193 int OCSP_response_status(OCSP_RESPONSE
*resp
)
195 return ASN1_ENUMERATED_get(resp
->responseStatus
);
199 * Extract basic response from OCSP_RESPONSE or NULL if no basic response
203 OCSP_BASICRESP
*OCSP_response_get1_basic(OCSP_RESPONSE
*resp
)
206 rb
= resp
->responseBytes
;
208 OCSPerr(OCSP_F_OCSP_RESPONSE_GET1_BASIC
, OCSP_R_NO_RESPONSE_DATA
);
211 if (OBJ_obj2nid(rb
->responseType
) != NID_id_pkix_OCSP_basic
) {
212 OCSPerr(OCSP_F_OCSP_RESPONSE_GET1_BASIC
, OCSP_R_NOT_BASIC_RESPONSE
);
216 return ASN1_item_unpack(rb
->response
, ASN1_ITEM_rptr(OCSP_BASICRESP
));
219 ASN1_OCTET_STRING
*OCSP_resp_get0_signature(OCSP_BASICRESP
*bs
)
221 return bs
->signature
;
225 * Return number of OCSP_SINGLERESP reponses present in a basic response.
228 int OCSP_resp_count(OCSP_BASICRESP
*bs
)
232 return sk_OCSP_SINGLERESP_num(bs
->tbsResponseData
.responses
);
235 /* Extract an OCSP_SINGLERESP response with a given index */
237 OCSP_SINGLERESP
*OCSP_resp_get0(OCSP_BASICRESP
*bs
, int idx
)
241 return sk_OCSP_SINGLERESP_value(bs
->tbsResponseData
.responses
, idx
);
244 ASN1_GENERALIZEDTIME
*OCSP_resp_get0_produced_at(OCSP_BASICRESP
* bs
)
248 return bs
->tbsResponseData
.producedAt
;
251 /* Look single response matching a given certificate ID */
253 int OCSP_resp_find(OCSP_BASICRESP
*bs
, OCSP_CERTID
*id
, int last
)
256 STACK_OF(OCSP_SINGLERESP
) *sresp
;
257 OCSP_SINGLERESP
*single
;
264 sresp
= bs
->tbsResponseData
.responses
;
265 for (i
= last
; i
< sk_OCSP_SINGLERESP_num(sresp
); i
++) {
266 single
= sk_OCSP_SINGLERESP_value(sresp
, i
);
267 if (!OCSP_id_cmp(id
, single
->certId
))
274 * Extract status information from an OCSP_SINGLERESP structure. Note: the
275 * revtime and reason values are only set if the certificate status is
276 * revoked. Returns numerical value of status.
279 int OCSP_single_get0_status(OCSP_SINGLERESP
*single
, int *reason
,
280 ASN1_GENERALIZEDTIME
**revtime
,
281 ASN1_GENERALIZEDTIME
**thisupd
,
282 ASN1_GENERALIZEDTIME
**nextupd
)
285 OCSP_CERTSTATUS
*cst
;
288 cst
= single
->certStatus
;
290 if (ret
== V_OCSP_CERTSTATUS_REVOKED
) {
291 OCSP_REVOKEDINFO
*rev
= cst
->value
.revoked
;
293 *revtime
= rev
->revocationTime
;
295 if (rev
->revocationReason
)
296 *reason
= ASN1_ENUMERATED_get(rev
->revocationReason
);
302 *thisupd
= single
->thisUpdate
;
304 *nextupd
= single
->nextUpdate
;
309 * This function combines the previous ones: look up a certificate ID and if
310 * found extract status information. Return 0 is successful.
313 int OCSP_resp_find_status(OCSP_BASICRESP
*bs
, OCSP_CERTID
*id
, int *status
,
315 ASN1_GENERALIZEDTIME
**revtime
,
316 ASN1_GENERALIZEDTIME
**thisupd
,
317 ASN1_GENERALIZEDTIME
**nextupd
)
320 OCSP_SINGLERESP
*single
;
321 i
= OCSP_resp_find(bs
, id
, -1);
322 /* Maybe check for multiple responses and give an error? */
325 single
= OCSP_resp_get0(bs
, i
);
326 i
= OCSP_single_get0_status(single
, reason
, revtime
, thisupd
, nextupd
);
333 * Check validity of thisUpdate and nextUpdate fields. It is possible that
334 * the request will take a few seconds to process and/or the time wont be
335 * totally accurate. Therefore to avoid rejecting otherwise valid time we
336 * allow the times to be within 'nsec' of the current time. Also to avoid
337 * accepting very old responses without a nextUpdate field an optional maxage
338 * parameter specifies the maximum age the thisUpdate field can be.
341 int OCSP_check_validity(ASN1_GENERALIZEDTIME
*thisupd
,
342 ASN1_GENERALIZEDTIME
*nextupd
, long nsec
, long maxsec
)
347 /* Check thisUpdate is valid and not more than nsec in the future */
348 if (!ASN1_GENERALIZEDTIME_check(thisupd
)) {
349 OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY
, OCSP_R_ERROR_IN_THISUPDATE_FIELD
);
352 t_tmp
= t_now
+ nsec
;
353 if (X509_cmp_time(thisupd
, &t_tmp
) > 0) {
354 OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY
, OCSP_R_STATUS_NOT_YET_VALID
);
359 * If maxsec specified check thisUpdate is not more than maxsec in
363 t_tmp
= t_now
- maxsec
;
364 if (X509_cmp_time(thisupd
, &t_tmp
) < 0) {
365 OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY
, OCSP_R_STATUS_TOO_OLD
);
374 /* Check nextUpdate is valid and not more than nsec in the past */
375 if (!ASN1_GENERALIZEDTIME_check(nextupd
)) {
376 OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY
, OCSP_R_ERROR_IN_NEXTUPDATE_FIELD
);
379 t_tmp
= t_now
- nsec
;
380 if (X509_cmp_time(nextupd
, &t_tmp
) < 0) {
381 OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY
, OCSP_R_STATUS_EXPIRED
);
386 /* Also don't allow nextUpdate to precede thisUpdate */
387 if (ASN1_STRING_cmp(nextupd
, thisupd
) < 0) {
388 OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY
,
389 OCSP_R_NEXTUPDATE_BEFORE_THISUPDATE
);
396 OCSP_CERTID
*OCSP_SINGLERESP_get0_id(OCSP_SINGLERESP
*single
)
398 return single
->certId
;