Skip to content

Commit c2af63b

Browse files
author
Danielle Madeley
committed
Use PEM armor/unarmor functions
1 parent c6d442d commit c2af63b

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

pkcs11/util/x509.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
Certificate handling utilities for X.509 (SSL) certificates.
33
"""
44

5-
from datetime import datetime
6-
75
from asn1crypto.x509 import Certificate
86

97
from ..constants import Attribute, ObjectClass, CertificateType
@@ -15,6 +13,8 @@ def decode_x509_public_key(der):
1513
Decode a DER-encoded X.509 certificate's public key into a set of
1614
attributes able to be passed to :meth:`pkcs11.Session.create_object`.
1715
16+
For PEM-encoded certificates, use :func:`asn1crypto.pem.unarmor`.
17+
1818
.. warning::
1919
2020
Does not verify certificate.
@@ -69,6 +69,8 @@ def decode_x509_certificate(der, extended_set=False):
6969
Optionally pass `extended_set` to include additional attributes:
7070
start date, end date and key identifiers.
7171
72+
For PEM-encoded certificates, use :func:`asn1crypto.pem.unarmor`.
73+
7274
.. warning::
7375
7476
Does not verify certificate.

tests/test_x509.py

+8-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
import subprocess
77
import datetime
88

9-
from asn1crypto.x509 import Certificate
9+
from asn1crypto import pem
10+
from asn1crypto.x509 import Certificate, TbsCertificate, Time, Name
11+
from asn1crypto.keys import RSAPublicKey
12+
from asn1crypto.csr import CertificationRequest, CertificationRequestInfo
1013

1114
import pkcs11
1215
from pkcs11.util.rsa import encode_rsa_public_key
@@ -29,7 +32,8 @@
2932
# -out cert.pem \
3033
# -days 365 \
3134
# -nodes
32-
CERT = base64.b64decode("""
35+
_, _, CERT = pem.unarmor(b"""
36+
-----BEGIN CERTIFICATE-----
3337
MIICKzCCAdWgAwIBAgIJAK3BO9rnLZd9MA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV
3438
BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX
3539
aWRnaXRzIFB0eSBMdGQwHhcNMTcwNjAyMDI0ODMyWhcNMTgwNjAyMDI0ODMyWjBF
@@ -42,6 +46,7 @@
4246
cm5ldCBXaWRnaXRzIFB0eSBMdGSCCQCtwTva5y2XfTAMBgNVHRMEBTADAQH/MA0G
4347
CSqGSIb3DQEBBQUAA0EAOdvMKLrIFOYF3aVLGharY196heO0fndm39sZAXJ4PItx
4448
n28DytHEdAoltksfJ2Ds3XAjQqcpI5eBbhIoN9Ckxg==
49+
-----END CERTIFICATE-----
4550
""")
4651

4752

@@ -163,9 +168,6 @@ def test_self_sign_certificate(self):
163168
# Warning: proof of concept code only!
164169
pub, priv = self.session.generate_keypair(KeyType.RSA, 1024)
165170

166-
from asn1crypto.x509 import TbsCertificate, Time, Name
167-
from asn1crypto.keys import RSAPublicKey
168-
169171
tbs = TbsCertificate({
170172
'version': 'v1',
171173
'serial_number': 1,
@@ -214,9 +216,7 @@ def test_self_sign_certificate(self):
214216
stdin=subprocess.PIPE,
215217
stdout=subprocess.DEVNULL) as proc:
216218

217-
proc.stdin.write(b'-----BEGIN CERTIFICATE-----\n')
218-
proc.stdin.write(base64.encodebytes(cert.dump()))
219-
proc.stdin.write(b'-----END CERTIFICATE-----\n')
219+
proc.stdin.write(pem.armor('CERTIFICATE', cert.dump()))
220220
proc.stdin.close()
221221

222222
self.assertEqual(proc.wait(), 0)
@@ -226,11 +226,6 @@ def test_sign_csr(self):
226226
# Warning: proof of concept code only!
227227
pub, priv = self.session.generate_keypair(KeyType.RSA, 1024)
228228

229-
from asn1crypto.csr import (CertificationRequest,
230-
CertificationRequestInfo)
231-
from asn1crypto.x509 import Name
232-
from asn1crypto.keys import RSAPublicKey
233-
234229
info = CertificationRequestInfo({
235230
'version': 0,
236231
'subject': Name.build({

0 commit comments

Comments
 (0)