ess_lib.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 "crypto/ess.h"
  14. #include "crypto/x509.h"
  15. static ESS_CERT_ID *ESS_CERT_ID_new_init(X509 *cert, int issuer_needed);
  16. static ESS_CERT_ID_V2 *ESS_CERT_ID_V2_new_init(const EVP_MD *hash_alg,
  17. X509 *cert, int issuer_needed);
  18. ESS_SIGNING_CERT *ossl_ess_signing_cert_new_init(X509 *signcert,
  19. STACK_OF(X509) *certs,
  20. int issuer_needed)
  21. {
  22. ESS_CERT_ID *cid = NULL;
  23. ESS_SIGNING_CERT *sc;
  24. int i;
  25. if ((sc = ESS_SIGNING_CERT_new()) == NULL)
  26. goto err;
  27. if (sc->cert_ids == NULL
  28. && (sc->cert_ids = sk_ESS_CERT_ID_new_null()) == NULL)
  29. goto err;
  30. if ((cid = ESS_CERT_ID_new_init(signcert, issuer_needed)) == NULL
  31. || !sk_ESS_CERT_ID_push(sc->cert_ids, cid))
  32. goto err;
  33. for (i = 0; i < sk_X509_num(certs); ++i) {
  34. X509 *cert = sk_X509_value(certs, i);
  35. if ((cid = ESS_CERT_ID_new_init(cert, 1)) == NULL
  36. || !sk_ESS_CERT_ID_push(sc->cert_ids, cid))
  37. goto err;
  38. }
  39. return sc;
  40. err:
  41. ESS_SIGNING_CERT_free(sc);
  42. ESS_CERT_ID_free(cid);
  43. ERR_raise(ERR_LIB_ESS, ERR_R_MALLOC_FAILURE);
  44. return NULL;
  45. }
  46. static ESS_CERT_ID *ESS_CERT_ID_new_init(X509 *cert, int issuer_needed)
  47. {
  48. ESS_CERT_ID *cid = NULL;
  49. GENERAL_NAME *name = NULL;
  50. unsigned char cert_sha1[SHA_DIGEST_LENGTH];
  51. /* Call for side-effect of computing hash and caching extensions */
  52. if (!ossl_x509v3_cache_extensions(cert))
  53. return NULL;
  54. if ((cid = ESS_CERT_ID_new()) == NULL)
  55. goto err;
  56. /* TODO(3.0): fetch sha1 algorithm from providers */
  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 (!issuer_needed)
  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. X509 *signcert,
  88. STACK_OF(X509) *certs,
  89. int issuer_needed)
  90. {
  91. ESS_CERT_ID_V2 *cid = NULL;
  92. ESS_SIGNING_CERT_V2 *sc;
  93. int i;
  94. if ((sc = ESS_SIGNING_CERT_V2_new()) == NULL)
  95. goto err;
  96. if ((cid = ESS_CERT_ID_V2_new_init(hash_alg, signcert, issuer_needed)) == NULL)
  97. goto err;
  98. if (!sk_ESS_CERT_ID_V2_push(sc->cert_ids, cid))
  99. goto err;
  100. cid = NULL;
  101. for (i = 0; i < sk_X509_num(certs); ++i) {
  102. X509 *cert = sk_X509_value(certs, i);
  103. if ((cid = ESS_CERT_ID_V2_new_init(hash_alg, cert, 1)) == NULL)
  104. goto err;
  105. if (!sk_ESS_CERT_ID_V2_push(sc->cert_ids, cid))
  106. goto err;
  107. cid = NULL;
  108. }
  109. return sc;
  110. err:
  111. ESS_SIGNING_CERT_V2_free(sc);
  112. ESS_CERT_ID_V2_free(cid);
  113. ERR_raise(ERR_LIB_ESS, ERR_R_MALLOC_FAILURE);
  114. return NULL;
  115. }
  116. static ESS_CERT_ID_V2 *ESS_CERT_ID_V2_new_init(const EVP_MD *hash_alg,
  117. X509 *cert, int issuer_needed)
  118. {
  119. ESS_CERT_ID_V2 *cid;
  120. GENERAL_NAME *name = NULL;
  121. unsigned char hash[EVP_MAX_MD_SIZE];
  122. unsigned int hash_len = sizeof(hash);
  123. X509_ALGOR *alg = NULL;
  124. memset(hash, 0, sizeof(hash));
  125. if ((cid = ESS_CERT_ID_V2_new()) == NULL)
  126. goto err;
  127. if (!EVP_MD_is_a(hash_alg, SN_sha256)) {
  128. alg = X509_ALGOR_new();
  129. if (alg == NULL)
  130. goto err;
  131. X509_ALGOR_set_md(alg, hash_alg);
  132. if (alg->algorithm == NULL)
  133. goto err;
  134. cid->hash_alg = alg;
  135. alg = NULL;
  136. } else {
  137. cid->hash_alg = NULL;
  138. }
  139. /* TODO(3.0): fetch sha1 algorithm from providers */
  140. if (!X509_digest(cert, hash_alg, hash, &hash_len))
  141. goto err;
  142. if (!ASN1_OCTET_STRING_set(cid->hash, hash, hash_len))
  143. goto err;
  144. if (!issuer_needed)
  145. return cid;
  146. if ((cid->issuer_serial = ESS_ISSUER_SERIAL_new()) == NULL)
  147. goto err;
  148. if ((name = GENERAL_NAME_new()) == NULL)
  149. goto err;
  150. name->type = GEN_DIRNAME;
  151. if ((name->d.dirn = X509_NAME_dup(X509_get_issuer_name(cert))) == NULL)
  152. goto err;
  153. if (!sk_GENERAL_NAME_push(cid->issuer_serial->issuer, name))
  154. goto err;
  155. name = NULL; /* Ownership is lost. */
  156. ASN1_INTEGER_free(cid->issuer_serial->serial);
  157. cid->issuer_serial->serial = ASN1_INTEGER_dup(X509_get0_serialNumber(cert));
  158. if (cid->issuer_serial->serial == NULL)
  159. goto err;
  160. return cid;
  161. err:
  162. X509_ALGOR_free(alg);
  163. GENERAL_NAME_free(name);
  164. ESS_CERT_ID_V2_free(cid);
  165. ERR_raise(ERR_LIB_ESS, ERR_R_MALLOC_FAILURE);
  166. return NULL;
  167. }
  168. ESS_SIGNING_CERT *ossl_ess_get_signing_cert(const PKCS7_SIGNER_INFO *si)
  169. {
  170. ASN1_TYPE *attr;
  171. const unsigned char *p;
  172. attr = PKCS7_get_signed_attribute(si, NID_id_smime_aa_signingCertificate);
  173. if (attr == NULL)
  174. return NULL;
  175. p = attr->value.sequence->data;
  176. return d2i_ESS_SIGNING_CERT(NULL, &p, attr->value.sequence->length);
  177. }
  178. ESS_SIGNING_CERT_V2 *ossl_ess_get_signing_cert_v2(const PKCS7_SIGNER_INFO *si)
  179. {
  180. ASN1_TYPE *attr;
  181. const unsigned char *p;
  182. attr = PKCS7_get_signed_attribute(si, NID_id_smime_aa_signingCertificateV2);
  183. if (attr == NULL)
  184. return NULL;
  185. p = attr->value.sequence->data;
  186. return d2i_ESS_SIGNING_CERT_V2(NULL, &p, attr->value.sequence->length);
  187. }
  188. int ossl_ess_signing_cert_add(PKCS7_SIGNER_INFO *si, ESS_SIGNING_CERT *sc)
  189. {
  190. ASN1_STRING *seq = NULL;
  191. unsigned char *p, *pp = NULL;
  192. int len;
  193. len = i2d_ESS_SIGNING_CERT(sc, NULL);
  194. if ((pp = OPENSSL_malloc(len)) == NULL) {
  195. ERR_raise(ERR_LIB_ESS, ERR_R_MALLOC_FAILURE);
  196. goto err;
  197. }
  198. p = pp;
  199. i2d_ESS_SIGNING_CERT(sc, &p);
  200. if ((seq = ASN1_STRING_new()) == NULL || !ASN1_STRING_set(seq, pp, len)) {
  201. ERR_raise(ERR_LIB_ESS, ERR_R_MALLOC_FAILURE);
  202. goto err;
  203. }
  204. OPENSSL_free(pp);
  205. pp = NULL;
  206. return PKCS7_add_signed_attribute(si,
  207. NID_id_smime_aa_signingCertificate,
  208. V_ASN1_SEQUENCE, seq);
  209. err:
  210. ASN1_STRING_free(seq);
  211. OPENSSL_free(pp);
  212. return 0;
  213. }
  214. int ossl_ess_signing_cert_v2_add(PKCS7_SIGNER_INFO *si, ESS_SIGNING_CERT_V2 *sc)
  215. {
  216. ASN1_STRING *seq = NULL;
  217. unsigned char *p, *pp = NULL;
  218. int len = i2d_ESS_SIGNING_CERT_V2(sc, NULL);
  219. if ((pp = OPENSSL_malloc(len)) == NULL) {
  220. ERR_raise(ERR_LIB_ESS, ERR_R_MALLOC_FAILURE);
  221. goto err;
  222. }
  223. p = pp;
  224. i2d_ESS_SIGNING_CERT_V2(sc, &p);
  225. if ((seq = ASN1_STRING_new()) == NULL || !ASN1_STRING_set(seq, pp, len)) {
  226. ERR_raise(ERR_LIB_ESS, ERR_R_MALLOC_FAILURE);
  227. goto err;
  228. }
  229. OPENSSL_free(pp);
  230. pp = NULL;
  231. return PKCS7_add_signed_attribute(si,
  232. NID_id_smime_aa_signingCertificateV2,
  233. V_ASN1_SEQUENCE, seq);
  234. err:
  235. ASN1_STRING_free(seq);
  236. OPENSSL_free(pp);
  237. return 0;
  238. }
  239. static int ess_issuer_serial_cmp(const ESS_ISSUER_SERIAL *is, const X509 *cert)
  240. {
  241. GENERAL_NAME *issuer;
  242. if (is == NULL || cert == NULL || sk_GENERAL_NAME_num(is->issuer) != 1)
  243. return -1;
  244. issuer = sk_GENERAL_NAME_value(is->issuer, 0);
  245. if (issuer->type != GEN_DIRNAME
  246. || X509_NAME_cmp(issuer->d.dirn, X509_get_issuer_name(cert)) != 0)
  247. return -1;
  248. return ASN1_INTEGER_cmp(is->serial, X509_get0_serialNumber(cert));
  249. }
  250. /*
  251. * Find cert referenced by |cid| (if not NULL, else |cidv2|) in |certs|.
  252. * If the cid{,v2} index is 0, the cert must be in the first in |certs| list.
  253. * Return 0 on not found, -1 on error, else 1 + the position in |certs|.
  254. */
  255. static int find(const ESS_CERT_ID *cid, const ESS_CERT_ID_V2 *cid_v2,
  256. int index, const STACK_OF(X509) *certs)
  257. {
  258. const X509 *cert;
  259. const EVP_MD *md;
  260. unsigned char cert_digest[EVP_MAX_MD_SIZE];
  261. unsigned int len, cid_hash_len;
  262. const ESS_ISSUER_SERIAL *is;
  263. int i;
  264. if (cid == NULL && cid_v2 == NULL) {
  265. ERR_raise(ERR_LIB_ESS, ERR_R_PASSED_INVALID_ARGUMENT);
  266. return -1;
  267. }
  268. /* Look for cert with cid in the certs. */
  269. for (i = 0; i < sk_X509_num(certs); ++i) {
  270. cert = sk_X509_value(certs, i);
  271. if (cid != NULL)
  272. md = EVP_sha1();
  273. else
  274. md = cid_v2->hash_alg == NULL ? EVP_sha256() :
  275. EVP_get_digestbyobj(cid_v2->hash_alg->algorithm);
  276. if (md == NULL) {
  277. ERR_raise(ERR_LIB_ESS, ESS_R_ESS_DIGEST_ALG_UNKNOWN);
  278. return -1;
  279. }
  280. cid_hash_len = cid != NULL ? cid->hash->length : cid_v2->hash->length;
  281. if (!X509_digest(cert, md, cert_digest, &len)
  282. || cid_hash_len != len) {
  283. ERR_raise(ERR_LIB_ESS, ESS_R_ESS_CERT_DIGEST_ERROR);
  284. return -1;
  285. }
  286. if (memcmp(cid != NULL ? cid->hash->data : cid_v2->hash->data,
  287. cert_digest, len) == 0) {
  288. is = cid != NULL ? cid->issuer_serial : cid_v2->issuer_serial;
  289. /* Well, it's not really required to match the serial numbers. */
  290. if (is == NULL || ess_issuer_serial_cmp(is, cert) == 0) {
  291. if ((i == 0) == (index == 0))
  292. return i + 1;
  293. ERR_raise(ERR_LIB_ESS, ESS_R_ESS_CERT_ID_WRONG_ORDER);
  294. return -1;
  295. }
  296. }
  297. }
  298. ERR_raise(ERR_LIB_ESS, ESS_R_ESS_CERT_ID_NOT_FOUND);
  299. return 0;
  300. }
  301. /*
  302. * If ESSCertID and/or ESSCertIDv2 exist, which must be non-empty if given,
  303. * check if their first ID entry matches the signer cert first in chain
  304. * and each further ID entry matches any further cert in the chain.
  305. */
  306. int ossl_ess_check_signing_certs(const ESS_SIGNING_CERT *ss,
  307. const ESS_SIGNING_CERT_V2 *ssv2,
  308. const STACK_OF(X509) *chain,
  309. int require_signing_cert)
  310. {
  311. int n_v1 = ss == NULL ? -1 : sk_ESS_CERT_ID_num(ss->cert_ids);
  312. int n_v2 = ssv2 == NULL ? -1 : sk_ESS_CERT_ID_V2_num(ssv2->cert_ids);
  313. int i;
  314. if (require_signing_cert && ss == NULL && ssv2 == NULL) {
  315. ERR_raise(ERR_LIB_CMS, ESS_R_MISSING_SIGNING_CERTIFICATE_ATTRIBUTE);
  316. return 0;
  317. }
  318. if (n_v1 == 0 || n_v2 == 0) {
  319. ERR_raise(ERR_LIB_ESS, ESS_R_EMPTY_ESS_CERT_ID_LIST);
  320. return 0;
  321. }
  322. /* If both ss and ssv2 exist, as required evaluate them independently. */
  323. for (i = 0; i < n_v1; i++)
  324. if (find(sk_ESS_CERT_ID_value(ss->cert_ids, i), NULL, i, chain) <= 0)
  325. return 0;
  326. for (i = 0; i < n_v2; i++)
  327. if (find(NULL, sk_ESS_CERT_ID_V2_value(ssv2->cert_ids, i), i, chain) <= 0)
  328. return 0;
  329. return 1;
  330. }