Backed out changeset 9a0e6453ea8f (bug 1906768) for causing android wpt failures...
[gecko.git] / dom / origin-trials / gen-keys.py
blob6d00d695cf024e16878ee5ae9c08ce3c66d8fecc
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 import sys
7 from pyasn1.codec.der import decoder
8 from pyasn1.type import univ
9 from pyasn1_modules import pem
12 def public_key_to_string(file, name):
13 out = "static const unsigned char " + name + "[65] = { "
14 with open(file) as f:
15 substrate = pem.readPemFromFile(
16 f, "-----BEGIN PUBLIC KEY-----", "-----END PUBLIC KEY-----"
18 key = decoder.decode(substrate)
19 ident = key[0][0]
20 assert ident[0] == univ.ObjectIdentifier(
21 "1.2.840.10045.2.1"
22 ), "should be an ECPublicKey"
23 assert ident[1] == univ.ObjectIdentifier(
24 "1.2.840.10045.3.1.7"
25 ), "should be a EcdsaP256 key"
26 bits = key[0][1]
27 assert isinstance(bits, univ.BitString), "Should be a bit string"
28 assert len(bits) == 520, "Should be 520 bits (65 bytes)"
29 for byte in bits.asOctets():
30 out += hex(byte) + ", "
31 out += "};"
32 return out
35 def generate(output, test_key, prod_key):
36 output.write(public_key_to_string(test_key, "kTestKey"))
37 output.write("\n\n")
38 output.write(public_key_to_string(prod_key, "kProdKey"))
41 if __name__ == "__main__":
42 generate(sys.stdout, *sys.argv[1:])