cms_lib.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. * Copyright 2008-2016 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 <openssl/asn1t.h>
  10. #include <openssl/x509v3.h>
  11. #include <openssl/err.h>
  12. #include <openssl/pem.h>
  13. #include <openssl/bio.h>
  14. #include <openssl/asn1.h>
  15. #include <openssl/cms.h>
  16. #include "cms_lcl.h"
  17. IMPLEMENT_ASN1_FUNCTIONS(CMS_ContentInfo)
  18. IMPLEMENT_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
  19. const ASN1_OBJECT *CMS_get0_type(const CMS_ContentInfo *cms)
  20. {
  21. return cms->contentType;
  22. }
  23. CMS_ContentInfo *cms_Data_create(void)
  24. {
  25. CMS_ContentInfo *cms;
  26. cms = CMS_ContentInfo_new();
  27. if (cms != NULL) {
  28. cms->contentType = OBJ_nid2obj(NID_pkcs7_data);
  29. /* Never detached */
  30. CMS_set_detached(cms, 0);
  31. }
  32. return cms;
  33. }
  34. BIO *cms_content_bio(CMS_ContentInfo *cms)
  35. {
  36. ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
  37. if (!pos)
  38. return NULL;
  39. /* If content detached data goes nowhere: create NULL BIO */
  40. if (!*pos)
  41. return BIO_new(BIO_s_null());
  42. /*
  43. * If content not detached and created return memory BIO
  44. */
  45. if (!*pos || ((*pos)->flags == ASN1_STRING_FLAG_CONT))
  46. return BIO_new(BIO_s_mem());
  47. /* Else content was read in: return read only BIO for it */
  48. return BIO_new_mem_buf((*pos)->data, (*pos)->length);
  49. }
  50. BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
  51. {
  52. BIO *cmsbio, *cont;
  53. if (icont)
  54. cont = icont;
  55. else
  56. cont = cms_content_bio(cms);
  57. if (!cont) {
  58. CMSerr(CMS_F_CMS_DATAINIT, CMS_R_NO_CONTENT);
  59. return NULL;
  60. }
  61. switch (OBJ_obj2nid(cms->contentType)) {
  62. case NID_pkcs7_data:
  63. return cont;
  64. case NID_pkcs7_signed:
  65. cmsbio = cms_SignedData_init_bio(cms);
  66. break;
  67. case NID_pkcs7_digest:
  68. cmsbio = cms_DigestedData_init_bio(cms);
  69. break;
  70. #ifdef ZLIB
  71. case NID_id_smime_ct_compressedData:
  72. cmsbio = cms_CompressedData_init_bio(cms);
  73. break;
  74. #endif
  75. case NID_pkcs7_encrypted:
  76. cmsbio = cms_EncryptedData_init_bio(cms);
  77. break;
  78. case NID_pkcs7_enveloped:
  79. cmsbio = cms_EnvelopedData_init_bio(cms);
  80. break;
  81. default:
  82. CMSerr(CMS_F_CMS_DATAINIT, CMS_R_UNSUPPORTED_TYPE);
  83. return NULL;
  84. }
  85. if (cmsbio)
  86. return BIO_push(cmsbio, cont);
  87. if (!icont)
  88. BIO_free(cont);
  89. return NULL;
  90. }
  91. int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
  92. {
  93. ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
  94. if (!pos)
  95. return 0;
  96. /* If embedded content find memory BIO and set content */
  97. if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT)) {
  98. BIO *mbio;
  99. unsigned char *cont;
  100. long contlen;
  101. mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
  102. if (!mbio) {
  103. CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_CONTENT_NOT_FOUND);
  104. return 0;
  105. }
  106. contlen = BIO_get_mem_data(mbio, &cont);
  107. /* Set bio as read only so its content can't be clobbered */
  108. BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
  109. BIO_set_mem_eof_return(mbio, 0);
  110. ASN1_STRING_set0(*pos, cont, contlen);
  111. (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
  112. }
  113. switch (OBJ_obj2nid(cms->contentType)) {
  114. case NID_pkcs7_data:
  115. case NID_pkcs7_enveloped:
  116. case NID_pkcs7_encrypted:
  117. case NID_id_smime_ct_compressedData:
  118. /* Nothing to do */
  119. return 1;
  120. case NID_pkcs7_signed:
  121. return cms_SignedData_final(cms, cmsbio);
  122. case NID_pkcs7_digest:
  123. return cms_DigestedData_do_final(cms, cmsbio, 0);
  124. default:
  125. CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_UNSUPPORTED_TYPE);
  126. return 0;
  127. }
  128. }
  129. /*
  130. * Return an OCTET STRING pointer to content. This allows it to be accessed
  131. * or set later.
  132. */
  133. ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
  134. {
  135. switch (OBJ_obj2nid(cms->contentType)) {
  136. case NID_pkcs7_data:
  137. return &cms->d.data;
  138. case NID_pkcs7_signed:
  139. return &cms->d.signedData->encapContentInfo->eContent;
  140. case NID_pkcs7_enveloped:
  141. return &cms->d.envelopedData->encryptedContentInfo->encryptedContent;
  142. case NID_pkcs7_digest:
  143. return &cms->d.digestedData->encapContentInfo->eContent;
  144. case NID_pkcs7_encrypted:
  145. return &cms->d.encryptedData->encryptedContentInfo->encryptedContent;
  146. case NID_id_smime_ct_authData:
  147. return &cms->d.authenticatedData->encapContentInfo->eContent;
  148. case NID_id_smime_ct_compressedData:
  149. return &cms->d.compressedData->encapContentInfo->eContent;
  150. default:
  151. if (cms->d.other->type == V_ASN1_OCTET_STRING)
  152. return &cms->d.other->value.octet_string;
  153. CMSerr(CMS_F_CMS_GET0_CONTENT, CMS_R_UNSUPPORTED_CONTENT_TYPE);
  154. return NULL;
  155. }
  156. }
  157. /*
  158. * Return an ASN1_OBJECT pointer to content type. This allows it to be
  159. * accessed or set later.
  160. */
  161. static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms)
  162. {
  163. switch (OBJ_obj2nid(cms->contentType)) {
  164. case NID_pkcs7_signed:
  165. return &cms->d.signedData->encapContentInfo->eContentType;
  166. case NID_pkcs7_enveloped:
  167. return &cms->d.envelopedData->encryptedContentInfo->contentType;
  168. case NID_pkcs7_digest:
  169. return &cms->d.digestedData->encapContentInfo->eContentType;
  170. case NID_pkcs7_encrypted:
  171. return &cms->d.encryptedData->encryptedContentInfo->contentType;
  172. case NID_id_smime_ct_authData:
  173. return &cms->d.authenticatedData->encapContentInfo->eContentType;
  174. case NID_id_smime_ct_compressedData:
  175. return &cms->d.compressedData->encapContentInfo->eContentType;
  176. default:
  177. CMSerr(CMS_F_CMS_GET0_ECONTENT_TYPE, CMS_R_UNSUPPORTED_CONTENT_TYPE);
  178. return NULL;
  179. }
  180. }
  181. const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms)
  182. {
  183. ASN1_OBJECT **petype;
  184. petype = cms_get0_econtent_type(cms);
  185. if (petype)
  186. return *petype;
  187. return NULL;
  188. }
  189. int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid)
  190. {
  191. ASN1_OBJECT **petype, *etype;
  192. petype = cms_get0_econtent_type(cms);
  193. if (!petype)
  194. return 0;
  195. if (!oid)
  196. return 1;
  197. etype = OBJ_dup(oid);
  198. if (!etype)
  199. return 0;
  200. ASN1_OBJECT_free(*petype);
  201. *petype = etype;
  202. return 1;
  203. }
  204. int CMS_is_detached(CMS_ContentInfo *cms)
  205. {
  206. ASN1_OCTET_STRING **pos;
  207. pos = CMS_get0_content(cms);
  208. if (!pos)
  209. return -1;
  210. if (*pos)
  211. return 0;
  212. return 1;
  213. }
  214. int CMS_set_detached(CMS_ContentInfo *cms, int detached)
  215. {
  216. ASN1_OCTET_STRING **pos;
  217. pos = CMS_get0_content(cms);
  218. if (!pos)
  219. return 0;
  220. if (detached) {
  221. ASN1_OCTET_STRING_free(*pos);
  222. *pos = NULL;
  223. return 1;
  224. }
  225. if (*pos == NULL)
  226. *pos = ASN1_OCTET_STRING_new();
  227. if (*pos != NULL) {
  228. /*
  229. * NB: special flag to show content is created and not read in.
  230. */
  231. (*pos)->flags |= ASN1_STRING_FLAG_CONT;
  232. return 1;
  233. }
  234. CMSerr(CMS_F_CMS_SET_DETACHED, ERR_R_MALLOC_FAILURE);
  235. return 0;
  236. }
  237. /* Create a digest BIO from an X509_ALGOR structure */
  238. BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm)
  239. {
  240. BIO *mdbio = NULL;
  241. const ASN1_OBJECT *digestoid;
  242. const EVP_MD *digest;
  243. X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
  244. digest = EVP_get_digestbyobj(digestoid);
  245. if (!digest) {
  246. CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO,
  247. CMS_R_UNKNOWN_DIGEST_ALGORITHM);
  248. goto err;
  249. }
  250. mdbio = BIO_new(BIO_f_md());
  251. if (mdbio == NULL || !BIO_set_md(mdbio, digest)) {
  252. CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO, CMS_R_MD_BIO_INIT_ERROR);
  253. goto err;
  254. }
  255. return mdbio;
  256. err:
  257. BIO_free(mdbio);
  258. return NULL;
  259. }
  260. /* Locate a message digest content from a BIO chain based on SignerInfo */
  261. int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
  262. X509_ALGOR *mdalg)
  263. {
  264. int nid;
  265. const ASN1_OBJECT *mdoid;
  266. X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
  267. nid = OBJ_obj2nid(mdoid);
  268. /* Look for digest type to match signature */
  269. for (;;) {
  270. EVP_MD_CTX *mtmp;
  271. chain = BIO_find_type(chain, BIO_TYPE_MD);
  272. if (chain == NULL) {
  273. CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,
  274. CMS_R_NO_MATCHING_DIGEST);
  275. return 0;
  276. }
  277. BIO_get_md_ctx(chain, &mtmp);
  278. if (EVP_MD_CTX_type(mtmp) == nid
  279. /*
  280. * Workaround for broken implementations that use signature
  281. * algorithm OID instead of digest.
  282. */
  283. || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid)
  284. return EVP_MD_CTX_copy_ex(mctx, mtmp);
  285. chain = BIO_next(chain);
  286. }
  287. }
  288. static STACK_OF(CMS_CertificateChoices)
  289. **cms_get0_certificate_choices(CMS_ContentInfo *cms)
  290. {
  291. switch (OBJ_obj2nid(cms->contentType)) {
  292. case NID_pkcs7_signed:
  293. return &cms->d.signedData->certificates;
  294. case NID_pkcs7_enveloped:
  295. if (cms->d.envelopedData->originatorInfo == NULL)
  296. return NULL;
  297. return &cms->d.envelopedData->originatorInfo->certificates;
  298. default:
  299. CMSerr(CMS_F_CMS_GET0_CERTIFICATE_CHOICES,
  300. CMS_R_UNSUPPORTED_CONTENT_TYPE);
  301. return NULL;
  302. }
  303. }
  304. CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
  305. {
  306. STACK_OF(CMS_CertificateChoices) **pcerts;
  307. CMS_CertificateChoices *cch;
  308. pcerts = cms_get0_certificate_choices(cms);
  309. if (!pcerts)
  310. return NULL;
  311. if (!*pcerts)
  312. *pcerts = sk_CMS_CertificateChoices_new_null();
  313. if (!*pcerts)
  314. return NULL;
  315. cch = M_ASN1_new_of(CMS_CertificateChoices);
  316. if (!cch)
  317. return NULL;
  318. if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) {
  319. M_ASN1_free_of(cch, CMS_CertificateChoices);
  320. return NULL;
  321. }
  322. return cch;
  323. }
  324. int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
  325. {
  326. CMS_CertificateChoices *cch;
  327. STACK_OF(CMS_CertificateChoices) **pcerts;
  328. int i;
  329. pcerts = cms_get0_certificate_choices(cms);
  330. if (!pcerts)
  331. return 0;
  332. for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
  333. cch = sk_CMS_CertificateChoices_value(*pcerts, i);
  334. if (cch->type == CMS_CERTCHOICE_CERT) {
  335. if (!X509_cmp(cch->d.certificate, cert)) {
  336. CMSerr(CMS_F_CMS_ADD0_CERT,
  337. CMS_R_CERTIFICATE_ALREADY_PRESENT);
  338. return 0;
  339. }
  340. }
  341. }
  342. cch = CMS_add0_CertificateChoices(cms);
  343. if (!cch)
  344. return 0;
  345. cch->type = CMS_CERTCHOICE_CERT;
  346. cch->d.certificate = cert;
  347. return 1;
  348. }
  349. int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
  350. {
  351. int r;
  352. r = CMS_add0_cert(cms, cert);
  353. if (r > 0)
  354. X509_up_ref(cert);
  355. return r;
  356. }
  357. static STACK_OF(CMS_RevocationInfoChoice)
  358. **cms_get0_revocation_choices(CMS_ContentInfo *cms)
  359. {
  360. switch (OBJ_obj2nid(cms->contentType)) {
  361. case NID_pkcs7_signed:
  362. return &cms->d.signedData->crls;
  363. case NID_pkcs7_enveloped:
  364. if (cms->d.envelopedData->originatorInfo == NULL)
  365. return NULL;
  366. return &cms->d.envelopedData->originatorInfo->crls;
  367. default:
  368. CMSerr(CMS_F_CMS_GET0_REVOCATION_CHOICES,
  369. CMS_R_UNSUPPORTED_CONTENT_TYPE);
  370. return NULL;
  371. }
  372. }
  373. CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
  374. {
  375. STACK_OF(CMS_RevocationInfoChoice) **pcrls;
  376. CMS_RevocationInfoChoice *rch;
  377. pcrls = cms_get0_revocation_choices(cms);
  378. if (!pcrls)
  379. return NULL;
  380. if (!*pcrls)
  381. *pcrls = sk_CMS_RevocationInfoChoice_new_null();
  382. if (!*pcrls)
  383. return NULL;
  384. rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
  385. if (!rch)
  386. return NULL;
  387. if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) {
  388. M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
  389. return NULL;
  390. }
  391. return rch;
  392. }
  393. int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
  394. {
  395. CMS_RevocationInfoChoice *rch;
  396. rch = CMS_add0_RevocationInfoChoice(cms);
  397. if (!rch)
  398. return 0;
  399. rch->type = CMS_REVCHOICE_CRL;
  400. rch->d.crl = crl;
  401. return 1;
  402. }
  403. int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl)
  404. {
  405. int r;
  406. r = CMS_add0_crl(cms, crl);
  407. if (r > 0)
  408. X509_CRL_up_ref(crl);
  409. return r;
  410. }
  411. STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
  412. {
  413. STACK_OF(X509) *certs = NULL;
  414. CMS_CertificateChoices *cch;
  415. STACK_OF(CMS_CertificateChoices) **pcerts;
  416. int i;
  417. pcerts = cms_get0_certificate_choices(cms);
  418. if (!pcerts)
  419. return NULL;
  420. for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
  421. cch = sk_CMS_CertificateChoices_value(*pcerts, i);
  422. if (cch->type == 0) {
  423. if (!certs) {
  424. certs = sk_X509_new_null();
  425. if (!certs)
  426. return NULL;
  427. }
  428. if (!sk_X509_push(certs, cch->d.certificate)) {
  429. sk_X509_pop_free(certs, X509_free);
  430. return NULL;
  431. }
  432. X509_up_ref(cch->d.certificate);
  433. }
  434. }
  435. return certs;
  436. }
  437. STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
  438. {
  439. STACK_OF(X509_CRL) *crls = NULL;
  440. STACK_OF(CMS_RevocationInfoChoice) **pcrls;
  441. CMS_RevocationInfoChoice *rch;
  442. int i;
  443. pcrls = cms_get0_revocation_choices(cms);
  444. if (!pcrls)
  445. return NULL;
  446. for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++) {
  447. rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
  448. if (rch->type == 0) {
  449. if (!crls) {
  450. crls = sk_X509_CRL_new_null();
  451. if (!crls)
  452. return NULL;
  453. }
  454. if (!sk_X509_CRL_push(crls, rch->d.crl)) {
  455. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  456. return NULL;
  457. }
  458. X509_CRL_up_ref(rch->d.crl);
  459. }
  460. }
  461. return crls;
  462. }
  463. int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert)
  464. {
  465. int ret;
  466. ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert));
  467. if (ret)
  468. return ret;
  469. return ASN1_INTEGER_cmp(ias->serialNumber, X509_get_serialNumber(cert));
  470. }
  471. int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert)
  472. {
  473. const ASN1_OCTET_STRING *cert_keyid = X509_get0_subject_key_id(cert);
  474. if (cert_keyid == NULL)
  475. return -1;
  476. return ASN1_OCTET_STRING_cmp(keyid, cert_keyid);
  477. }
  478. int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert)
  479. {
  480. CMS_IssuerAndSerialNumber *ias;
  481. ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber);
  482. if (!ias)
  483. goto err;
  484. if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert)))
  485. goto err;
  486. if (!ASN1_STRING_copy(ias->serialNumber, X509_get_serialNumber(cert)))
  487. goto err;
  488. M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber);
  489. *pias = ias;
  490. return 1;
  491. err:
  492. M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber);
  493. CMSerr(CMS_F_CMS_SET1_IAS, ERR_R_MALLOC_FAILURE);
  494. return 0;
  495. }
  496. int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert)
  497. {
  498. ASN1_OCTET_STRING *keyid = NULL;
  499. const ASN1_OCTET_STRING *cert_keyid;
  500. cert_keyid = X509_get0_subject_key_id(cert);
  501. if (cert_keyid == NULL) {
  502. CMSerr(CMS_F_CMS_SET1_KEYID, CMS_R_CERTIFICATE_HAS_NO_KEYID);
  503. return 0;
  504. }
  505. keyid = ASN1_STRING_dup(cert_keyid);
  506. if (!keyid) {
  507. CMSerr(CMS_F_CMS_SET1_KEYID, ERR_R_MALLOC_FAILURE);
  508. return 0;
  509. }
  510. ASN1_OCTET_STRING_free(*pkeyid);
  511. *pkeyid = keyid;
  512. return 1;
  513. }