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