Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/lib/rex/proto/crypto_asn1/cms.rb
Views: 18091
module Rex::Proto::CryptoAsn1::Cms1class Attribute < RASN1::Model2sequence :attribute,3content: [objectid(:attribute_type),4set_of(:attribute_values, RASN1::Types::Any)5]6end78class Certificate9# Rather than specifying the entire structure of a certificate, we pass this off10# to OpenSSL, effectively providing an interface between RASN and OpenSSL.1112attr_accessor :options1314def initialize(options={})15self.options = options16end1718def to_der19self.options[:openssl_certificate]&.to_der || ''20end2122# RASN1 Glue method - Say if DER can be built (not default value, not optional without value, has a value)23# @return [Boolean]24# @since 0.1225def can_build?26!to_der.empty?27end2829# RASN1 Glue method30def primitive?31false32end3334# RASN1 Glue method35def value36options[:openssl_certificate]37end3839def parse!(str, ber: false)40self.options[:openssl_certificate] = OpenSSL::X509::Certificate.new(str)41to_der.length42end43end4445class AlgorithmIdentifier < RASN1::Model46sequence :algorithm_identifier,47content: [objectid(:algorithm),48any(:parameters, optional: true)49]50end5152class KeyDerivationAlgorithmIdentifier < AlgorithmIdentifier53end5455class KeyEncryptionAlgorithmIdentifier < AlgorithmIdentifier56end5758class ContentEncryptionAlgorithmIdentifier < AlgorithmIdentifier59end6061class OriginatorInfo < RASN1::Model62sequence :originator_info,63content: [set_of(:certs, Certificate, implicit: 0, optional: true),64# CRLs - not implemented65]66end6768class ContentType < RASN1::Types::ObjectId69end7071class EncryptedContent < RASN1::Types::OctetString72end7374class EncryptedContentInfo < RASN1::Model75sequence :encrypted_content_info,76content: [model(:content_type, ContentType),77model(:content_encryption_algorithm, ContentEncryptionAlgorithmIdentifier),78wrapper(model(:encrypted_content, EncryptedContent), implicit: 0, optional: true)79]80end8182class Name83# Rather than specifying the entire structure of a name, we pass this off84# to OpenSSL, effectively providing an interface between RASN and OpenSSL.85attr_accessor :value8687def initialize(options={})88end8990def parse!(str, ber: false)91self.value = OpenSSL::X509::Name.new(str)92to_der.length93end9495def to_der96self.value.to_der97end98end99100class IssuerAndSerialNumber < RASN1::Model101sequence :signer_identifier,102content: [model(:issuer, Name),103integer(:serial_number)104]105end106107class CmsVersion < RASN1::Types::Integer108end109110class SubjectKeyIdentifier < RASN1::Types::OctetString111end112113class UserKeyingMaterial < RASN1::Types::OctetString114end115116class RecipientIdentifier < RASN1::Model117choice :recipient_identifier,118content: [model(:issuer_and_serial_number, IssuerAndSerialNumber),119wrapper(model(:subject_key_identifier, SubjectKeyIdentifier), implicit: 0)]120end121122class EncryptedKey < RASN1::Types::OctetString123end124125class OtherKeyAttribute < RASN1::Model126sequence :other_key_attribute,127content: [objectid(:key_attr_id),128any(:key_attr, optional: true)129]130end131132class RecipientKeyIdentifier < RASN1::Model133sequence :recipient_key_identifier,134content: [model(:subject_key_identifier, SubjectKeyIdentifier),135generalized_time(:date, optional: true),136wrapper(model(:other, OtherKeyAttribute), optional: true)137]138139end140141class KeyAgreeRecipientIdentifier < RASN1::Model142choice :key_agree_recipient_identifier,143content: [model(:issuer_and_serial_number, IssuerAndSerialNumber),144wrapper(model(:r_key_id, RecipientKeyIdentifier), implicit: 0)]145end146147class RecipientEncryptedKey < RASN1::Model148sequence :recipient_encrypted_key,149content: [model(:rid, KeyAgreeRecipientIdentifier),150model(:encrypted_key, EncryptedKey)]151end152153class KEKIdentifier < RASN1::Model154sequence :kek_identifier,155content: [octet_string(:key_identifier),156generalized_time(:date, optional: true),157wrapper(model(:other, OtherKeyAttribute), optional: true)]158end159160class KeyTransRecipientInfo < RASN1::Model161sequence :key_trans_recipient_info,162content: [model(:cms_version, CmsVersion),163model(:rid, RecipientIdentifier),164model(:key_encryption_algorithm, KeyEncryptionAlgorithmIdentifier),165model(:encrypted_key, EncryptedKey)166]167end168169class OriginatorPublicKey < RASN1::Model170sequence :originator_public_key,171content: [model(:algorithm, AlgorithmIdentifier),172bit_string(:public_key)]173end174175class OriginatorIdentifierOrKey < RASN1::Model176choice :originator_identifier_or_key,177content: [model(:issuer_and_serial_number, IssuerAndSerialNumber),178model(:subject_key_identifier, SubjectKeyIdentifier),179model(:originator_public_key, OriginatorPublicKey)180]181end182183class KeyAgreeRecipientInfo < RASN1::Model184sequence :key_agree_recipient_info,185content: [model(:cms_version, CmsVersion),186wrapper(model(:originator, OriginatorIdentifierOrKey), explicit: 0),187wrapper(model(:ukm, UserKeyingMaterial), explicit: 1, optional: true),188model(:key_encryption_algorithm, KeyEncryptionAlgorithmIdentifier),189sequence_of(:recipient_encrypted_keys, RecipientEncryptedKey)190]191end192193class KEKRecipientInfo < RASN1::Model194sequence :kek_recipient_info,195content: [model(:cms_version, CmsVersion),196model(:kekid, KEKIdentifier),197model(:key_encryption_algorithm, KeyEncryptionAlgorithmIdentifier),198model(:encrypted_key, EncryptedKey)199]200end201202class PasswordRecipientInfo < RASN1::Model203sequence :password_recipient_info,204content: [model(:cms_version, CmsVersion),205wrapper(model(:key_derivation_algorithm, KeyDerivationAlgorithmIdentifier), explicit: 0, optional: true),206model(:key_encryption_algorithm, KeyEncryptionAlgorithmIdentifier),207model(:encrypted_key, EncryptedKey)208]209end210211class OtherRecipientInfo < RASN1::Model212sequence :other_recipient_info,213content: [objectid(:ore_type),214any(:ory_value)215]216end217218class RecipientInfo < RASN1::Model219choice :recipient_info,220content: [model(:ktri, KeyTransRecipientInfo),221wrapper(model(:kari, KeyAgreeRecipientInfo), implicit: 1),222wrapper(model(:kekri, KEKRecipientInfo), implicit: 2),223wrapper(model(:pwri, PasswordRecipientInfo), implicit: 3),224wrapper(model(:ori, OtherRecipientInfo), implicit: 4)]225end226227class EnvelopedData < RASN1::Model228sequence :enveloped_data,229explicit: 0, constructed: true,230content: [model(:cms_version, CmsVersion),231wrapper(model(:originator_info, OriginatorInfo), implict: 0, optional: true),232set_of(:recipient_infos, RecipientInfo),233model(:encrypted_content_info, EncryptedContentInfo),234set_of(:unprotected_attrs, Attribute, implicit: 1, optional: true),235]236end237238class SignerInfo < RASN1::Model239sequence :signer_info,240content: [integer(:version),241model(:sid, IssuerAndSerialNumber),242model(:digest_algorithm, AlgorithmIdentifier),243set_of(:signed_attrs, Attribute, implicit: 0, optional: true),244model(:signature_algorithm, AlgorithmIdentifier),245octet_string(:signature),246]247end248249class EncapsulatedContentInfo < RASN1::Model250sequence :encapsulated_content_info,251content: [objectid(:econtent_type),252octet_string(:econtent, explicit: 0, constructed: true, optional: true)253]254255def econtent256if self[:econtent_type].value == Rex::Proto::CryptoAsn1::OIDs::OID_DIFFIE_HELLMAN_KEYDATA.value257Rex::Proto::Kerberos::Model::Pkinit::KdcDhKeyInfo.parse(self[:econtent].value)258elsif self[:econtent_type].value == Rex::Proto::Kerberos::Model::OID::PkinitAuthData259Rex::Proto::Kerberos::Model::Pkinit::AuthPack.parse(self[:econtent].value)260end261end262end263264class SignedData < RASN1::Model265sequence :signed_data,266explicit: 0, constructed: true,267content: [integer(:version),268set_of(:digest_algorithms, AlgorithmIdentifier),269model(:encap_content_info, EncapsulatedContentInfo),270set_of(:certificates, Certificate, implicit: 0, optional: true),271# CRLs - not implemented272set_of(:signer_infos, SignerInfo)273]274end275276class ContentInfo < RASN1::Model277sequence :content_info,278content: [model(:content_type, ContentType),279any(:data)280]281282def enveloped_data283if self[:content_type].value == Rex::Proto::CryptoAsn1::OIDs::OID_CMS_ENVELOPED_DATA.value284EnvelopedData.parse(self[:data].value)285end286end287288def signed_data289if self[:content_type].value == Rex::Proto::CryptoAsn1::OIDs::OID_CMS_SIGNED_DATA.value290SignedData.parse(self[:data].value)291end292end293end294end295296