ocsp_lib.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 <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 <openssl/asn1t.h>
  73. /* Convert a certificate and its issuer to an OCSP_CERTID */
  74. OCSP_CERTID *OCSP_cert_to_id(const EVP_MD *dgst, X509 *subject, X509 *issuer)
  75. {
  76. X509_NAME *iname;
  77. ASN1_INTEGER *serial;
  78. ASN1_BIT_STRING *ikey;
  79. #ifndef OPENSSL_NO_SHA1
  80. if (!dgst)
  81. dgst = EVP_sha1();
  82. #endif
  83. if (subject) {
  84. iname = X509_get_issuer_name(subject);
  85. serial = X509_get_serialNumber(subject);
  86. } else {
  87. iname = X509_get_subject_name(issuer);
  88. serial = NULL;
  89. }
  90. ikey = X509_get0_pubkey_bitstr(issuer);
  91. return OCSP_cert_id_new(dgst, iname, ikey, serial);
  92. }
  93. OCSP_CERTID *OCSP_cert_id_new(const EVP_MD *dgst,
  94. X509_NAME *issuerName,
  95. ASN1_BIT_STRING *issuerKey,
  96. ASN1_INTEGER *serialNumber)
  97. {
  98. int nid;
  99. unsigned int i;
  100. X509_ALGOR *alg;
  101. OCSP_CERTID *cid = NULL;
  102. unsigned char md[EVP_MAX_MD_SIZE];
  103. if (!(cid = OCSP_CERTID_new()))
  104. goto err;
  105. alg = cid->hashAlgorithm;
  106. if (alg->algorithm != NULL)
  107. ASN1_OBJECT_free(alg->algorithm);
  108. if ((nid = EVP_MD_type(dgst)) == NID_undef) {
  109. OCSPerr(OCSP_F_OCSP_CERT_ID_NEW, OCSP_R_UNKNOWN_NID);
  110. goto err;
  111. }
  112. if (!(alg->algorithm = OBJ_nid2obj(nid)))
  113. goto err;
  114. if ((alg->parameter = ASN1_TYPE_new()) == NULL)
  115. goto err;
  116. alg->parameter->type = V_ASN1_NULL;
  117. if (!X509_NAME_digest(issuerName, dgst, md, &i))
  118. goto digerr;
  119. if (!(ASN1_OCTET_STRING_set(cid->issuerNameHash, md, i)))
  120. goto err;
  121. /* Calculate the issuerKey hash, excluding tag and length */
  122. if (!EVP_Digest(issuerKey->data, issuerKey->length, md, &i, dgst, NULL))
  123. goto err;
  124. if (!(ASN1_OCTET_STRING_set(cid->issuerKeyHash, md, i)))
  125. goto err;
  126. if (serialNumber) {
  127. ASN1_INTEGER_free(cid->serialNumber);
  128. if (!(cid->serialNumber = ASN1_INTEGER_dup(serialNumber)))
  129. goto err;
  130. }
  131. return cid;
  132. digerr:
  133. OCSPerr(OCSP_F_OCSP_CERT_ID_NEW, OCSP_R_DIGEST_ERR);
  134. err:
  135. if (cid)
  136. OCSP_CERTID_free(cid);
  137. return NULL;
  138. }
  139. int OCSP_id_issuer_cmp(OCSP_CERTID *a, OCSP_CERTID *b)
  140. {
  141. int ret;
  142. ret = OBJ_cmp(a->hashAlgorithm->algorithm, b->hashAlgorithm->algorithm);
  143. if (ret)
  144. return ret;
  145. ret = ASN1_OCTET_STRING_cmp(a->issuerNameHash, b->issuerNameHash);
  146. if (ret)
  147. return ret;
  148. return ASN1_OCTET_STRING_cmp(a->issuerKeyHash, b->issuerKeyHash);
  149. }
  150. int OCSP_id_cmp(OCSP_CERTID *a, OCSP_CERTID *b)
  151. {
  152. int ret;
  153. ret = OCSP_id_issuer_cmp(a, b);
  154. if (ret)
  155. return ret;
  156. return ASN1_INTEGER_cmp(a->serialNumber, b->serialNumber);
  157. }
  158. /*
  159. * Parse a URL and split it up into host, port and path components and
  160. * whether it is SSL.
  161. */
  162. int OCSP_parse_url(const char *url, char **phost, char **pport, char **ppath,
  163. int *pssl)
  164. {
  165. char *p, *buf;
  166. char *host, *port;
  167. *phost = NULL;
  168. *pport = NULL;
  169. *ppath = NULL;
  170. /* dup the buffer since we are going to mess with it */
  171. buf = BUF_strdup(url);
  172. if (!buf)
  173. goto mem_err;
  174. /* Check for initial colon */
  175. p = strchr(buf, ':');
  176. if (!p)
  177. goto parse_err;
  178. *(p++) = '\0';
  179. if (!strcmp(buf, "http")) {
  180. *pssl = 0;
  181. port = "80";
  182. } else if (!strcmp(buf, "https")) {
  183. *pssl = 1;
  184. port = "443";
  185. } else
  186. goto parse_err;
  187. /* Check for double slash */
  188. if ((p[0] != '/') || (p[1] != '/'))
  189. goto parse_err;
  190. p += 2;
  191. host = p;
  192. /* Check for trailing part of path */
  193. p = strchr(p, '/');
  194. if (!p)
  195. *ppath = BUF_strdup("/");
  196. else {
  197. *ppath = BUF_strdup(p);
  198. /* Set start of path to 0 so hostname is valid */
  199. *p = '\0';
  200. }
  201. if (!*ppath)
  202. goto mem_err;
  203. p = host;
  204. if (host[0] == '[') {
  205. /* ipv6 literal */
  206. host++;
  207. p = strchr(host, ']');
  208. if (!p)
  209. goto parse_err;
  210. *p = '\0';
  211. p++;
  212. }
  213. /* Look for optional ':' for port number */
  214. if ((p = strchr(p, ':'))) {
  215. *p = 0;
  216. port = p + 1;
  217. }
  218. *pport = BUF_strdup(port);
  219. if (!*pport)
  220. goto mem_err;
  221. *phost = BUF_strdup(host);
  222. if (!*phost)
  223. goto mem_err;
  224. OPENSSL_free(buf);
  225. return 1;
  226. mem_err:
  227. OCSPerr(OCSP_F_OCSP_PARSE_URL, ERR_R_MALLOC_FAILURE);
  228. goto err;
  229. parse_err:
  230. OCSPerr(OCSP_F_OCSP_PARSE_URL, OCSP_R_ERROR_PARSING_URL);
  231. err:
  232. if (buf)
  233. OPENSSL_free(buf);
  234. if (*ppath)
  235. OPENSSL_free(*ppath);
  236. if (*pport)
  237. OPENSSL_free(*pport);
  238. if (*phost)
  239. OPENSSL_free(*phost);
  240. return 0;
  241. }
  242. IMPLEMENT_ASN1_DUP_FUNCTION(OCSP_CERTID)