aboutsummaryrefslogtreecommitdiff
path: root/tests/test_rfc6960.py
blob: 33acbb9adb0377f86027c3e23eaced5d8778ee23 (plain)
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
#
# 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.type import univ

from pyasn1_modules import pem
from pyasn1_modules import rfc5280
from pyasn1_modules import rfc4055
from pyasn1_modules import rfc6960

try:
    import unittest2 as unittest

except ImportError:
    import unittest


class OCSPRequestTestCase(unittest.TestCase):
    ocsp_req_pem_text = """\
MGowaDBBMD8wPTAJBgUrDgMCGgUABBS3ZrMV9C5Dko03aH13cEZeppg3wgQUkqR1LKSevoFE63n8
isWVpesQdXMCBDXe9M+iIzAhMB8GCSsGAQUFBzABAgQSBBBjdJOiIW9EKJGELNNf/rdA
"""

    def setUp(self):
        self.asn1Spec = rfc6960.OCSPRequest()

    def testDerCodec(self):
        substrate = pem.readBase64fromText(self.ocsp_req_pem_text)
        asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec)
        assert not rest
        assert asn1Object.prettyPrint()
        assert der_encode(asn1Object) == substrate

        assert asn1Object['tbsRequest']['version'] == 0

        count = 0
        for extn in asn1Object['tbsRequest']['requestExtensions']:
            assert extn['extnID'] in rfc5280.certificateExtensionsMap.keys()
            ev, rest = der_decode(extn['extnValue'],
                asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']])
            assert not rest
            assert ev.prettyPrint()
            assert der_encode(ev) == extn['extnValue']
            count += 1

        assert count == 1

    def testOpenTypes(self):
        substrate = pem.readBase64fromText(self.ocsp_req_pem_text)
        asn1Object, rest = der_decode(substrate,
           asn1Spec=self.asn1Spec,
           decodeOpenTypes=True)
        assert not rest
        assert asn1Object.prettyPrint()
        assert der_encode(asn1Object) == substrate

        assert asn1Object['tbsRequest']['version'] == 0
        for req in  asn1Object['tbsRequest']['requestList']:
            ha = req['reqCert']['hashAlgorithm']
            assert ha['algorithm'] == rfc4055.id_sha1
            assert ha['parameters'] == univ.Null("")


class OCSPResponseTestCase(unittest.TestCase):
    ocsp_resp_pem_text = """\
MIIEvQoBAKCCBLYwggSyBgkrBgEFBQcwAQEEggSjMIIEnzCCAQ+hgYAwfjELMAkGA1UEBhMCQVUx
EzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEV
MBMGA1UEAxMMc25tcGxhYnMuY29tMSAwHgYJKoZIhvcNAQkBFhFpbmZvQHNubXBsYWJzLmNvbRgP
MjAxMjA0MTExNDA5MjJaMFQwUjA9MAkGBSsOAwIaBQAEFLdmsxX0LkOSjTdofXdwRl6mmDfCBBSS
pHUspJ6+gUTrefyKxZWl6xB1cwIENd70z4IAGA8yMDEyMDQxMTE0MDkyMlqhIzAhMB8GCSsGAQUF
BzABAgQSBBBjdJOiIW9EKJGELNNf/rdAMA0GCSqGSIb3DQEBBQUAA4GBADk7oRiCy4ew1u0N52QL
RFpW+tdb0NfkV2Xyu+HChKiTThZPr9ZXalIgkJ1w3BAnzhbB0JX/zq7Pf8yEz/OrQ4GGH7HyD3Vg
PkMu+J6I3A2An+bUQo99AmCbZ5/tSHtDYQMQt3iNbv1fk0yvDm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    def setUp(self):
        self.asn1Spec = rfc6960.OCSPResponse()

    def testDerCodec(self):
        substrate = pem.readBase64fromText(self.ocsp_resp_pem_text)
        asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec)
        assert not rest
        assert asn1Object.prettyPrint()
        assert der_encode(asn1Object) == substrate

        assert asn1Object['responseStatus'] == 0
        rb = asn1Object['responseBytes']
        assert rb['responseType'] in rfc6960.ocspResponseMap.keys()
        resp, rest = der_decode(rb['response'],
                asn1Spec=rfc6960.ocspResponseMap[rb['responseType']])
        assert not rest
        assert resp.prettyPrint()
        assert der_encode(resp) == rb['response']

        resp['tbsResponseData']['version'] == 0
        count = 0
        for extn in resp['tbsResponseData']['responseExtensions']:
            assert extn['extnID'] in rfc5280.certificateExtensionsMap.keys()
            ev, rest = der_decode(extn['extnValue'],
                asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']])
            assert not rest
            assert ev.prettyPrint()
            assert der_encode(ev) == extn['extnValue']
            count += 1

        assert count == 1

    def testOpenTypes(self):
        substrate = pem.readBase64fromText(self.ocsp_resp_pem_text)
        asn1Object, rest = der_decode(substrate,
            asn1Spec=self.asn1Spec,
            decodeOpenTypes=True)
        assert not rest
        assert asn1Object.prettyPrint()
        assert der_encode(asn1Object) == substrate

        assert asn1Object['responseStatus'] == 0
        rb = asn1Object['responseBytes']
        assert rb['responseType'] in rfc6960.ocspResponseMap.keys()
        resp, rest = der_decode(rb['response'],
                asn1Spec=rfc6960.ocspResponseMap[rb['responseType']],
                decodeOpenTypes=True)
        assert not rest
        assert resp.prettyPrint()
        assert der_encode(resp) == rb['response']

        resp['tbsResponseData']['version'] == 0
        for rdn in resp['tbsResponseData']['responderID']['byName']['rdnSequence']:
            for attr in rdn:
                if attr['type'] == rfc5280.id_emailAddress:
                    assert attr['value'] == 'info@snmplabs.com'

        for r in resp['tbsResponseData']['responses']:
            ha = r['certID']['hashAlgorithm']
            assert ha['algorithm'] == rfc4055.id_sha1
            assert ha['parameters'] == univ.Null("")


suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])

if __name__ == '__main__':
    import sys

    result = unittest.TextTestRunner(verbosity=2).run(suite)
    sys.exit(not result.wasSuccessful())