ocsp_lib.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. EVP_Digest(issuerKey->data, issuerKey->length, md, &i, dgst, NULL);
  117. if (!(ASN1_OCTET_STRING_set(cid->issuerKeyHash, md, i))) goto err;
  118. if (serialNumber)
  119. {
  120. ASN1_INTEGER_free(cid->serialNumber);
  121. if (!(cid->serialNumber = ASN1_INTEGER_dup(serialNumber))) goto err;
  122. }
  123. return cid;
  124. digerr:
  125. OCSPerr(OCSP_F_OCSP_CERT_ID_NEW,OCSP_R_DIGEST_ERR);
  126. err:
  127. if (cid) OCSP_CERTID_free(cid);
  128. return NULL;
  129. }
  130. int OCSP_id_issuer_cmp(OCSP_CERTID *a, OCSP_CERTID *b)
  131. {
  132. int ret;
  133. ret = OBJ_cmp(a->hashAlgorithm->algorithm, b->hashAlgorithm->algorithm);
  134. if (ret) return ret;
  135. ret = ASN1_OCTET_STRING_cmp(a->issuerNameHash, b->issuerNameHash);
  136. if (ret) return ret;
  137. return ASN1_OCTET_STRING_cmp(a->issuerKeyHash, b->issuerKeyHash);
  138. }
  139. int OCSP_id_cmp(OCSP_CERTID *a, OCSP_CERTID *b)
  140. {
  141. int ret;
  142. ret = OCSP_id_issuer_cmp(a, b);
  143. if (ret) return ret;
  144. return ASN1_INTEGER_cmp(a->serialNumber, b->serialNumber);
  145. }
  146. /* Parse a URL and split it up into host, port and path components and whether
  147. * it is SSL.
  148. */
  149. int OCSP_parse_url(char *url, char **phost, char **pport, char **ppath, int *pssl)
  150. {
  151. char *p, *buf;
  152. char *host, *port;
  153. /* dup the buffer since we are going to mess with it */
  154. buf = BUF_strdup(url);
  155. if (!buf) goto mem_err;
  156. *phost = NULL;
  157. *pport = NULL;
  158. *ppath = NULL;
  159. /* Check for initial colon */
  160. p = strchr(buf, ':');
  161. if (!p) goto parse_err;
  162. *(p++) = '\0';
  163. if (!strcmp(buf, "http"))
  164. {
  165. *pssl = 0;
  166. port = "80";
  167. }
  168. else if (!strcmp(buf, "https"))
  169. {
  170. *pssl = 1;
  171. port = "443";
  172. }
  173. else
  174. goto parse_err;
  175. /* Check for double slash */
  176. if ((p[0] != '/') || (p[1] != '/'))
  177. goto parse_err;
  178. p += 2;
  179. host = p;
  180. /* Check for trailing part of path */
  181. p = strchr(p, '/');
  182. if (!p)
  183. *ppath = BUF_strdup("/");
  184. else
  185. {
  186. *ppath = BUF_strdup(p);
  187. /* Set start of path to 0 so hostname is valid */
  188. *p = '\0';
  189. }
  190. if (!*ppath) goto mem_err;
  191. /* Look for optional ':' for port number */
  192. if ((p = strchr(host, ':')))
  193. {
  194. *p = 0;
  195. port = p + 1;
  196. }
  197. else
  198. {
  199. /* Not found: set default port */
  200. if (*pssl) port = "443";
  201. else port = "80";
  202. }
  203. *pport = BUF_strdup(port);
  204. if (!*pport) goto mem_err;
  205. *phost = BUF_strdup(host);
  206. if (!*phost) goto mem_err;
  207. OPENSSL_free(buf);
  208. return 1;
  209. mem_err:
  210. OCSPerr(OCSP_F_OCSP_PARSE_URL, ERR_R_MALLOC_FAILURE);
  211. goto err;
  212. parse_err:
  213. OCSPerr(OCSP_F_OCSP_PARSE_URL, OCSP_R_ERROR_PARSING_URL);
  214. err:
  215. if (buf) OPENSSL_free(buf);
  216. if (*ppath) OPENSSL_free(*ppath);
  217. if (*pport) OPENSSL_free(*pport);
  218. if (*phost) OPENSSL_free(*phost);
  219. return 0;
  220. }
  221. IMPLEMENT_ASN1_DUP_FUNCTION(OCSP_CERTID)