]> Cypherpunks.ru repositories - pygost.git/blob - pygost/test_x509.py
Simplify GOST3410Curve initialization
[pygost.git] / pygost / test_x509.py
1 # coding: utf-8
2 # PyGOST -- Pure Python GOST cryptographic functions library
3 # Copyright (C) 2015-2019 Sergey Matveev <stargrave@stargrave.org>
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 from base64 import b64decode
19 from unittest import skipIf
20 from unittest import TestCase
21
22 from pygost.gost3410 import CURVES
23 from pygost.gost3410 import prv_unmarshal
24 from pygost.gost3410 import pub_unmarshal
25 from pygost.gost3410 import public_key
26 from pygost.gost3410 import verify
27 from pygost.gost34112012256 import GOST34112012256
28 from pygost.gost34112012512 import GOST34112012512
29 from pygost.utils import hexdec
30
31 try:
32     from pyderasn import OctetString
33
34     from pygost.asn1schemas.oids import id_tc26_gost3410_2012_256
35     from pygost.asn1schemas.oids import id_tc26_gost3410_2012_512
36     from pygost.asn1schemas.x509 import Certificate
37 except ImportError:
38     pyderasn_exists = False
39 else:
40     pyderasn_exists = True
41
42
43 @skipIf(not pyderasn_exists, "PyDERASN dependency is required")
44 class TestCertificate(TestCase):
45     """Certificate test vectors from "Использования алгоритмов ГОСТ Р
46     34.10, ГОСТ Р 34.11 в профиле сертификата и списке отзыва
47     сертификатов (CRL) инфраструктуры открытых ключей X.509"
48     (TK26IOK.pdf)
49     """
50
51     def process_cert(self, curve_name, mode, hasher, prv_key_raw, cert_raw):
52         cert, tail = Certificate().decode(cert_raw, ctx={
53             "defines_by_path": (
54                 (
55                     (
56                         "tbsCertificate",
57                         "subjectPublicKeyInfo",
58                         "algorithm",
59                         "algorithm",
60                     ),
61                     (
62                         (
63                             ("..", "subjectPublicKey"),
64                             {
65                                 id_tc26_gost3410_2012_256: OctetString(),
66                                 id_tc26_gost3410_2012_512: OctetString(),
67                             },
68                         ),
69                     ),
70                 ),
71             ),
72         })
73         self.assertSequenceEqual(tail, b"")
74         curve = CURVES[curve_name]
75         prv_key = prv_unmarshal(prv_key_raw)
76         spk = cert["tbsCertificate"]["subjectPublicKeyInfo"]["subjectPublicKey"]
77         self.assertIsNotNone(spk.defined)
78         _, pub_key_raw = spk.defined
79         pub_key = pub_unmarshal(bytes(pub_key_raw), mode=mode)
80         self.assertSequenceEqual(pub_key, public_key(curve, prv_key))
81         self.assertTrue(verify(
82             curve,
83             pub_key,
84             hasher(cert["tbsCertificate"].encode()).digest()[::-1],
85             bytes(cert["signatureValue"]),
86             mode=mode,
87         ))
88
89     def test_256(self):
90         cert_raw = b64decode("""
91 MIICYjCCAg+gAwIBAgIBATAKBggqhQMHAQEDAjBWMSkwJwYJKoZIhvcNAQkBFhpH
92 b3N0UjM0MTAtMjAxMkBleGFtcGxlLmNvbTEpMCcGA1UEAxMgR29zdFIzNDEwLTIw
93 MTIgKDI1NiBiaXQpIGV4YW1wbGUwHhcNMTMxMTA1MTQwMjM3WhcNMzAxMTAxMTQw
94 MjM3WjBWMSkwJwYJKoZIhvcNAQkBFhpHb3N0UjM0MTAtMjAxMkBleGFtcGxlLmNv
95 bTEpMCcGA1UEAxMgR29zdFIzNDEwLTIwMTIgKDI1NiBiaXQpIGV4YW1wbGUwZjAf
96 BggqhQMHAQEBATATBgcqhQMCAiQABggqhQMHAQECAgNDAARAut/Qw1MUq9KPqkdH
97 C2xAF3K7TugHfo9n525D2s5mFZdD5pwf90/i4vF0mFmr9nfRwMYP4o0Pg1mOn5Rl
98 aXNYraOBwDCBvTAdBgNVHQ4EFgQU1fIeN1HaPbw+XWUzbkJ+kHJUT0AwCwYDVR0P
99 BAQDAgHGMA8GA1UdEwQIMAYBAf8CAQEwfgYDVR0BBHcwdYAU1fIeN1HaPbw+XWUz
100 bkJ+kHJUT0ChWqRYMFYxKTAnBgkqhkiG9w0BCQEWGkdvc3RSMzQxMC0yMDEyQGV4
101 YW1wbGUuY29tMSkwJwYDVQQDEyBHb3N0UjM0MTAtMjAxMiAoMjU2IGJpdCkgZXhh
102 bXBsZYIBATAKBggqhQMHAQEDAgNBAF5bm4BbARR6hJLEoWJkOsYV3Hd7kXQQjz3C
103 dqQfmHrz6TI6Xojdh/t8ckODv/587NS5/6KsM77vc6Wh90NAT2s=
104         """)
105         prv_key_raw = hexdec("BFCF1D623E5CDD3032A7C6EABB4A923C46E43D640FFEAAF2C3ED39A8FA399924")[::-1]
106         self.process_cert(
107             "id-GostR3410-2001-CryptoPro-XchA-ParamSet",
108             2001,
109             GOST34112012256,
110             prv_key_raw,
111             cert_raw,
112         )
113
114     def test_512(self):
115         cert_raw = b64decode("""
116 MIIC6DCCAlSgAwIBAgIBATAKBggqhQMHAQEDAzBWMSkwJwYJKoZIhvcNAQkBFhpH
117 b3N0UjM0MTAtMjAxMkBleGFtcGxlLmNvbTEpMCcGA1UEAxMgR29zdFIzNDEwLTIw
118 MTIgKDUxMiBiaXQpIGV4YW1wbGUwHhcNMTMxMDA0MDczNjA0WhcNMzAxMDAxMDcz
119 NjA0WjBWMSkwJwYJKoZIhvcNAQkBFhpHb3N0UjM0MTAtMjAxMkBleGFtcGxlLmNv
120 bTEpMCcGA1UEAxMgR29zdFIzNDEwLTIwMTIgKDUxMiBiaXQpIGV4YW1wbGUwgaow
121 IQYIKoUDBwEBAQIwFQYJKoUDBwECAQICBggqhQMHAQECAwOBhAAEgYATGQ9VCiM5
122 FRGCQ8MEz2F1dANqhaEuywa8CbxOnTvaGJpFQVXQwkwvLFAKh7hk542vOEtxpKtT
123 CXfGf84nRhMH/Q9bZeAc2eO/yhxrsQhTBufa1Fuou2oe/jUOaG6RAtUUvRzhNTpp
124 RGGl1+EIY2vzzUua9j9Ol/gAoy/LNKQIfqOBwDCBvTAdBgNVHQ4EFgQUPcbTRXJZ
125 nHtjj+eBP7b5lcTMekIwCwYDVR0PBAQDAgHGMA8GA1UdEwQIMAYBAf8CAQEwfgYD
126 VR0BBHcwdYAUPcbTRXJZnHtjj+eBP7b5lcTMekKhWqRYMFYxKTAnBgkqhkiG9w0B
127 CQEWGkdvc3RSMzQxMC0yMDEyQGV4YW1wbGUuY29tMSkwJwYDVQQDEyBHb3N0UjM0
128 MTAtMjAxMiAoNTEyIGJpdCkgZXhhbXBsZYIBATAKBggqhQMHAQEDAwOBgQBObS7o
129 ppPTXzHyVR1DtPa8b57nudJzI4czhsfeX5HDntOq45t9B/qSs8dC6eGxbhHZ9zCO
130 SFtxWYdmg0au8XI9Xb8vTC1qdwWID7FFjMWDNQZb6lYh/J+8F2xKylvB5nIlRZqO
131 o3eUNFkNyHJwQCk2WoOlO16zwGk2tdKH4KmD5w==
132         """)
133         prv_key_raw = hexdec("3FC01CDCD4EC5F972EB482774C41E66DB7F380528DFE9E67992BA05AEE462435757530E641077CE587B976C8EEB48C48FD33FD175F0C7DE6A44E014E6BCB074B")[::-1]
134         self.process_cert(
135             "id-tc26-gost-3410-12-512-paramSetB",
136             2012,
137             GOST34112012512,
138             prv_key_raw,
139             cert_raw,
140         )