| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/python | |
| 2 # Copyright (c) 2016 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 # TODO(svaldez): Deduplicate various annotate_test_data. | |
| 6 | |
| 7 """This script is called without any arguments to re-format all of the *.pem | |
| 8 files in the script's parent directory. | |
| 9 | |
| 10 The main formatting change is to run "openssl asn1parse" for each of the PEM | |
| 11 block sections, and add that output to the comment. It also runs the command | |
| 12 on the OCTET STRING representing BasicOCSPResponse. | |
| 13 | |
| 14 """ | |
| 15 | |
| 16 import glob | |
| 17 import os | |
| 18 import re | |
| 19 import base64 | |
| 20 import subprocess | |
| 21 | |
| 22 | |
| 23 def Transform(file_data): | |
| 24 """Returns a transformed (formatted) version of file_data""" | |
| 25 | |
| 26 result = '' | |
| 27 | |
| 28 for block in GetPemBlocks(file_data): | |
| 29 if len(result) != 0: | |
| 30 result += '\n' | |
| 31 | |
| 32 # If there was a user comment (non-script-generated comment) associated | |
| 33 # with the block, output it immediately before the block. | |
| 34 user_comment = GetUserComment(block.comment) | |
| 35 if user_comment: | |
| 36 result += user_comment + '\n' | |
| 37 | |
| 38 generated_comment = GenerateCommentForBlock(block.name, block.data) | |
| 39 result += generated_comment + '\n' | |
| 40 | |
| 41 | |
| 42 result += MakePemBlockString(block.name, block.data) | |
| 43 | |
| 44 return result | |
| 45 | |
| 46 | |
| 47 def GenerateCommentForBlock(block_name, block_data): | |
| 48 """Returns a string describing the ASN.1 structure of block_data""" | |
| 49 | |
| 50 p = subprocess.Popen(['openssl', 'asn1parse', '-i', '-inform', 'DER'], | |
| 51 stdout=subprocess.PIPE, stdin=subprocess.PIPE, | |
| 52 stderr=subprocess.PIPE) | |
| 53 stdout_data, stderr_data = p.communicate(input=block_data) | |
| 54 generated_comment = '$ openssl asn1parse -i < [%s]\n%s' % (block_name, | |
| 55 stdout_data) | |
| 56 | |
| 57 # The OCTET STRING encoded BasicOCSPResponse is also parsed out using | |
| 58 #'openssl asn1parse'. | |
| 59 if block_name == 'OCSP RESPONSE': | |
| 60 if '[HEX DUMP]:' in generated_comment: | |
| 61 (generated_comment, response) = generated_comment.split('[HEX DUMP]:', 1) | |
| 62 response = response.replace('\n', '') | |
| 63 if len(response) % 2 != 0: | |
| 64 response = '0' + response | |
| 65 response = GenerateCommentForBlock('INNER', response.decode('hex')) | |
| 66 response = response.split('\n', 1)[1] | |
| 67 response = response.replace(': ', ': ') | |
| 68 generated_comment += '\n%s' % (response) | |
| 69 return generated_comment.strip('\n') | |
| 70 | |
| 71 | |
| 72 | |
| 73 def GetUserComment(comment): | |
| 74 """Removes any script-generated lines (everything after the $ openssl line)""" | |
| 75 | |
| 76 # Consider everything after "$ openssl" to be a generated comment. | |
| 77 comment = comment.split('$ openssl asn1parse -i', 1)[0] | |
| 78 if IsEntirelyWhiteSpace(comment): | |
| 79 comment = '' | |
| 80 return comment | |
| 81 | |
| 82 | |
| 83 def MakePemBlockString(name, data): | |
| 84 return ('-----BEGIN %s-----\n' | |
| 85 '%s' | |
| 86 '-----END %s-----\n') % (name, EncodeDataForPem(data), name) | |
| 87 | |
| 88 | |
| 89 def GetPemFilePaths(): | |
| 90 """Returns an iterable for all the paths to the PEM test files""" | |
| 91 | |
| 92 base_dir = os.path.dirname(os.path.realpath(__file__)) | |
| 93 return glob.iglob(os.path.join(base_dir, '*.pem')) | |
| 94 | |
| 95 | |
| 96 def ReadFileToString(path): | |
| 97 with open(path, 'r') as f: | |
| 98 return f.read() | |
| 99 | |
| 100 | |
| 101 def WrapTextToLineWidth(text, column_width): | |
| 102 result = '' | |
| 103 pos = 0 | |
| 104 while pos < len(text): | |
| 105 result += text[pos : pos + column_width] + '\n' | |
| 106 pos += column_width | |
| 107 return result | |
| 108 | |
| 109 | |
| 110 def EncodeDataForPem(data): | |
| 111 result = base64.b64encode(data) | |
| 112 return WrapTextToLineWidth(result, 75) | |
| 113 | |
| 114 | |
| 115 class PemBlock(object): | |
| 116 def __init__(self): | |
| 117 self.name = None | |
| 118 self.data = None | |
| 119 self.comment = None | |
| 120 | |
| 121 | |
| 122 def StripAllWhitespace(text): | |
| 123 pattern = re.compile(r'\s+') | |
| 124 return re.sub(pattern, '', text) | |
| 125 | |
| 126 | |
| 127 def IsEntirelyWhiteSpace(text): | |
| 128 return len(StripAllWhitespace(text)) == 0 | |
| 129 | |
| 130 | |
| 131 def DecodePemBlockData(text): | |
| 132 text = StripAllWhitespace(text) | |
| 133 return base64.b64decode(text) | |
| 134 | |
| 135 | |
| 136 def GetPemBlocks(data): | |
| 137 """Returns an iterable of PemBlock""" | |
| 138 | |
| 139 comment_start = 0 | |
| 140 | |
| 141 regex = re.compile(r'-----BEGIN ([\w ]+)-----(.*?)-----END \1-----', | |
| 142 re.DOTALL) | |
| 143 | |
| 144 for match in regex.finditer(data): | |
| 145 block = PemBlock() | |
| 146 | |
| 147 block.name = match.group(1) | |
| 148 block.data = DecodePemBlockData(match.group(2)) | |
| 149 | |
| 150 # Keep track of any non-PEM text above blocks | |
| 151 block.comment = data[comment_start : match.start()].strip() | |
| 152 comment_start = match.end() | |
| 153 | |
| 154 yield block | |
| 155 | |
| 156 | |
| 157 def WriteStringToFile(data, path): | |
| 158 with open(path, "w") as f: | |
| 159 f.write(data) | |
| 160 | |
| 161 | |
| 162 def main(): | |
| 163 for path in GetPemFilePaths(): | |
| 164 print "Processing %s ..." % (path) | |
| 165 original_data = ReadFileToString(path) | |
| 166 transformed_data = Transform(original_data) | |
| 167 if original_data != transformed_data: | |
| 168 WriteStringToFile(transformed_data, path) | |
| 169 print "Rewrote %s" % (path) | |
| 170 | |
| 171 | |
| 172 if __name__ == "__main__": | |
| 173 main() | |
| OLD | NEW |