ocsp_lib.c 7.7 KB

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