summaryrefslogtreecommitdiffstats
path: root/python/pyasn1-modules/pyasn1_modules/pem.py
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /python/pyasn1-modules/pyasn1_modules/pem.py
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'python/pyasn1-modules/pyasn1_modules/pem.py')
-rw-r--r--python/pyasn1-modules/pyasn1_modules/pem.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/python/pyasn1-modules/pyasn1_modules/pem.py b/python/pyasn1-modules/pyasn1_modules/pem.py
new file mode 100644
index 000000000..d8d815873
--- /dev/null
+++ b/python/pyasn1-modules/pyasn1_modules/pem.py
@@ -0,0 +1,51 @@
+import base64, sys
+
+stSpam, stHam, stDump = 0, 1, 2
+
+# The markers parameters is in form ('start1', 'stop1'), ('start2', 'stop2')...
+# Return is (marker-index, substrate)
+def readPemBlocksFromFile(fileObj, *markers):
+ startMarkers = dict(map(lambda x: (x[1],x[0]),
+ enumerate(map(lambda x: x[0], markers))))
+ stopMarkers = dict(map(lambda x: (x[1],x[0]),
+ enumerate(map(lambda x: x[1], markers))))
+ idx = -1; substrate = ''
+ state = stSpam
+ while 1:
+ certLine = fileObj.readline()
+ if not certLine:
+ break
+ certLine = certLine.strip()
+ if state == stSpam:
+ if certLine in startMarkers:
+ certLines = []
+ idx = startMarkers[certLine]
+ state = stHam
+ continue
+ if state == stHam:
+ if certLine in stopMarkers and stopMarkers[certLine] == idx:
+ state = stDump
+ else:
+ certLines.append(certLine)
+ if state == stDump:
+ if sys.version_info[0] <= 2:
+ substrate = ''.join([ base64.b64decode(x) for x in certLines ])
+ else:
+ substrate = ''.encode().join([ base64.b64decode(x.encode()) for x in certLines ])
+ break
+ return idx, substrate
+
+# Backward compatibility routine
+def readPemFromFile(fileObj,
+ startMarker='-----BEGIN CERTIFICATE-----',
+ endMarker='-----END CERTIFICATE-----'):
+ idx, substrate = readPemBlocksFromFile(fileObj, (startMarker, endMarker))
+ return substrate
+
+def readBase64FromFile(fileObj):
+ if sys.version_info[0] <= 2:
+ return ''.join([ base64.b64decode(x) for x in fileObj.readlines() ])
+ else:
+ return ''.encode().join(
+ [ base64.b64decode(x.encode()) for x in fileObj.readlines() ]
+ )