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.decoder import decode as der_decode
12from pyasn1.codec.der.encoder import encode as der_encode
13
14from pyasn1_modules import pem
15from pyasn1_modules import rfc6402
16from pyasn1_modules import rfc7894
17
18try:
19    import unittest2 as unittest
20except ImportError:
21    import unittest
22
23
24class AlternativeChallengePasswordTestCase(unittest.TestCase):
25    otp_pem_text = """\
26MIICsjCCAZwCAQAwJDELMAkGA1UEBhMCVVMxFTATBgNVBAMTDDRUUzJWMk5MWEE2
27WjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKmF0oUj5+1rBB+pUO8X
287FPxer+1BhWOa54RTSucJmBaLx0H95qNaBCcctNDl1kcmIro/a0zMcEvj5Do29vQ
29lStJdTeJ/B3X4qzOGShupxJcAhCreRZjN6Yz3T9z0zJ8OPnRvJOzcSiIzlubc9lK
30Cpq4U0UsCLLfymOgL9NH4lZi96J+PFuJr0J+rTY38076U2jcPqNq5/L/d6NV9Sz2
31IVOvCK1kqP/nElJVibIQZvj9YESLUKyVAfTNxLj3+IpioOOv2dT3kB9wdi4plAVi
32UFEUvED1okRrI29+LdPV1UXglOCksyJIIw+DgDtutDE5Co6QkTNURFEdKIV9Sg13
33zEECAwEAAaBLMBkGCyqGSIb3DQEJEAI4MQoTCDkwNTAzODQ2MC4GCSqGSIb3DQEJ
34DjEhMB8wHQYDVR0OBBYEFBj12LVowM16Ed0D+AmoElKNYP/kMAsGCSqGSIb3DQEB
35CwOCAQEAZZdDWKejs3UVfgZI3R9cMWGijmscVeZrjwFVkn7MI9pEDZ2aS1QaRYjY
361cu9j3i+LQp9LWPIW/ztYk11e/OcZp3fo8pZ+MT66n7YTWfDXNkqqA5xmI84DMEx
37/cqenyzOBZWqpZGx7eyM9BtnrdeJ0r2qSc7LYU25FbIQFJJf8IvgMAXWMs50fvs2
38Gzns447x952se2ReQ3vYhXdHvYYcgAZfSJZvK+nCmhzzqowv5p15Y5S+IHpBSXTO
39a1qhNW4cjdicQZUeQ2R5kiuwZ+8vHaq9jKxAEk0hBeqG6RQaxvNOBQhHtTLNGw/C
40NmaF8Y2Sl/MgvC5tjs0Ck0/r3lsoLQ==
41"""
42
43    def setUp(self):
44        self.asn1Spec = rfc6402.CertificationRequest()
45
46    def testDerCodec(self):
47        substrate = pem.readBase64fromText(self.otp_pem_text)
48        asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec)
49        assert not rest
50        assert asn1Object.prettyPrint()
51        assert der_encode(asn1Object) == substrate
52
53        assert asn1Object['certificationRequestInfo']['version'] == 0
54
55        for attr in asn1Object['certificationRequestInfo']['attributes']:
56            assert attr['attrType'] in rfc6402.cmcControlAttributesMap.keys()
57            av, rest = der_decode(attr['attrValues'][0],
58                rfc6402.cmcControlAttributesMap[attr['attrType']])
59            assert not rest
60            assert der_encode(av) == attr['attrValues'][0]
61
62            if attr['attrType'] == rfc7894.id_aa_otpChallenge:
63                assert av['printableString'] == '90503846'
64
65    def testOpenTypes(self):
66        substrate = pem.readBase64fromText(self.otp_pem_text)
67        asn1Object, rest = der_decode(substrate,
68            asn1Spec=self.asn1Spec,
69            decodeOpenTypes=True)
70        assert not rest
71        assert asn1Object.prettyPrint()
72        assert der_encode(asn1Object) == substrate
73
74        for attr in asn1Object['certificationRequestInfo']['attributes']:
75            assert attr['attrType'] in rfc6402.cmcControlAttributesMap.keys()
76            if attr['attrType'] == rfc7894.id_aa_otpChallenge:
77                assert attr['attrValues'][0]['printableString'] == '90503846'
78
79
80suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
81
82if __name__ == '__main__':
83    import sys
84
85    result = unittest.TextTestRunner(verbosity=2).run(suite)
86    sys.exit(not result.wasSuccessful())