OLD | NEW |
(Empty) | |
| 1 # -*- coding: utf-8 -*- |
| 2 # |
| 3 # Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu> |
| 4 # |
| 5 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 # you may not use this file except in compliance with the License. |
| 7 # You may obtain a copy of the License at |
| 8 # |
| 9 # https://www.apache.org/licenses/LICENSE-2.0 |
| 10 # |
| 11 # Unless required by applicable law or agreed to in writing, software |
| 12 # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 # See the License for the specific language governing permissions and |
| 15 # limitations under the License. |
| 16 |
| 17 """Functions that load and write PEM-encoded files.""" |
| 18 |
| 19 import base64 |
| 20 from rsa._compat import b, is_bytes |
| 21 |
| 22 |
| 23 def _markers(pem_marker): |
| 24 """ |
| 25 Returns the start and end PEM markers |
| 26 """ |
| 27 |
| 28 if is_bytes(pem_marker): |
| 29 pem_marker = pem_marker.decode('utf-8') |
| 30 |
| 31 return (b('-----BEGIN %s-----' % pem_marker), |
| 32 b('-----END %s-----' % pem_marker)) |
| 33 |
| 34 |
| 35 def load_pem(contents, pem_marker): |
| 36 """Loads a PEM file. |
| 37 |
| 38 :param contents: the contents of the file to interpret |
| 39 :param pem_marker: the marker of the PEM content, such as 'RSA PRIVATE KEY' |
| 40 when your file has '-----BEGIN RSA PRIVATE KEY-----' and |
| 41 '-----END RSA PRIVATE KEY-----' markers. |
| 42 |
| 43 :return: the base64-decoded content between the start and end markers. |
| 44 |
| 45 @raise ValueError: when the content is invalid, for example when the start |
| 46 marker cannot be found. |
| 47 |
| 48 """ |
| 49 |
| 50 # We want bytes, not text. If it's text, it can be converted to ASCII bytes. |
| 51 if not is_bytes(contents): |
| 52 contents = contents.encode('ascii') |
| 53 |
| 54 (pem_start, pem_end) = _markers(pem_marker) |
| 55 |
| 56 pem_lines = [] |
| 57 in_pem_part = False |
| 58 |
| 59 for line in contents.splitlines(): |
| 60 line = line.strip() |
| 61 |
| 62 # Skip empty lines |
| 63 if not line: |
| 64 continue |
| 65 |
| 66 # Handle start marker |
| 67 if line == pem_start: |
| 68 if in_pem_part: |
| 69 raise ValueError('Seen start marker "%s" twice' % pem_start) |
| 70 |
| 71 in_pem_part = True |
| 72 continue |
| 73 |
| 74 # Skip stuff before first marker |
| 75 if not in_pem_part: |
| 76 continue |
| 77 |
| 78 # Handle end marker |
| 79 if in_pem_part and line == pem_end: |
| 80 in_pem_part = False |
| 81 break |
| 82 |
| 83 # Load fields |
| 84 if b(':') in line: |
| 85 continue |
| 86 |
| 87 pem_lines.append(line) |
| 88 |
| 89 # Do some sanity checks |
| 90 if not pem_lines: |
| 91 raise ValueError('No PEM start marker "%s" found' % pem_start) |
| 92 |
| 93 if in_pem_part: |
| 94 raise ValueError('No PEM end marker "%s" found' % pem_end) |
| 95 |
| 96 # Base64-decode the contents |
| 97 pem = b('').join(pem_lines) |
| 98 return base64.standard_b64decode(pem) |
| 99 |
| 100 |
| 101 def save_pem(contents, pem_marker): |
| 102 """Saves a PEM file. |
| 103 |
| 104 :param contents: the contents to encode in PEM format |
| 105 :param pem_marker: the marker of the PEM content, such as 'RSA PRIVATE KEY' |
| 106 when your file has '-----BEGIN RSA PRIVATE KEY-----' and |
| 107 '-----END RSA PRIVATE KEY-----' markers. |
| 108 |
| 109 :return: the base64-encoded content between the start and end markers. |
| 110 |
| 111 """ |
| 112 |
| 113 (pem_start, pem_end) = _markers(pem_marker) |
| 114 |
| 115 b64 = base64.standard_b64encode(contents).replace(b('\n'), b('')) |
| 116 pem_lines = [pem_start] |
| 117 |
| 118 for block_start in range(0, len(b64), 64): |
| 119 block = b64[block_start:block_start + 64] |
| 120 pem_lines.append(block) |
| 121 |
| 122 pem_lines.append(pem_end) |
| 123 pem_lines.append(b('')) |
| 124 |
| 125 return b('\n').join(pem_lines) |
OLD | NEW |