ess_lib.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 <string.h>
  10. #include <openssl/x509v3.h>
  11. #include <openssl/err.h>
  12. #include <openssl/ess.h>
  13. #include "internal/sizes.h"
  14. #include "crypto/ess.h"
  15. #include "crypto/x509.h"
  16. static ESS_CERT_ID *ESS_CERT_ID_new_init(const X509 *cert,
  17. int set_issuer_serial);
  18. static ESS_CERT_ID_V2 *ESS_CERT_ID_V2_new_init(const EVP_MD *hash_alg,
  19. const X509 *cert,
  20. int set_issuer_serial);
  21. ESS_SIGNING_CERT *OSSL_ESS_signing_cert_new_init(const X509 *signcert,
  22. const STACK_OF(X509) *certs,
  23. int set_issuer_serial)
  24. {
  25. ESS_CERT_ID *cid = NULL;
  26. ESS_SIGNING_CERT *sc;
  27. int i;
  28. if ((sc = ESS_SIGNING_CERT_new()) == NULL)
  29. goto err;
  30. if (sc->cert_ids == NULL
  31. && (sc->cert_ids = sk_ESS_CERT_ID_new_null()) == NULL)
  32. goto err;
  33. if ((cid = ESS_CERT_ID_new_init(signcert, set_issuer_serial)) == NULL
  34. || !sk_ESS_CERT_ID_push(sc->cert_ids, cid))
  35. goto err;
  36. for (i = 0; i < sk_X509_num(certs); ++i) {
  37. X509 *cert = sk_X509_value(certs, i);
  38. if ((cid = ESS_CERT_ID_new_init(cert, 1)) == NULL
  39. || !sk_ESS_CERT_ID_push(sc->cert_ids, cid))
  40. goto err;
  41. }
  42. return sc;
  43. err:
  44. ESS_SIGNING_CERT_free(sc);
  45. ESS_CERT_ID_free(cid);
  46. ERR_raise(ERR_LIB_ESS, ERR_R_MALLOC_FAILURE);
  47. return NULL;
  48. }
  49. static ESS_CERT_ID *ESS_CERT_ID_new_init(const X509 *cert,
  50. int set_issuer_serial)
  51. {
  52. ESS_CERT_ID *cid = NULL;
  53. GENERAL_NAME *name = NULL;
  54. unsigned char cert_sha1[SHA_DIGEST_LENGTH];
  55. if ((cid = ESS_CERT_ID_new()) == NULL)
  56. goto err;
  57. if (!X509_digest(cert, EVP_sha1(), cert_sha1, NULL))
  58. goto err;
  59. if (!ASN1_OCTET_STRING_set(cid->hash, cert_sha1, SHA_DIGEST_LENGTH))
  60. goto err;
  61. /* Setting the issuer/serial if requested. */
  62. if (!set_issuer_serial)
  63. return cid;
  64. if (cid->issuer_serial == NULL
  65. && (cid->issuer_serial = ESS_ISSUER_SERIAL_new()) == NULL)
  66. goto err;
  67. if ((name = GENERAL_NAME_new()) == NULL)
  68. goto err;
  69. name->type = GEN_DIRNAME;
  70. if ((name->d.dirn = X509_NAME_dup(X509_get_issuer_name(cert))) == NULL)
  71. goto err;
  72. if (!sk_GENERAL_NAME_push(cid->issuer_serial->issuer, name))
  73. goto err;
  74. name = NULL; /* Ownership is lost. */
  75. ASN1_INTEGER_free(cid->issuer_serial->serial);
  76. if ((cid->issuer_serial->serial =
  77. ASN1_INTEGER_dup(X509_get0_serialNumber(cert))) == NULL)
  78. goto err;
  79. return cid;
  80. err:
  81. GENERAL_NAME_free(name);
  82. ESS_CERT_ID_free(cid);
  83. ERR_raise(ERR_LIB_ESS, ERR_R_MALLOC_FAILURE);
  84. return NULL;
  85. }
  86. ESS_SIGNING_CERT_V2 *OSSL_ESS_signing_cert_v2_new_init(const EVP_MD *hash_alg,
  87. const X509 *signcert,
  88. const
  89. STACK_OF(X509) *certs,
  90. int set_issuer_serial)
  91. {
  92. ESS_CERT_ID_V2 *cid = NULL;
  93. ESS_SIGNING_CERT_V2 *sc;
  94. int i;
  95. if ((sc = ESS_SIGNING_CERT_V2_new()) == NULL)
  96. goto err;
  97. cid = ESS_CERT_ID_V2_new_init(hash_alg, signcert, set_issuer_serial);
  98. if (cid == NULL)
  99. goto err;
  100. if (!sk_ESS_CERT_ID_V2_push(sc->cert_ids, cid))
  101. goto err;
  102. cid = NULL;
  103. for (i = 0; i < sk_X509_num(certs); ++i) {
  104. X509 *cert = sk_X509_value(certs, i);
  105. if ((cid = ESS_CERT_ID_V2_new_init(hash_alg, cert, 1)) == NULL)
  106. goto err;
  107. if (!sk_ESS_CERT_ID_V2_push(sc->cert_ids, cid))
  108. goto err;
  109. cid = NULL;
  110. }
  111. return sc;
  112. err:
  113. ESS_SIGNING_CERT_V2_free(sc);
  114. ESS_CERT_ID_V2_free(cid);
  115. ERR_raise(ERR_LIB_ESS, ERR_R_MALLOC_FAILURE);
  116. return NULL;
  117. }
  118. static ESS_CERT_ID_V2 *ESS_CERT_ID_V2_new_init(const EVP_MD *hash_alg,
  119. const X509 *cert,
  120. int set_issuer_serial)
  121. {
  122. ESS_CERT_ID_V2 *cid;
  123. GENERAL_NAME *name = NULL;
  124. unsigned char hash[EVP_MAX_MD_SIZE];
  125. unsigned int hash_len = sizeof(hash);
  126. X509_ALGOR *alg = NULL;
  127. memset(hash, 0, sizeof(hash));
  128. if ((cid = ESS_CERT_ID_V2_new()) == NULL)
  129. goto err;
  130. if (!EVP_MD_is_a(hash_alg, SN_sha256)) {
  131. alg = X509_ALGOR_new();
  132. if (alg == NULL)
  133. goto err;
  134. X509_ALGOR_set_md(alg, hash_alg);
  135. if (alg->algorithm == NULL)
  136. goto err;
  137. cid->hash_alg = alg;
  138. alg = NULL;
  139. } else {
  140. cid->hash_alg = NULL;
  141. }
  142. if (!X509_digest(cert, hash_alg, hash, &hash_len))
  143. goto err;
  144. if (!ASN1_OCTET_STRING_set(cid->hash, hash, hash_len))
  145. goto err;
  146. if (!set_issuer_serial)
  147. return cid;
  148. if ((cid->issuer_serial = ESS_ISSUER_SERIAL_new()) == NULL)
  149. goto err;
  150. if ((name = GENERAL_NAME_new()) == NULL)
  151. goto err;
  152. name->type = GEN_DIRNAME;
  153. if ((name->d.dirn = X509_NAME_dup(X509_get_issuer_name(cert))) == NULL)
  154. goto err;
  155. if (!sk_GENERAL_NAME_push(cid->issuer_serial->issuer, name))
  156. goto err;
  157. name = NULL; /* Ownership is lost. */
  158. ASN1_INTEGER_free(cid->issuer_serial->serial);
  159. cid->issuer_serial->serial = ASN1_INTEGER_dup(X509_get0_serialNumber(cert));
  160. if (cid->issuer_serial->serial == NULL)
  161. goto err;
  162. return cid;
  163. err:
  164. X509_ALGOR_free(alg);
  165. GENERAL_NAME_free(name);
  166. ESS_CERT_ID_V2_free(cid);
  167. ERR_raise(ERR_LIB_ESS, ERR_R_MALLOC_FAILURE);
  168. return NULL;
  169. }
  170. static int ess_issuer_serial_cmp(const ESS_ISSUER_SERIAL *is, const X509 *cert)
  171. {
  172. GENERAL_NAME *issuer;
  173. if (is == NULL || cert == NULL || sk_GENERAL_NAME_num(is->issuer) != 1)
  174. return -1;
  175. issuer = sk_GENERAL_NAME_value(is->issuer, 0);
  176. if (issuer->type != GEN_DIRNAME
  177. || X509_NAME_cmp(issuer->d.dirn, X509_get_issuer_name(cert)) != 0)
  178. return -1;
  179. return ASN1_INTEGER_cmp(is->serial, X509_get0_serialNumber(cert));
  180. }
  181. /*
  182. * Find the cert in |certs| referenced by |cid| if not NULL, else by |cid_v2|.
  183. * The cert must be the first one in |certs| if and only if |index| is 0.
  184. * Return 0 on not found, -1 on error, else 1 + the position in |certs|.
  185. */
  186. static int find(const ESS_CERT_ID *cid, const ESS_CERT_ID_V2 *cid_v2,
  187. int index, const STACK_OF(X509) *certs)
  188. {
  189. const X509 *cert;
  190. EVP_MD *md = NULL;
  191. char name[OSSL_MAX_NAME_SIZE];
  192. unsigned char cert_digest[EVP_MAX_MD_SIZE];
  193. unsigned int len, cid_hash_len;
  194. const ESS_ISSUER_SERIAL *is;
  195. int i;
  196. int ret = -1;
  197. if (cid == NULL && cid_v2 == NULL) {
  198. ERR_raise(ERR_LIB_ESS, ERR_R_PASSED_INVALID_ARGUMENT);
  199. return -1;
  200. }
  201. if (cid != NULL)
  202. strcpy(name, "SHA1");
  203. else if (cid_v2->hash_alg == NULL)
  204. strcpy(name, "SHA256");
  205. else
  206. OBJ_obj2txt(name, sizeof(name), cid_v2->hash_alg->algorithm, 0);
  207. (void)ERR_set_mark();
  208. md = EVP_MD_fetch(NULL, name, NULL);
  209. if (md == NULL)
  210. md = (EVP_MD *)EVP_get_digestbyname(name);
  211. if (md == NULL) {
  212. (void)ERR_clear_last_mark();
  213. ERR_raise(ERR_LIB_ESS, ESS_R_ESS_DIGEST_ALG_UNKNOWN);
  214. goto end;
  215. }
  216. (void)ERR_pop_to_mark();
  217. for (i = 0; i < sk_X509_num(certs); ++i) {
  218. cert = sk_X509_value(certs, i);
  219. cid_hash_len = cid != NULL ? cid->hash->length : cid_v2->hash->length;
  220. if (!X509_digest(cert, md, cert_digest, &len)
  221. || cid_hash_len != len) {
  222. ERR_raise(ERR_LIB_ESS, ESS_R_ESS_CERT_DIGEST_ERROR);
  223. goto end;
  224. }
  225. if (memcmp(cid != NULL ? cid->hash->data : cid_v2->hash->data,
  226. cert_digest, len) == 0) {
  227. is = cid != NULL ? cid->issuer_serial : cid_v2->issuer_serial;
  228. /* Well, it's not really required to match the serial numbers. */
  229. if (is == NULL || ess_issuer_serial_cmp(is, cert) == 0) {
  230. if ((i == 0) == (index == 0)) {
  231. ret = i + 1;
  232. goto end;
  233. }
  234. ERR_raise(ERR_LIB_ESS, ESS_R_ESS_CERT_ID_WRONG_ORDER);
  235. goto end;
  236. }
  237. }
  238. }
  239. ret = 0;
  240. ERR_raise(ERR_LIB_ESS, ESS_R_ESS_CERT_ID_NOT_FOUND);
  241. end:
  242. EVP_MD_free(md);
  243. return ret;
  244. }
  245. int OSSL_ESS_check_signing_certs(const ESS_SIGNING_CERT *ss,
  246. const ESS_SIGNING_CERT_V2 *ssv2,
  247. const STACK_OF(X509) *chain,
  248. int require_signing_cert)
  249. {
  250. int n_v1 = ss == NULL ? -1 : sk_ESS_CERT_ID_num(ss->cert_ids);
  251. int n_v2 = ssv2 == NULL ? -1 : sk_ESS_CERT_ID_V2_num(ssv2->cert_ids);
  252. int i, ret;
  253. if (require_signing_cert && ss == NULL && ssv2 == NULL) {
  254. ERR_raise(ERR_LIB_CMS, ESS_R_MISSING_SIGNING_CERTIFICATE_ATTRIBUTE);
  255. return -1;
  256. }
  257. if (n_v1 == 0 || n_v2 == 0) {
  258. ERR_raise(ERR_LIB_ESS, ESS_R_EMPTY_ESS_CERT_ID_LIST);
  259. return -1;
  260. }
  261. /* If both ss and ssv2 exist, as required evaluate them independently. */
  262. for (i = 0; i < n_v1; i++) {
  263. ret = find(sk_ESS_CERT_ID_value(ss->cert_ids, i), NULL, i, chain);
  264. if (ret <= 0)
  265. return ret;
  266. }
  267. for (i = 0; i < n_v2; i++) {
  268. ret = find(NULL, sk_ESS_CERT_ID_V2_value(ssv2->cert_ids, i), i, chain);
  269. if (ret <= 0)
  270. return ret;
  271. }
  272. return 1;
  273. }