|
| 1 | +# |
| 2 | +# This file is part of pyasn1-modules software. |
| 3 | +# |
| 4 | +# Copyright (c) 2020, Vigil Security, LLC |
| 5 | +# License: http://snmplabs.com/pyasn1/license.html |
| 6 | +# |
| 7 | +import sys |
| 8 | +import unittest |
| 9 | + |
| 10 | +from pyasn1.codec.der.decoder import decode as der_decoder |
| 11 | +from pyasn1.codec.der.encoder import encode as der_encoder |
| 12 | + |
| 13 | +from pyasn1_modules import pem |
| 14 | +from pyasn1_modules import rfc5652 |
| 15 | +from pyasn1_modules import rfc8769 |
| 16 | + |
| 17 | + |
| 18 | +class CBORContentTestCase(unittest.TestCase): |
| 19 | + pem_text = """\ |
| 20 | +MIIEHwYJKoZIhvcNAQcCoIIEEDCCBAwCAQMxDTALBglghkgBZQMEAgIwIQYLKoZIhvcNAQkQ |
| 21 | +ASygEgQQgw9kUnVzc/tADzMzMzMzM6CCAnwwggJ4MIIB/qADAgECAgkApbNUKBuwbjswCgYI |
| 22 | +KoZIzj0EAwMwPzELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAlZBMRAwDgYDVQQHDAdIZXJuZG9u |
| 23 | +MREwDwYDVQQKDAhCb2d1cyBDQTAeFw0xOTA1MjkxNDQ1NDFaFw0yMDA1MjgxNDQ1NDFaMHAx |
| 24 | +CzAJBgNVBAYTAlVTMQswCQYDVQQIEwJWQTEQMA4GA1UEBxMHSGVybmRvbjEQMA4GA1UEChMH |
| 25 | +RXhhbXBsZTEOMAwGA1UEAxMFQWxpY2UxIDAeBgkqhkiG9w0BCQEWEWFsaWNlQGV4YW1wbGUu |
| 26 | +Y29tMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE+M2fBy/sRA6V1pKFqecRTE8+LuAHtZxes1wm |
| 27 | +JZrBBg+bz7uYZfYQxI3dVB0YCSD6Mt3yXFlnmfBRwoqyArbjIBYrDbHBv2k8Csg2DhQ7qs/w |
| 28 | +to8hMKoFgkcscqIbiV7Zo4GUMIGRMAsGA1UdDwQEAwIHgDBCBglghkgBhvhCAQ0ENRYzVGhp |
| 29 | +cyBjZXJ0aWZpY2F0ZSBjYW5ub3QgYmUgdHJ1c3RlZCBmb3IgYW55IHB1cnBvc2UuMB0GA1Ud |
| 30 | +DgQWBBTEuloOPnrjPIGw9AKqaLsW4JYONTAfBgNVHSMEGDAWgBTyNds0BNqlVfK9aQOZsGLs |
| 31 | +4hUIwTAKBggqhkjOPQQDAwNoADBlAjBjuR/RNbgL3kRhmn+PJTeKaL9sh/oQgHOYTgLmSnv3 |
| 32 | ++NDCkhfKuMNoo/tHrkmihYgCMQC94MaerDIrQpi0IDh+v0QSAv9rMife8tClafXWtDwwL8MS |
| 33 | +7oAh0ymT446Uizxx3PUxggFTMIIBTwIBATBMMD8xCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJW |
| 34 | +QTEQMA4GA1UEBwwHSGVybmRvbjERMA8GA1UECgwIQm9ndXMgQ0ECCQCls1QoG7BuOzALBglg |
| 35 | +hkgBZQMEAgKgezAaBgkqhkiG9w0BCQMxDQYLKoZIhvcNAQkQASwwHAYJKoZIhvcNAQkFMQ8X |
| 36 | +DTIwMDExNDIyMjIxNVowPwYJKoZIhvcNAQkEMTIEMADSWdHn4vsesm9XnjJq1WxkoV6EtD+f |
| 37 | +qDAs1JEpZMZ+n8AtUxvC5SFobYpGCl+fsDAKBggqhkjOPQQDAwRmMGQCMGclPwvZLwVJqgON |
| 38 | +mOfnxSF8Cqn3AC+ZFBg7VplspiuhKPNIyu3IofqZjCxw0TzSpAIwEK0JxNlY28KDb5te0iN6 |
| 39 | +I2hw+am26W+PRyltVVGUAISHM2kA4tG39HcxEQi+6HJx |
| 40 | +""" |
| 41 | + |
| 42 | + def testDerCodec(self): |
| 43 | + substrate = pem.readBase64fromText(self.pem_text) |
| 44 | + |
| 45 | + layers = { } |
| 46 | + layers.update(rfc5652.cmsContentTypesMap) |
| 47 | + |
| 48 | + getNextLayer = { |
| 49 | + rfc5652.id_ct_contentInfo: lambda x: x['contentType'], |
| 50 | + rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContentType'], |
| 51 | + } |
| 52 | + |
| 53 | + getNextSubstrate = { |
| 54 | + rfc5652.id_ct_contentInfo: lambda x: x['content'], |
| 55 | + rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContent'], |
| 56 | + } |
| 57 | + |
| 58 | + next_layer = rfc5652.id_ct_contentInfo |
| 59 | + while next_layer in layers: |
| 60 | + asn1Object, rest = der_decoder( |
| 61 | + substrate, asn1Spec=layers[next_layer]) |
| 62 | + |
| 63 | + self.assertFalse(rest) |
| 64 | + self.assertTrue(asn1Object.prettyPrint()) |
| 65 | + self.assertEqual(substrate, der_encoder(asn1Object)) |
| 66 | + |
| 67 | + substrate = getNextSubstrate[next_layer](asn1Object) |
| 68 | + next_layer = getNextLayer[next_layer](asn1Object) |
| 69 | + |
| 70 | + self.assertEqual(rfc8769.id_ct_cbor, next_layer) |
| 71 | + |
| 72 | + |
| 73 | +class CBORSequenceContentTestCase(unittest.TestCase): |
| 74 | + pem_text = """\ |
| 75 | +MIIEKQYJKoZIhvcNAQcCoIIEGjCCBBYCAQMxDTALBglghkgBZQMEAgIwKgYLKoZIhvcNAQkQ |
| 76 | +AS2gGwQZgw9kUnVzc/tADzMzMzMzM6MDCSD1YWFhYqCCAnwwggJ4MIIB/qADAgECAgkApbNU |
| 77 | +KBuwbjswCgYIKoZIzj0EAwMwPzELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAlZBMRAwDgYDVQQH |
| 78 | +DAdIZXJuZG9uMREwDwYDVQQKDAhCb2d1cyBDQTAeFw0xOTA1MjkxNDQ1NDFaFw0yMDA1Mjgx |
| 79 | +NDQ1NDFaMHAxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJWQTEQMA4GA1UEBxMHSGVybmRvbjEQ |
| 80 | +MA4GA1UEChMHRXhhbXBsZTEOMAwGA1UEAxMFQWxpY2UxIDAeBgkqhkiG9w0BCQEWEWFsaWNl |
| 81 | +QGV4YW1wbGUu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46fsWc5tA+mMARTrxZjSXsDneeAWpw== |
| 95 | +""" |
| 96 | + |
| 97 | + def testDerCodec(self): |
| 98 | + substrate = pem.readBase64fromText(self.pem_text) |
| 99 | + |
| 100 | + layers = { } |
| 101 | + layers.update(rfc5652.cmsContentTypesMap) |
| 102 | + |
| 103 | + getNextLayer = { |
| 104 | + rfc5652.id_ct_contentInfo: lambda x: x['contentType'], |
| 105 | + rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContentType'], |
| 106 | + } |
| 107 | + |
| 108 | + getNextSubstrate = { |
| 109 | + rfc5652.id_ct_contentInfo: lambda x: x['content'], |
| 110 | + rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContent'], |
| 111 | + } |
| 112 | + |
| 113 | + next_layer = rfc5652.id_ct_contentInfo |
| 114 | + while next_layer in layers: |
| 115 | + asn1Object, rest = der_decoder( |
| 116 | + substrate, asn1Spec=layers[next_layer]) |
| 117 | + |
| 118 | + self.assertFalse(rest) |
| 119 | + self.assertTrue(asn1Object.prettyPrint()) |
| 120 | + self.assertEqual(substrate, der_encoder(asn1Object)) |
| 121 | + |
| 122 | + substrate = getNextSubstrate[next_layer](asn1Object) |
| 123 | + next_layer = getNextLayer[next_layer](asn1Object) |
| 124 | + |
| 125 | + self.assertEqual(rfc8769.id_ct_cborSequence, next_layer) |
| 126 | + |
| 127 | + |
| 128 | +suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) |
| 129 | + |
| 130 | +if __name__ == '__main__': |
| 131 | + import sys |
| 132 | + |
| 133 | + result = unittest.TextTestRunner(verbosity=2).run(suite) |
| 134 | + sys.exit(not result.wasSuccessful()) |
0 commit comments