10Sstevel@tonic-gate /* ocsp_lib.c */
20Sstevel@tonic-gate /* Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
30Sstevel@tonic-gate * project. */
40Sstevel@tonic-gate
50Sstevel@tonic-gate /* History:
60Sstevel@tonic-gate This file was transfered to Richard Levitte from CertCo by Kathy
70Sstevel@tonic-gate Weinhold in mid-spring 2000 to be included in OpenSSL or released
80Sstevel@tonic-gate as a patch kit. */
90Sstevel@tonic-gate
100Sstevel@tonic-gate /* ====================================================================
110Sstevel@tonic-gate * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
140Sstevel@tonic-gate * modification, are permitted provided that the following conditions
150Sstevel@tonic-gate * are met:
160Sstevel@tonic-gate *
170Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
180Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
210Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
220Sstevel@tonic-gate * the documentation and/or other materials provided with the
230Sstevel@tonic-gate * distribution.
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
260Sstevel@tonic-gate * software must display the following acknowledgment:
270Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
280Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
290Sstevel@tonic-gate *
300Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
310Sstevel@tonic-gate * endorse or promote products derived from this software without
320Sstevel@tonic-gate * prior written permission. For written permission, please contact
330Sstevel@tonic-gate * openssl-core@openssl.org.
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
360Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
370Sstevel@tonic-gate * permission of the OpenSSL Project.
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
400Sstevel@tonic-gate * acknowledgment:
410Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
420Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
430Sstevel@tonic-gate *
440Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
450Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
460Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
470Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
480Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
490Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
500Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
510Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
520Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
530Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
540Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
550Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
560Sstevel@tonic-gate * ====================================================================
570Sstevel@tonic-gate *
580Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
590Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
600Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
610Sstevel@tonic-gate *
620Sstevel@tonic-gate */
630Sstevel@tonic-gate
640Sstevel@tonic-gate #include <stdio.h>
650Sstevel@tonic-gate #include <cryptlib.h>
660Sstevel@tonic-gate #include <openssl/objects.h>
670Sstevel@tonic-gate #include <openssl/rand.h>
680Sstevel@tonic-gate #include <openssl/x509.h>
690Sstevel@tonic-gate #include <openssl/pem.h>
700Sstevel@tonic-gate #include <openssl/x509v3.h>
710Sstevel@tonic-gate #include <openssl/ocsp.h>
720Sstevel@tonic-gate
730Sstevel@tonic-gate /* Convert a certificate and its issuer to an OCSP_CERTID */
740Sstevel@tonic-gate
OCSP_cert_to_id(const EVP_MD * dgst,X509 * subject,X509 * issuer)750Sstevel@tonic-gate OCSP_CERTID *OCSP_cert_to_id(const EVP_MD *dgst, X509 *subject, X509 *issuer)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate X509_NAME *iname;
780Sstevel@tonic-gate ASN1_INTEGER *serial;
790Sstevel@tonic-gate ASN1_BIT_STRING *ikey;
800Sstevel@tonic-gate #ifndef OPENSSL_NO_SHA1
810Sstevel@tonic-gate if(!dgst) dgst = EVP_sha1();
820Sstevel@tonic-gate #endif
830Sstevel@tonic-gate if (subject)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate iname = X509_get_issuer_name(subject);
860Sstevel@tonic-gate serial = X509_get_serialNumber(subject);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate else
890Sstevel@tonic-gate {
900Sstevel@tonic-gate iname = X509_get_subject_name(issuer);
910Sstevel@tonic-gate serial = NULL;
920Sstevel@tonic-gate }
930Sstevel@tonic-gate ikey = X509_get0_pubkey_bitstr(issuer);
940Sstevel@tonic-gate return OCSP_cert_id_new(dgst, iname, ikey, serial);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate
970Sstevel@tonic-gate
OCSP_cert_id_new(const EVP_MD * dgst,X509_NAME * issuerName,ASN1_BIT_STRING * issuerKey,ASN1_INTEGER * serialNumber)980Sstevel@tonic-gate OCSP_CERTID *OCSP_cert_id_new(const EVP_MD *dgst,
990Sstevel@tonic-gate X509_NAME *issuerName,
1000Sstevel@tonic-gate ASN1_BIT_STRING* issuerKey,
1010Sstevel@tonic-gate ASN1_INTEGER *serialNumber)
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate int nid;
1040Sstevel@tonic-gate unsigned int i;
1050Sstevel@tonic-gate X509_ALGOR *alg;
1060Sstevel@tonic-gate OCSP_CERTID *cid = NULL;
1070Sstevel@tonic-gate unsigned char md[EVP_MAX_MD_SIZE];
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate if (!(cid = OCSP_CERTID_new())) goto err;
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate alg = cid->hashAlgorithm;
1120Sstevel@tonic-gate if (alg->algorithm != NULL) ASN1_OBJECT_free(alg->algorithm);
1130Sstevel@tonic-gate if ((nid = EVP_MD_type(dgst)) == NID_undef)
1140Sstevel@tonic-gate {
115*2139Sjp161948 OCSPerr(OCSP_F_OCSP_CERT_ID_NEW,OCSP_R_UNKNOWN_NID);
1160Sstevel@tonic-gate goto err;
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate if (!(alg->algorithm=OBJ_nid2obj(nid))) goto err;
1190Sstevel@tonic-gate if ((alg->parameter=ASN1_TYPE_new()) == NULL) goto err;
1200Sstevel@tonic-gate alg->parameter->type=V_ASN1_NULL;
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate if (!X509_NAME_digest(issuerName, dgst, md, &i)) goto digerr;
1230Sstevel@tonic-gate if (!(ASN1_OCTET_STRING_set(cid->issuerNameHash, md, i))) goto err;
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate /* Calculate the issuerKey hash, excluding tag and length */
1260Sstevel@tonic-gate EVP_Digest(issuerKey->data, issuerKey->length, md, &i, dgst, NULL);
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate if (!(ASN1_OCTET_STRING_set(cid->issuerKeyHash, md, i))) goto err;
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate if (serialNumber)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate ASN1_INTEGER_free(cid->serialNumber);
1330Sstevel@tonic-gate if (!(cid->serialNumber = ASN1_INTEGER_dup(serialNumber))) goto err;
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate return cid;
1360Sstevel@tonic-gate digerr:
137*2139Sjp161948 OCSPerr(OCSP_F_OCSP_CERT_ID_NEW,OCSP_R_DIGEST_ERR);
1380Sstevel@tonic-gate err:
1390Sstevel@tonic-gate if (cid) OCSP_CERTID_free(cid);
1400Sstevel@tonic-gate return NULL;
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate
OCSP_id_issuer_cmp(OCSP_CERTID * a,OCSP_CERTID * b)1430Sstevel@tonic-gate int OCSP_id_issuer_cmp(OCSP_CERTID *a, OCSP_CERTID *b)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate int ret;
1460Sstevel@tonic-gate ret = OBJ_cmp(a->hashAlgorithm->algorithm, b->hashAlgorithm->algorithm);
1470Sstevel@tonic-gate if (ret) return ret;
1480Sstevel@tonic-gate ret = ASN1_OCTET_STRING_cmp(a->issuerNameHash, b->issuerNameHash);
1490Sstevel@tonic-gate if (ret) return ret;
1500Sstevel@tonic-gate return ASN1_OCTET_STRING_cmp(a->issuerKeyHash, b->issuerKeyHash);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate
OCSP_id_cmp(OCSP_CERTID * a,OCSP_CERTID * b)1530Sstevel@tonic-gate int OCSP_id_cmp(OCSP_CERTID *a, OCSP_CERTID *b)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate int ret;
1560Sstevel@tonic-gate ret = OCSP_id_issuer_cmp(a, b);
1570Sstevel@tonic-gate if (ret) return ret;
1580Sstevel@tonic-gate return ASN1_INTEGER_cmp(a->serialNumber, b->serialNumber);
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate /* Parse a URL and split it up into host, port and path components and whether
1630Sstevel@tonic-gate * it is SSL.
1640Sstevel@tonic-gate */
1650Sstevel@tonic-gate
OCSP_parse_url(char * url,char ** phost,char ** pport,char ** ppath,int * pssl)1660Sstevel@tonic-gate int OCSP_parse_url(char *url, char **phost, char **pport, char **ppath, int *pssl)
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate char *p, *buf;
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate char *host, *port;
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate /* dup the buffer since we are going to mess with it */
1730Sstevel@tonic-gate buf = BUF_strdup(url);
1740Sstevel@tonic-gate if (!buf) goto mem_err;
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate *phost = NULL;
1770Sstevel@tonic-gate *pport = NULL;
1780Sstevel@tonic-gate *ppath = NULL;
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate /* Check for initial colon */
1810Sstevel@tonic-gate p = strchr(buf, ':');
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate if (!p) goto parse_err;
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate *(p++) = '\0';
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate if (!strcmp(buf, "http"))
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate *pssl = 0;
1900Sstevel@tonic-gate port = "80";
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate else if (!strcmp(buf, "https"))
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate *pssl = 1;
1950Sstevel@tonic-gate port = "443";
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate else
1980Sstevel@tonic-gate goto parse_err;
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate /* Check for double slash */
2010Sstevel@tonic-gate if ((p[0] != '/') || (p[1] != '/'))
2020Sstevel@tonic-gate goto parse_err;
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate p += 2;
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate host = p;
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate /* Check for trailing part of path */
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate p = strchr(p, '/');
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate if (!p)
2130Sstevel@tonic-gate *ppath = BUF_strdup("/");
2140Sstevel@tonic-gate else
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate *ppath = BUF_strdup(p);
2170Sstevel@tonic-gate /* Set start of path to 0 so hostname is valid */
2180Sstevel@tonic-gate *p = '\0';
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate if (!*ppath) goto mem_err;
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate /* Look for optional ':' for port number */
2240Sstevel@tonic-gate if ((p = strchr(host, ':')))
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate *p = 0;
2270Sstevel@tonic-gate port = p + 1;
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate else
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate /* Not found: set default port */
2320Sstevel@tonic-gate if (*pssl) port = "443";
2330Sstevel@tonic-gate else port = "80";
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate *pport = BUF_strdup(port);
2370Sstevel@tonic-gate if (!*pport) goto mem_err;
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate *phost = BUF_strdup(host);
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate if (!*phost) goto mem_err;
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate OPENSSL_free(buf);
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate return 1;
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate mem_err:
2480Sstevel@tonic-gate OCSPerr(OCSP_F_OCSP_PARSE_URL, ERR_R_MALLOC_FAILURE);
2490Sstevel@tonic-gate goto err;
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate parse_err:
2520Sstevel@tonic-gate OCSPerr(OCSP_F_OCSP_PARSE_URL, OCSP_R_ERROR_PARSING_URL);
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate err:
2560Sstevel@tonic-gate if (buf) OPENSSL_free(buf);
2570Sstevel@tonic-gate if (*ppath) OPENSSL_free(*ppath);
2580Sstevel@tonic-gate if (*pport) OPENSSL_free(*pport);
2590Sstevel@tonic-gate if (*phost) OPENSSL_free(*phost);
2600Sstevel@tonic-gate return 0;
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate }
263