1
2
3
4 from .cryptomath import base64ToBytes
5 import binascii
6
7
8
9
12
15
17 """Decode a PEM string into a bytearray of its payload.
18
19 The input must contain an appropriate PEM prefix and postfix
20 based on the input name string, e.g. for name="CERTIFICATE":
21
22 -----BEGIN CERTIFICATE-----
23 MIIBXDCCAUSgAwIBAgIBADANBgkqhkiG9w0BAQUFADAPMQ0wCwYDVQQDEwRUQUNL
24 ...
25 KoZIhvcNAQEFBQADAwA5kw==
26 -----END CERTIFICATE-----
27
28 The first such PEM block in the input will be found, and its
29 payload will be base64 decoded and returned.
30 """
31 prefix = "-----BEGIN %s-----" % name
32 postfix = "-----END %s-----" % name
33 start = s.find(prefix)
34 if start == -1:
35 raise SyntaxError("Missing PEM prefix")
36 end = s.find(postfix, start+len(prefix))
37 if end == -1:
38 raise SyntaxError("Missing PEM postfix")
39 s = s[start+len("-----BEGIN %s-----" % name) : end]
40 retBytes = a2b_base64(s)
41 return retBytes
42
44 """Decode a sequence of PEM blocks into a list of bytearrays.
45
46 The input must contain any number of PEM blocks, each with the appropriate
47 PEM prefix and postfix based on the input name string, e.g. for
48 name="TACK BREAK SIG". Arbitrary text can appear between and before and
49 after the PEM blocks. For example:
50
51 " Created by TACK.py 0.9.3 Created at 2012-02-01T00:30:10Z -----BEGIN TACK
52 BREAK SIG-----
53 ATKhrz5C6JHJW8BF5fLVrnQss6JnWVyEaC0p89LNhKPswvcC9/s6+vWLd9snYTUv
54 YMEBdw69PUP8JB4AdqA3K6Ap0Fgd9SSTOECeAKOUAym8zcYaXUwpk0+WuPYa7Zmm
55 SkbOlK4ywqt+amhWbg9txSGUwFO5tWUHT3QrnRlE/e3PeNFXLx5Bckg= -----END TACK
56 BREAK SIG----- Created by TACK.py 0.9.3 Created at 2012-02-01T00:30:11Z
57 -----BEGIN TACK BREAK SIG-----
58 ATKhrz5C6JHJW8BF5fLVrnQss6JnWVyEaC0p89LNhKPswvcC9/s6+vWLd9snYTUv
59 YMEBdw69PUP8JB4AdqA3K6BVCWfcjN36lx6JwxmZQncS6sww7DecFO/qjSePCxwM
60 +kdDqX/9/183nmjx6bf0ewhPXkA0nVXsDYZaydN8rJU1GaMlnjcIYxY= -----END TACK
61 BREAK SIG----- "
62
63 All such PEM blocks will be found, decoded, and return in an ordered list
64 of bytearrays, which may have zero elements if not PEM blocks are found.
65 """
66 bList = []
67 prefix = "-----BEGIN %s-----" % name
68 postfix = "-----END %s-----" % name
69 while 1:
70 start = s.find(prefix)
71 if start == -1:
72 return bList
73 end = s.find(postfix, start+len(prefix))
74 if end == -1:
75 raise SyntaxError("Missing PEM postfix")
76 s2 = s[start+len(prefix) : end]
77 retBytes = a2b_base64(s2)
78 bList.append(retBytes)
79 s = s[end+len(postfix) : ]
80
82 """Encode a payload bytearray into a PEM string.
83
84 The input will be base64 encoded, then wrapped in a PEM prefix/postfix
85 based on the name string, e.g. for name="CERTIFICATE":
86
87 -----BEGIN CERTIFICATE-----
88 MIIBXDCCAUSgAwIBAgIBADANBgkqhkiG9w0BAQUFADAPMQ0wCwYDVQQDEwRUQUNL
89 ...
90 KoZIhvcNAQEFBQADAwA5kw==
91 -----END CERTIFICATE-----
92 """
93 s1 = b2a_base64(b)[:-1]
94 s2 = ""
95 while s1:
96 s2 += s1[:64] + "\n"
97 s1 = s1[64:]
98 s = ("-----BEGIN %s-----\n" % name) + s2 + \
99 ("-----END %s-----\n" % name)
100 return s
101
103 searchStr = "-----BEGIN %s-----" % name
104 return searchStr in inStr
105