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