1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
#!/usr/bin/env python3
# Copyright 2014 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
This utility takes a JSON input that describes a CRLSet and produces a
CRLSet from it.
The input is taken on stdin and is a dict with the following keys:
- BlockedBySPKI: An array of strings, where each string is a filename
containing a PEM certificate, from which an SPKI will be extracted.
- BlockedByHash: A dict of string to an array of strings. The dict key is
a filename containing a PEM certificate, representing the issuer cert,
while the array of strings contain the filenames of PEM format
certificates whose serials are blocked.
- LimitedSubjects: A dict of string to an array of strings, where the key is
a filename containing a PEM format certificate, and the strings are the
filenames of PEM format certificates. Certificates that share a Subject
with the key will be restricted to the set of SPKIs extracted from the
files in the values.
- Sequence: An optional integer sequence number to use for the CRLSet. If
not present, defaults to 1.
For example:
{
"BlockedBySPKI": ["/tmp/blocked-certificate"],
"BlockedByHash": {
"/tmp/intermediate-certificate": [1, 2, 3]
},
"LimitedSubjects": {
"/tmp/limited-certificate": [
"/tmp/limited-certificate",
"/tmp/limited-certificate2"
]
},
"Sequence": 23
}
"""
import base64
import collections
import hashlib
import json
import optparse
import six
import struct
import sys
def _pem_cert_to_binary(pem_filename):
"""Decodes the first PEM-encoded certificate in a given file into binary
Args:
pem_filename: A filename that contains a PEM-encoded certificate. It may
contain additional data (keys, textual representation) which will be
ignored
Returns:
A byte array containing the decoded certificate data
"""
pem_data = ""
started = False
with open(pem_filename, 'r') as pem_file:
for line in pem_file:
if not started:
if line.startswith('-----BEGIN CERTIFICATE'):
started = True
else:
if line.startswith('-----END CERTIFICATE'):
break
pem_data += line[:-1].strip()
return base64.b64decode(pem_data)
def _parse_asn1_element(der_bytes):
"""Parses a DER-encoded tag/Length/Value into its component parts
Args:
der_bytes: A DER-encoded ASN.1 data type
Returns:
A tuple of the ASN.1 tag value, the length of the ASN.1 header that was
read, the sequence of bytes for the value, and then any data from der_bytes
that was not part of the tag/Length/Value.
"""
tag = six.indexbytes(der_bytes, 0)
length = six.indexbytes(der_bytes, 1)
header_length = 2
if length & 0x80:
num_length_bytes = length & 0x7f
length = 0
for i in range(2, 2 + num_length_bytes):
length <<= 8
length += six.indexbytes(der_bytes, i)
header_length = 2 + num_length_bytes
contents = der_bytes[:header_length + length]
rest = der_bytes[header_length + length:]
return (tag, header_length, contents, rest)
class ASN1Iterator(object):
"""Iterator that parses and iterates through a ASN.1 DER structure"""
def __init__(self, contents):
self._tag = 0
self._header_length = 0
self._rest = None
self._contents = contents
self.step_into()
def step_into(self):
"""Begins processing the inner contents of the next ASN.1 element"""
(self._tag, self._header_length, self._contents, self._rest) = (
_parse_asn1_element(self._contents[self._header_length:]))
def step_over(self):
"""Skips/ignores the next ASN.1 element"""
(self._tag, self._header_length, self._contents, self._rest) = (
_parse_asn1_element(self._rest))
def tag(self):
"""Returns the ASN.1 tag of the current element"""
return self._tag
def contents(self):
"""Returns the raw data of the current element"""
return self._contents
def encoded_value(self):
"""Returns the encoded value of the current element (i.e. without header)"""
return self._contents[self._header_length:]
def _der_cert_to_spki(der_bytes):
"""Returns the subjectPublicKeyInfo of a DER-encoded certificate
Args:
der_bytes: A DER-encoded certificate (RFC 5280)
Returns:
A byte array containing the subjectPublicKeyInfo
"""
iterator = ASN1Iterator(der_bytes)
iterator.step_into() # enter certificate structure
iterator.step_into() # enter TBSCertificate
iterator.step_over() # over version
iterator.step_over() # over serial
iterator.step_over() # over signature algorithm
iterator.step_over() # over issuer name
iterator.step_over() # over validity
iterator.step_over() # over subject name
return iterator.contents()
def der_cert_to_spki_hash(der_cert):
"""Gets the SHA-256 hash of the subjectPublicKeyInfo of a DER encoded cert
Args:
der_cert: A string containing the DER-encoded certificate
Returns:
The SHA-256 hash of the certificate, as a byte sequence
"""
return hashlib.sha256(_der_cert_to_spki(der_cert)).digest()
def pem_cert_file_to_spki_hash(pem_filename):
"""Gets the SHA-256 hash of the subjectPublicKeyInfo of a cert in a file
Args:
pem_filename: A file containing a PEM-encoded certificate.
Returns:
The SHA-256 hash of the first certificate in the file, as a byte sequence
"""
return der_cert_to_spki_hash(_pem_cert_to_binary(pem_filename))
def der_cert_to_subject_hash(der_bytes):
"""Returns SHA256(subject) of a DER-encoded certificate
Args:
der_bytes: A DER-encoded certificate (RFC 5280)
Returns:
The SHA-256 hash of the certificate's subject.
"""
iterator = ASN1Iterator(der_bytes)
iterator.step_into() # enter certificate structure
iterator.step_into() # enter TBSCertificate
iterator.step_over() # over version
iterator.step_over() # over serial
iterator.step_over() # over signature algorithm
iterator.step_over() # over issuer name
iterator.step_over() # over validity
return hashlib.sha256(iterator.contents()).digest()
def pem_cert_file_to_subject_hash(pem_filename):
"""Gets the SHA-256 hash of the subject of a cert in a file
Args:
pem_filename: A file containing a PEM-encoded certificate.
Returns:
The SHA-256 hash of the subject of the first certificate in the file, as a
byte sequence
"""
return der_cert_to_subject_hash(_pem_cert_to_binary(pem_filename))
def der_cert_to_serial(der_bytes):
"""Gets the serial of a DER-encoded certificate, omitting leading 0x00
Args:
der_bytes: A DER-encoded certificates (RFC 5280)
Returns:
The encoded serial number value (omitting tag and length), and omitting
any leading 0x00 used to indicate it is a positive INTEGER.
"""
iterator = ASN1Iterator(der_bytes)
iterator.step_into() # enter certificate structure
iterator.step_into() # enter TBSCertificate
iterator.step_over() # over version
raw_serial = iterator.encoded_value()
if six.indexbytes(raw_serial, 0) == 0x00 and len(raw_serial) > 1:
raw_serial = raw_serial[1:]
return raw_serial
def pem_cert_file_to_serial(pem_filename):
"""Gets the DER-encoded serial of a cert in a file, omitting leading 0x00
Args:
pem_filename: A file containing a PEM-encoded certificate.
Returns:
The DER-encoded serial as a byte sequence
"""
return der_cert_to_serial(_pem_cert_to_binary(pem_filename))
def main():
parser = optparse.OptionParser(description=sys.modules[__name__].__doc__)
parser.add_option('-o', '--output',
help='Specifies the output file. The default is stdout.')
options, _ = parser.parse_args()
outfile = sys.stdout
if options.output and options.output != '-':
outfile = open(options.output, 'wb')
config = json.load(sys.stdin)
blocked_spkis = [
base64.b64encode(pem_cert_file_to_spki_hash(pem_file)).decode('ascii')
for pem_file in config.get('BlockedBySPKI', [])
]
parents = {
pem_cert_file_to_spki_hash(pem_file): [
pem_cert_file_to_serial(issued_cert_file)
for issued_cert_file in issued_certs
]
for pem_file, issued_certs in config.get('BlockedByHash', {}).items()
}
limited_subjects = {
base64.b64encode(pem_cert_file_to_subject_hash(pem_file)).decode('ascii'):
[
base64.b64encode(pem_cert_file_to_spki_hash(filename)).decode('ascii')
for filename in allowed_pems
]
for pem_file, allowed_pems in config.get('LimitedSubjects', {}).items()
}
known_interception_spkis = [
base64.b64encode(pem_cert_file_to_spki_hash(pem_file)).decode('ascii')
for pem_file in config.get('KnownInterceptionSPKIs', [])
]
blocked_interception_spkis = [
base64.b64encode(pem_cert_file_to_spki_hash(pem_file)).decode('ascii')
for pem_file in config.get('BlockedInterceptionSPKIs', [])
]
header_json = {
'Version': 0,
'ContentType': 'CRLSet',
'Sequence': int(config.get("Sequence", 1)),
'NumParents': len(parents),
'BlockedSPKIs': blocked_spkis,
'LimitedSubjects': limited_subjects,
'KnownInterceptionSPKIs': known_interception_spkis,
'BlockedInterceptionSPKIs': blocked_interception_spkis
}
header = json.dumps(header_json)
outfile.write(struct.pack('<H', len(header)))
outfile.write(header.encode('utf-8'))
for spki, serials in sorted(parents.items()):
outfile.write(spki)
outfile.write(struct.pack('<I', len(serials)))
for serial in serials:
raw_serial = []
if not serial:
raw_serial = b'\x00'
else:
raw_serial = serial
outfile.write(struct.pack('<B', len(raw_serial)))
outfile.write(raw_serial)
return 0
if __name__ == '__main__':
sys.exit(main())
|