ocsp_lib.c 6.9 KB

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