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