ocsp_lib.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/objects.h>
  12. #include <openssl/x509.h>
  13. #include <openssl/pem.h>
  14. #include <openssl/x509v3.h>
  15. #include <openssl/ocsp.h>
  16. #include "ocsp_local.h"
  17. #include <openssl/asn1t.h>
  18. /* Convert a certificate and its issuer to an OCSP_CERTID */
  19. OCSP_CERTID *OCSP_cert_to_id(const EVP_MD *dgst, const X509 *subject,
  20. const X509 *issuer)
  21. {
  22. X509_NAME *iname;
  23. const ASN1_INTEGER *serial;
  24. ASN1_BIT_STRING *ikey;
  25. if (!dgst)
  26. dgst = EVP_sha1();
  27. if (subject) {
  28. iname = X509_get_issuer_name(subject);
  29. serial = X509_get0_serialNumber(subject);
  30. } else {
  31. iname = X509_get_subject_name(issuer);
  32. serial = NULL;
  33. }
  34. ikey = X509_get0_pubkey_bitstr(issuer);
  35. return OCSP_cert_id_new(dgst, iname, ikey, serial);
  36. }
  37. OCSP_CERTID *OCSP_cert_id_new(const EVP_MD *dgst,
  38. const X509_NAME *issuerName,
  39. const ASN1_BIT_STRING *issuerKey,
  40. const ASN1_INTEGER *serialNumber)
  41. {
  42. int nid;
  43. unsigned int i;
  44. X509_ALGOR *alg;
  45. OCSP_CERTID *cid = NULL;
  46. unsigned char md[EVP_MAX_MD_SIZE];
  47. if ((cid = OCSP_CERTID_new()) == NULL)
  48. goto err;
  49. alg = &cid->hashAlgorithm;
  50. ASN1_OBJECT_free(alg->algorithm);
  51. if ((nid = EVP_MD_type(dgst)) == NID_undef) {
  52. OCSPerr(OCSP_F_OCSP_CERT_ID_NEW, OCSP_R_UNKNOWN_NID);
  53. goto err;
  54. }
  55. if ((alg->algorithm = OBJ_nid2obj(nid)) == NULL)
  56. goto err;
  57. if ((alg->parameter = ASN1_TYPE_new()) == NULL)
  58. goto err;
  59. alg->parameter->type = V_ASN1_NULL;
  60. if (!X509_NAME_digest(issuerName, dgst, md, &i))
  61. goto digerr;
  62. if (!(ASN1_OCTET_STRING_set(&cid->issuerNameHash, md, i)))
  63. goto err;
  64. /* Calculate the issuerKey hash, excluding tag and length */
  65. if (!EVP_Digest(issuerKey->data, issuerKey->length, md, &i, dgst, NULL))
  66. goto err;
  67. if (!(ASN1_OCTET_STRING_set(&cid->issuerKeyHash, md, i)))
  68. goto err;
  69. if (serialNumber) {
  70. if (ASN1_STRING_copy(&cid->serialNumber, serialNumber) == 0)
  71. goto err;
  72. }
  73. return cid;
  74. digerr:
  75. OCSPerr(OCSP_F_OCSP_CERT_ID_NEW, OCSP_R_DIGEST_ERR);
  76. err:
  77. OCSP_CERTID_free(cid);
  78. return NULL;
  79. }
  80. int OCSP_id_issuer_cmp(const OCSP_CERTID *a, const OCSP_CERTID *b)
  81. {
  82. int ret;
  83. ret = OBJ_cmp(a->hashAlgorithm.algorithm, b->hashAlgorithm.algorithm);
  84. if (ret)
  85. return ret;
  86. ret = ASN1_OCTET_STRING_cmp(&a->issuerNameHash, &b->issuerNameHash);
  87. if (ret)
  88. return ret;
  89. return ASN1_OCTET_STRING_cmp(&a->issuerKeyHash, &b->issuerKeyHash);
  90. }
  91. int OCSP_id_cmp(const OCSP_CERTID *a, const OCSP_CERTID *b)
  92. {
  93. int ret;
  94. ret = OCSP_id_issuer_cmp(a, b);
  95. if (ret)
  96. return ret;
  97. return ASN1_INTEGER_cmp(&a->serialNumber, &b->serialNumber);
  98. }
  99. /*
  100. * Parse a URL and split it up into host, port and path components and
  101. * whether it is SSL.
  102. */
  103. int OCSP_parse_url(const char *url, char **phost, char **pport, char **ppath,
  104. int *pssl)
  105. {
  106. char *p, *buf;
  107. char *host, *port;
  108. *phost = NULL;
  109. *pport = NULL;
  110. *ppath = NULL;
  111. /* dup the buffer since we are going to mess with it */
  112. buf = OPENSSL_strdup(url);
  113. if (!buf)
  114. goto mem_err;
  115. /* Check for initial colon */
  116. p = strchr(buf, ':');
  117. if (!p)
  118. goto parse_err;
  119. *(p++) = '\0';
  120. if (strcmp(buf, "http") == 0) {
  121. *pssl = 0;
  122. port = "80";
  123. } else if (strcmp(buf, "https") == 0) {
  124. *pssl = 1;
  125. port = "443";
  126. } else
  127. goto parse_err;
  128. /* Check for double slash */
  129. if ((p[0] != '/') || (p[1] != '/'))
  130. goto parse_err;
  131. p += 2;
  132. host = p;
  133. /* Check for trailing part of path */
  134. p = strchr(p, '/');
  135. if (!p)
  136. *ppath = OPENSSL_strdup("/");
  137. else {
  138. *ppath = OPENSSL_strdup(p);
  139. /* Set start of path to 0 so hostname is valid */
  140. *p = '\0';
  141. }
  142. if (!*ppath)
  143. goto mem_err;
  144. p = host;
  145. if (host[0] == '[') {
  146. /* ipv6 literal */
  147. host++;
  148. p = strchr(host, ']');
  149. if (!p)
  150. goto parse_err;
  151. *p = '\0';
  152. p++;
  153. }
  154. /* Look for optional ':' for port number */
  155. if ((p = strchr(p, ':'))) {
  156. *p = 0;
  157. port = p + 1;
  158. }
  159. *pport = OPENSSL_strdup(port);
  160. if (!*pport)
  161. goto mem_err;
  162. *phost = OPENSSL_strdup(host);
  163. if (!*phost)
  164. goto mem_err;
  165. OPENSSL_free(buf);
  166. return 1;
  167. mem_err:
  168. OCSPerr(OCSP_F_OCSP_PARSE_URL, ERR_R_MALLOC_FAILURE);
  169. goto err;
  170. parse_err:
  171. OCSPerr(OCSP_F_OCSP_PARSE_URL, OCSP_R_ERROR_PARSING_URL);
  172. err:
  173. OPENSSL_free(buf);
  174. OPENSSL_free(*ppath);
  175. *ppath = NULL;
  176. OPENSSL_free(*pport);
  177. *pport = NULL;
  178. OPENSSL_free(*phost);
  179. *phost = NULL;
  180. return 0;
  181. }
  182. IMPLEMENT_ASN1_DUP_FUNCTION(OCSP_CERTID)