ocsp_lib.c 7.0 KB

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