Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(594)

Side by Side Diff: net/data/parse_ocsp_unittest/annotate_test_data.py

Issue 1541213002: Adding OCSP Parser (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moving Verify to end. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # Copyright (c) 2015 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2015 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """This script is called without any arguments to re-format all of the *.pem 6 """This script is called without any arguments to re-format all of the *.pem
7 files in the script's parent directory. 7 files in the script's parent directory.
8 8
9 The main formatting change is to run "openssl asn1parse" for each of the PEM 9 The main formatting change is to run "openssl asn1parse" for each of the PEM
10 block sections (except for DATA), and add that output to the comment. 10 block sections (except for DATA), and add that output to the comment.
eroman 2016/02/16 23:42:26 This comment is not correct anymore.
svaldez 2016/02/17 16:46:47 Done.
11 11
12 Refer to the README file for more information. 12 Refer to the README file for more information.
13 """ 13 """
14 14
15 import glob 15 import glob
16 import os 16 import os
17 import re 17 import re
18 import base64 18 import base64
19 import subprocess 19 import subprocess
20 20
21 21
22 def Transform(file_data): 22 def Transform(file_data):
23 """Returns a transformed (formatted) version of file_data""" 23 """Returns a transformed (formatted) version of file_data"""
eroman 2016/02/16 23:42:26 Should probably have a TODO somewhere about not du
svaldez 2016/02/17 16:46:47 Done.
24 24
25 result = '' 25 result = ''
26 26
27 for block in GetPemBlocks(file_data): 27 for block in GetPemBlocks(file_data):
28 if len(result) != 0: 28 if len(result) != 0:
29 result += '\n' 29 result += '\n'
30 30
31 # If there was a user comment (non-script-generated comment) associated 31 # If there was a user comment (non-script-generated comment) associated
32 # with the block, output it immediately before the block. 32 # with the block, output it immediately before the block.
33 user_comment = GetUserComment(block.comment) 33 user_comment = GetUserComment(block.comment)
34 if user_comment: 34 if user_comment:
35 result += user_comment 35 result += user_comment
36 36
37 # For every block except for DATA, try to pretty print the parsed ASN.1. 37 # For every block except for DATA, try to pretty print the parsed ASN.1.
38 # DATA blocks likely would be DER in practice, but for the purposes of 38 # DATA blocks likely would be DER in practice, but for the purposes of
39 # these tests seeing its structure doesn't clarify 39 # these tests seeing its structure doesn't clarify
40 # anything and is just a distraction. 40 # anything and is just a distraction.
41 if block.name != 'DATA': 41 if block.name != 'DATA':
eroman 2016/02/16 23:42:26 Is this relevant?
svaldez 2016/02/17 16:46:47 Done.
42 generated_comment = GenerateCommentForBlock(block.name, block.data) 42 generated_comment = GenerateCommentForBlock(block.name, block.data)
43 result += generated_comment + '\n' 43 result += generated_comment + '\n'
44 44
45 45
46 result += MakePemBlockString(block.name, block.data) 46 result += MakePemBlockString(block.name, block.data)
47 47
48 return result 48 return result
49 49
50 50
51 def GenerateCommentForBlock(block_name, block_data): 51 def GenerateCommentForBlock(block_name, block_data):
52 """Returns a string describing the ASN.1 structure of block_data""" 52 """Returns a string describing the ASN.1 structure of block_data"""
53 53
54 p = subprocess.Popen(['openssl', 'asn1parse', '-i', '-inform', 'DER'], 54 p = subprocess.Popen(['openssl', 'asn1parse', '-i', '-inform', 'DER'],
55 stdout=subprocess.PIPE, stdin=subprocess.PIPE, 55 stdout=subprocess.PIPE, stdin=subprocess.PIPE,
56 stderr=subprocess.PIPE) 56 stderr=subprocess.PIPE)
57 stdout_data, stderr_data = p.communicate(input=block_data) 57 stdout_data, stderr_data = p.communicate(input=block_data)
58 generated_comment = '$ openssl asn1parse -i < [%s]\n%s' % (block_name, 58 generated_comment = '$ openssl asn1parse -i < [%s]\n%s' % (block_name,
59 stdout_data) 59 stdout_data)
60
61 if block_name == 'OCSP RESPONSE':
eroman 2016/02/16 23:42:26 Add a comment explaining this.
svaldez 2016/02/17 16:46:47 Done.
62 if '[HEX DUMP]:' in generated_comment:
63 (generated_comment, response) = generated_comment.split('[HEX DUMP]:', 1)
eroman 2016/02/16 23:42:26 I recall encountering a bug with OpenSSL's hex dum
svaldez 2016/02/17 16:46:47 Acknowledged.
64 response = response.replace('\n', '')
65 if len(response) % 2 != 0:
66 response = '0' + response
67 response = GenerateCommentForBlock('INNER', response.decode('hex'))
68 response = response.split('\n', 1)[1]
69 response = response.replace(': ', ': ')
70 generated_comment += '\n%s' % (response)
60 return generated_comment.strip('\n') 71 return generated_comment.strip('\n')
61 72
62 73
63 74
64 def GetUserComment(comment): 75 def GetUserComment(comment):
65 """Removes any script-generated lines (everything after the $ openssl line)""" 76 """Removes any script-generated lines (everything after the $ openssl line)"""
66 77
67 # Consider everything after "$ openssl" to be a generated comment. 78 # Consider everything after "$ openssl" to be a generated comment.
68 comment = comment.split('$ openssl asn1parse -i', 1)[0] 79 comment = comment.split('$ openssl asn1parse -i', 1)[0]
69 if IsEntirelyWhiteSpace(comment): 80 if IsEntirelyWhiteSpace(comment):
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 print "Processing %s ..." % (path) 166 print "Processing %s ..." % (path)
156 original_data = ReadFileToString(path) 167 original_data = ReadFileToString(path)
157 transformed_data = Transform(original_data) 168 transformed_data = Transform(original_data)
158 if original_data != transformed_data: 169 if original_data != transformed_data:
159 WriteStringToFile(transformed_data, path) 170 WriteStringToFile(transformed_data, path)
160 print "Rewrote %s" % (path) 171 print "Rewrote %s" % (path)
161 172
162 173
163 if __name__ == "__main__": 174 if __name__ == "__main__":
164 main() 175 main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698