ess_lib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Copyright 2019-2020 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 *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. ESSerr(ESS_F_ESS_SIGNING_CERT_NEW_INIT, 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 (!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. ESSerr(ESS_F_ESS_CERT_ID_NEW_INIT, ERR_R_MALLOC_FAILURE);
  84. return NULL;
  85. }
  86. ESS_SIGNING_CERT_V2 *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. ESSerr(ESS_F_ESS_SIGNING_CERT_V2_NEW_INIT, 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 (hash_alg != EVP_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. ESSerr(ESS_F_ESS_CERT_ID_V2_NEW_INIT, ERR_R_MALLOC_FAILURE);
  166. return NULL;
  167. }
  168. ESS_SIGNING_CERT *ESS_SIGNING_CERT_get(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 *ESS_SIGNING_CERT_V2_get(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 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. ESSerr(ESS_F_ESS_SIGNING_CERT_ADD, 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. ESSerr(ESS_F_ESS_SIGNING_CERT_ADD, 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 ESS_SIGNING_CERT_V2_add(PKCS7_SIGNER_INFO *si,
  215. ESS_SIGNING_CERT_V2 *sc)
  216. {
  217. ASN1_STRING *seq = NULL;
  218. unsigned char *p, *pp = NULL;
  219. int len = i2d_ESS_SIGNING_CERT_V2(sc, NULL);
  220. if ((pp = OPENSSL_malloc(len)) == NULL) {
  221. ESSerr(ESS_F_ESS_SIGNING_CERT_V2_ADD, ERR_R_MALLOC_FAILURE);
  222. goto err;
  223. }
  224. p = pp;
  225. i2d_ESS_SIGNING_CERT_V2(sc, &p);
  226. if ((seq = ASN1_STRING_new()) == NULL || !ASN1_STRING_set(seq, pp, len)) {
  227. ESSerr(ESS_F_ESS_SIGNING_CERT_V2_ADD, ERR_R_MALLOC_FAILURE);
  228. goto err;
  229. }
  230. OPENSSL_free(pp);
  231. pp = NULL;
  232. return PKCS7_add_signed_attribute(si,
  233. NID_id_smime_aa_signingCertificateV2,
  234. V_ASN1_SEQUENCE, seq);
  235. err:
  236. ASN1_STRING_free(seq);
  237. OPENSSL_free(pp);
  238. return 0;
  239. }
  240. static int ess_issuer_serial_cmp(const ESS_ISSUER_SERIAL *is, const X509 *cert)
  241. {
  242. GENERAL_NAME *issuer;
  243. if (is == NULL || cert == NULL || sk_GENERAL_NAME_num(is->issuer) != 1)
  244. return -1;
  245. issuer = sk_GENERAL_NAME_value(is->issuer, 0);
  246. if (issuer->type != GEN_DIRNAME
  247. || X509_NAME_cmp(issuer->d.dirn, X509_get_issuer_name(cert)) != 0)
  248. return -1;
  249. return ASN1_INTEGER_cmp(is->serial, X509_get0_serialNumber(cert));
  250. }
  251. /* Returns < 0 if certificate is not found, certificate index otherwise. */
  252. int ess_find_cert(const STACK_OF(ESS_CERT_ID) *cert_ids, X509 *cert)
  253. {
  254. int i;
  255. unsigned char cert_sha1[SHA_DIGEST_LENGTH];
  256. if (cert_ids == NULL || cert == NULL)
  257. return -1;
  258. /* Recompute SHA1 hash of certificate if necessary (side effect). */
  259. if (!x509v3_cache_extensions(cert))
  260. return -1;
  261. /* TODO(3.0): fetch sha1 algorithm from providers */
  262. if (!X509_digest(cert, EVP_sha1(), cert_sha1, NULL))
  263. return -1;
  264. /* Look for cert in the cert_ids vector. */
  265. for (i = 0; i < sk_ESS_CERT_ID_num(cert_ids); ++i) {
  266. const ESS_CERT_ID *cid = sk_ESS_CERT_ID_value(cert_ids, i);
  267. if (cid->hash->length == SHA_DIGEST_LENGTH
  268. && memcmp(cid->hash->data, cert_sha1, SHA_DIGEST_LENGTH) == 0) {
  269. const ESS_ISSUER_SERIAL *is = cid->issuer_serial;
  270. if (is == NULL || ess_issuer_serial_cmp(is, cert) == 0)
  271. return i;
  272. }
  273. }
  274. return -1;
  275. }
  276. /* Returns < 0 if certificate is not found, certificate index otherwise. */
  277. int ess_find_cert_v2(const STACK_OF(ESS_CERT_ID_V2) *cert_ids, const X509 *cert)
  278. {
  279. int i;
  280. unsigned char cert_digest[EVP_MAX_MD_SIZE];
  281. unsigned int len;
  282. /* Look for cert in the cert_ids vector. */
  283. for (i = 0; i < sk_ESS_CERT_ID_V2_num(cert_ids); ++i) {
  284. const ESS_CERT_ID_V2 *cid = sk_ESS_CERT_ID_V2_value(cert_ids, i);
  285. const EVP_MD *md;
  286. if (cid == NULL)
  287. return -1;
  288. if (cid->hash_alg != NULL)
  289. md = EVP_get_digestbyobj(cid->hash_alg->algorithm);
  290. else
  291. md = EVP_sha256();
  292. /* TODO(3.0): fetch sha1 algorithm from providers */
  293. if (!X509_digest(cert, md, cert_digest, &len))
  294. return -1;
  295. if (cid->hash->length != (int)len)
  296. return -1;
  297. if (memcmp(cid->hash->data, cert_digest, cid->hash->length) == 0) {
  298. const ESS_ISSUER_SERIAL *is = cid->issuer_serial;
  299. if (is == NULL || ess_issuer_serial_cmp(is, cert) == 0)
  300. return i;
  301. }
  302. }
  303. return -1;
  304. }