xref: /onnv-gate/usr/src/common/openssl/crypto/ocsp/ocsp_lib.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* ocsp_lib.c */
2*0Sstevel@tonic-gate /* Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
3*0Sstevel@tonic-gate  * project. */
4*0Sstevel@tonic-gate 
5*0Sstevel@tonic-gate /* History:
6*0Sstevel@tonic-gate    This file was transfered to Richard Levitte from CertCo by Kathy
7*0Sstevel@tonic-gate    Weinhold in mid-spring 2000 to be included in OpenSSL or released
8*0Sstevel@tonic-gate    as a patch kit. */
9*0Sstevel@tonic-gate 
10*0Sstevel@tonic-gate /* ====================================================================
11*0Sstevel@tonic-gate  * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
12*0Sstevel@tonic-gate  *
13*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
14*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
15*0Sstevel@tonic-gate  * are met:
16*0Sstevel@tonic-gate  *
17*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
18*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
21*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
22*0Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
23*0Sstevel@tonic-gate  *    distribution.
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
26*0Sstevel@tonic-gate  *    software must display the following acknowledgment:
27*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
28*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
29*0Sstevel@tonic-gate  *
30*0Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
31*0Sstevel@tonic-gate  *    endorse or promote products derived from this software without
32*0Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
33*0Sstevel@tonic-gate  *    openssl-core@openssl.org.
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
36*0Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
37*0Sstevel@tonic-gate  *    permission of the OpenSSL Project.
38*0Sstevel@tonic-gate  *
39*0Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
40*0Sstevel@tonic-gate  *    acknowledgment:
41*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
42*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
43*0Sstevel@tonic-gate  *
44*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
45*0Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47*0Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
48*0Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49*0Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51*0Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
53*0Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54*0Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
55*0Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
56*0Sstevel@tonic-gate  * ====================================================================
57*0Sstevel@tonic-gate  *
58*0Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
59*0Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
60*0Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
61*0Sstevel@tonic-gate  *
62*0Sstevel@tonic-gate  */
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate #include <stdio.h>
65*0Sstevel@tonic-gate #include <cryptlib.h>
66*0Sstevel@tonic-gate #include <openssl/objects.h>
67*0Sstevel@tonic-gate #include <openssl/rand.h>
68*0Sstevel@tonic-gate #include <openssl/x509.h>
69*0Sstevel@tonic-gate #include <openssl/pem.h>
70*0Sstevel@tonic-gate #include <openssl/x509v3.h>
71*0Sstevel@tonic-gate #include <openssl/ocsp.h>
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate /* Convert a certificate and its issuer to an OCSP_CERTID */
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate OCSP_CERTID *OCSP_cert_to_id(const EVP_MD *dgst, X509 *subject, X509 *issuer)
76*0Sstevel@tonic-gate {
77*0Sstevel@tonic-gate 	X509_NAME *iname;
78*0Sstevel@tonic-gate 	ASN1_INTEGER *serial;
79*0Sstevel@tonic-gate 	ASN1_BIT_STRING *ikey;
80*0Sstevel@tonic-gate #ifndef OPENSSL_NO_SHA1
81*0Sstevel@tonic-gate 	if(!dgst) dgst = EVP_sha1();
82*0Sstevel@tonic-gate #endif
83*0Sstevel@tonic-gate 	if (subject)
84*0Sstevel@tonic-gate 		{
85*0Sstevel@tonic-gate 		iname = X509_get_issuer_name(subject);
86*0Sstevel@tonic-gate 		serial = X509_get_serialNumber(subject);
87*0Sstevel@tonic-gate 		}
88*0Sstevel@tonic-gate 	else
89*0Sstevel@tonic-gate 		{
90*0Sstevel@tonic-gate 		iname = X509_get_subject_name(issuer);
91*0Sstevel@tonic-gate 		serial = NULL;
92*0Sstevel@tonic-gate 		}
93*0Sstevel@tonic-gate 	ikey = X509_get0_pubkey_bitstr(issuer);
94*0Sstevel@tonic-gate 	return OCSP_cert_id_new(dgst, iname, ikey, serial);
95*0Sstevel@tonic-gate }
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate OCSP_CERTID *OCSP_cert_id_new(const EVP_MD *dgst,
99*0Sstevel@tonic-gate 			      X509_NAME *issuerName,
100*0Sstevel@tonic-gate 			      ASN1_BIT_STRING* issuerKey,
101*0Sstevel@tonic-gate 			      ASN1_INTEGER *serialNumber)
102*0Sstevel@tonic-gate         {
103*0Sstevel@tonic-gate 	int nid;
104*0Sstevel@tonic-gate         unsigned int i;
105*0Sstevel@tonic-gate 	X509_ALGOR *alg;
106*0Sstevel@tonic-gate 	OCSP_CERTID *cid = NULL;
107*0Sstevel@tonic-gate 	unsigned char md[EVP_MAX_MD_SIZE];
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	if (!(cid = OCSP_CERTID_new())) goto err;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	alg = cid->hashAlgorithm;
112*0Sstevel@tonic-gate 	if (alg->algorithm != NULL) ASN1_OBJECT_free(alg->algorithm);
113*0Sstevel@tonic-gate 	if ((nid = EVP_MD_type(dgst)) == NID_undef)
114*0Sstevel@tonic-gate 	        {
115*0Sstevel@tonic-gate 		OCSPerr(OCSP_F_CERT_ID_NEW,OCSP_R_UNKNOWN_NID);
116*0Sstevel@tonic-gate 		goto err;
117*0Sstevel@tonic-gate 		}
118*0Sstevel@tonic-gate 	if (!(alg->algorithm=OBJ_nid2obj(nid))) goto err;
119*0Sstevel@tonic-gate 	if ((alg->parameter=ASN1_TYPE_new()) == NULL) goto err;
120*0Sstevel@tonic-gate 	alg->parameter->type=V_ASN1_NULL;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	if (!X509_NAME_digest(issuerName, dgst, md, &i)) goto digerr;
123*0Sstevel@tonic-gate 	if (!(ASN1_OCTET_STRING_set(cid->issuerNameHash, md, i))) goto err;
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	/* Calculate the issuerKey hash, excluding tag and length */
126*0Sstevel@tonic-gate 	EVP_Digest(issuerKey->data, issuerKey->length, md, &i, dgst, NULL);
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	if (!(ASN1_OCTET_STRING_set(cid->issuerKeyHash, md, i))) goto err;
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	if (serialNumber)
131*0Sstevel@tonic-gate 		{
132*0Sstevel@tonic-gate 		ASN1_INTEGER_free(cid->serialNumber);
133*0Sstevel@tonic-gate 		if (!(cid->serialNumber = ASN1_INTEGER_dup(serialNumber))) goto err;
134*0Sstevel@tonic-gate 		}
135*0Sstevel@tonic-gate 	return cid;
136*0Sstevel@tonic-gate digerr:
137*0Sstevel@tonic-gate 	OCSPerr(OCSP_F_CERT_ID_NEW,OCSP_R_DIGEST_ERR);
138*0Sstevel@tonic-gate err:
139*0Sstevel@tonic-gate 	if (cid) OCSP_CERTID_free(cid);
140*0Sstevel@tonic-gate 	return NULL;
141*0Sstevel@tonic-gate 	}
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate int OCSP_id_issuer_cmp(OCSP_CERTID *a, OCSP_CERTID *b)
144*0Sstevel@tonic-gate 	{
145*0Sstevel@tonic-gate 	int ret;
146*0Sstevel@tonic-gate 	ret = OBJ_cmp(a->hashAlgorithm->algorithm, b->hashAlgorithm->algorithm);
147*0Sstevel@tonic-gate 	if (ret) return ret;
148*0Sstevel@tonic-gate 	ret = ASN1_OCTET_STRING_cmp(a->issuerNameHash, b->issuerNameHash);
149*0Sstevel@tonic-gate 	if (ret) return ret;
150*0Sstevel@tonic-gate 	return ASN1_OCTET_STRING_cmp(a->issuerKeyHash, b->issuerKeyHash);
151*0Sstevel@tonic-gate 	}
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate int OCSP_id_cmp(OCSP_CERTID *a, OCSP_CERTID *b)
154*0Sstevel@tonic-gate 	{
155*0Sstevel@tonic-gate 	int ret;
156*0Sstevel@tonic-gate 	ret = OCSP_id_issuer_cmp(a, b);
157*0Sstevel@tonic-gate 	if (ret) return ret;
158*0Sstevel@tonic-gate 	return ASN1_INTEGER_cmp(a->serialNumber, b->serialNumber);
159*0Sstevel@tonic-gate 	}
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate /* Parse a URL and split it up into host, port and path components and whether
163*0Sstevel@tonic-gate  * it is SSL.
164*0Sstevel@tonic-gate  */
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate int OCSP_parse_url(char *url, char **phost, char **pport, char **ppath, int *pssl)
167*0Sstevel@tonic-gate 	{
168*0Sstevel@tonic-gate 	char *p, *buf;
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	char *host, *port;
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	/* dup the buffer since we are going to mess with it */
173*0Sstevel@tonic-gate 	buf = BUF_strdup(url);
174*0Sstevel@tonic-gate 	if (!buf) goto mem_err;
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	*phost = NULL;
177*0Sstevel@tonic-gate 	*pport = NULL;
178*0Sstevel@tonic-gate 	*ppath = NULL;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	/* Check for initial colon */
181*0Sstevel@tonic-gate 	p = strchr(buf, ':');
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	if (!p) goto parse_err;
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	*(p++) = '\0';
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	if (!strcmp(buf, "http"))
188*0Sstevel@tonic-gate 		{
189*0Sstevel@tonic-gate 		*pssl = 0;
190*0Sstevel@tonic-gate 		port = "80";
191*0Sstevel@tonic-gate 		}
192*0Sstevel@tonic-gate 	else if (!strcmp(buf, "https"))
193*0Sstevel@tonic-gate 		{
194*0Sstevel@tonic-gate 		*pssl = 1;
195*0Sstevel@tonic-gate 		port = "443";
196*0Sstevel@tonic-gate 		}
197*0Sstevel@tonic-gate 	else
198*0Sstevel@tonic-gate 		goto parse_err;
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	/* Check for double slash */
201*0Sstevel@tonic-gate 	if ((p[0] != '/') || (p[1] != '/'))
202*0Sstevel@tonic-gate 		goto parse_err;
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	p += 2;
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	host = p;
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	/* Check for trailing part of path */
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	p = strchr(p, '/');
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	if (!p)
213*0Sstevel@tonic-gate 		*ppath = BUF_strdup("/");
214*0Sstevel@tonic-gate 	else
215*0Sstevel@tonic-gate 		{
216*0Sstevel@tonic-gate 		*ppath = BUF_strdup(p);
217*0Sstevel@tonic-gate 		/* Set start of path to 0 so hostname is valid */
218*0Sstevel@tonic-gate 		*p = '\0';
219*0Sstevel@tonic-gate 		}
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	if (!*ppath) goto mem_err;
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	/* Look for optional ':' for port number */
224*0Sstevel@tonic-gate 	if ((p = strchr(host, ':')))
225*0Sstevel@tonic-gate 		{
226*0Sstevel@tonic-gate 		*p = 0;
227*0Sstevel@tonic-gate 		port = p + 1;
228*0Sstevel@tonic-gate 		}
229*0Sstevel@tonic-gate 	else
230*0Sstevel@tonic-gate 		{
231*0Sstevel@tonic-gate 		/* Not found: set default port */
232*0Sstevel@tonic-gate 		if (*pssl) port = "443";
233*0Sstevel@tonic-gate 		else port = "80";
234*0Sstevel@tonic-gate 		}
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 	*pport = BUF_strdup(port);
237*0Sstevel@tonic-gate 	if (!*pport) goto mem_err;
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 	*phost = BUF_strdup(host);
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	if (!*phost) goto mem_err;
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	OPENSSL_free(buf);
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 	return 1;
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	mem_err:
248*0Sstevel@tonic-gate 	OCSPerr(OCSP_F_OCSP_PARSE_URL, ERR_R_MALLOC_FAILURE);
249*0Sstevel@tonic-gate 	goto err;
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	parse_err:
252*0Sstevel@tonic-gate 	OCSPerr(OCSP_F_OCSP_PARSE_URL, OCSP_R_ERROR_PARSING_URL);
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 	err:
256*0Sstevel@tonic-gate 	if (buf) OPENSSL_free(buf);
257*0Sstevel@tonic-gate 	if (*ppath) OPENSSL_free(*ppath);
258*0Sstevel@tonic-gate 	if (*pport) OPENSSL_free(*pport);
259*0Sstevel@tonic-gate 	if (*phost) OPENSSL_free(*phost);
260*0Sstevel@tonic-gate 	return 0;
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 	}
263