OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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 """ Read a CRX file and write out the App ID and the Full Hash of the ID. | 6 """ Read a CRX file and write out the App ID and the Full Hash of the ID. |
7 See: http://code.google.com/chrome/extensions/crx.html | 7 See: http://code.google.com/chrome/extensions/crx.html |
8 and 'http://stackoverflow.com/questions/' | 8 and 'http://stackoverflow.com/questions/' |
9 + '1882981/google-chrome-alphanumeric-hashes-to-identify-extensions' | 9 + '1882981/google-chrome-alphanumeric-hashes-to-identify-extensions' |
10 for docs on the format. | 10 for docs on the format. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 version = f.read(4) | 68 version = f.read(4) |
69 if not version[0] != EXPECTED_CRX_VERSION: | 69 if not version[0] != EXPECTED_CRX_VERSION: |
70 raise Exception('Invalid version number: %s (expecting %s)' % | 70 raise Exception('Invalid version number: %s (expecting %s)' % |
71 (version, | 71 (version, |
72 EXPECTED_CRX_VERSION)) | 72 EXPECTED_CRX_VERSION)) |
73 pub_key_len_bytes = HexToInt(f.read(4)) | 73 pub_key_len_bytes = HexToInt(f.read(4)) |
74 sig_len_bytes = HexToInt(f.read(4)) | 74 sig_len_bytes = HexToInt(f.read(4)) |
75 pub_key = f.read(pub_key_len_bytes) | 75 pub_key = f.read(pub_key_len_bytes) |
76 return pub_key | 76 return pub_key |
77 | 77 |
78 def GetPublicKeyFromPath(filepath): | 78 def GetPublicKeyFromPath(filepath, is_win_path=False): |
79 # Normalize the path for windows to have capital drive letters. | 79 # Normalize the path for windows to have capital drive letters. |
80 # We intentionally don't check if sys.platform == 'win32' and just | 80 # We intentionally don't check if sys.platform == 'win32' and just |
81 # check if this looks like drive letter so that we can test this | 81 # check if this looks like drive letter so that we can test this |
82 # even on posix systems. | 82 # even on posix systems. |
83 if (len(filepath) >= 2 and | 83 if (len(filepath) >= 2 and |
84 filepath[0].islower() and | 84 filepath[0].islower() and |
85 filepath[1] == ':'): | 85 filepath[1] == ':'): |
86 return filepath[0].upper() + filepath[1:] | 86 filepath = filepath[0].upper() + filepath[1:] |
| 87 |
| 88 # On Windows, filepaths are encoded using UTF-16, little endian byte order, |
| 89 # using "wide characters" that are 16 bits in size. On POSIX systems, the |
| 90 # encoding is generally UTF-8, which has the property of being equivalent to |
| 91 # ASCII when only ASCII characters are in the path. |
| 92 if is_win_path: |
| 93 filepath = filepath.encode('utf-16le') |
| 94 |
87 return filepath | 95 return filepath |
88 | 96 |
89 def GetPublicKeyUnpacked(f, filepath): | 97 def GetPublicKeyUnpacked(f, filepath): |
90 manifest = json.load(f) | 98 manifest = json.load(f) |
91 if 'key' not in manifest: | 99 if 'key' not in manifest: |
92 # Use the path as the public key. | 100 # Use the path as the public key. |
93 # See Extension::GenerateIdForPath in extension.cc | 101 # See Extension::GenerateIdForPath in extension.cc |
94 return GetPublicKeyFromPath(filepath) | 102 return GetPublicKeyFromPath(filepath) |
95 else: | 103 else: |
96 return base64.standard_b64decode(manifest['key']) | 104 return base64.standard_b64decode(manifest['key']) |
97 | 105 |
98 def HasPublicKey(filename): | 106 def HasPublicKey(filename): |
99 if os.path.isdir(filename): | 107 if os.path.isdir(filename): |
100 with open(os.path.join(filename, 'manifest.json'), 'rb') as f: | 108 with open(os.path.join(filename, 'manifest.json'), 'rb') as f: |
101 manifest = json.load(f) | 109 manifest = json.load(f) |
102 return 'key' in manifest | 110 return 'key' in manifest |
103 return False | 111 return False |
104 | 112 |
105 def GetPublicKey(filename, from_file_path): | 113 def GetPublicKey(filename, from_file_path, is_win_path=False): |
106 if from_file_path: | 114 if from_file_path: |
107 return GetPublicKeyFromPath(filename) | 115 return GetPublicKeyFromPath( |
| 116 filename, is_win_path=is_win_path) |
108 | 117 |
109 pub_key = '' | 118 pub_key = '' |
110 if os.path.isdir(filename): | 119 if os.path.isdir(filename): |
111 # Assume it's an unpacked extension | 120 # Assume it's an unpacked extension |
112 f = open(os.path.join(filename, 'manifest.json'), 'rb') | 121 f = open(os.path.join(filename, 'manifest.json'), 'rb') |
113 pub_key = GetPublicKeyUnpacked(f, filename) | 122 pub_key = GetPublicKeyUnpacked(f, filename) |
114 f.close() | 123 f.close() |
115 else: | 124 else: |
116 # Assume it's a packed extension. | 125 # Assume it's a packed extension. |
117 f = open(filename, 'rb') | 126 f = open(filename, 'rb') |
118 pub_key = GetPublicKeyPacked(f) | 127 pub_key = GetPublicKeyPacked(f) |
119 f.close() | 128 f.close() |
120 return pub_key | 129 return pub_key |
121 | 130 |
122 def GetCRXHash(filename, from_file_path=False): | 131 def GetCRXHash(filename, from_file_path=False, is_win_path=False): |
123 pub_key = GetPublicKey(filename, from_file_path) | 132 pub_key = GetPublicKey(filename, from_file_path, is_win_path=is_win_path) |
124 pub_key_hash = hashlib.sha256(pub_key).digest() | 133 pub_key_hash = hashlib.sha256(pub_key).digest() |
125 return HexTo256(pub_key_hash) | 134 return HexTo256(pub_key_hash) |
126 | 135 |
127 def GetCRXAppID(filename, from_file_path=False): | 136 def GetCRXAppID(filename, from_file_path=False, is_win_path=False): |
128 pub_key = GetPublicKey(filename, from_file_path) | 137 pub_key = GetPublicKey(filename, from_file_path, is_win_path=is_win_path) |
129 pub_key_hash = hashlib.sha256(pub_key).digest() | 138 pub_key_hash = hashlib.sha256(pub_key).digest() |
130 # AppID is the MPDecimal of only the first 128 bits of the hash. | 139 # AppID is the MPDecimal of only the first 128 bits of the hash. |
131 return HexToMPDecimal(pub_key_hash[:128/8]) | 140 return HexToMPDecimal(pub_key_hash[:128/8]) |
132 | 141 |
133 def main(argv): | 142 def main(argv): |
134 if len(argv) != 2: | 143 if len(argv) != 2: |
135 usage(argv) | 144 usage(argv) |
136 return 1 | 145 return 1 |
137 print 'Raw Bytes: %s' % GetCRXHash(sys.argv[1]) | 146 print 'Raw Bytes: %s' % GetCRXHash(sys.argv[1]) |
138 print 'AppID: %s' % GetCRXAppID(sys.argv[1]) | 147 print 'AppID: %s' % GetCRXAppID(sys.argv[1]) |
139 | 148 |
140 | 149 |
141 if __name__ == '__main__': | 150 if __name__ == '__main__': |
142 sys.exit(main(sys.argv)) | 151 sys.exit(main(sys.argv)) |
OLD | NEW |