1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
#
# 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
import unittest
from pyasn1.codec.der.decoder import decode as der_decoder
from pyasn1.codec.der.encoder import encode as der_encoder
from pyasn1.type import univ
from pyasn1_modules import pem
from pyasn1_modules import rfc5083
from pyasn1_modules import rfc5652
from pyasn1_modules import rfc8696
class KeyTransPSKTestCase(unittest.TestCase):
key_trans_psk_pem_text = """\
MIICigYLKoZIhvcNAQkQARegggJ5MIICdQIBADGCAiekggIjBgsqhkiG9w0BCRANATCCAhIC
AQAEE3B0Zi1rbWM6MTM2MTQxMjIxMTIwDQYLKoZIhvcNAQkQAx0wCwYJYIZIAWUDBAEtMIIB
sDCCAawCAQKAFJ7rZ8m5WnTUTS8WOWaA6AG1y6ScMA0GCSqGSIb3DQEBAQUABIIBgKo/Hkhu
eoOdn1/cIEpt38NbEEdSC586IWcG+0l+ND9pcmQvvKvscpvFFVAjqLjvoXGatmSazr2Q4BVS
yWKm0JqlyVWEAhRsU7wNlD7zRAKI8+obWpU57gjEKs13D8gb1PI2YPZWajN1Ye+yHSF6h+fb
7YtaQepxTGHYF0LgHaAC8cqtgwIRW8N4Gnvl0Uuz+YEZXUX0I8fvJG6MKCEFzwHvfrfPb3rW
B8k7BHfekRpY+793JNrjSP2lY+W0fhqBN8dALDKGqlbUCyojMQkQiD/iXSBRbZWiJ1CE92iT
x7Ji9irq8rhYDNoDP2vghJUaepoZgIJwPWqhoTH+KRPqHTjLnnbi/TGzEdeO5h0C9Gc0DVzs
9OHvHknQ7mSxPT9xKMXGztVT+P3a9ct6TaMotpMqL9cuZxTYGpHMYNkLSUXFSadAGFrgP7QV
FGwC/Z/YomEzSLPgZi8HnVHsAGkJzXxmM/PJBu4dAXcKjEv/GgpmaS2B7gKHUpTyyAgdsBsy
2AQo6glHJQ+mbNUlWV5Sppqq3ojvzxsPEIq+KRBgORsc31kH82tAZ+RTQjA3BgkqhkiG9w0B
BwEwGwYJYIZIAWUDBAEuMA4EDMr+ur76ztut3sr4iIANmvLRbyFUf87+2bPvLQQMoOWSXMGE
4BckY8RM
"""
def setUp(self):
self.asn1Spec = rfc5652.ContentInfo()
def testDerCodec(self):
substrate = pem.readBase64fromText(self.key_trans_psk_pem_text)
asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
self.assertFalse(rest)
self.assertTrue(asn1Object.prettyPrint())
self.assertEqual(substrate, der_encoder(asn1Object))
self.assertEqual(
rfc5083.id_ct_authEnvelopedData, asn1Object['contentType'])
aed, rest = der_decoder(
asn1Object['content'],
asn1Spec=rfc5083.AuthEnvelopedData())
self.assertFalse(rest)
self.assertTrue(aed.prettyPrint())
self.assertEqual(asn1Object['content'], der_encoder(aed))
self.assertEqual(0, aed['version'])
ri = aed['recipientInfos'][0]
self.assertEqual(rfc8696.id_ori_keyTransPSK, ri['ori']['oriType'])
ktpsk, rest = der_decoder(
ri['ori']['oriValue'],
asn1Spec=rfc8696.KeyTransPSKRecipientInfo())
self.assertFalse(rest)
self.assertTrue(ktpsk.prettyPrint())
self.assertEqual(ri['ori']['oriValue'], der_encoder(ktpsk))
self.assertEqual(0, ktpsk['version'])
ktri = ktpsk['ktris'][0]
self.assertEqual(2, ktri['version'])
def testOtherRecipientInfoMap(self):
substrate = pem.readBase64fromText(self.key_trans_psk_pem_text)
asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
self.assertFalse(rest)
self.assertTrue(asn1Object.prettyPrint())
self.assertEqual(substrate, der_encoder(asn1Object))
self.assertEqual(
rfc5083.id_ct_authEnvelopedData, asn1Object['contentType'])
aed, rest = der_decoder(
asn1Object['content'],
asn1Spec=rfc5083.AuthEnvelopedData())
self.assertFalse(rest)
self.assertTrue(aed.prettyPrint())
self.assertEqual(asn1Object['content'], der_encoder(aed))
self.assertEqual(0, aed['version'])
ri = aed['recipientInfos'][0]
self.assertIn(ri['ori']['oriType'], rfc5652.otherRecipientInfoMap)
ori, rest = der_decoder(
ri['ori']['oriValue'],
asn1Spec=rfc5652.otherRecipientInfoMap[ri['ori']['oriType']])
self.assertFalse(rest)
self.assertTrue(ori.prettyPrint())
self.assertEqual(ri['ori']['oriValue'], der_encoder(ori))
class KeyAgreePSKTestCase(unittest.TestCase):
key_agree_psk_pem_text = """\
MIIBRwYLKoZIhvcNAQkQARegggE2MIIBMgIBADGB5aSB4gYLKoZIhvcNAQkQDQIwgdICAQAE
FHB0Zi1rbWM6MjE2ODQwMTEwMTIxoFWhUzATBgYrgQQBCwEGCWCGSAFlAwQBLQM8AAQ5G0Em
Jk/2ks8sXY1kzbuG3Uu3ttWwQRXALFDJICjvYfr+yTpOQVkchm88FAh9MEkw4NKctokKNgps
MA0GCyqGSIb3DQEJEAMdMAsGCWCGSAFlAwQBLTBEMEKgFgQU6CGLmLi32Gtenr3IrrjE7NwF
xSkEKCKf4LReQAA+fYJE7Bt+f/ssjcoWw29XNyIlU6cSY6kr3giGamAtY/QwNwYJKoZIhvcN
AQcBMBsGCWCGSAFlAwQBLjAOBAzbrd7K+IjK/rq++s6ADfxtb4I+PtLSCdDG/88EDFUCYMQu
WylxlCbB/w==
"""
def setUp(self):
self.asn1Spec = rfc5652.ContentInfo()
def testDerCodec(self):
substrate = pem.readBase64fromText(self.key_agree_psk_pem_text)
asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
self.assertFalse(rest)
self.assertTrue(asn1Object.prettyPrint())
self.assertEqual(substrate, der_encoder(asn1Object))
self.assertEqual(
rfc5083.id_ct_authEnvelopedData, asn1Object['contentType'])
aed, rest = der_decoder(
asn1Object['content'],
asn1Spec=rfc5083.AuthEnvelopedData())
self.assertFalse(rest)
self.assertTrue(aed.prettyPrint())
self.assertEqual(asn1Object['content'], der_encoder(aed))
self.assertEqual(0, aed['version'])
ri = aed['recipientInfos'][0]
self.assertEqual(rfc8696.id_ori_keyAgreePSK, ri['ori']['oriType'])
kapsk, rest = der_decoder(
ri['ori']['oriValue'],
asn1Spec=rfc8696.KeyAgreePSKRecipientInfo())
self.assertFalse(rest)
self.assertTrue(kapsk.prettyPrint())
self.assertEqual(ri['ori']['oriValue'], der_encoder(kapsk))
self.assertEqual(0, kapsk['version'])
rek = kapsk['recipientEncryptedKeys'][0]
ski = rek['rid']['rKeyId']['subjectKeyIdentifier']
expected_ski = univ.OctetString(
hexValue='e8218b98b8b7d86b5e9ebdc8aeb8c4ecdc05c529')
self.assertEqual(expected_ski, ski)
def testOtherRecipientInfoMap(self):
substrate = pem.readBase64fromText(self.key_agree_psk_pem_text)
asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
self.assertFalse(rest)
self.assertTrue(asn1Object.prettyPrint())
self.assertEqual(substrate, der_encoder(asn1Object))
self.assertEqual(
rfc5083.id_ct_authEnvelopedData, asn1Object['contentType'])
aed, rest = der_decoder(
asn1Object['content'],
asn1Spec=rfc5083.AuthEnvelopedData())
self.assertFalse(rest)
self.assertTrue(aed.prettyPrint())
self.assertEqual(asn1Object['content'], der_encoder(aed))
self.assertEqual(0, aed['version'])
ri = aed['recipientInfos'][0]
self.assertIn(ri['ori']['oriType'], rfc5652.otherRecipientInfoMap)
ori, rest = der_decoder(
ri['ori']['oriValue'],
asn1Spec=rfc5652.otherRecipientInfoMap[ri['ori']['oriType']])
self.assertFalse(rest)
self.assertTrue(ori.prettyPrint())
self.assertEqual(ri['ori']['oriValue'], der_encoder(ori))
suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
if __name__ == '__main__':
unittest.TextTestRunner(verbosity=2).run(suite)
|