|
| 1 | +# Copyright 2023 The GmSSL Project. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the License); you may |
| 4 | +# not use this file except in compliance with the License. |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + |
| 8 | +from gmssl import * |
| 9 | + |
| 10 | +cert_txt = '''\ |
| 11 | +-----BEGIN CERTIFICATE----- |
| 12 | +MIIBszCCAVegAwIBAgIIaeL+wBcKxnswDAYIKoEcz1UBg3UFADAuMQswCQYDVQQG |
| 13 | +EwJDTjEOMAwGA1UECgwFTlJDQUMxDzANBgNVBAMMBlJPT1RDQTAeFw0xMjA3MTQw |
| 14 | +MzExNTlaFw00MjA3MDcwMzExNTlaMC4xCzAJBgNVBAYTAkNOMQ4wDAYDVQQKDAVO |
| 15 | +UkNBQzEPMA0GA1UEAwwGUk9PVENBMFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAE |
| 16 | +MPCca6pmgcchsTf2UnBeL9rtp4nw+itk1Kzrmbnqo05lUwkwlWK+4OIrtFdAqnRT |
| 17 | +V7Q9v1htkv42TsIutzd126NdMFswHwYDVR0jBBgwFoAUTDKxl9kzG8SmBcHG5Yti |
| 18 | +W/CXdlgwDAYDVR0TBAUwAwEB/zALBgNVHQ8EBAMCAQYwHQYDVR0OBBYEFEwysZfZ |
| 19 | +MxvEpgXBxuWLYlvwl3ZYMAwGCCqBHM9VAYN1BQADSAAwRQIgG1bSLeOXp3oB8H7b |
| 20 | +53W+CKOPl2PknmWEq/lMhtn25HkCIQDaHDgWxWFtnCrBjH16/W3Ezn7/U/Vjo5xI |
| 21 | +pDoiVhsLwg== |
| 22 | +-----END CERTIFICATE-----''' |
| 23 | +with open('ROOTCA.pem', 'w') as file: |
| 24 | + file.write(cert_txt) |
| 25 | + file.close() |
| 26 | + |
| 27 | +cert = Sm2Certificate() |
| 28 | +cert.import_pem('ROOTCA.pem') |
| 29 | + |
| 30 | +print("Certificate") |
| 31 | + |
| 32 | +serial = cert.get_serial_number() |
| 33 | +print("Serial :", serial.hex()) |
| 34 | + |
| 35 | +validity = cert.get_validity() |
| 36 | +print("Validity.notBefore :", validity.not_before) |
| 37 | +print("Validity.notAfter :", validity.not_after) |
| 38 | + |
| 39 | +issuer = cert.get_issuer() |
| 40 | +print("Issuer :") |
| 41 | +for key in issuer: |
| 42 | + if key == 'raw_data': |
| 43 | + print(" ", key, ":", issuer[key].hex()) |
| 44 | + else: |
| 45 | + print(" ", key, ":", issuer[key]) |
| 46 | + |
| 47 | + |
| 48 | +subject = cert.get_subject() |
| 49 | +print("Subject :") |
| 50 | +for key in subject: |
| 51 | + if key == 'raw_data': |
| 52 | + print(" ", key, ":", subject[key].hex()) |
| 53 | + else: |
| 54 | + print(" ", key, ":", subject[key]) |
| 55 | + |
| 56 | +public_key = cert.get_subject_public_key() |
| 57 | +public_key.export_public_key_info_pem('subject_public_key.pem') |
| 58 | + |
| 59 | +file = open('subject_public_key.pem',mode='r') |
| 60 | +fulltext = file.read() |
| 61 | +file.close() |
| 62 | +print("Subject Public Key:") |
| 63 | +print(fulltext) |
| 64 | + |
| 65 | + |
0 commit comments