3 * Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
8 * History: This file was transfered to Richard Levitte from CertCo by Kathy
9 * Weinhold in mid-spring 2000 to be included in OpenSSL or released as a
13 /* ====================================================================
14 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in
25 * the documentation and/or other materials provided with the
28 * 3. All advertising materials mentioning features or use of this
29 * software must display the following acknowledgment:
30 * "This product includes software developed by the OpenSSL Project
31 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
33 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
34 * endorse or promote products derived from this software without
35 * prior written permission. For written permission, please contact
36 * openssl-core@openssl.org.
38 * 5. Products derived from this software may not be called "OpenSSL"
39 * nor may "OpenSSL" appear in their names without prior written
40 * permission of the OpenSSL Project.
42 * 6. Redistributions of any form whatsoever must retain the following
44 * "This product includes software developed by the OpenSSL Project
45 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
47 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
48 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
50 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
51 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
53 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
54 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
56 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
58 * OF THE POSSIBILITY OF SUCH DAMAGE.
59 * ====================================================================
61 * This product includes cryptographic software written by Eric Young
62 * (eay@cryptsoft.com). This product includes software written by Tim
63 * Hudson (tjh@cryptsoft.com).
69 #include <openssl/objects.h>
70 #include <openssl/rand.h>
71 #include <openssl/x509.h>
72 #include <openssl/pem.h>
73 #include <openssl/x509v3.h>
74 #include <openssl/ocsp.h>
76 #include <openssl/asn1t.h>
78 /* Convert a certificate and its issuer to an OCSP_CERTID */
80 OCSP_CERTID *OCSP_cert_to_id(const EVP_MD *dgst, X509 *subject, X509 *issuer)
84 ASN1_BIT_STRING *ikey;
88 iname = X509_get_issuer_name(subject);
89 serial = X509_get_serialNumber(subject);
91 iname = X509_get_subject_name(issuer);
94 ikey = X509_get0_pubkey_bitstr(issuer);
95 return OCSP_cert_id_new(dgst, iname, ikey, serial);
98 OCSP_CERTID *OCSP_cert_id_new(const EVP_MD *dgst,
99 X509_NAME *issuerName,
100 ASN1_BIT_STRING *issuerKey,
101 ASN1_INTEGER *serialNumber)
106 OCSP_CERTID *cid = NULL;
107 unsigned char md[EVP_MAX_MD_SIZE];
109 if (!(cid = OCSP_CERTID_new()))
112 alg = cid->hashAlgorithm;
113 if (alg->algorithm != NULL)
114 ASN1_OBJECT_free(alg->algorithm);
115 if ((nid = EVP_MD_type(dgst)) == NID_undef) {
116 OCSPerr(OCSP_F_OCSP_CERT_ID_NEW, OCSP_R_UNKNOWN_NID);
119 if (!(alg->algorithm = OBJ_nid2obj(nid)))
121 if ((alg->parameter = ASN1_TYPE_new()) == NULL)
123 alg->parameter->type = V_ASN1_NULL;
125 if (!X509_NAME_digest(issuerName, dgst, md, &i))
127 if (!(ASN1_OCTET_STRING_set(cid->issuerNameHash, md, i)))
130 /* Calculate the issuerKey hash, excluding tag and length */
131 if (!EVP_Digest(issuerKey->data, issuerKey->length, md, &i, dgst, NULL))
134 if (!(ASN1_OCTET_STRING_set(cid->issuerKeyHash, md, i)))
138 ASN1_INTEGER_free(cid->serialNumber);
139 if (!(cid->serialNumber = ASN1_INTEGER_dup(serialNumber)))
144 OCSPerr(OCSP_F_OCSP_CERT_ID_NEW, OCSP_R_DIGEST_ERR);
147 OCSP_CERTID_free(cid);
151 int OCSP_id_issuer_cmp(OCSP_CERTID *a, OCSP_CERTID *b)
154 ret = OBJ_cmp(a->hashAlgorithm->algorithm, b->hashAlgorithm->algorithm);
157 ret = ASN1_OCTET_STRING_cmp(a->issuerNameHash, b->issuerNameHash);
160 return ASN1_OCTET_STRING_cmp(a->issuerKeyHash, b->issuerKeyHash);
163 int OCSP_id_cmp(OCSP_CERTID *a, OCSP_CERTID *b)
166 ret = OCSP_id_issuer_cmp(a, b);
169 return ASN1_INTEGER_cmp(a->serialNumber, b->serialNumber);
173 * Parse a URL and split it up into host, port and path components and
177 int OCSP_parse_url(const char *url, char **phost, char **pport, char **ppath,
188 /* dup the buffer since we are going to mess with it */
189 buf = BUF_strdup(url);
193 /* Check for initial colon */
194 p = strchr(buf, ':');
201 if (!strcmp(buf, "http")) {
204 } else if (!strcmp(buf, "https")) {
210 /* Check for double slash */
211 if ((p[0] != '/') || (p[1] != '/'))
218 /* Check for trailing part of path */
223 *ppath = BUF_strdup("/");
225 *ppath = BUF_strdup(p);
226 /* Set start of path to 0 so hostname is valid */
234 if (host[0] == '[') {
237 p = strchr(host, ']');
244 /* Look for optional ':' for port number */
245 if ((p = strchr(p, ':'))) {
249 /* Not found: set default port */
256 *pport = BUF_strdup(port);
260 *phost = BUF_strdup(host);
270 OCSPerr(OCSP_F_OCSP_PARSE_URL, ERR_R_MALLOC_FAILURE);
274 OCSPerr(OCSP_F_OCSP_PARSE_URL, OCSP_R_ERROR_PARSING_URL);
280 OPENSSL_free(*ppath);
282 OPENSSL_free(*pport);
284 OPENSSL_free(*phost);
289 IMPLEMENT_ASN1_DUP_FUNCTION(OCSP_CERTID)