Skip to content

Commit e7e27d0

Browse files
committed
RFC 7468 allows a larger character set in PEM label
1 parent b763a75 commit e7e27d0

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

asn1crypto/pem.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ def armor(type_name, der_bytes, headers=None):
109109
return output.getvalue()
110110

111111

112+
# RFC 7468#page-5
113+
LABEL_CHARS = '[!-,.-~]' # 0x21-0x2C, 0x2E-0x7E
114+
LABEL_PAT = re.compile(f'^(?:---- |-----)BEGIN ({LABEL_CHARS}(([- ]?{LABEL_CHARS})*))?(?: ----|-----)'.encode('ascii'))
115+
116+
112117
def _unarmor(pem_bytes):
113118
"""
114119
Convert a PEM-encoded byte string into one or more DER-encoded byte strings
@@ -144,14 +149,16 @@ def _unarmor(pem_bytes):
144149
found_start = False
145150
found_end = False
146151

152+
147153
for line in pem_bytes.splitlines(False):
148154
if line == b'':
149155
continue
150156

151157
if state == "trash":
152158
# Look for a starting line since some CA cert bundle show the cert
153-
# into in a parsed format above each PEM block
154-
type_name_match = re.match(b'^(?:---- |-----)BEGIN ([A-Z0-9 ]+)(?: ----|-----)', line)
159+
# info in a parsed format above each PEM block
160+
161+
type_name_match = LABEL_PAT.match(line)
155162
if not type_name_match:
156163
continue
157164
object_type = type_name_match.group(1).decode('ascii')

0 commit comments

Comments
 (0)