1 /* $OpenBSD: ocsp_lib.c,v 1.23 2018/08/24 20:03:21 tb Exp $ */
2 /* Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
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
10 /* ====================================================================
11 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
39 * 6. Redistributions of any form whatsoever must retain the following
41 * "This product includes software developed by the OpenSSL Project
42 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
67 #include <openssl/opensslconf.h>
69 #include <openssl/asn1t.h>
70 #include <openssl/err.h>
71 #include <openssl/objects.h>
72 #include <openssl/ocsp.h>
73 #include <openssl/pem.h>
74 #include <openssl/x509.h>
75 #include <openssl/x509v3.h>
77 /* Convert a certificate and its issuer to an OCSP_CERTID */
80 OCSP_cert_to_id(const EVP_MD
*dgst
, const X509
*subject
, const X509
*issuer
)
83 const ASN1_INTEGER
*serial
;
84 ASN1_BIT_STRING
*ikey
;
86 #ifndef OPENSSL_NO_SHA1
91 iname
= X509_get_issuer_name(subject
);
92 serial
= X509_get0_serialNumber(subject
);
94 iname
= X509_get_subject_name(issuer
);
97 ikey
= X509_get0_pubkey_bitstr(issuer
);
98 return OCSP_cert_id_new(dgst
, iname
, ikey
, serial
);
102 OCSP_cert_id_new(const EVP_MD
*dgst
, const X509_NAME
*issuerName
,
103 const ASN1_BIT_STRING
*issuerKey
, const ASN1_INTEGER
*serialNumber
)
108 OCSP_CERTID
*cid
= NULL
;
109 unsigned char md
[EVP_MAX_MD_SIZE
];
111 if (!(cid
= OCSP_CERTID_new()))
114 alg
= cid
->hashAlgorithm
;
115 if (alg
->algorithm
!= NULL
)
116 ASN1_OBJECT_free(alg
->algorithm
);
117 if ((nid
= EVP_MD_type(dgst
)) == NID_undef
) {
118 OCSPerror(OCSP_R_UNKNOWN_NID
);
121 if (!(alg
->algorithm
= OBJ_nid2obj(nid
)))
123 if ((alg
->parameter
= ASN1_TYPE_new()) == NULL
)
125 alg
->parameter
->type
= V_ASN1_NULL
;
127 if (!X509_NAME_digest(issuerName
, dgst
, md
, &i
))
129 if (!(ASN1_OCTET_STRING_set(cid
->issuerNameHash
, md
, i
)))
132 /* Calculate the issuerKey hash, excluding tag and length */
133 if (!EVP_Digest(issuerKey
->data
, issuerKey
->length
, md
, &i
, dgst
, NULL
))
136 if (!(ASN1_OCTET_STRING_set(cid
->issuerKeyHash
, md
, i
)))
140 ASN1_INTEGER_free(cid
->serialNumber
);
141 if (!(cid
->serialNumber
= ASN1_INTEGER_dup(serialNumber
)))
147 OCSPerror(OCSP_R_DIGEST_ERR
);
150 OCSP_CERTID_free(cid
);
155 OCSP_id_issuer_cmp(OCSP_CERTID
*a
, OCSP_CERTID
*b
)
159 ret
= OBJ_cmp(a
->hashAlgorithm
->algorithm
, b
->hashAlgorithm
->algorithm
);
162 ret
= ASN1_OCTET_STRING_cmp(a
->issuerNameHash
, b
->issuerNameHash
);
165 return ASN1_OCTET_STRING_cmp(a
->issuerKeyHash
, b
->issuerKeyHash
);
169 OCSP_id_cmp(OCSP_CERTID
*a
, OCSP_CERTID
*b
)
173 ret
= OCSP_id_issuer_cmp(a
, b
);
176 return ASN1_INTEGER_cmp(a
->serialNumber
, b
->serialNumber
);
179 /* Parse a URL and split it up into host, port and path components and whether
183 OCSP_parse_url(const char *url
, char **phost
, char **pport
, char **ppath
,
186 char *host
, *path
, *port
, *tmp
;
188 *phost
= *pport
= *ppath
= NULL
;
191 if (strncmp(url
, "https://", 8) == 0) {
193 host
= strdup(url
+ 8);
194 } else if (strncmp(url
, "http://", 7) == 0)
195 host
= strdup(url
+ 7);
197 OCSPerror(OCSP_R_ERROR_PARSING_URL
);
201 OCSPerror(ERR_R_MALLOC_FAILURE
);
205 if ((tmp
= strchr(host
, '/')) != NULL
) {
211 if ((tmp
= strchr(host
, ':')) != NULL
) {
212 port
= strdup(tmp
+ 1);
216 port
= strdup("443");
221 if (path
== NULL
|| port
== NULL
) {
225 OCSPerror(ERR_R_MALLOC_FAILURE
);
236 OCSP_CERTID_dup(OCSP_CERTID
*x
)
238 return ASN1_item_dup(&OCSP_CERTID_it
, x
);