• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# This file is part of pyasn1-modules software.
3#
4# Created by Russ Housley
5# Copyright (c) 2019, Vigil Security, LLC
6# License: http://snmplabs.com/pyasn1/license.html
7#
8
9import sys
10
11from pyasn1.codec.der import decoder as der_decoder
12from pyasn1.codec.der import encoder as der_encoder
13
14from pyasn1.type import univ
15
16from pyasn1_modules import pem
17from pyasn1_modules import rfc5280
18from pyasn1_modules import rfc4055
19
20try:
21    import unittest2 as unittest
22except ImportError:
23    import unittest
24
25
26class PSSDefautTestCase(unittest.TestCase):
27    pss_default_pem_text = "MAsGCSqGSIb3DQEBCg=="
28
29    def setUp(self):
30        self.asn1Spec = rfc5280.AlgorithmIdentifier()
31
32    def testDerCodec(self):
33        substrate = pem.readBase64fromText(self.pss_default_pem_text)
34        asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec)
35        assert not rest
36        assert asn1Object.prettyPrint()
37        assert asn1Object[0] == rfc4055.id_RSASSA_PSS
38        assert der_encoder.encode(asn1Object) == substrate
39        assert substrate == der_encoder.encode(asn1Object)
40
41    def testOpenTypes(self):
42        substrate = pem.readBase64fromText(self.pss_default_pem_text)
43        asn1Object, rest = der_decoder.decode(substrate,
44                                              asn1Spec=self.asn1Spec,
45                                              decodeOpenTypes=True)
46        assert not rest
47        assert asn1Object.prettyPrint()
48        assert der_encoder.encode(asn1Object) == substrate
49        assert not asn1Object['parameters'].hasValue()
50
51
52class PSSSHA512TestCase(unittest.TestCase):
53    pss_sha512_pem_text = "MDwGCSqGSIb3DQEBCjAvoA8wDQYJYIZIAWUDBAIDBQChHDAaBgkqhkiG9w0BAQgwDQYJYIZIAWUDBAIDBQA="
54
55    def setUp(self):
56        self.asn1Spec = rfc5280.AlgorithmIdentifier()
57
58    def testDerCodec(self):
59        substrate = pem.readBase64fromText(self.pss_sha512_pem_text)
60        asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec)
61        assert not rest
62        assert asn1Object.prettyPrint()
63        assert asn1Object[0] == rfc4055.id_RSASSA_PSS
64        assert der_encoder.encode(asn1Object) == substrate
65        assert substrate == der_encoder.encode(asn1Object)
66
67    def testOpenTypes(self):
68        substrate = pem.readBase64fromText(self.pss_sha512_pem_text)
69        asn1Object, rest = der_decoder.decode(substrate,
70                                              asn1Spec=self.asn1Spec,
71                                              decodeOpenTypes=True)
72        assert not rest
73        assert asn1Object.prettyPrint()
74        assert der_encoder.encode(asn1Object) == substrate
75        assert asn1Object['parameters'].hasValue()
76        assert asn1Object['parameters']['saltLength'] == 20
77
78
79class OAEPDefautTestCase(unittest.TestCase):
80    oaep_default_pem_text = "MAsGCSqGSIb3DQEBBw=="
81
82    def setUp(self):
83        self.asn1Spec = rfc5280.AlgorithmIdentifier()
84
85    def testDerCodec(self):
86        substrate = pem.readBase64fromText(self.oaep_default_pem_text)
87        asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec)
88        assert not rest
89        assert asn1Object.prettyPrint()
90        assert asn1Object[0] == rfc4055.id_RSAES_OAEP
91        assert der_encoder.encode(asn1Object) == substrate
92        assert substrate == der_encoder.encode(asn1Object)
93
94    def testOpenTypes(self):
95        substrate = pem.readBase64fromText(self.oaep_default_pem_text)
96        asn1Object, rest = der_decoder.decode(substrate,
97                                              asn1Spec=self.asn1Spec,
98                                              decodeOpenTypes=True)
99        assert not rest
100        assert asn1Object.prettyPrint()
101        assert der_encoder.encode(asn1Object) == substrate
102        assert not asn1Object['parameters'].hasValue()
103
104
105class OAEPSHA256TestCase(unittest.TestCase):
106    oaep_sha256_pem_text = "MDwGCSqGSIb3DQEBBzAvoA8wDQYJYIZIAWUDBAIBBQChHDAaBgkqhkiG9w0BAQgwDQYJYIZIAWUDBAIBBQA="
107
108    def setUp(self):
109        self.asn1Spec = rfc5280.AlgorithmIdentifier()
110
111    def testDerCodec(self):
112        substrate = pem.readBase64fromText(self.oaep_sha256_pem_text)
113        asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec)
114        assert not rest
115        assert asn1Object.prettyPrint()
116        assert asn1Object[0] == rfc4055.id_RSAES_OAEP
117        assert der_encoder.encode(asn1Object) == substrate
118        assert substrate == der_encoder.encode(asn1Object)
119
120    def testOpenTypes(self):
121        substrate = pem.readBase64fromText(self.oaep_sha256_pem_text)
122        asn1Object, rest = der_decoder.decode(substrate,
123                                              asn1Spec=self.asn1Spec,
124                                              decodeOpenTypes=True)
125        assert not rest
126        assert asn1Object.prettyPrint()
127        assert der_encoder.encode(asn1Object) == substrate
128        assert asn1Object['parameters'].hasValue()
129        oaep_p = asn1Object['parameters']
130        assert oaep_p['hashFunc']['parameters'] == univ.Null("")
131        assert oaep_p['maskGenFunc']['parameters']['parameters'] == univ.Null("")
132
133
134class OAEPFullTestCase(unittest.TestCase):
135    oaep_full_pem_text = "MFMGCSqGSIb3DQEBBzBGoA8wDQYJYIZIAWUDBAICBQChHDAaBgkqhkiG9w0BAQgwDQYJYIZIAWUDBAICBQCiFTATBgkqhkiG9w0BAQkEBmZvb2Jhcg=="
136
137    def setUp(self):
138        self.asn1Spec = rfc5280.AlgorithmIdentifier()
139
140    def testDerCodec(self):
141        substrate = pem.readBase64fromText(self.oaep_full_pem_text)
142        asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec)
143        assert not rest
144        assert asn1Object.prettyPrint()
145        assert asn1Object[0] == rfc4055.id_RSAES_OAEP
146        assert der_encoder.encode(asn1Object) == substrate
147        assert substrate == der_encoder.encode(asn1Object)
148
149    def testOpenTypes(self):
150        substrate = pem.readBase64fromText(self.oaep_full_pem_text)
151        asn1Object, rest = der_decoder.decode(substrate,
152                                              asn1Spec=self.asn1Spec,
153                                              decodeOpenTypes=True)
154        assert not rest
155        assert asn1Object.prettyPrint()
156        assert der_encoder.encode(asn1Object) == substrate
157        assert asn1Object['parameters'].hasValue()
158        oaep_p = asn1Object['parameters']
159        assert oaep_p['hashFunc']['parameters'] == univ.Null("")
160        assert oaep_p['maskGenFunc']['parameters']['parameters'] == univ.Null("")
161        assert oaep_p['pSourceFunc']['parameters'] == univ.OctetString(value='foobar')
162
163
164suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
165
166if __name__ == '__main__':
167    import sys
168
169    result = unittest.TextTestRunner(verbosity=2).run(suite)
170    sys.exit(not result.wasSuccessful())
171