1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2008-2020 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci#include <openssl/asn1t.h> 11e1051a39Sopenharmony_ci#include <openssl/pem.h> 12e1051a39Sopenharmony_ci#include <openssl/x509v3.h> 13e1051a39Sopenharmony_ci#include <openssl/cms.h> 14e1051a39Sopenharmony_ci#include "cms_local.h" 15e1051a39Sopenharmony_ci 16e1051a39Sopenharmony_ci 17e1051a39Sopenharmony_ciASN1_SEQUENCE(CMS_IssuerAndSerialNumber) = { 18e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_IssuerAndSerialNumber, issuer, X509_NAME), 19e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_IssuerAndSerialNumber, serialNumber, ASN1_INTEGER) 20e1051a39Sopenharmony_ci} ASN1_SEQUENCE_END(CMS_IssuerAndSerialNumber) 21e1051a39Sopenharmony_ci 22e1051a39Sopenharmony_ciASN1_SEQUENCE(CMS_OtherCertificateFormat) = { 23e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_OtherCertificateFormat, otherCertFormat, ASN1_OBJECT), 24e1051a39Sopenharmony_ci ASN1_OPT(CMS_OtherCertificateFormat, otherCert, ASN1_ANY) 25e1051a39Sopenharmony_ci} static_ASN1_SEQUENCE_END(CMS_OtherCertificateFormat) 26e1051a39Sopenharmony_ci 27e1051a39Sopenharmony_ciASN1_CHOICE(CMS_CertificateChoices) = { 28e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_CertificateChoices, d.certificate, X509), 29e1051a39Sopenharmony_ci ASN1_IMP(CMS_CertificateChoices, d.extendedCertificate, ASN1_SEQUENCE, 0), 30e1051a39Sopenharmony_ci ASN1_IMP(CMS_CertificateChoices, d.v1AttrCert, ASN1_SEQUENCE, 1), 31e1051a39Sopenharmony_ci ASN1_IMP(CMS_CertificateChoices, d.v2AttrCert, ASN1_SEQUENCE, 2), 32e1051a39Sopenharmony_ci ASN1_IMP(CMS_CertificateChoices, d.other, CMS_OtherCertificateFormat, 3) 33e1051a39Sopenharmony_ci} ASN1_CHOICE_END(CMS_CertificateChoices) 34e1051a39Sopenharmony_ci 35e1051a39Sopenharmony_ciASN1_CHOICE(CMS_SignerIdentifier) = { 36e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_SignerIdentifier, d.issuerAndSerialNumber, CMS_IssuerAndSerialNumber), 37e1051a39Sopenharmony_ci ASN1_IMP(CMS_SignerIdentifier, d.subjectKeyIdentifier, ASN1_OCTET_STRING, 0) 38e1051a39Sopenharmony_ci} static_ASN1_CHOICE_END(CMS_SignerIdentifier) 39e1051a39Sopenharmony_ci 40e1051a39Sopenharmony_ciASN1_NDEF_SEQUENCE(CMS_EncapsulatedContentInfo) = { 41e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_EncapsulatedContentInfo, eContentType, ASN1_OBJECT), 42e1051a39Sopenharmony_ci ASN1_NDEF_EXP_OPT(CMS_EncapsulatedContentInfo, eContent, ASN1_OCTET_STRING_NDEF, 0) 43e1051a39Sopenharmony_ci} static_ASN1_NDEF_SEQUENCE_END(CMS_EncapsulatedContentInfo) 44e1051a39Sopenharmony_ci 45e1051a39Sopenharmony_ci/* Minor tweak to operation: free up signer key, cert */ 46e1051a39Sopenharmony_cistatic int cms_si_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, 47e1051a39Sopenharmony_ci void *exarg) 48e1051a39Sopenharmony_ci{ 49e1051a39Sopenharmony_ci if (operation == ASN1_OP_FREE_POST) { 50e1051a39Sopenharmony_ci CMS_SignerInfo *si = (CMS_SignerInfo *)*pval; 51e1051a39Sopenharmony_ci EVP_PKEY_free(si->pkey); 52e1051a39Sopenharmony_ci X509_free(si->signer); 53e1051a39Sopenharmony_ci EVP_MD_CTX_free(si->mctx); 54e1051a39Sopenharmony_ci EVP_PKEY_CTX_free(si->pctx); 55e1051a39Sopenharmony_ci } 56e1051a39Sopenharmony_ci return 1; 57e1051a39Sopenharmony_ci} 58e1051a39Sopenharmony_ci 59e1051a39Sopenharmony_ciASN1_SEQUENCE_cb(CMS_SignerInfo, cms_si_cb) = { 60e1051a39Sopenharmony_ci ASN1_EMBED(CMS_SignerInfo, version, INT32), 61e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_SignerInfo, sid, CMS_SignerIdentifier), 62e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_SignerInfo, digestAlgorithm, X509_ALGOR), 63e1051a39Sopenharmony_ci ASN1_IMP_SET_OF_OPT(CMS_SignerInfo, signedAttrs, X509_ATTRIBUTE, 0), 64e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_SignerInfo, signatureAlgorithm, X509_ALGOR), 65e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_SignerInfo, signature, ASN1_OCTET_STRING), 66e1051a39Sopenharmony_ci ASN1_IMP_SET_OF_OPT(CMS_SignerInfo, unsignedAttrs, X509_ATTRIBUTE, 1) 67e1051a39Sopenharmony_ci} ASN1_SEQUENCE_END_cb(CMS_SignerInfo, CMS_SignerInfo) 68e1051a39Sopenharmony_ci 69e1051a39Sopenharmony_ciASN1_SEQUENCE(CMS_OtherRevocationInfoFormat) = { 70e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_OtherRevocationInfoFormat, otherRevInfoFormat, ASN1_OBJECT), 71e1051a39Sopenharmony_ci ASN1_OPT(CMS_OtherRevocationInfoFormat, otherRevInfo, ASN1_ANY) 72e1051a39Sopenharmony_ci} static_ASN1_SEQUENCE_END(CMS_OtherRevocationInfoFormat) 73e1051a39Sopenharmony_ci 74e1051a39Sopenharmony_ciASN1_CHOICE(CMS_RevocationInfoChoice) = { 75e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_RevocationInfoChoice, d.crl, X509_CRL), 76e1051a39Sopenharmony_ci ASN1_IMP(CMS_RevocationInfoChoice, d.other, CMS_OtherRevocationInfoFormat, 1) 77e1051a39Sopenharmony_ci} ASN1_CHOICE_END(CMS_RevocationInfoChoice) 78e1051a39Sopenharmony_ci 79e1051a39Sopenharmony_ciASN1_NDEF_SEQUENCE(CMS_SignedData) = { 80e1051a39Sopenharmony_ci ASN1_EMBED(CMS_SignedData, version, INT32), 81e1051a39Sopenharmony_ci ASN1_SET_OF(CMS_SignedData, digestAlgorithms, X509_ALGOR), 82e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_SignedData, encapContentInfo, CMS_EncapsulatedContentInfo), 83e1051a39Sopenharmony_ci ASN1_IMP_SET_OF_OPT(CMS_SignedData, certificates, CMS_CertificateChoices, 0), 84e1051a39Sopenharmony_ci ASN1_IMP_SET_OF_OPT(CMS_SignedData, crls, CMS_RevocationInfoChoice, 1), 85e1051a39Sopenharmony_ci ASN1_SET_OF(CMS_SignedData, signerInfos, CMS_SignerInfo) 86e1051a39Sopenharmony_ci} ASN1_NDEF_SEQUENCE_END(CMS_SignedData) 87e1051a39Sopenharmony_ci 88e1051a39Sopenharmony_ciASN1_SEQUENCE(CMS_OriginatorInfo) = { 89e1051a39Sopenharmony_ci ASN1_IMP_SET_OF_OPT(CMS_OriginatorInfo, certificates, CMS_CertificateChoices, 0), 90e1051a39Sopenharmony_ci ASN1_IMP_SET_OF_OPT(CMS_OriginatorInfo, crls, CMS_RevocationInfoChoice, 1) 91e1051a39Sopenharmony_ci} static_ASN1_SEQUENCE_END(CMS_OriginatorInfo) 92e1051a39Sopenharmony_ci 93e1051a39Sopenharmony_cistatic int cms_ec_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, 94e1051a39Sopenharmony_ci void *exarg) 95e1051a39Sopenharmony_ci{ 96e1051a39Sopenharmony_ci CMS_EncryptedContentInfo *ec = (CMS_EncryptedContentInfo *)*pval; 97e1051a39Sopenharmony_ci 98e1051a39Sopenharmony_ci if (operation == ASN1_OP_FREE_POST) 99e1051a39Sopenharmony_ci OPENSSL_clear_free(ec->key, ec->keylen); 100e1051a39Sopenharmony_ci return 1; 101e1051a39Sopenharmony_ci} 102e1051a39Sopenharmony_ci 103e1051a39Sopenharmony_ciASN1_NDEF_SEQUENCE_cb(CMS_EncryptedContentInfo, cms_ec_cb) = { 104e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_EncryptedContentInfo, contentType, ASN1_OBJECT), 105e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_EncryptedContentInfo, contentEncryptionAlgorithm, X509_ALGOR), 106e1051a39Sopenharmony_ci ASN1_IMP_OPT(CMS_EncryptedContentInfo, encryptedContent, ASN1_OCTET_STRING_NDEF, 0) 107e1051a39Sopenharmony_ci} ASN1_NDEF_SEQUENCE_END_cb(CMS_EncryptedContentInfo, CMS_EncryptedContentInfo) 108e1051a39Sopenharmony_ci 109e1051a39Sopenharmony_ciASN1_SEQUENCE(CMS_KeyTransRecipientInfo) = { 110e1051a39Sopenharmony_ci ASN1_EMBED(CMS_KeyTransRecipientInfo, version, INT32), 111e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_KeyTransRecipientInfo, rid, CMS_SignerIdentifier), 112e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_KeyTransRecipientInfo, keyEncryptionAlgorithm, X509_ALGOR), 113e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_KeyTransRecipientInfo, encryptedKey, ASN1_OCTET_STRING) 114e1051a39Sopenharmony_ci} ASN1_SEQUENCE_END(CMS_KeyTransRecipientInfo) 115e1051a39Sopenharmony_ci 116e1051a39Sopenharmony_ciASN1_SEQUENCE(CMS_OtherKeyAttribute) = { 117e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_OtherKeyAttribute, keyAttrId, ASN1_OBJECT), 118e1051a39Sopenharmony_ci ASN1_OPT(CMS_OtherKeyAttribute, keyAttr, ASN1_ANY) 119e1051a39Sopenharmony_ci} ASN1_SEQUENCE_END(CMS_OtherKeyAttribute) 120e1051a39Sopenharmony_ci 121e1051a39Sopenharmony_ciASN1_SEQUENCE(CMS_RecipientKeyIdentifier) = { 122e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_RecipientKeyIdentifier, subjectKeyIdentifier, ASN1_OCTET_STRING), 123e1051a39Sopenharmony_ci ASN1_OPT(CMS_RecipientKeyIdentifier, date, ASN1_GENERALIZEDTIME), 124e1051a39Sopenharmony_ci ASN1_OPT(CMS_RecipientKeyIdentifier, other, CMS_OtherKeyAttribute) 125e1051a39Sopenharmony_ci} ASN1_SEQUENCE_END(CMS_RecipientKeyIdentifier) 126e1051a39Sopenharmony_ci 127e1051a39Sopenharmony_ciASN1_CHOICE(CMS_KeyAgreeRecipientIdentifier) = { 128e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_KeyAgreeRecipientIdentifier, d.issuerAndSerialNumber, CMS_IssuerAndSerialNumber), 129e1051a39Sopenharmony_ci ASN1_IMP(CMS_KeyAgreeRecipientIdentifier, d.rKeyId, CMS_RecipientKeyIdentifier, 0) 130e1051a39Sopenharmony_ci} static_ASN1_CHOICE_END(CMS_KeyAgreeRecipientIdentifier) 131e1051a39Sopenharmony_ci 132e1051a39Sopenharmony_cistatic int cms_rek_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, 133e1051a39Sopenharmony_ci void *exarg) 134e1051a39Sopenharmony_ci{ 135e1051a39Sopenharmony_ci CMS_RecipientEncryptedKey *rek = (CMS_RecipientEncryptedKey *)*pval; 136e1051a39Sopenharmony_ci if (operation == ASN1_OP_FREE_POST) { 137e1051a39Sopenharmony_ci EVP_PKEY_free(rek->pkey); 138e1051a39Sopenharmony_ci } 139e1051a39Sopenharmony_ci return 1; 140e1051a39Sopenharmony_ci} 141e1051a39Sopenharmony_ci 142e1051a39Sopenharmony_ciASN1_SEQUENCE_cb(CMS_RecipientEncryptedKey, cms_rek_cb) = { 143e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_RecipientEncryptedKey, rid, CMS_KeyAgreeRecipientIdentifier), 144e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_RecipientEncryptedKey, encryptedKey, ASN1_OCTET_STRING) 145e1051a39Sopenharmony_ci} ASN1_SEQUENCE_END_cb(CMS_RecipientEncryptedKey, CMS_RecipientEncryptedKey) 146e1051a39Sopenharmony_ci 147e1051a39Sopenharmony_ciASN1_SEQUENCE(CMS_OriginatorPublicKey) = { 148e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_OriginatorPublicKey, algorithm, X509_ALGOR), 149e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_OriginatorPublicKey, publicKey, ASN1_BIT_STRING) 150e1051a39Sopenharmony_ci} ASN1_SEQUENCE_END(CMS_OriginatorPublicKey) 151e1051a39Sopenharmony_ci 152e1051a39Sopenharmony_ciASN1_CHOICE(CMS_OriginatorIdentifierOrKey) = { 153e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_OriginatorIdentifierOrKey, d.issuerAndSerialNumber, CMS_IssuerAndSerialNumber), 154e1051a39Sopenharmony_ci ASN1_IMP(CMS_OriginatorIdentifierOrKey, d.subjectKeyIdentifier, ASN1_OCTET_STRING, 0), 155e1051a39Sopenharmony_ci ASN1_IMP(CMS_OriginatorIdentifierOrKey, d.originatorKey, CMS_OriginatorPublicKey, 1) 156e1051a39Sopenharmony_ci} static_ASN1_CHOICE_END(CMS_OriginatorIdentifierOrKey) 157e1051a39Sopenharmony_ci 158e1051a39Sopenharmony_cistatic int cms_kari_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, 159e1051a39Sopenharmony_ci void *exarg) 160e1051a39Sopenharmony_ci{ 161e1051a39Sopenharmony_ci CMS_KeyAgreeRecipientInfo *kari = (CMS_KeyAgreeRecipientInfo *)*pval; 162e1051a39Sopenharmony_ci if (operation == ASN1_OP_NEW_POST) { 163e1051a39Sopenharmony_ci kari->ctx = EVP_CIPHER_CTX_new(); 164e1051a39Sopenharmony_ci if (kari->ctx == NULL) 165e1051a39Sopenharmony_ci return 0; 166e1051a39Sopenharmony_ci EVP_CIPHER_CTX_set_flags(kari->ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW); 167e1051a39Sopenharmony_ci kari->pctx = NULL; 168e1051a39Sopenharmony_ci } else if (operation == ASN1_OP_FREE_POST) { 169e1051a39Sopenharmony_ci EVP_PKEY_CTX_free(kari->pctx); 170e1051a39Sopenharmony_ci EVP_CIPHER_CTX_free(kari->ctx); 171e1051a39Sopenharmony_ci } 172e1051a39Sopenharmony_ci return 1; 173e1051a39Sopenharmony_ci} 174e1051a39Sopenharmony_ci 175e1051a39Sopenharmony_ciASN1_SEQUENCE_cb(CMS_KeyAgreeRecipientInfo, cms_kari_cb) = { 176e1051a39Sopenharmony_ci ASN1_EMBED(CMS_KeyAgreeRecipientInfo, version, INT32), 177e1051a39Sopenharmony_ci ASN1_EXP(CMS_KeyAgreeRecipientInfo, originator, CMS_OriginatorIdentifierOrKey, 0), 178e1051a39Sopenharmony_ci ASN1_EXP_OPT(CMS_KeyAgreeRecipientInfo, ukm, ASN1_OCTET_STRING, 1), 179e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_KeyAgreeRecipientInfo, keyEncryptionAlgorithm, X509_ALGOR), 180e1051a39Sopenharmony_ci ASN1_SEQUENCE_OF(CMS_KeyAgreeRecipientInfo, recipientEncryptedKeys, CMS_RecipientEncryptedKey) 181e1051a39Sopenharmony_ci} ASN1_SEQUENCE_END_cb(CMS_KeyAgreeRecipientInfo, CMS_KeyAgreeRecipientInfo) 182e1051a39Sopenharmony_ci 183e1051a39Sopenharmony_ciASN1_SEQUENCE(CMS_KEKIdentifier) = { 184e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_KEKIdentifier, keyIdentifier, ASN1_OCTET_STRING), 185e1051a39Sopenharmony_ci ASN1_OPT(CMS_KEKIdentifier, date, ASN1_GENERALIZEDTIME), 186e1051a39Sopenharmony_ci ASN1_OPT(CMS_KEKIdentifier, other, CMS_OtherKeyAttribute) 187e1051a39Sopenharmony_ci} static_ASN1_SEQUENCE_END(CMS_KEKIdentifier) 188e1051a39Sopenharmony_ci 189e1051a39Sopenharmony_ciASN1_SEQUENCE(CMS_KEKRecipientInfo) = { 190e1051a39Sopenharmony_ci ASN1_EMBED(CMS_KEKRecipientInfo, version, INT32), 191e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_KEKRecipientInfo, kekid, CMS_KEKIdentifier), 192e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_KEKRecipientInfo, keyEncryptionAlgorithm, X509_ALGOR), 193e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_KEKRecipientInfo, encryptedKey, ASN1_OCTET_STRING) 194e1051a39Sopenharmony_ci} ASN1_SEQUENCE_END(CMS_KEKRecipientInfo) 195e1051a39Sopenharmony_ci 196e1051a39Sopenharmony_ciASN1_SEQUENCE(CMS_PasswordRecipientInfo) = { 197e1051a39Sopenharmony_ci ASN1_EMBED(CMS_PasswordRecipientInfo, version, INT32), 198e1051a39Sopenharmony_ci ASN1_IMP_OPT(CMS_PasswordRecipientInfo, keyDerivationAlgorithm, X509_ALGOR, 0), 199e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_PasswordRecipientInfo, keyEncryptionAlgorithm, X509_ALGOR), 200e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_PasswordRecipientInfo, encryptedKey, ASN1_OCTET_STRING) 201e1051a39Sopenharmony_ci} ASN1_SEQUENCE_END(CMS_PasswordRecipientInfo) 202e1051a39Sopenharmony_ci 203e1051a39Sopenharmony_ciASN1_SEQUENCE(CMS_OtherRecipientInfo) = { 204e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_OtherRecipientInfo, oriType, ASN1_OBJECT), 205e1051a39Sopenharmony_ci ASN1_OPT(CMS_OtherRecipientInfo, oriValue, ASN1_ANY) 206e1051a39Sopenharmony_ci} static_ASN1_SEQUENCE_END(CMS_OtherRecipientInfo) 207e1051a39Sopenharmony_ci 208e1051a39Sopenharmony_ci/* Free up RecipientInfo additional data */ 209e1051a39Sopenharmony_cistatic int cms_ri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, 210e1051a39Sopenharmony_ci void *exarg) 211e1051a39Sopenharmony_ci{ 212e1051a39Sopenharmony_ci if (operation == ASN1_OP_FREE_PRE) { 213e1051a39Sopenharmony_ci CMS_RecipientInfo *ri = (CMS_RecipientInfo *)*pval; 214e1051a39Sopenharmony_ci if (ri->type == CMS_RECIPINFO_TRANS) { 215e1051a39Sopenharmony_ci CMS_KeyTransRecipientInfo *ktri = ri->d.ktri; 216e1051a39Sopenharmony_ci EVP_PKEY_free(ktri->pkey); 217e1051a39Sopenharmony_ci X509_free(ktri->recip); 218e1051a39Sopenharmony_ci EVP_PKEY_CTX_free(ktri->pctx); 219e1051a39Sopenharmony_ci } else if (ri->type == CMS_RECIPINFO_KEK) { 220e1051a39Sopenharmony_ci CMS_KEKRecipientInfo *kekri = ri->d.kekri; 221e1051a39Sopenharmony_ci OPENSSL_clear_free(kekri->key, kekri->keylen); 222e1051a39Sopenharmony_ci } else if (ri->type == CMS_RECIPINFO_PASS) { 223e1051a39Sopenharmony_ci CMS_PasswordRecipientInfo *pwri = ri->d.pwri; 224e1051a39Sopenharmony_ci OPENSSL_clear_free(pwri->pass, pwri->passlen); 225e1051a39Sopenharmony_ci } 226e1051a39Sopenharmony_ci } 227e1051a39Sopenharmony_ci return 1; 228e1051a39Sopenharmony_ci} 229e1051a39Sopenharmony_ci 230e1051a39Sopenharmony_ciASN1_CHOICE_cb(CMS_RecipientInfo, cms_ri_cb) = { 231e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_RecipientInfo, d.ktri, CMS_KeyTransRecipientInfo), 232e1051a39Sopenharmony_ci ASN1_IMP(CMS_RecipientInfo, d.kari, CMS_KeyAgreeRecipientInfo, 1), 233e1051a39Sopenharmony_ci ASN1_IMP(CMS_RecipientInfo, d.kekri, CMS_KEKRecipientInfo, 2), 234e1051a39Sopenharmony_ci ASN1_IMP(CMS_RecipientInfo, d.pwri, CMS_PasswordRecipientInfo, 3), 235e1051a39Sopenharmony_ci ASN1_IMP(CMS_RecipientInfo, d.ori, CMS_OtherRecipientInfo, 4) 236e1051a39Sopenharmony_ci} ASN1_CHOICE_END_cb(CMS_RecipientInfo, CMS_RecipientInfo, type) 237e1051a39Sopenharmony_ci 238e1051a39Sopenharmony_ciASN1_NDEF_SEQUENCE(CMS_EnvelopedData) = { 239e1051a39Sopenharmony_ci ASN1_EMBED(CMS_EnvelopedData, version, INT32), 240e1051a39Sopenharmony_ci ASN1_IMP_OPT(CMS_EnvelopedData, originatorInfo, CMS_OriginatorInfo, 0), 241e1051a39Sopenharmony_ci ASN1_SET_OF(CMS_EnvelopedData, recipientInfos, CMS_RecipientInfo), 242e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_EnvelopedData, encryptedContentInfo, CMS_EncryptedContentInfo), 243e1051a39Sopenharmony_ci ASN1_IMP_SET_OF_OPT(CMS_EnvelopedData, unprotectedAttrs, X509_ATTRIBUTE, 1) 244e1051a39Sopenharmony_ci} ASN1_NDEF_SEQUENCE_END(CMS_EnvelopedData) 245e1051a39Sopenharmony_ci 246e1051a39Sopenharmony_ciASN1_NDEF_SEQUENCE(CMS_DigestedData) = { 247e1051a39Sopenharmony_ci ASN1_EMBED(CMS_DigestedData, version, INT32), 248e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_DigestedData, digestAlgorithm, X509_ALGOR), 249e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_DigestedData, encapContentInfo, CMS_EncapsulatedContentInfo), 250e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_DigestedData, digest, ASN1_OCTET_STRING) 251e1051a39Sopenharmony_ci} ASN1_NDEF_SEQUENCE_END(CMS_DigestedData) 252e1051a39Sopenharmony_ci 253e1051a39Sopenharmony_ciASN1_NDEF_SEQUENCE(CMS_EncryptedData) = { 254e1051a39Sopenharmony_ci ASN1_EMBED(CMS_EncryptedData, version, INT32), 255e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_EncryptedData, encryptedContentInfo, CMS_EncryptedContentInfo), 256e1051a39Sopenharmony_ci ASN1_IMP_SET_OF_OPT(CMS_EncryptedData, unprotectedAttrs, X509_ATTRIBUTE, 1) 257e1051a39Sopenharmony_ci} ASN1_NDEF_SEQUENCE_END(CMS_EncryptedData) 258e1051a39Sopenharmony_ci 259e1051a39Sopenharmony_ci/* Defined in RFC 5083 - Section 2.1. AuthEnvelopedData Type */ 260e1051a39Sopenharmony_ciASN1_NDEF_SEQUENCE(CMS_AuthEnvelopedData) = { 261e1051a39Sopenharmony_ci ASN1_EMBED(CMS_AuthEnvelopedData, version, INT32), 262e1051a39Sopenharmony_ci ASN1_IMP_OPT(CMS_AuthEnvelopedData, originatorInfo, CMS_OriginatorInfo, 0), 263e1051a39Sopenharmony_ci ASN1_SET_OF(CMS_AuthEnvelopedData, recipientInfos, CMS_RecipientInfo), 264e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_AuthEnvelopedData, authEncryptedContentInfo, CMS_EncryptedContentInfo), 265e1051a39Sopenharmony_ci ASN1_IMP_SET_OF_OPT(CMS_AuthEnvelopedData, authAttrs, X509_ALGOR, 2), 266e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_AuthEnvelopedData, mac, ASN1_OCTET_STRING), 267e1051a39Sopenharmony_ci ASN1_IMP_SET_OF_OPT(CMS_AuthEnvelopedData, unauthAttrs, X509_ALGOR, 3) 268e1051a39Sopenharmony_ci} ASN1_NDEF_SEQUENCE_END(CMS_AuthEnvelopedData) 269e1051a39Sopenharmony_ci 270e1051a39Sopenharmony_ciASN1_NDEF_SEQUENCE(CMS_AuthenticatedData) = { 271e1051a39Sopenharmony_ci ASN1_EMBED(CMS_AuthenticatedData, version, INT32), 272e1051a39Sopenharmony_ci ASN1_IMP_OPT(CMS_AuthenticatedData, originatorInfo, CMS_OriginatorInfo, 0), 273e1051a39Sopenharmony_ci ASN1_SET_OF(CMS_AuthenticatedData, recipientInfos, CMS_RecipientInfo), 274e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_AuthenticatedData, macAlgorithm, X509_ALGOR), 275e1051a39Sopenharmony_ci ASN1_IMP(CMS_AuthenticatedData, digestAlgorithm, X509_ALGOR, 1), 276e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_AuthenticatedData, encapContentInfo, CMS_EncapsulatedContentInfo), 277e1051a39Sopenharmony_ci ASN1_IMP_SET_OF_OPT(CMS_AuthenticatedData, authAttrs, X509_ALGOR, 2), 278e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_AuthenticatedData, mac, ASN1_OCTET_STRING), 279e1051a39Sopenharmony_ci ASN1_IMP_SET_OF_OPT(CMS_AuthenticatedData, unauthAttrs, X509_ALGOR, 3) 280e1051a39Sopenharmony_ci} static_ASN1_NDEF_SEQUENCE_END(CMS_AuthenticatedData) 281e1051a39Sopenharmony_ci 282e1051a39Sopenharmony_ciASN1_NDEF_SEQUENCE(CMS_CompressedData) = { 283e1051a39Sopenharmony_ci ASN1_EMBED(CMS_CompressedData, version, INT32), 284e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_CompressedData, compressionAlgorithm, X509_ALGOR), 285e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_CompressedData, encapContentInfo, CMS_EncapsulatedContentInfo), 286e1051a39Sopenharmony_ci} ASN1_NDEF_SEQUENCE_END(CMS_CompressedData) 287e1051a39Sopenharmony_ci 288e1051a39Sopenharmony_ci/* This is the ANY DEFINED BY table for the top level ContentInfo structure */ 289e1051a39Sopenharmony_ci 290e1051a39Sopenharmony_ciASN1_ADB_TEMPLATE(cms_default) = ASN1_EXP(CMS_ContentInfo, d.other, ASN1_ANY, 0); 291e1051a39Sopenharmony_ci 292e1051a39Sopenharmony_ciASN1_ADB(CMS_ContentInfo) = { 293e1051a39Sopenharmony_ci ADB_ENTRY(NID_pkcs7_data, ASN1_NDEF_EXP(CMS_ContentInfo, d.data, ASN1_OCTET_STRING_NDEF, 0)), 294e1051a39Sopenharmony_ci ADB_ENTRY(NID_pkcs7_signed, ASN1_NDEF_EXP(CMS_ContentInfo, d.signedData, CMS_SignedData, 0)), 295e1051a39Sopenharmony_ci ADB_ENTRY(NID_pkcs7_enveloped, ASN1_NDEF_EXP(CMS_ContentInfo, d.envelopedData, CMS_EnvelopedData, 0)), 296e1051a39Sopenharmony_ci ADB_ENTRY(NID_pkcs7_digest, ASN1_NDEF_EXP(CMS_ContentInfo, d.digestedData, CMS_DigestedData, 0)), 297e1051a39Sopenharmony_ci ADB_ENTRY(NID_pkcs7_encrypted, ASN1_NDEF_EXP(CMS_ContentInfo, d.encryptedData, CMS_EncryptedData, 0)), 298e1051a39Sopenharmony_ci ADB_ENTRY(NID_id_smime_ct_authEnvelopedData, ASN1_NDEF_EXP(CMS_ContentInfo, d.authEnvelopedData, CMS_AuthEnvelopedData, 0)), 299e1051a39Sopenharmony_ci ADB_ENTRY(NID_id_smime_ct_authData, ASN1_NDEF_EXP(CMS_ContentInfo, d.authenticatedData, CMS_AuthenticatedData, 0)), 300e1051a39Sopenharmony_ci ADB_ENTRY(NID_id_smime_ct_compressedData, ASN1_NDEF_EXP(CMS_ContentInfo, d.compressedData, CMS_CompressedData, 0)), 301e1051a39Sopenharmony_ci} ASN1_ADB_END(CMS_ContentInfo, 0, contentType, 0, &cms_default_tt, NULL); 302e1051a39Sopenharmony_ci 303e1051a39Sopenharmony_ci/* CMS streaming support */ 304e1051a39Sopenharmony_cistatic int cms_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, 305e1051a39Sopenharmony_ci void *exarg) 306e1051a39Sopenharmony_ci{ 307e1051a39Sopenharmony_ci ASN1_STREAM_ARG *sarg = exarg; 308e1051a39Sopenharmony_ci CMS_ContentInfo *cms = NULL; 309e1051a39Sopenharmony_ci if (pval) 310e1051a39Sopenharmony_ci cms = (CMS_ContentInfo *)*pval; 311e1051a39Sopenharmony_ci else 312e1051a39Sopenharmony_ci return 1; 313e1051a39Sopenharmony_ci switch (operation) { 314e1051a39Sopenharmony_ci 315e1051a39Sopenharmony_ci case ASN1_OP_STREAM_PRE: 316e1051a39Sopenharmony_ci if (CMS_stream(&sarg->boundary, cms) <= 0) 317e1051a39Sopenharmony_ci return 0; 318e1051a39Sopenharmony_ci /* fall thru */ 319e1051a39Sopenharmony_ci case ASN1_OP_DETACHED_PRE: 320e1051a39Sopenharmony_ci sarg->ndef_bio = CMS_dataInit(cms, sarg->out); 321e1051a39Sopenharmony_ci if (!sarg->ndef_bio) 322e1051a39Sopenharmony_ci return 0; 323e1051a39Sopenharmony_ci break; 324e1051a39Sopenharmony_ci 325e1051a39Sopenharmony_ci case ASN1_OP_STREAM_POST: 326e1051a39Sopenharmony_ci case ASN1_OP_DETACHED_POST: 327e1051a39Sopenharmony_ci if (CMS_dataFinal(cms, sarg->ndef_bio) <= 0) 328e1051a39Sopenharmony_ci return 0; 329e1051a39Sopenharmony_ci break; 330e1051a39Sopenharmony_ci 331e1051a39Sopenharmony_ci case ASN1_OP_FREE_POST: 332e1051a39Sopenharmony_ci OPENSSL_free(cms->ctx.propq); 333e1051a39Sopenharmony_ci break; 334e1051a39Sopenharmony_ci 335e1051a39Sopenharmony_ci } 336e1051a39Sopenharmony_ci return 1; 337e1051a39Sopenharmony_ci} 338e1051a39Sopenharmony_ci 339e1051a39Sopenharmony_ciASN1_NDEF_SEQUENCE_cb(CMS_ContentInfo, cms_cb) = { 340e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_ContentInfo, contentType, ASN1_OBJECT), 341e1051a39Sopenharmony_ci ASN1_ADB_OBJECT(CMS_ContentInfo) 342e1051a39Sopenharmony_ci} ASN1_NDEF_SEQUENCE_END_cb(CMS_ContentInfo, CMS_ContentInfo) 343e1051a39Sopenharmony_ci 344e1051a39Sopenharmony_ci/* Specials for signed attributes */ 345e1051a39Sopenharmony_ci 346e1051a39Sopenharmony_ci/* 347e1051a39Sopenharmony_ci * When signing attributes we want to reorder them to match the sorted 348e1051a39Sopenharmony_ci * encoding. 349e1051a39Sopenharmony_ci */ 350e1051a39Sopenharmony_ci 351e1051a39Sopenharmony_ciASN1_ITEM_TEMPLATE(CMS_Attributes_Sign) = 352e1051a39Sopenharmony_ci ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_ORDER, 0, CMS_ATTRIBUTES, X509_ATTRIBUTE) 353e1051a39Sopenharmony_ciASN1_ITEM_TEMPLATE_END(CMS_Attributes_Sign) 354e1051a39Sopenharmony_ci 355e1051a39Sopenharmony_ci/* 356e1051a39Sopenharmony_ci * When verifying attributes we need to use the received order. So we use 357e1051a39Sopenharmony_ci * SEQUENCE OF and tag it to SET OF 358e1051a39Sopenharmony_ci */ 359e1051a39Sopenharmony_ci 360e1051a39Sopenharmony_ciASN1_ITEM_TEMPLATE(CMS_Attributes_Verify) = 361e1051a39Sopenharmony_ci ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_IMPTAG | ASN1_TFLG_UNIVERSAL, 362e1051a39Sopenharmony_ci V_ASN1_SET, CMS_ATTRIBUTES, X509_ATTRIBUTE) 363e1051a39Sopenharmony_ciASN1_ITEM_TEMPLATE_END(CMS_Attributes_Verify) 364e1051a39Sopenharmony_ci 365e1051a39Sopenharmony_ci 366e1051a39Sopenharmony_ci 367e1051a39Sopenharmony_ciASN1_CHOICE(CMS_ReceiptsFrom) = { 368e1051a39Sopenharmony_ci ASN1_IMP_EMBED(CMS_ReceiptsFrom, d.allOrFirstTier, INT32, 0), 369e1051a39Sopenharmony_ci ASN1_IMP_SEQUENCE_OF(CMS_ReceiptsFrom, d.receiptList, GENERAL_NAMES, 1) 370e1051a39Sopenharmony_ci} static_ASN1_CHOICE_END(CMS_ReceiptsFrom) 371e1051a39Sopenharmony_ci 372e1051a39Sopenharmony_ciASN1_SEQUENCE(CMS_ReceiptRequest) = { 373e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_ReceiptRequest, signedContentIdentifier, ASN1_OCTET_STRING), 374e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_ReceiptRequest, receiptsFrom, CMS_ReceiptsFrom), 375e1051a39Sopenharmony_ci ASN1_SEQUENCE_OF(CMS_ReceiptRequest, receiptsTo, GENERAL_NAMES) 376e1051a39Sopenharmony_ci} ASN1_SEQUENCE_END(CMS_ReceiptRequest) 377e1051a39Sopenharmony_ci 378e1051a39Sopenharmony_ciASN1_SEQUENCE(CMS_Receipt) = { 379e1051a39Sopenharmony_ci ASN1_EMBED(CMS_Receipt, version, INT32), 380e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_Receipt, contentType, ASN1_OBJECT), 381e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_Receipt, signedContentIdentifier, ASN1_OCTET_STRING), 382e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_Receipt, originatorSignatureValue, ASN1_OCTET_STRING) 383e1051a39Sopenharmony_ci} ASN1_SEQUENCE_END(CMS_Receipt) 384e1051a39Sopenharmony_ci 385e1051a39Sopenharmony_ci/* 386e1051a39Sopenharmony_ci * Utilities to encode the CMS_SharedInfo structure used during key 387e1051a39Sopenharmony_ci * derivation. 388e1051a39Sopenharmony_ci */ 389e1051a39Sopenharmony_ci 390e1051a39Sopenharmony_citypedef struct { 391e1051a39Sopenharmony_ci X509_ALGOR *keyInfo; 392e1051a39Sopenharmony_ci ASN1_OCTET_STRING *entityUInfo; 393e1051a39Sopenharmony_ci ASN1_OCTET_STRING *suppPubInfo; 394e1051a39Sopenharmony_ci} CMS_SharedInfo; 395e1051a39Sopenharmony_ci 396e1051a39Sopenharmony_ciASN1_SEQUENCE(CMS_SharedInfo) = { 397e1051a39Sopenharmony_ci ASN1_SIMPLE(CMS_SharedInfo, keyInfo, X509_ALGOR), 398e1051a39Sopenharmony_ci ASN1_EXP_OPT(CMS_SharedInfo, entityUInfo, ASN1_OCTET_STRING, 0), 399e1051a39Sopenharmony_ci ASN1_EXP_OPT(CMS_SharedInfo, suppPubInfo, ASN1_OCTET_STRING, 2), 400e1051a39Sopenharmony_ci} static_ASN1_SEQUENCE_END(CMS_SharedInfo) 401e1051a39Sopenharmony_ci 402e1051a39Sopenharmony_ciint CMS_SharedInfo_encode(unsigned char **pder, X509_ALGOR *kekalg, 403e1051a39Sopenharmony_ci ASN1_OCTET_STRING *ukm, int keylen) 404e1051a39Sopenharmony_ci{ 405e1051a39Sopenharmony_ci union { 406e1051a39Sopenharmony_ci CMS_SharedInfo *pecsi; 407e1051a39Sopenharmony_ci ASN1_VALUE *a; 408e1051a39Sopenharmony_ci } intsi = { 409e1051a39Sopenharmony_ci NULL 410e1051a39Sopenharmony_ci }; 411e1051a39Sopenharmony_ci 412e1051a39Sopenharmony_ci ASN1_OCTET_STRING oklen; 413e1051a39Sopenharmony_ci unsigned char kl[4]; 414e1051a39Sopenharmony_ci CMS_SharedInfo ecsi; 415e1051a39Sopenharmony_ci 416e1051a39Sopenharmony_ci keylen <<= 3; 417e1051a39Sopenharmony_ci kl[0] = (keylen >> 24) & 0xff; 418e1051a39Sopenharmony_ci kl[1] = (keylen >> 16) & 0xff; 419e1051a39Sopenharmony_ci kl[2] = (keylen >> 8) & 0xff; 420e1051a39Sopenharmony_ci kl[3] = keylen & 0xff; 421e1051a39Sopenharmony_ci oklen.length = 4; 422e1051a39Sopenharmony_ci oklen.data = kl; 423e1051a39Sopenharmony_ci oklen.type = V_ASN1_OCTET_STRING; 424e1051a39Sopenharmony_ci oklen.flags = 0; 425e1051a39Sopenharmony_ci ecsi.keyInfo = kekalg; 426e1051a39Sopenharmony_ci ecsi.entityUInfo = ukm; 427e1051a39Sopenharmony_ci ecsi.suppPubInfo = &oklen; 428e1051a39Sopenharmony_ci intsi.pecsi = &ecsi; 429e1051a39Sopenharmony_ci return ASN1_item_i2d(intsi.a, pder, ASN1_ITEM_rptr(CMS_SharedInfo)); 430e1051a39Sopenharmony_ci} 431