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.type import univ 15 16from pyasn1_modules import pem 17from pyasn1_modules import rfc5280 18from pyasn1_modules import rfc5480 19from pyasn1_modules import rfc6402 20from pyasn1_modules import rfc6955 21 22try: 23 import unittest2 as unittest 24 25except ImportError: 26 import unittest 27 28 29class CertificationRequestTestCase(unittest.TestCase): 30 pem_text = """\ 31MIIDPDCCArsCAQAwTjELMAkGA1UEBhMCVVMxETAPBgNVBAoTCFhFVEkgSW5jMRAw 32DgYDVQQLEwdUZXN0aW5nMRowGAYDVQQDExFQS0lYIEV4YW1wbGUgVXNlcjCCAkEw 33ggG2BgcqhkjOPgIBMIIBqQKBgQCUhOBFbH9pUWI+VoB8aOfFqZ6edHSU7ZCMHcTh 34ShSC9dKUDBnjuRC7EbnlpfuOIVFjAoaqBrghNrZ/Nt/R1mhbeXwdWhR1H2qTdZPO 35u5dyivAPI51H9tSzx/D05vYrwjLhiWe+fgau+NABa4sq9QLXtqhjlIOwGzF9Uhre 365QOFJwKBgCamMixaK9QzK1zcBodTP5AGYVA4PtK5fYEcEhDFDFPUZNGOMAcIjN0/ 37Ci8s1ht/V4bQ2rtuNioY6NO8cDF6SLZOGG7dHyIG6z/q1EFp2ZveR5V6cpHSCX9J 38XDsDM1HI8Tma/wTVbn6UPQO49jEVJkiVqFzeR4i0aToAp4ae2tHNAiEA6HL6lvAR 39QPXy3P07XXiUsYUB5Wk3IfclubpxSvxgMPsCYQCjkQHAqG6kTaBW/Gz+H6ewzQ+U 40hwwlvpd2jevlpAldq4PNgAs1Z38MjqcxmDKFOUCdEZjY3rh/hpuvjWc9tna0YS8h 414UsOaP9TPofd2HFWaEfc9yBjSzxfeHGD5nCe4pIwGgMVABzVOg0Xgm0KgXWBRhCO 42PtsJ5Jg0AgE3A4GEAAKBgBNjoYUEjEaoiOv0XqiTdK79rp6WJxJlxEwHBj4Y/pS4 43qHlIvS40tkfKBDCh7DP9GgstnlDJeA+uauy1a2q+slzasp94LLl34nkrJb8uC1lK 44k0v4s+yBNK6XR1LgqCmY7NGwyitveovbTo2lFX5+rzNiCZ4PEUSMwY2iEZ5T77Lo 45oCEwHwYJKoZIhvcNAQkOMRIwEDAOBgNVHQ8BAf8EBAMCAwgwDAYIKwYBBQUHBgMF 46AANtADBqMFIwSDELMAkGA1UEBhMCVVMxETAPBgNVBAoTCFhFVEkgSW5jMRAwDgYD 47VQQLEwdUZXN0aW5nMRQwEgYDVQQDEwtSb290IERTQSBDQQIGANo5tuLLBBQtBXf+ 48Xo9l9a+tyVybAsCoiClhYw== 49""" 50 51 def setUp(self): 52 self.asn1Spec = rfc6402.CertificationRequest() 53 54 def testDerCodec(self): 55 substrate = pem.readBase64fromText(self.pem_text) 56 asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec) 57 assert not rest 58 assert asn1Object.prettyPrint() 59 assert der_encode(asn1Object) == substrate 60 61 spki_a = asn1Object['certificationRequestInfo']['subjectPublicKeyInfo']['algorithm'] 62 assert spki_a['algorithm'] == rfc5480.dhpublicnumber 63 assert spki_a['algorithm'] in rfc5280.algorithmIdentifierMap.keys() 64 params, rest = der_decode(spki_a['parameters'], asn1Spec=rfc6955.DomainParameters()) 65 assert not rest 66 assert params.prettyPrint() 67 assert der_encode(params) == spki_a['parameters'] 68 assert params['validationParms']['pgenCounter'] == 55 69 70 sig_a = asn1Object['signatureAlgorithm'] 71 assert sig_a['algorithm'] == rfc6955.id_dhPop_static_sha1_hmac_sha1 72 assert sig_a['algorithm'] in rfc5280.algorithmIdentifierMap.keys() 73 assert sig_a['parameters'] == der_encode(univ.Null("")) 74 75 def testOpenTypes(self): 76 substrate = pem.readBase64fromText(self.pem_text) 77 asn1Object, rest = der_decode(substrate, 78 asn1Spec=self.asn1Spec, 79 decodeOpenTypes=True) 80 assert not rest 81 assert asn1Object.prettyPrint() 82 assert der_encode(asn1Object) == substrate 83 84 spki_a = asn1Object['certificationRequestInfo']['subjectPublicKeyInfo']['algorithm'] 85 assert spki_a['algorithm'] == rfc5480.dhpublicnumber 86 assert spki_a['parameters']['validationParms']['pgenCounter'] == 55 87 88 sig_a = asn1Object['signatureAlgorithm'] 89 assert sig_a['algorithm'] == rfc6955.id_dhPop_static_sha1_hmac_sha1 90 assert sig_a['parameters'] == univ.Null("") 91 92 93suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 94 95if __name__ == '__main__': 96 import sys 97 98 result = unittest.TextTestRunner(verbosity=2).run(suite) 99 sys.exit(not result.wasSuccessful()) 100