ocsp_lib.c revision 1.13
1/* $OpenBSD: ocsp_lib.c,v 1.13 2014/07/10 22:45:57 jsing Exp $ */ 2/* Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL 3 * project. */ 4 5/* History: 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 8 as a patch kit. */ 9 10/* ==================================================================== 11 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 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 23 * distribution. 24 * 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/)" 29 * 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. 34 * 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. 38 * 39 * 6. Redistributions of any form whatsoever must retain the following 40 * acknowledgment: 41 * "This product includes software developed by the OpenSSL Project 42 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 43 * 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 * ==================================================================== 57 * 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). 61 * 62 */ 63 64#include <cryptlib.h> 65#include <stdio.h> 66#include <string.h> 67 68#include <openssl/opensslconf.h> 69 70#include <openssl/objects.h> 71#include <openssl/rand.h> 72#include <openssl/x509.h> 73#include <openssl/pem.h> 74#include <openssl/x509v3.h> 75#include <openssl/ocsp.h> 76#include <openssl/asn1t.h> 77 78/* Convert a certificate and its issuer to an OCSP_CERTID */ 79 80OCSP_CERTID * 81OCSP_cert_to_id(const EVP_MD *dgst, X509 *subject, X509 *issuer) 82{ 83 X509_NAME *iname; 84 ASN1_INTEGER *serial; 85 ASN1_BIT_STRING *ikey; 86 87#ifndef OPENSSL_NO_SHA1 88 if (!dgst) 89 dgst = EVP_sha1(); 90#endif 91 if (subject) { 92 iname = X509_get_issuer_name(subject); 93 serial = X509_get_serialNumber(subject); 94 } else { 95 iname = X509_get_subject_name(issuer); 96 serial = NULL; 97 } 98 ikey = X509_get0_pubkey_bitstr(issuer); 99 return OCSP_cert_id_new(dgst, iname, ikey, serial); 100} 101 102OCSP_CERTID * 103OCSP_cert_id_new(const EVP_MD *dgst, X509_NAME *issuerName, 104 ASN1_BIT_STRING* issuerKey, ASN1_INTEGER *serialNumber) 105{ 106 int nid; 107 unsigned int i; 108 X509_ALGOR *alg; 109 OCSP_CERTID *cid = NULL; 110 unsigned char md[EVP_MAX_MD_SIZE]; 111 112 if (!(cid = OCSP_CERTID_new())) 113 goto err; 114 115 alg = cid->hashAlgorithm; 116 if (alg->algorithm != NULL) 117 ASN1_OBJECT_free(alg->algorithm); 118 if ((nid = EVP_MD_type(dgst)) == NID_undef) { 119 OCSPerr(OCSP_F_OCSP_CERT_ID_NEW, OCSP_R_UNKNOWN_NID); 120 goto err; 121 } 122 if (!(alg->algorithm = OBJ_nid2obj(nid))) 123 goto err; 124 if ((alg->parameter = ASN1_TYPE_new()) == NULL) 125 goto err; 126 alg->parameter->type = V_ASN1_NULL; 127 128 if (!X509_NAME_digest(issuerName, dgst, md, &i)) 129 goto digerr; 130 if (!(ASN1_OCTET_STRING_set(cid->issuerNameHash, md, i))) 131 goto err; 132 133 /* Calculate the issuerKey hash, excluding tag and length */ 134 if (!EVP_Digest(issuerKey->data, issuerKey->length, md, &i, dgst, NULL)) 135 goto err; 136 137 if (!(ASN1_OCTET_STRING_set(cid->issuerKeyHash, md, i))) 138 goto err; 139 140 if (serialNumber) { 141 ASN1_INTEGER_free(cid->serialNumber); 142 if (!(cid->serialNumber = ASN1_INTEGER_dup(serialNumber))) 143 goto err; 144 } 145 return cid; 146 147digerr: 148 OCSPerr(OCSP_F_OCSP_CERT_ID_NEW, OCSP_R_DIGEST_ERR); 149err: 150 if (cid) 151 OCSP_CERTID_free(cid); 152 return NULL; 153} 154 155int 156OCSP_id_issuer_cmp(OCSP_CERTID *a, OCSP_CERTID *b) 157{ 158 int ret; 159 160 ret = OBJ_cmp(a->hashAlgorithm->algorithm, b->hashAlgorithm->algorithm); 161 if (ret) 162 return ret; 163 ret = ASN1_OCTET_STRING_cmp(a->issuerNameHash, b->issuerNameHash); 164 if (ret) 165 return ret; 166 return ASN1_OCTET_STRING_cmp(a->issuerKeyHash, b->issuerKeyHash); 167} 168 169int 170OCSP_id_cmp(OCSP_CERTID *a, OCSP_CERTID *b) 171{ 172 int ret; 173 174 ret = OCSP_id_issuer_cmp(a, b); 175 if (ret) 176 return ret; 177 return ASN1_INTEGER_cmp(a->serialNumber, b->serialNumber); 178} 179 180/* Parse a URL and split it up into host, port and path components and whether 181 * it is SSL. 182 */ 183int 184OCSP_parse_url(char *url, char **phost, char **pport, char **ppath, int *pssl) 185{ 186 char *p, *buf; 187 char *host, *port; 188 189 *phost = NULL; 190 *pport = NULL; 191 *ppath = NULL; 192 193 /* dup the buffer since we are going to mess with it */ 194 buf = BUF_strdup(url); 195 if (!buf) 196 goto mem_err; 197 198 /* Check for initial colon */ 199 p = strchr(buf, ':'); 200 if (!p) 201 goto parse_err; 202 203 *(p++) = '\0'; 204 205 if (!strcmp(buf, "http")) { 206 *pssl = 0; 207 port = "80"; 208 } else if (!strcmp(buf, "https")) { 209 *pssl = 1; 210 port = "443"; 211 } else 212 goto parse_err; 213 214 /* Check for double slash */ 215 if ((p[0] != '/') || (p[1] != '/')) 216 goto parse_err; 217 218 p += 2; 219 220 host = p; 221 222 /* Check for trailing part of path */ 223 p = strchr(p, '/'); 224 if (!p) 225 *ppath = BUF_strdup("/"); 226 else { 227 *ppath = BUF_strdup(p); 228 /* Set start of path to 0 so hostname is valid */ 229 *p = '\0'; 230 } 231 232 if (!*ppath) 233 goto mem_err; 234 235 /* Look for optional ':' for port number */ 236 if ((p = strchr(host, ':'))) { 237 *p = 0; 238 port = p + 1; 239 } else { 240 /* Not found: set default port */ 241 if (*pssl) 242 port = "443"; 243 else 244 port = "80"; 245 } 246 247 *pport = BUF_strdup(port); 248 if (!*pport) 249 goto mem_err; 250 251 *phost = BUF_strdup(host); 252 253 if (!*phost) 254 goto mem_err; 255 256 free(buf); 257 258 return 1; 259 260mem_err: 261 OCSPerr(OCSP_F_OCSP_PARSE_URL, ERR_R_MALLOC_FAILURE); 262 goto err; 263 264parse_err: 265 OCSPerr(OCSP_F_OCSP_PARSE_URL, OCSP_R_ERROR_PARSING_URL); 266 267err: 268 free(buf); 269 free(*ppath); 270 free(*pport); 271 free(*phost); 272 *phost = NULL; 273 *pport = NULL; 274 *ppath = NULL; 275 return 0; 276} 277 278IMPLEMENT_ASN1_DUP_FUNCTION(OCSP_CERTID) 279