Path: blob/master/lib/rex/proto/crypto_asn1/cms.rb
19721 views
module Rex::Proto::CryptoAsn1::Cms1class Attribute < RASN1::Model2sequence :attribute,3content: [4objectid(:attribute_type),5set_of(:attribute_values, RASN1::Types::Any)6]7end89class Certificate10# Rather than specifying the entire structure of a certificate, we pass this off11# to OpenSSL, effectively providing an interface between RASN and OpenSSL.1213attr_accessor :options1415def initialize(options = {})16self.options = options17end1819def to_der20options[:openssl_certificate]&.to_der || ''21end2223# RASN1 Glue method - Say if DER can be built (not default value, not optional without value, has a value)24# @return [Boolean]25# @since 0.1226def can_build?27!to_der.empty?28end2930# RASN1 Glue method31def primitive?32false33end3435# RASN1 Glue method36def value37options[:openssl_certificate]38end3940def parse!(str, ber: false)41options[:openssl_certificate] = OpenSSL::X509::Certificate.new(str)42to_der.length43end44end4546# see: https://datatracker.ietf.org/doc/rfc5911/47class CCMParameters < RASN1::Model48sequence :gcm_parameters,49content: [50octet_string(:aes_nonce),51integer(:aes_ccm_icvlen)52]53end5455# see: https://datatracker.ietf.org/doc/rfc5911/56class GCMParameters < RASN1::Model57sequence :gcm_parameters,58content: [59octet_string(:aes_nonce),60integer(:aes_gcm_icvlen)61]62end6364class AlgorithmIdentifier < RASN1::Model65sequence :algorithm_identifier,66content: [67objectid(:algorithm),68any(:parameters, optional: true)69]7071def ccm_parameters72CCMParameters.parse(self[:parameters].value)73end7475def gcm_parameters76GCMParameters.parse(self[:parameters].value)77end78end7980class KeyDerivationAlgorithmIdentifier < AlgorithmIdentifier81end8283class KeyEncryptionAlgorithmIdentifier < AlgorithmIdentifier84end8586class ContentEncryptionAlgorithmIdentifier < AlgorithmIdentifier87end8889class OriginatorInfo < RASN1::Model90sequence :originator_info,91content: [set_of(:certs, Certificate, implicit: 0, optional: true),]92# CRLs - not implemented93end9495class ContentType < RASN1::Types::ObjectId96end9798class EncryptedContent < RASN1::Types::OctetString99end100101class EncryptedContentInfo < RASN1::Model102sequence :encrypted_content_info,103content: [104model(:content_type, ContentType),105model(:content_encryption_algorithm, ContentEncryptionAlgorithmIdentifier),106wrapper(model(:encrypted_content, EncryptedContent), implicit: 0, optional: true)107]108end109110class Name111# Rather than specifying the entire structure of a name, we pass this off112# to OpenSSL, effectively providing an interface between RASN and OpenSSL.113attr_accessor :value114115def initialize(options = {}); end116117def parse!(str, ber: false)118self.value = OpenSSL::X509::Name.new(str)119to_der.length120end121122def to_der123value.to_der124end125end126127class IssuerAndSerialNumber < RASN1::Model128sequence :signer_identifier,129content: [130model(:issuer, Name),131integer(:serial_number)132]133end134135class CmsVersion < RASN1::Types::Integer136end137138class SubjectKeyIdentifier < RASN1::Types::OctetString139end140141class UserKeyingMaterial < RASN1::Types::OctetString142end143144class RecipientIdentifier < RASN1::Model145choice :recipient_identifier,146content: [147model(:issuer_and_serial_number, IssuerAndSerialNumber),148wrapper(model(:subject_key_identifier, SubjectKeyIdentifier), implicit: 0)149]150end151152class EncryptedKey < RASN1::Types::OctetString153end154155class OtherKeyAttribute < RASN1::Model156sequence :other_key_attribute,157content: [158objectid(:key_attr_id),159any(:key_attr, optional: true)160]161end162163class RecipientKeyIdentifier < RASN1::Model164sequence :recipient_key_identifier,165content: [166model(:subject_key_identifier, SubjectKeyIdentifier),167generalized_time(:date, optional: true),168wrapper(model(:other, OtherKeyAttribute), optional: true)169]170171end172173class KeyAgreeRecipientIdentifier < RASN1::Model174choice :key_agree_recipient_identifier,175content: [176model(:issuer_and_serial_number, IssuerAndSerialNumber),177wrapper(model(:r_key_id, RecipientKeyIdentifier), implicit: 0)178]179end180181class RecipientEncryptedKey < RASN1::Model182sequence :recipient_encrypted_key,183content: [184model(:rid, KeyAgreeRecipientIdentifier),185model(:encrypted_key, EncryptedKey)186]187end188189class KEKIdentifier < RASN1::Model190sequence :kek_identifier,191content: [192octet_string(:key_identifier),193generalized_time(:date, optional: true),194wrapper(model(:other, OtherKeyAttribute), optional: true)195]196end197198class KeyTransRecipientInfo < RASN1::Model199sequence :key_trans_recipient_info,200content: [201model(:cms_version, CmsVersion),202model(:rid, RecipientIdentifier),203model(:key_encryption_algorithm, KeyEncryptionAlgorithmIdentifier),204model(:encrypted_key, EncryptedKey)205]206end207208class OriginatorPublicKey < RASN1::Model209sequence :originator_public_key,210content: [211model(:algorithm, AlgorithmIdentifier),212bit_string(:public_key)213]214end215216class OriginatorIdentifierOrKey < RASN1::Model217choice :originator_identifier_or_key,218content: [219model(:issuer_and_serial_number, IssuerAndSerialNumber),220model(:subject_key_identifier, SubjectKeyIdentifier),221model(:originator_public_key, OriginatorPublicKey)222]223end224225class KeyAgreeRecipientInfo < RASN1::Model226sequence :key_agree_recipient_info,227content: [228model(:cms_version, CmsVersion),229wrapper(model(:originator, OriginatorIdentifierOrKey), explicit: 0),230wrapper(model(:ukm, UserKeyingMaterial), explicit: 1, optional: true),231model(:key_encryption_algorithm, KeyEncryptionAlgorithmIdentifier),232sequence_of(:recipient_encrypted_keys, RecipientEncryptedKey)233]234end235236class KEKRecipientInfo < RASN1::Model237sequence :kek_recipient_info,238content: [239model(:cms_version, CmsVersion),240model(:kekid, KEKIdentifier),241model(:key_encryption_algorithm, KeyEncryptionAlgorithmIdentifier),242model(:encrypted_key, EncryptedKey)243]244end245246class PasswordRecipientInfo < RASN1::Model247sequence :password_recipient_info,248content: [249model(:cms_version, CmsVersion),250wrapper(model(:key_derivation_algorithm, KeyDerivationAlgorithmIdentifier), explicit: 0, optional: true),251model(:key_encryption_algorithm, KeyEncryptionAlgorithmIdentifier),252model(:encrypted_key, EncryptedKey)253]254end255256class OtherRecipientInfo < RASN1::Model257sequence :other_recipient_info,258content: [259objectid(:ore_type),260any(:ory_value)261]262end263264class RecipientInfo < RASN1::Model265choice :recipient_info,266content: [267model(:ktri, KeyTransRecipientInfo),268wrapper(model(:kari, KeyAgreeRecipientInfo), implicit: 1),269wrapper(model(:kekri, KEKRecipientInfo), implicit: 2),270wrapper(model(:pwri, PasswordRecipientInfo), implicit: 3),271wrapper(model(:ori, OtherRecipientInfo), implicit: 4)272]273end274275class EnvelopedData < RASN1::Model276sequence :enveloped_data,277explicit: 0, constructed: true,278content: [279model(:cms_version, CmsVersion),280wrapper(model(:originator_info, OriginatorInfo), implict: 0, optional: true),281set_of(:recipient_infos, RecipientInfo),282model(:encrypted_content_info, EncryptedContentInfo),283set_of(:unprotected_attrs, Attribute, implicit: 1, optional: true),284]285end286287class SignerInfo < RASN1::Model288sequence :signer_info,289content: [290integer(:version),291model(:sid, IssuerAndSerialNumber),292model(:digest_algorithm, AlgorithmIdentifier),293set_of(:signed_attrs, Attribute, implicit: 0, optional: true),294model(:signature_algorithm, AlgorithmIdentifier),295octet_string(:signature),296]297end298299class EncapsulatedContentInfo < RASN1::Model300sequence :encapsulated_content_info,301content: [302objectid(:econtent_type),303octet_string(:econtent, explicit: 0, constructed: true, optional: true)304]305306def econtent307if self[:econtent_type].value == Rex::Proto::CryptoAsn1::OIDs::OID_DIFFIE_HELLMAN_KEYDATA.value308Rex::Proto::Kerberos::Model::Pkinit::KdcDhKeyInfo.parse(self[:econtent].value)309elsif self[:econtent_type].value == Rex::Proto::Kerberos::Model::OID::PkinitAuthData310Rex::Proto::Kerberos::Model::Pkinit::AuthPack.parse(self[:econtent].value)311end312end313end314315class SignedData < RASN1::Model316sequence :signed_data,317explicit: 0, constructed: true,318content: [319integer(:version),320set_of(:digest_algorithms, AlgorithmIdentifier),321model(:encap_content_info, EncapsulatedContentInfo),322set_of(:certificates, Certificate, implicit: 0, optional: true),323# CRLs - not implemented324set_of(:signer_infos, SignerInfo)325]326end327328class ContentInfo < RASN1::Model329sequence :content_info,330content: [331model(:content_type, ContentType),332any(:data)333]334335def enveloped_data336if self[:content_type].value == Rex::Proto::CryptoAsn1::OIDs::OID_CMS_ENVELOPED_DATA.value337EnvelopedData.parse(self[:data].value)338end339end340341def signed_data342if self[:content_type].value == Rex::Proto::CryptoAsn1::OIDs::OID_CMS_SIGNED_DATA.value343SignedData.parse(self[:data].value)344end345end346end347end348349350