Skip to content

Commit 303c5b2

Browse files
committed
Add SM2 and Certificate examples
1 parent 74895a0 commit 303c5b2

6 files changed

Lines changed: 190 additions & 3 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,4 @@ dmypy.json
128128
# Pyre type checker
129129
.pyre/
130130
*.pem
131-
131+
.DS_Store

examples/sm2_cert.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+

examples/sm2_enc.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
# run sm2_key.py first
11+
12+
13+
14+
# Sender
15+
16+
public_key = Sm2Key()
17+
public_key.import_public_key_info_pem('sm2pub.pem')
18+
19+
plaintext = b'Plaintext message'
20+
ciphertext = public_key.encrypt(plaintext)
21+
22+
23+
# Receiver
24+
25+
private_key = Sm2Key()
26+
private_key.import_encrypted_private_key_info_pem('sm2.pem', 'password')
27+
28+
decrypted = private_key.decrypt(ciphertext)
29+
30+
print("plaintext :", plaintext.hex())
31+
print("ciphertext :", ciphertext.hex())
32+
print("decrypted :", decrypted.hex())
33+

examples/sm2_key.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
sm2 = Sm2Key()
11+
sm2.generate_key()
12+
13+
sm2.export_encrypted_private_key_info_pem('sm2.pem', 'password')
14+
print('export private key to encrypted file sm2.pem')
15+
16+
sm2.export_public_key_info_pem('sm2pub.pem')
17+
print('export public key to file sm2pub.pem')
18+
19+
private_key = Sm2Key()
20+
private_key.import_encrypted_private_key_info_pem('sm2.pem', 'password')
21+
print("private key has private key :", private_key.has_private_key())
22+
print("private key has public key :", private_key.has_public_key())
23+
24+
public_key = Sm2Key()
25+
public_key.import_public_key_info_pem('sm2pub.pem')
26+
print("public key has private key :", public_key.has_private_key())
27+
print("public key has public key :", public_key.has_public_key())
28+

examples/sm2_sign.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
# run sm2_key.py first
11+
12+
13+
# Signer
14+
15+
private_key = Sm2Key()
16+
private_key.import_encrypted_private_key_info_pem('sm2.pem', 'password')
17+
18+
z = private_key.compute_z(SM2_DEFAULT_ID)
19+
20+
sm3 = Sm3()
21+
sm3.update(z)
22+
sm3.update(b'abc')
23+
dgst = sm3.digest()
24+
25+
sig = private_key.sign(dgst)
26+
print("signature1 :", sig.hex())
27+
28+
signer = Sm2Signature(private_key, SM2_DEFAULT_ID, DO_SIGN)
29+
signer.update(b'abc')
30+
sig2 = signer.sign()
31+
print("signature2 :", sig2.hex())
32+
33+
# Verifier
34+
35+
public_key = Sm2Key()
36+
public_key.import_public_key_info_pem('sm2pub.pem')
37+
38+
z = public_key.compute_z(SM2_DEFAULT_ID)
39+
40+
sm3 = Sm3()
41+
sm3.update(z)
42+
sm3.update(b'abc')
43+
dgst = sm3.digest()
44+
45+
ret = public_key.verify(dgst, sig)
46+
print("Verify signature1 success :", ret)
47+
48+
ret = public_key.verify(dgst, sig2)
49+
print("Verify signature2 success :", ret)
50+
51+
verifier = Sm2Signature(public_key, SM2_DEFAULT_ID, DO_VERIFY)
52+
verifier.update(b'abc')
53+
ret = verifier.verify(sig)
54+
print("Verify signature1 success :", ret)
55+
56+
verifier = Sm2Signature(public_key, SM2_DEFAULT_ID, DO_VERIFY)
57+
verifier.update(b'abc')
58+
ret = verifier.verify(sig2)
59+
print("Verify signature2 success :", ret)
60+
61+

gmssl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ def get_issuer(self):
953953
issuer_raw = create_string_buffer(issuer_len.value)
954954
libc.memcpy(issuer_raw, issuer_ptr, issuer_len)
955955

956-
issuer = { "raw_data" : issuer_raw }
956+
issuer = { "raw_data" : issuer_raw.raw }
957957
gmssl_parse_name(issuer, issuer_ptr, issuer_len)
958958
return issuer
959959

@@ -965,7 +965,7 @@ def get_subject(self):
965965
subject_raw = create_string_buffer(subject_len.value)
966966
libc.memcpy(subject_raw, subject_ptr, subject_len)
967967

968-
subject = { "raw_data" : subject_raw }
968+
subject = { "raw_data" : subject_raw.raw }
969969
gmssl_parse_name(subject, subject_ptr, subject_len)
970970
return subject
971971

0 commit comments

Comments
 (0)