From 96451d80749a1da28417d2ed5324c0274e4c1865 Mon Sep 17 00:00:00 2001 From: Russ Housley Date: Thu, 23 May 2019 16:41:06 -0400 Subject: Add support for RFC 3274 (#36) * Add support for RFC 3274 --- pyasn1_modules/rfc3274.py | 48 +++++++++++++++++++++++++++++++++++++ pyasn1_modules/rfc3565.py | 4 ++-- tests/test_rfc3274.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 pyasn1_modules/rfc3274.py create mode 100644 tests/test_rfc3274.py diff --git a/pyasn1_modules/rfc3274.py b/pyasn1_modules/rfc3274.py new file mode 100644 index 0000000..bf8a7a6 --- /dev/null +++ b/pyasn1_modules/rfc3274.py @@ -0,0 +1,48 @@ +# +# This file is part of pyasn1-modules software. +# +# Created by Russ Housley with assistance from asn1ate v.0.6.0. +# +# Copyright (c) 2019, Vigil Security, LLC +# License: http://snmplabs.com/pyasn1/license.html +# +# CMS Compressed Data Content Type +# +# ASN.1 source from: +# https://www.rfc-editor.org/rfc/rfc3274.txt +# + +from pyasn1.type import namedtype +from pyasn1.type import univ + +from pyasn1_modules import rfc5280 +from pyasn1_modules import rfc5652 + + +class CompressionAlgorithmIdentifier(rfc5280.AlgorithmIdentifier): + pass + + +# The CMS Compressed Data Content Type + +id_ct_compressedData = univ.ObjectIdentifier('1.2.840.113549.1.9.16.1.9') + +class CompressedData(univ.Sequence): + pass + +CompressedData.componentType = namedtype.NamedTypes( + namedtype.NamedType('version', rfc5652.CMSVersion()), # Always set to 0 + namedtype.NamedType('compressionAlgorithm', CompressionAlgorithmIdentifier()), + namedtype.NamedType('encapContentInfo', rfc5652.EncapsulatedContentInfo()) +) + + +# Algorithm identifier for the zLib Compression Algorithm +# This includes cpa_zlibCompress as defined in RFC 6268, +# from https://www.rfc-editor.org/rfc/rfc6268.txt + +id_alg_zlibCompress = univ.ObjectIdentifier('1.2.840.113549.1.9.16.3.8') + +cpa_zlibCompress = rfc5280.AlgorithmIdentifier() +cpa_zlibCompress['algorithm'] = id_alg_zlibCompress +# cpa_zlibCompress['parameters'] are absent \ No newline at end of file diff --git a/pyasn1_modules/rfc3565.py b/pyasn1_modules/rfc3565.py index 2cac273..c4b742d 100644 --- a/pyasn1_modules/rfc3565.py +++ b/pyasn1_modules/rfc3565.py @@ -1,8 +1,8 @@ # Copyright (c) 2019, Vigil Security, LLC # License: http://snmplabs.com/pyasn1/license.html # -# Use of the Elliptic Curve Diffie-Hellman Key Agreement Algorithm -# with X25519 and X448 in the Cryptographic Message Syntax (CMS) +# Use of the Advanced Encryption Standard (AES) Encryption +# Algorithm in the Cryptographic Message Syntax (CMS) # # ASN.1 source from: # https://www.rfc-editor.org/rfc/rfc3565.txt diff --git a/tests/test_rfc3274.py b/tests/test_rfc3274.py new file mode 100644 index 0000000..fbf44a2 --- /dev/null +++ b/tests/test_rfc3274.py @@ -0,0 +1,61 @@ +# +# This file is part of pyasn1-modules software. +# +# Created by Russ Housley +# Copyright (c) 2019, Vigil Security, LLC +# License: http://snmplabs.com/pyasn1/license.html +# + +import sys + +from pyasn1.codec.der.decoder import decode as der_decode +from pyasn1.codec.der.encoder import encode as der_encode + +from pyasn1_modules import pem +from pyasn1_modules import rfc3274 +from pyasn1_modules import rfc5652 + +try: + import unittest2 as unittest +except ImportError: + import unittest + + +class CompressedDataTestCase(unittest.TestCase): + compressed_data_pem_text = """\ +MIIB7wYLKoZIhvcNAQkQAQmgggHeMIIB2gIBADANBgsqhkiG9w0BCRADCDCCAcQG +CSqGSIb3DQEHAaCCAbUEggGxeJxVksGO1DAQRO/+ir4xK4VlNSAhcUPRrgRiLgw/ +0Il7Egu7bdntMOHraSezMJyixOWq19XpIwuxvP2xJvoEQld5lzw6Nub7Sw/vjx8/ +dJDq4F2ZyYJj+FqZ4Pj0dOzA0sUxFUC4xBxQ2gNqcTzBGEPKVApZY1EQsKn6vCaJ +U8Y0uxFOeowTwXllwSsc+tP5Qe9tOCCK8wjQ32zUcvcZSDMIJCOX4PQgMqQcF2c3 +Dq5hoAzxAmgXVN+JSqfUo6+2YclMhrwLjlHaVRVutplsZYs8rvBL2WblqN7CTD4B +MqAIjj8pd1ASUXMyNbXccWeDYd0sxlsGYIhVp3i1l6jgr3qtUeUehbIpQqnAoVSN +1IqKm7hZaI3EY2tLIR86RbD//ONCGb2HsPdnivvdqvrsZY51mlu+NjTjQhpKWz0p +FvRlWw9ae7+fVgKKie0SeFpIZYemoyuG5HUS2QY6fTk9N6zz+dsuUyr9Xghs5Ddi +1LbZbVoNHDyFNv19jL7qiv9uuLK/XTD3Kqct1JS822vS8vWXpMzYBtal/083rMap +XQ7u2qbaKFtZ7V96NH8ApkUFkg== +""" + + def setUp(self): + self.asn1Spec = rfc5652.ContentInfo() + + def testDerCodec(self): + substrate = pem.readBase64fromText(self.compressed_data_pem_text) + asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec) + assert not rest + assert asn1Object.prettyPrint() + assert der_encode(asn1Object) == substrate + + assert asn1Object['contentType'] == rfc3274.id_ct_compressedData + cd, rest = der_decode(asn1Object['content'], asn1Spec=rfc3274.CompressedData()) + assert not rest + assert cd.prettyPrint() + assert der_encode(cd) == asn1Object['content'] + + assert cd['compressionAlgorithm']['algorithm'] == rfc3274.id_alg_zlibCompress + assert cd['encapContentInfo']['eContentType'] == rfc5652.id_data + +suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) + +if __name__ == '__main__': + unittest.TextTestRunner(verbosity=2).run(suite) -- cgit v1.2.3