ocsp_lib.c revision 269682
1109998Smarkm/* ocsp_lib.c */
2109998Smarkm/* Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
3109998Smarkm * project. */
4109998Smarkm
5109998Smarkm/* History:
6109998Smarkm   This file was transfered to Richard Levitte from CertCo by Kathy
7109998Smarkm   Weinhold in mid-spring 2000 to be included in OpenSSL or released
8109998Smarkm   as a patch kit. */
9109998Smarkm
10109998Smarkm/* ====================================================================
11109998Smarkm * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
12109998Smarkm *
13109998Smarkm * Redistribution and use in source and binary forms, with or without
14109998Smarkm * modification, are permitted provided that the following conditions
15109998Smarkm * are met:
16109998Smarkm *
17109998Smarkm * 1. Redistributions of source code must retain the above copyright
18109998Smarkm *    notice, this list of conditions and the following disclaimer.
19109998Smarkm *
20109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright
21109998Smarkm *    notice, this list of conditions and the following disclaimer in
22109998Smarkm *    the documentation and/or other materials provided with the
23109998Smarkm *    distribution.
24109998Smarkm *
25109998Smarkm * 3. All advertising materials mentioning features or use of this
26109998Smarkm *    software must display the following acknowledgment:
27109998Smarkm *    "This product includes software developed by the OpenSSL Project
28109998Smarkm *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
29109998Smarkm *
30109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
31109998Smarkm *    endorse or promote products derived from this software without
32109998Smarkm *    prior written permission. For written permission, please contact
33109998Smarkm *    openssl-core@openssl.org.
34109998Smarkm *
35109998Smarkm * 5. Products derived from this software may not be called "OpenSSL"
36109998Smarkm *    nor may "OpenSSL" appear in their names without prior written
37109998Smarkm *    permission of the OpenSSL Project.
38109998Smarkm *
39109998Smarkm * 6. Redistributions of any form whatsoever must retain the following
40109998Smarkm *    acknowledgment:
41109998Smarkm *    "This product includes software developed by the OpenSSL Project
42109998Smarkm *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
43109998Smarkm *
44109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
45109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47109998Smarkm * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
48109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
53109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
55109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE.
56109998Smarkm * ====================================================================
57109998Smarkm *
58109998Smarkm * This product includes cryptographic software written by Eric Young
59109998Smarkm * (eay@cryptsoft.com).  This product includes software written by Tim
60109998Smarkm * Hudson (tjh@cryptsoft.com).
61109998Smarkm *
62109998Smarkm */
63109998Smarkm
64109998Smarkm#include <stdio.h>
65109998Smarkm#include <cryptlib.h>
66109998Smarkm#include <openssl/objects.h>
67109998Smarkm#include <openssl/rand.h>
68109998Smarkm#include <openssl/x509.h>
69109998Smarkm#include <openssl/pem.h>
70109998Smarkm#include <openssl/x509v3.h>
71109998Smarkm#include <openssl/ocsp.h>
72238405Sjkim#include <openssl/asn1t.h>
73109998Smarkm
74109998Smarkm/* Convert a certificate and its issuer to an OCSP_CERTID */
75109998Smarkm
76109998SmarkmOCSP_CERTID *OCSP_cert_to_id(const EVP_MD *dgst, X509 *subject, X509 *issuer)
77109998Smarkm{
78109998Smarkm	X509_NAME *iname;
79109998Smarkm	ASN1_INTEGER *serial;
80109998Smarkm	ASN1_BIT_STRING *ikey;
81109998Smarkm#ifndef OPENSSL_NO_SHA1
82109998Smarkm	if(!dgst) dgst = EVP_sha1();
83109998Smarkm#endif
84109998Smarkm	if (subject)
85109998Smarkm		{
86109998Smarkm		iname = X509_get_issuer_name(subject);
87109998Smarkm		serial = X509_get_serialNumber(subject);
88109998Smarkm		}
89109998Smarkm	else
90109998Smarkm		{
91109998Smarkm		iname = X509_get_subject_name(issuer);
92109998Smarkm		serial = NULL;
93109998Smarkm		}
94109998Smarkm	ikey = X509_get0_pubkey_bitstr(issuer);
95109998Smarkm	return OCSP_cert_id_new(dgst, iname, ikey, serial);
96109998Smarkm}
97109998Smarkm
98109998Smarkm
99109998SmarkmOCSP_CERTID *OCSP_cert_id_new(const EVP_MD *dgst,
100109998Smarkm			      X509_NAME *issuerName,
101109998Smarkm			      ASN1_BIT_STRING* issuerKey,
102109998Smarkm			      ASN1_INTEGER *serialNumber)
103109998Smarkm        {
104109998Smarkm	int nid;
105109998Smarkm        unsigned int i;
106109998Smarkm	X509_ALGOR *alg;
107109998Smarkm	OCSP_CERTID *cid = NULL;
108109998Smarkm	unsigned char md[EVP_MAX_MD_SIZE];
109109998Smarkm
110109998Smarkm	if (!(cid = OCSP_CERTID_new())) goto err;
111109998Smarkm
112109998Smarkm	alg = cid->hashAlgorithm;
113109998Smarkm	if (alg->algorithm != NULL) ASN1_OBJECT_free(alg->algorithm);
114109998Smarkm	if ((nid = EVP_MD_type(dgst)) == NID_undef)
115109998Smarkm	        {
116160814Ssimon		OCSPerr(OCSP_F_OCSP_CERT_ID_NEW,OCSP_R_UNKNOWN_NID);
117109998Smarkm		goto err;
118109998Smarkm		}
119109998Smarkm	if (!(alg->algorithm=OBJ_nid2obj(nid))) goto err;
120109998Smarkm	if ((alg->parameter=ASN1_TYPE_new()) == NULL) goto err;
121109998Smarkm	alg->parameter->type=V_ASN1_NULL;
122109998Smarkm
123109998Smarkm	if (!X509_NAME_digest(issuerName, dgst, md, &i)) goto digerr;
124109998Smarkm	if (!(ASN1_OCTET_STRING_set(cid->issuerNameHash, md, i))) goto err;
125109998Smarkm
126109998Smarkm	/* Calculate the issuerKey hash, excluding tag and length */
127238405Sjkim	if (!EVP_Digest(issuerKey->data, issuerKey->length, md, &i, dgst, NULL))
128238405Sjkim		goto err;
129109998Smarkm
130109998Smarkm	if (!(ASN1_OCTET_STRING_set(cid->issuerKeyHash, md, i))) goto err;
131109998Smarkm
132109998Smarkm	if (serialNumber)
133109998Smarkm		{
134109998Smarkm		ASN1_INTEGER_free(cid->serialNumber);
135109998Smarkm		if (!(cid->serialNumber = ASN1_INTEGER_dup(serialNumber))) goto err;
136109998Smarkm		}
137109998Smarkm	return cid;
138109998Smarkmdigerr:
139160814Ssimon	OCSPerr(OCSP_F_OCSP_CERT_ID_NEW,OCSP_R_DIGEST_ERR);
140109998Smarkmerr:
141109998Smarkm	if (cid) OCSP_CERTID_free(cid);
142109998Smarkm	return NULL;
143109998Smarkm	}
144109998Smarkm
145109998Smarkmint OCSP_id_issuer_cmp(OCSP_CERTID *a, OCSP_CERTID *b)
146109998Smarkm	{
147109998Smarkm	int ret;
148109998Smarkm	ret = OBJ_cmp(a->hashAlgorithm->algorithm, b->hashAlgorithm->algorithm);
149109998Smarkm	if (ret) return ret;
150109998Smarkm	ret = ASN1_OCTET_STRING_cmp(a->issuerNameHash, b->issuerNameHash);
151109998Smarkm	if (ret) return ret;
152109998Smarkm	return ASN1_OCTET_STRING_cmp(a->issuerKeyHash, b->issuerKeyHash);
153109998Smarkm	}
154109998Smarkm
155109998Smarkmint OCSP_id_cmp(OCSP_CERTID *a, OCSP_CERTID *b)
156109998Smarkm	{
157109998Smarkm	int ret;
158109998Smarkm	ret = OCSP_id_issuer_cmp(a, b);
159109998Smarkm	if (ret) return ret;
160109998Smarkm	return ASN1_INTEGER_cmp(a->serialNumber, b->serialNumber);
161109998Smarkm	}
162109998Smarkm
163109998Smarkm
164109998Smarkm/* Parse a URL and split it up into host, port and path components and whether
165109998Smarkm * it is SSL.
166109998Smarkm */
167109998Smarkm
168109998Smarkmint OCSP_parse_url(char *url, char **phost, char **pport, char **ppath, int *pssl)
169109998Smarkm	{
170109998Smarkm	char *p, *buf;
171109998Smarkm
172109998Smarkm	char *host, *port;
173109998Smarkm
174237657Sjkim	*phost = NULL;
175237657Sjkim	*pport = NULL;
176237657Sjkim	*ppath = NULL;
177237657Sjkim
178109998Smarkm	/* dup the buffer since we are going to mess with it */
179109998Smarkm	buf = BUF_strdup(url);
180109998Smarkm	if (!buf) goto mem_err;
181109998Smarkm
182109998Smarkm	/* Check for initial colon */
183109998Smarkm	p = strchr(buf, ':');
184109998Smarkm
185109998Smarkm	if (!p) goto parse_err;
186109998Smarkm
187109998Smarkm	*(p++) = '\0';
188109998Smarkm
189109998Smarkm	if (!strcmp(buf, "http"))
190109998Smarkm		{
191109998Smarkm		*pssl = 0;
192109998Smarkm		port = "80";
193109998Smarkm		}
194109998Smarkm	else if (!strcmp(buf, "https"))
195109998Smarkm		{
196109998Smarkm		*pssl = 1;
197109998Smarkm		port = "443";
198109998Smarkm		}
199109998Smarkm	else
200109998Smarkm		goto parse_err;
201109998Smarkm
202109998Smarkm	/* Check for double slash */
203109998Smarkm	if ((p[0] != '/') || (p[1] != '/'))
204109998Smarkm		goto parse_err;
205109998Smarkm
206109998Smarkm	p += 2;
207109998Smarkm
208109998Smarkm	host = p;
209109998Smarkm
210109998Smarkm	/* Check for trailing part of path */
211109998Smarkm
212109998Smarkm	p = strchr(p, '/');
213109998Smarkm
214109998Smarkm	if (!p)
215109998Smarkm		*ppath = BUF_strdup("/");
216109998Smarkm	else
217109998Smarkm		{
218109998Smarkm		*ppath = BUF_strdup(p);
219109998Smarkm		/* Set start of path to 0 so hostname is valid */
220109998Smarkm		*p = '\0';
221109998Smarkm		}
222109998Smarkm
223109998Smarkm	if (!*ppath) goto mem_err;
224109998Smarkm
225269682Sjkim	p = host;
226269682Sjkim	if(host[0] == '[')
227269682Sjkim		{
228269682Sjkim		/* ipv6 literal */
229269682Sjkim		host++;
230269682Sjkim		p = strchr(host, ']');
231269682Sjkim		if(!p) goto parse_err;
232269682Sjkim		*p = '\0';
233269682Sjkim		p++;
234269682Sjkim		}
235269682Sjkim
236109998Smarkm	/* Look for optional ':' for port number */
237269682Sjkim	if ((p = strchr(p, ':')))
238109998Smarkm		{
239109998Smarkm		*p = 0;
240109998Smarkm		port = p + 1;
241109998Smarkm		}
242109998Smarkm	else
243109998Smarkm		{
244109998Smarkm		/* Not found: set default port */
245109998Smarkm		if (*pssl) port = "443";
246109998Smarkm		else port = "80";
247109998Smarkm		}
248109998Smarkm
249109998Smarkm	*pport = BUF_strdup(port);
250109998Smarkm	if (!*pport) goto mem_err;
251109998Smarkm
252109998Smarkm	*phost = BUF_strdup(host);
253109998Smarkm
254109998Smarkm	if (!*phost) goto mem_err;
255109998Smarkm
256109998Smarkm	OPENSSL_free(buf);
257109998Smarkm
258109998Smarkm	return 1;
259109998Smarkm
260109998Smarkm	mem_err:
261109998Smarkm	OCSPerr(OCSP_F_OCSP_PARSE_URL, ERR_R_MALLOC_FAILURE);
262109998Smarkm	goto err;
263109998Smarkm
264109998Smarkm	parse_err:
265109998Smarkm	OCSPerr(OCSP_F_OCSP_PARSE_URL, OCSP_R_ERROR_PARSING_URL);
266109998Smarkm
267109998Smarkm
268109998Smarkm	err:
269127128Snectar	if (buf) OPENSSL_free(buf);
270109998Smarkm	if (*ppath) OPENSSL_free(*ppath);
271109998Smarkm	if (*pport) OPENSSL_free(*pport);
272109998Smarkm	if (*phost) OPENSSL_free(*phost);
273109998Smarkm	return 0;
274109998Smarkm
275109998Smarkm	}
276238405Sjkim
277238405SjkimIMPLEMENT_ASN1_DUP_FUNCTION(OCSP_CERTID)
278