tlslite.utils.pem

Package tlslite :: Package utils :: Module pem |
[hide private]
[frames] | no frames]

# Source Code for Module tlslite.utils.pem

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