2
0

ocsp_lib.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* ocsp_lib.c */
  2. /*
  3. * Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
  4. * project.
  5. */
  6. /*
  7. * History: This file was transfered to Richard Levitte from CertCo by Kathy
  8. * Weinhold in mid-spring 2000 to be included in OpenSSL or released as a
  9. * patch kit.
  10. */
  11. /* ====================================================================
  12. * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in
  23. * the documentation and/or other materials provided with the
  24. * distribution.
  25. *
  26. * 3. All advertising materials mentioning features or use of this
  27. * software must display the following acknowledgment:
  28. * "This product includes software developed by the OpenSSL Project
  29. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  30. *
  31. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  32. * endorse or promote products derived from this software without
  33. * prior written permission. For written permission, please contact
  34. * openssl-core@openssl.org.
  35. *
  36. * 5. Products derived from this software may not be called "OpenSSL"
  37. * nor may "OpenSSL" appear in their names without prior written
  38. * permission of the OpenSSL Project.
  39. *
  40. * 6. Redistributions of any form whatsoever must retain the following
  41. * acknowledgment:
  42. * "This product includes software developed by the OpenSSL Project
  43. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  44. *
  45. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  46. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  47. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  48. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  49. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  50. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  51. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  52. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  53. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  54. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  55. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  56. * OF THE POSSIBILITY OF SUCH DAMAGE.
  57. * ====================================================================
  58. *
  59. * This product includes cryptographic software written by Eric Young
  60. * (eay@cryptsoft.com). This product includes software written by Tim
  61. * Hudson (tjh@cryptsoft.com).
  62. *
  63. */
  64. #include <stdio.h>
  65. #include "internal/cryptlib.h"
  66. #include <openssl/objects.h>
  67. #include <openssl/rand.h>
  68. #include <openssl/x509.h>
  69. #include <openssl/pem.h>
  70. #include <openssl/x509v3.h>
  71. #include <openssl/ocsp.h>
  72. #include "ocsp_lcl.h"
  73. #include <openssl/asn1t.h>
  74. /* Convert a certificate and its issuer to an OCSP_CERTID */
  75. OCSP_CERTID *OCSP_cert_to_id(const EVP_MD *dgst, X509 *subject, X509 *issuer)
  76. {
  77. X509_NAME *iname;
  78. ASN1_INTEGER *serial;
  79. ASN1_BIT_STRING *ikey;
  80. if (!dgst)
  81. dgst = EVP_sha1();
  82. if (subject) {
  83. iname = X509_get_issuer_name(subject);
  84. serial = X509_get_serialNumber(subject);
  85. } else {
  86. iname = X509_get_subject_name(issuer);
  87. serial = NULL;
  88. }
  89. ikey = X509_get0_pubkey_bitstr(issuer);
  90. return OCSP_cert_id_new(dgst, iname, ikey, serial);
  91. }
  92. OCSP_CERTID *OCSP_cert_id_new(const EVP_MD *dgst,
  93. X509_NAME *issuerName,
  94. ASN1_BIT_STRING *issuerKey,
  95. ASN1_INTEGER *serialNumber)
  96. {
  97. int nid;
  98. unsigned int i;
  99. X509_ALGOR *alg;
  100. OCSP_CERTID *cid = NULL;
  101. unsigned char md[EVP_MAX_MD_SIZE];
  102. if ((cid = OCSP_CERTID_new()) == NULL)
  103. goto err;
  104. alg = &cid->hashAlgorithm;
  105. ASN1_OBJECT_free(alg->algorithm);
  106. if ((nid = EVP_MD_type(dgst)) == NID_undef) {
  107. OCSPerr(OCSP_F_OCSP_CERT_ID_NEW, OCSP_R_UNKNOWN_NID);
  108. goto err;
  109. }
  110. if ((alg->algorithm = OBJ_nid2obj(nid)) == NULL)
  111. goto err;
  112. if ((alg->parameter = ASN1_TYPE_new()) == NULL)
  113. goto err;
  114. alg->parameter->type = V_ASN1_NULL;
  115. if (!X509_NAME_digest(issuerName, dgst, md, &i))
  116. goto digerr;
  117. if (!(ASN1_OCTET_STRING_set(&cid->issuerNameHash, md, i)))
  118. goto err;
  119. /* Calculate the issuerKey hash, excluding tag and length */
  120. if (!EVP_Digest(issuerKey->data, issuerKey->length, md, &i, dgst, NULL))
  121. goto err;
  122. if (!(ASN1_OCTET_STRING_set(&cid->issuerKeyHash, md, i)))
  123. goto err;
  124. if (serialNumber) {
  125. if (ASN1_STRING_copy(&cid->serialNumber, serialNumber) == 0)
  126. goto err;
  127. }
  128. return cid;
  129. digerr:
  130. OCSPerr(OCSP_F_OCSP_CERT_ID_NEW, OCSP_R_DIGEST_ERR);
  131. err:
  132. OCSP_CERTID_free(cid);
  133. return NULL;
  134. }
  135. int OCSP_id_issuer_cmp(OCSP_CERTID *a, OCSP_CERTID *b)
  136. {
  137. int ret;
  138. ret = OBJ_cmp(a->hashAlgorithm.algorithm, b->hashAlgorithm.algorithm);
  139. if (ret)
  140. return ret;
  141. ret = ASN1_OCTET_STRING_cmp(&a->issuerNameHash, &b->issuerNameHash);
  142. if (ret)
  143. return ret;
  144. return ASN1_OCTET_STRING_cmp(&a->issuerKeyHash, &b->issuerKeyHash);
  145. }
  146. int OCSP_id_cmp(OCSP_CERTID *a, OCSP_CERTID *b)
  147. {
  148. int ret;
  149. ret = OCSP_id_issuer_cmp(a, b);
  150. if (ret)
  151. return ret;
  152. return ASN1_INTEGER_cmp(&a->serialNumber, &b->serialNumber);
  153. }
  154. /*
  155. * Parse a URL and split it up into host, port and path components and
  156. * whether it is SSL.
  157. */
  158. int OCSP_parse_url(const char *url, char **phost, char **pport, char **ppath,
  159. int *pssl)
  160. {
  161. char *p, *buf;
  162. char *host, *port;
  163. *phost = NULL;
  164. *pport = NULL;
  165. *ppath = NULL;
  166. /* dup the buffer since we are going to mess with it */
  167. buf = OPENSSL_strdup(url);
  168. if (!buf)
  169. goto mem_err;
  170. /* Check for initial colon */
  171. p = strchr(buf, ':');
  172. if (!p)
  173. goto parse_err;
  174. *(p++) = '\0';
  175. if (strcmp(buf, "http") == 0) {
  176. *pssl = 0;
  177. port = "80";
  178. } else if (strcmp(buf, "https") == 0) {
  179. *pssl = 1;
  180. port = "443";
  181. } else
  182. goto parse_err;
  183. /* Check for double slash */
  184. if ((p[0] != '/') || (p[1] != '/'))
  185. goto parse_err;
  186. p += 2;
  187. host = p;
  188. /* Check for trailing part of path */
  189. p = strchr(p, '/');
  190. if (!p)
  191. *ppath = OPENSSL_strdup("/");
  192. else {
  193. *ppath = OPENSSL_strdup(p);
  194. /* Set start of path to 0 so hostname is valid */
  195. *p = '\0';
  196. }
  197. if (!*ppath)
  198. goto mem_err;
  199. p = host;
  200. if (host[0] == '[') {
  201. /* ipv6 literal */
  202. host++;
  203. p = strchr(host, ']');
  204. if (!p)
  205. goto parse_err;
  206. *p = '\0';
  207. p++;
  208. }
  209. /* Look for optional ':' for port number */
  210. if ((p = strchr(p, ':'))) {
  211. *p = 0;
  212. port = p + 1;
  213. }
  214. *pport = OPENSSL_strdup(port);
  215. if (!*pport)
  216. goto mem_err;
  217. *phost = OPENSSL_strdup(host);
  218. if (!*phost)
  219. goto mem_err;
  220. OPENSSL_free(buf);
  221. return 1;
  222. mem_err:
  223. OCSPerr(OCSP_F_OCSP_PARSE_URL, ERR_R_MALLOC_FAILURE);
  224. goto err;
  225. parse_err:
  226. OCSPerr(OCSP_F_OCSP_PARSE_URL, OCSP_R_ERROR_PARSING_URL);
  227. err:
  228. OPENSSL_free(buf);
  229. OPENSSL_free(*ppath);
  230. OPENSSL_free(*pport);
  231. OPENSSL_free(*phost);
  232. return 0;
  233. }
  234. IMPLEMENT_ASN1_DUP_FUNCTION(OCSP_CERTID)