sl@0: /* ocsp_lib.c */ sl@0: /* Written by Tom Titchener for the OpenSSL sl@0: * project. */ sl@0: sl@0: /* History: sl@0: This file was transfered to Richard Levitte from CertCo by Kathy sl@0: Weinhold in mid-spring 2000 to be included in OpenSSL or released sl@0: as a patch kit. */ sl@0: sl@0: /* ==================================================================== sl@0: * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: /* Convert a certificate and its issuer to an OCSP_CERTID */ sl@0: sl@0: EXPORT_C OCSP_CERTID *OCSP_cert_to_id(const EVP_MD *dgst, X509 *subject, X509 *issuer) sl@0: { sl@0: X509_NAME *iname; sl@0: ASN1_INTEGER *serial; sl@0: ASN1_BIT_STRING *ikey; sl@0: #ifndef OPENSSL_NO_SHA1 sl@0: if(!dgst) dgst = EVP_sha1(); sl@0: #endif sl@0: if (subject) sl@0: { sl@0: iname = X509_get_issuer_name(subject); sl@0: serial = X509_get_serialNumber(subject); sl@0: } sl@0: else sl@0: { sl@0: iname = X509_get_subject_name(issuer); sl@0: serial = NULL; sl@0: } sl@0: ikey = X509_get0_pubkey_bitstr(issuer); sl@0: return OCSP_cert_id_new(dgst, iname, ikey, serial); sl@0: } sl@0: sl@0: sl@0: EXPORT_C OCSP_CERTID *OCSP_cert_id_new(const EVP_MD *dgst, sl@0: X509_NAME *issuerName, sl@0: ASN1_BIT_STRING* issuerKey, sl@0: ASN1_INTEGER *serialNumber) sl@0: { sl@0: int nid; sl@0: unsigned int i; sl@0: X509_ALGOR *alg; sl@0: OCSP_CERTID *cid = NULL; sl@0: unsigned char md[EVP_MAX_MD_SIZE]; sl@0: sl@0: if (!(cid = OCSP_CERTID_new())) goto err; sl@0: sl@0: alg = cid->hashAlgorithm; sl@0: if (alg->algorithm != NULL) ASN1_OBJECT_free(alg->algorithm); sl@0: if ((nid = EVP_MD_type(dgst)) == NID_undef) sl@0: { sl@0: OCSPerr(OCSP_F_OCSP_CERT_ID_NEW,OCSP_R_UNKNOWN_NID); sl@0: goto err; sl@0: } sl@0: if (!(alg->algorithm=OBJ_nid2obj(nid))) goto err; sl@0: if ((alg->parameter=ASN1_TYPE_new()) == NULL) goto err; sl@0: alg->parameter->type=V_ASN1_NULL; sl@0: sl@0: if (!X509_NAME_digest(issuerName, dgst, md, &i)) goto digerr; sl@0: if (!(ASN1_OCTET_STRING_set(cid->issuerNameHash, md, i))) goto err; sl@0: sl@0: /* Calculate the issuerKey hash, excluding tag and length */ sl@0: EVP_Digest(issuerKey->data, issuerKey->length, md, &i, dgst, NULL); sl@0: sl@0: if (!(ASN1_OCTET_STRING_set(cid->issuerKeyHash, md, i))) goto err; sl@0: sl@0: if (serialNumber) sl@0: { sl@0: ASN1_INTEGER_free(cid->serialNumber); sl@0: if (!(cid->serialNumber = ASN1_INTEGER_dup(serialNumber))) goto err; sl@0: } sl@0: return cid; sl@0: digerr: sl@0: OCSPerr(OCSP_F_OCSP_CERT_ID_NEW,OCSP_R_DIGEST_ERR); sl@0: err: sl@0: if (cid) OCSP_CERTID_free(cid); sl@0: return NULL; sl@0: } sl@0: sl@0: EXPORT_C int OCSP_id_issuer_cmp(OCSP_CERTID *a, OCSP_CERTID *b) sl@0: { sl@0: int ret; sl@0: ret = OBJ_cmp(a->hashAlgorithm->algorithm, b->hashAlgorithm->algorithm); sl@0: if (ret) return ret; sl@0: ret = ASN1_OCTET_STRING_cmp(a->issuerNameHash, b->issuerNameHash); sl@0: if (ret) return ret; sl@0: return ASN1_OCTET_STRING_cmp(a->issuerKeyHash, b->issuerKeyHash); sl@0: } sl@0: sl@0: EXPORT_C int OCSP_id_cmp(OCSP_CERTID *a, OCSP_CERTID *b) sl@0: { sl@0: int ret; sl@0: ret = OCSP_id_issuer_cmp(a, b); sl@0: if (ret) return ret; sl@0: return ASN1_INTEGER_cmp(a->serialNumber, b->serialNumber); sl@0: } sl@0: sl@0: sl@0: /* Parse a URL and split it up into host, port and path components and whether sl@0: * it is SSL. sl@0: */ sl@0: sl@0: EXPORT_C int OCSP_parse_url(char *url, char **phost, char **pport, char **ppath, int *pssl) sl@0: { sl@0: char *p, *buf; sl@0: sl@0: char *host, *port; sl@0: sl@0: /* dup the buffer since we are going to mess with it */ sl@0: buf = BUF_strdup(url); sl@0: if (!buf) goto mem_err; sl@0: sl@0: *phost = NULL; sl@0: *pport = NULL; sl@0: *ppath = NULL; sl@0: sl@0: /* Check for initial colon */ sl@0: p = strchr(buf, ':'); sl@0: sl@0: if (!p) goto parse_err; sl@0: sl@0: *(p++) = '\0'; sl@0: sl@0: if (!strcmp(buf, "http")) sl@0: { sl@0: *pssl = 0; sl@0: port = "80"; sl@0: } sl@0: else if (!strcmp(buf, "https")) sl@0: { sl@0: *pssl = 1; sl@0: port = "443"; sl@0: } sl@0: else sl@0: goto parse_err; sl@0: sl@0: /* Check for double slash */ sl@0: if ((p[0] != '/') || (p[1] != '/')) sl@0: goto parse_err; sl@0: sl@0: p += 2; sl@0: sl@0: host = p; sl@0: sl@0: /* Check for trailing part of path */ sl@0: sl@0: p = strchr(p, '/'); sl@0: sl@0: if (!p) sl@0: *ppath = BUF_strdup("/"); sl@0: else sl@0: { sl@0: *ppath = BUF_strdup(p); sl@0: /* Set start of path to 0 so hostname is valid */ sl@0: *p = '\0'; sl@0: } sl@0: sl@0: if (!*ppath) goto mem_err; sl@0: sl@0: /* Look for optional ':' for port number */ sl@0: if ((p = strchr(host, ':'))) sl@0: { sl@0: *p = 0; sl@0: port = p + 1; sl@0: } sl@0: else sl@0: { sl@0: /* Not found: set default port */ sl@0: if (*pssl) port = "443"; sl@0: else port = "80"; sl@0: } sl@0: sl@0: *pport = BUF_strdup(port); sl@0: if (!*pport) goto mem_err; sl@0: sl@0: *phost = BUF_strdup(host); sl@0: sl@0: if (!*phost) goto mem_err; sl@0: sl@0: OPENSSL_free(buf); sl@0: sl@0: return 1; sl@0: sl@0: mem_err: sl@0: OCSPerr(OCSP_F_OCSP_PARSE_URL, ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: sl@0: parse_err: sl@0: OCSPerr(OCSP_F_OCSP_PARSE_URL, OCSP_R_ERROR_PARSING_URL); sl@0: sl@0: sl@0: err: sl@0: if (buf) OPENSSL_free(buf); sl@0: if (*ppath) OPENSSL_free(*ppath); sl@0: if (*pport) OPENSSL_free(*pport); sl@0: if (*phost) OPENSSL_free(*phost); sl@0: return 0; sl@0: sl@0: }