X-Git-Url: http://www.git.cypherpunks.ru/?p=pygost.git;a=blobdiff_plain;f=pygost%2Fasn1schemas%2Fcms.py;h=2df3633158de3bc0fd65edbef29d0a45031b1b6b;hp=e3f3244b4c9148e3cb1d8364587688eb13029830;hb=141ec28f940fbba3edb67252bc4e8707502b87ce;hpb=5ecaafbe1a87f9a311a18574653e6dbc75a776b9 diff --git a/pygost/asn1schemas/cms.py b/pygost/asn1schemas/cms.py index e3f3244..2df3633 100644 --- a/pygost/asn1schemas/cms.py +++ b/pygost/asn1schemas/cms.py @@ -1,6 +1,6 @@ # coding: utf-8 # PyGOST -- Pure Python GOST cryptographic functions library -# Copyright (C) 2015-2019 Sergey Matveev +# Copyright (C) 2015-2021 Sergey Matveev # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -28,14 +28,26 @@ from pyderasn import SetOf from pyderasn import tag_ctxc from pyderasn import tag_ctxp +from pygost.asn1schemas.oids import id_cms_mac_attr +from pygost.asn1schemas.oids import id_contentType from pygost.asn1schemas.oids import id_digestedData +from pygost.asn1schemas.oids import id_encryptedData from pygost.asn1schemas.oids import id_envelopedData from pygost.asn1schemas.oids import id_Gost28147_89 +from pygost.asn1schemas.oids import id_gostr3412_2015_kuznyechik_ctracpkm +from pygost.asn1schemas.oids import id_gostr3412_2015_kuznyechik_ctracpkm_omac +from pygost.asn1schemas.oids import id_gostr3412_2015_kuznyechik_wrap_kexp15 +from pygost.asn1schemas.oids import id_gostr3412_2015_magma_ctracpkm +from pygost.asn1schemas.oids import id_gostr3412_2015_magma_ctracpkm_omac +from pygost.asn1schemas.oids import id_gostr3412_2015_magma_wrap_kexp15 +from pygost.asn1schemas.oids import id_messageDigest from pygost.asn1schemas.oids import id_signedData from pygost.asn1schemas.oids import id_tc26_gost3410_2012_256 from pygost.asn1schemas.oids import id_tc26_gost3410_2012_512 from pygost.asn1schemas.x509 import AlgorithmIdentifier from pygost.asn1schemas.x509 import Certificate +from pygost.asn1schemas.x509 import CertificateSerialNumber +from pygost.asn1schemas.x509 import Name from pygost.asn1schemas.x509 import SubjectPublicKeyInfo @@ -47,10 +59,25 @@ class ContentType(ObjectIdentifier): pass +class IssuerAndSerialNumber(Sequence): + schema = ( + ("issuer", Name()), + ("serialNumber", CertificateSerialNumber()), + ) + + +class KeyIdentifier(OctetString): + pass + + +class SubjectKeyIdentifier(KeyIdentifier): + pass + + class RecipientIdentifier(Choice): schema = ( - ("issuerAndSerialNumber", Any()), - # ("subjectKeyIdentifier", SubjectKeyIdentifier(impl=tag_ctxp(0))), + ("issuerAndSerialNumber", IssuerAndSerialNumber()), + ("subjectKeyIdentifier", SubjectKeyIdentifier(impl=tag_ctxp(0))), ) @@ -91,12 +118,32 @@ class GostR3410KeyTransport(Sequence): ) +class GostR3410KeyTransport2019(Sequence): + schema = ( + ("encryptedKey", OctetString()), + ("ephemeralPublicKey", SubjectPublicKeyInfo()), + ("ukm", OctetString()), + ) + + +class GostR341012KEGParameters(Sequence): + schema = ( + ("algorithm", ObjectIdentifier()), + ) + + class KeyEncryptionAlgorithmIdentifier(AlgorithmIdentifier): schema = ( ("algorithm", ObjectIdentifier(defines=( + (("parameters",), { + id_gostr3412_2015_magma_wrap_kexp15: GostR341012KEGParameters(), + id_gostr3412_2015_kuznyechik_wrap_kexp15: GostR341012KEGParameters(), + }), (("..", "encryptedKey"), { id_tc26_gost3410_2012_256: GostR3410KeyTransport(), id_tc26_gost3410_2012_512: GostR3410KeyTransport(), + id_gostr3412_2015_magma_wrap_kexp15: GostR3410KeyTransport2019(), + id_gostr3412_2015_kuznyechik_wrap_kexp15: GostR3410KeyTransport2019(), }), (("..", "recipientEncryptedKeys", any, "encryptedKey"), { id_tc26_gost3410_2012_256: Gost2814789EncryptedKey(), @@ -129,8 +176,8 @@ class OriginatorPublicKey(Sequence): class OriginatorIdentifierOrKey(Choice): schema = ( - # ("issuerAndSerialNumber", IssuerAndSerialNumber()), - # ("subjectKeyIdentifier", SubjectKeyIdentifier(impl=tag_ctxp(0))), + ("issuerAndSerialNumber", IssuerAndSerialNumber()), + ("subjectKeyIdentifier", SubjectKeyIdentifier(impl=tag_ctxp(0))), ("originatorKey", OriginatorPublicKey(impl=tag_ctxc(1))), ) @@ -141,7 +188,7 @@ class UserKeyingMaterial(OctetString): class KeyAgreeRecipientIdentifier(Choice): schema = ( - ("issuerAndSerialNumber", Any()), + ("issuerAndSerialNumber", IssuerAndSerialNumber()), # ("rKeyId", RecipientKeyIdentifier(impl=tag_ctxc(0))), ) @@ -193,10 +240,22 @@ class Gost2814789Parameters(Sequence): ) +class Gost341215EncryptionParameters(Sequence): + schema = ( + ("ukm", OctetString()), + ) + + class ContentEncryptionAlgorithmIdentifier(AlgorithmIdentifier): schema = ( ("algorithm", ObjectIdentifier(defines=( - (("parameters",), {id_Gost28147_89: Gost2814789Parameters()}), + (("parameters",), { + id_Gost28147_89: Gost2814789Parameters(), + id_gostr3412_2015_magma_ctracpkm: Gost341215EncryptionParameters(), + id_gostr3412_2015_kuznyechik_ctracpkm: Gost341215EncryptionParameters(), + id_gostr3412_2015_magma_ctracpkm_omac: Gost341215EncryptionParameters(), + id_gostr3412_2015_kuznyechik_ctracpkm_omac: Gost341215EncryptionParameters(), + }), ))), ("parameters", Any(optional=True)), ) @@ -214,13 +273,68 @@ class EncryptedContentInfo(Sequence): ) +class Digest(OctetString): + pass + + +class AttributeValue(Any): + pass + + +class AttributeValues(SetOf): + schema = AttributeValue() + + +class EncryptedMac(OctetString): + pass + + +class Attribute(Sequence): + schema = ( + ("attrType", ObjectIdentifier(defines=( + (("attrValues",), { + id_contentType: ObjectIdentifier(), + id_messageDigest: Digest(), + id_cms_mac_attr: EncryptedMac(), + },), + ))), + ("attrValues", AttributeValues()), + ) + + +class UnprotectedAttributes(SetOf): + schema = Attribute() + bounds = (1, float("+inf")) + + +class CertificateChoices(Choice): + schema = ( + ("certificate", Certificate()), + # ("extendedCertificate", OctetString(impl=tag_ctxp(0))), + # ("v1AttrCert", AttributeCertificateV1(impl=tag_ctxc(1))), # V1 is osbolete + # ("v2AttrCert", AttributeCertificateV2(impl=tag_ctxc(2))), + # ("other", OtherCertificateFormat(impl=tag_ctxc(3))), + ) + + +class CertificateSet(SetOf): + schema = CertificateChoices() + + +class OriginatorInfo(Sequence): + schema = ( + ("certs", CertificateSet(impl=tag_ctxc(0), optional=True)), + # ("crls", RevocationInfoChoices(impl=tag_ctxc(1), optional=True)), + ) + + class EnvelopedData(Sequence): schema = ( ("version", CMSVersion()), - # ("originatorInfo", OriginatorInfo(impl=tag_ctxc(0), optional=True)), + ("originatorInfo", OriginatorInfo(impl=tag_ctxc(0), optional=True)), ("recipientInfos", RecipientInfos()), ("encryptedContentInfo", EncryptedContentInfo()), - # ("unprotectedAttrs", UnprotectedAttributes(impl=tag_ctxc(1), optional=True)), + ("unprotectedAttrs", UnprotectedAttributes(impl=tag_ctxc(1), optional=True)), ) @@ -233,8 +347,8 @@ class EncapsulatedContentInfo(Sequence): class SignerIdentifier(Choice): schema = ( - ("issuerAndSerialNumber", Any()), - # ("subjectKeyIdentifier", SubjectKeyIdentifier(impl=tag_ctxp(0))), + ("issuerAndSerialNumber", IssuerAndSerialNumber()), + ("subjectKeyIdentifier", SubjectKeyIdentifier(impl=tag_ctxp(0))), ) @@ -254,12 +368,17 @@ class SignatureValue(OctetString): pass +class SignedAttributes(SetOf): + schema = Attribute() + bounds = (1, float("+inf")) + + class SignerInfo(Sequence): schema = ( ("version", CMSVersion()), ("sid", SignerIdentifier()), ("digestAlgorithm", DigestAlgorithmIdentifier()), - # ("signedAttrs", SignedAttributes(impl=tag_ctxc(0), optional=True)), + ("signedAttrs", SignedAttributes(impl=tag_ctxc(0), optional=True)), ("signatureAlgorithm", SignatureAlgorithmIdentifier()), ("signature", SignatureValue()), # ("unsignedAttrs", UnsignedAttributes(impl=tag_ctxc(1), optional=True)), @@ -270,20 +389,6 @@ class SignerInfos(SetOf): schema = SignerInfo() -class CertificateChoices(Choice): - schema = ( - ('certificate', Certificate()), - # ('extendedCertificate', ExtendedCertificate(impl=tag_ctxp(0))), - # ('v1AttrCert', AttributeCertificateV1(impl=tag_ctxc(1))), # V1 is osbolete - # ('v2AttrCert', AttributeCertificateV2(impl=tag_ctxc(2))), - # ('other', OtherCertificateFormat(impl=tag_ctxc(3))), - ) - - -class CertificateSet(SetOf): - schema = CertificateChoices() - - class SignedData(Sequence): schema = ( ("version", CMSVersion()), @@ -295,10 +400,6 @@ class SignedData(Sequence): ) -class Digest(OctetString): - pass - - class DigestedData(Sequence): schema = ( ("version", CMSVersion()), @@ -308,11 +409,20 @@ class DigestedData(Sequence): ) +class EncryptedData(Sequence): + schema = ( + ("version", CMSVersion()), + ("encryptedContentInfo", EncryptedContentInfo()), + ("unprotectedAttrs", UnprotectedAttributes(impl=tag_ctxc(1), optional=True)), + ) + + class ContentInfo(Sequence): schema = ( ("contentType", ContentType(defines=( (("content",), { id_digestedData: DigestedData(), + id_encryptedData: EncryptedData(), id_envelopedData: EnvelopedData(), id_signedData: SignedData(), }),