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