Source release 15.0.0
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
# Copyright 2017 Google Inc. All Rights Reserved.
|
||||
# Copyright 2017 Google LLC. All Rights Reserved.
|
||||
|
||||
import base64
|
||||
import datetime
|
||||
import os
|
||||
import shutil
|
||||
@@ -15,7 +16,7 @@ from cryptography.hazmat.primitives.asymmetric import padding
|
||||
from cryptography.x509 import oid
|
||||
|
||||
import oem_certificate
|
||||
import oem_certificate_generator_helper as oem_cert_gen_helper
|
||||
import oem_certificate_test_helper as oem_cert_test_helper
|
||||
|
||||
|
||||
class ArgParseObject(object):
|
||||
@@ -32,7 +33,7 @@ class OemCertificateTest(unittest.TestCase):
|
||||
system_id)
|
||||
|
||||
def test_generate_csr(self):
|
||||
args = oem_cert_gen_helper.setup_csr_args()
|
||||
args = oem_cert_test_helper.setup_csr_args()
|
||||
oem_certificate.generate_csr(args)
|
||||
# Verify CSR.
|
||||
csr = x509.load_pem_x509_csr(args.output_csr_file.getvalue(),
|
||||
@@ -67,7 +68,7 @@ class OemCertificateTest(unittest.TestCase):
|
||||
private_key.public_key().public_numbers())
|
||||
|
||||
def test_generate_csr_(self):
|
||||
args = oem_cert_gen_helper.setup_csr_args(common_name='MyCommonName')
|
||||
args = oem_cert_test_helper.setup_csr_args(common_name='MyCommonName')
|
||||
oem_certificate.generate_csr(args)
|
||||
# Verify CSR.
|
||||
csr = x509.load_pem_x509_csr(args.output_csr_file.getvalue(),
|
||||
@@ -103,7 +104,7 @@ class OemCertificateTest(unittest.TestCase):
|
||||
private_key.public_key().public_numbers())
|
||||
|
||||
def test_generate_csr_with_keysize4096_and_passphrase(self):
|
||||
args = oem_cert_gen_helper.setup_csr_args(
|
||||
args = oem_cert_test_helper.setup_csr_args(
|
||||
key_size=4096, passphrase='passphrase_4096')
|
||||
oem_certificate.generate_csr(args)
|
||||
private_key = serialization.load_der_private_key(
|
||||
@@ -119,14 +120,14 @@ class OemCertificateTest(unittest.TestCase):
|
||||
private_key.public_key().public_numbers())
|
||||
|
||||
def test_generate_intermediate_certificate(self):
|
||||
csr_args = oem_cert_gen_helper.setup_csr_args()
|
||||
csr_args = oem_cert_test_helper.setup_csr_args()
|
||||
oem_certificate.generate_csr(csr_args)
|
||||
csr_bytes = csr_args.output_csr_file.getvalue()
|
||||
csr = x509.load_pem_x509_csr(csr_bytes, backends.default_backend())
|
||||
|
||||
root_key, root_certificate = (
|
||||
oem_cert_gen_helper.create_root_certificate_and_key())
|
||||
args = oem_cert_gen_helper.setup_intermediate_cert_args(
|
||||
oem_cert_test_helper.create_root_certificate_and_key())
|
||||
args = oem_cert_test_helper.setup_intermediate_cert_args(
|
||||
csr_bytes, root_key, root_certificate)
|
||||
oem_certificate.generate_intermediate_certificate(args)
|
||||
|
||||
@@ -150,18 +151,20 @@ class OemCertificateTest(unittest.TestCase):
|
||||
|
||||
def test_generate_intermediate_with_cert_mismatch_root_cert_and_key(self):
|
||||
root_key1, _ = (
|
||||
oem_cert_gen_helper.create_root_certificate_and_key())
|
||||
_, root_certificate2 = oem_cert_gen_helper.create_root_certificate_and_key()
|
||||
args = oem_cert_gen_helper.setup_intermediate_cert_args(
|
||||
oem_cert_test_helper.create_root_certificate_and_key())
|
||||
_, root_certificate2 = oem_cert_test_helper.create_root_certificate_and_key(
|
||||
)
|
||||
args = oem_cert_test_helper.setup_intermediate_cert_args(
|
||||
'some csr data', root_key1, root_certificate2)
|
||||
with self.assertRaises(ValueError) as context:
|
||||
oem_certificate.generate_intermediate_certificate(args)
|
||||
self.assertTrue('certificate does not match' in str(context.exception))
|
||||
|
||||
def test_generate_leaf_certificate(self):
|
||||
def test_generate_leaf_certificate_from_pem_intermediate_cert(self):
|
||||
intermediate_key_bytes, intermediate_certificate_bytes = (
|
||||
oem_cert_gen_helper.create_intermediate_certificate_and_key_bytes())
|
||||
args = oem_cert_gen_helper.setup_leaf_cert_args(
|
||||
oem_cert_test_helper.create_intermediate_certificate_and_key_bytes(
|
||||
pem_format=True))
|
||||
args = oem_cert_test_helper.setup_leaf_cert_args(
|
||||
intermediate_key_bytes, intermediate_certificate_bytes)
|
||||
oem_certificate.generate_leaf_certificate(args)
|
||||
|
||||
@@ -173,7 +176,7 @@ class OemCertificateTest(unittest.TestCase):
|
||||
intermediate_cert = certificates[1]
|
||||
leaf_cert = certificates[0]
|
||||
self.assertEqual(
|
||||
intermediate_cert.public_bytes(serialization.Encoding.DER),
|
||||
intermediate_cert.public_bytes(serialization.Encoding.PEM),
|
||||
intermediate_certificate_bytes)
|
||||
intermediate_cert.public_key().verify(leaf_cert.signature,
|
||||
leaf_cert.tbs_certificate_bytes,
|
||||
@@ -199,23 +202,46 @@ class OemCertificateTest(unittest.TestCase):
|
||||
self.assertEqual(leaf_cert.public_key().public_numbers(),
|
||||
leaf_key.public_key().public_numbers())
|
||||
|
||||
def test_generate_leaf_certificate_from_der_intermediate_cert(self):
|
||||
intermediate_key_bytes, intermediate_certificate_bytes = (
|
||||
oem_cert_test_helper.create_intermediate_certificate_and_key_bytes(
|
||||
pem_format=False))
|
||||
args = oem_cert_test_helper.setup_leaf_cert_args(
|
||||
intermediate_key_bytes, intermediate_certificate_bytes)
|
||||
oem_certificate.generate_leaf_certificate(args)
|
||||
|
||||
certificate_chain = oem_certificate.X509CertificateChain.load_der(
|
||||
args.output_certificate_file.getvalue())
|
||||
|
||||
certificates = list(certificate_chain)
|
||||
self.assertEqual(len(certificates), 2)
|
||||
intermediate_cert = certificates[1]
|
||||
leaf_cert = certificates[0]
|
||||
self.assertEqual(
|
||||
intermediate_cert.public_bytes(serialization.Encoding.DER),
|
||||
intermediate_certificate_bytes)
|
||||
intermediate_cert.public_key().verify(leaf_cert.signature,
|
||||
leaf_cert.tbs_certificate_bytes,
|
||||
padding.PKCS1v15(),
|
||||
leaf_cert.signature_hash_algorithm)
|
||||
|
||||
def test_generate_leaf_certificate_with_keysize4096_and_passphrase(self):
|
||||
intermediate_key_bytes, intermediate_certificate_bytes = (
|
||||
oem_cert_gen_helper.create_intermediate_certificate_and_key_bytes())
|
||||
args = oem_cert_gen_helper.setup_leaf_cert_args(
|
||||
oem_cert_test_helper.create_intermediate_certificate_and_key_bytes())
|
||||
args = oem_cert_test_helper.setup_leaf_cert_args(
|
||||
intermediate_key_bytes,
|
||||
intermediate_certificate_bytes,
|
||||
key_size=4096,
|
||||
passphrase='leaf passphrase')
|
||||
oem_certificate.generate_leaf_certificate(args)
|
||||
serialization.load_der_private_key(
|
||||
leaf_key = serialization.load_der_private_key(
|
||||
args.output_private_key_file.getvalue(),
|
||||
'leaf passphrase',
|
||||
backend=backends.default_backend())
|
||||
self.assertEqual(4096, args.key_size)
|
||||
self.assertEqual(4096, leaf_key.key_size)
|
||||
|
||||
def test_get_csr_info(self):
|
||||
args = oem_cert_gen_helper.setup_csr_args()
|
||||
args = oem_cert_test_helper.setup_csr_args()
|
||||
oem_certificate.generate_csr(args)
|
||||
args.file = StringIO.StringIO(args.output_csr_file.getvalue())
|
||||
output = StringIO.StringIO()
|
||||
@@ -230,9 +256,33 @@ class OemCertificateTest(unittest.TestCase):
|
||||
Key Size: 4096"""
|
||||
self.assertEqual(output.getvalue(), textwrap.dedent(expected_info))
|
||||
|
||||
def test_get_certificate_info(self):
|
||||
def test_get_pem_certificate_info(self):
|
||||
_, intermediate_certificate_bytes = (
|
||||
oem_cert_gen_helper.create_intermediate_certificate_and_key_bytes())
|
||||
oem_cert_test_helper.create_intermediate_certificate_and_key_bytes(
|
||||
pem_format=True))
|
||||
args = ArgParseObject()
|
||||
args.file = StringIO.StringIO(intermediate_certificate_bytes)
|
||||
output = StringIO.StringIO()
|
||||
oem_certificate.get_info(args, output)
|
||||
expected_info = """\
|
||||
Certificate Subject Name:
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.6, name=countryName)>, value=u'US')>
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.8, name=stateOrProvinceName)>, value=u'WA')>
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.7, name=localityName)>, value=u'Kirkland')>
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.10, name=organizationName)>, value=u'CompanyXYZ')>
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.11, name=organizationalUnitName)>, value=u'ContentProtection')>
|
||||
Issuer Name:
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.3, name=commonName)>, value=u'root_cert')>
|
||||
Key Size: 4096
|
||||
Widevine System Id: 2001
|
||||
Not valid before: 2001-08-09 00:00:00
|
||||
Not valid after: 2001-11-17 00:00:00"""
|
||||
self.assertEqual(output.getvalue(), textwrap.dedent(expected_info))
|
||||
|
||||
def test_get_der_certificate_info(self):
|
||||
_, intermediate_certificate_bytes = (
|
||||
oem_cert_test_helper.create_intermediate_certificate_and_key_bytes(
|
||||
pem_format=False))
|
||||
args = ArgParseObject()
|
||||
args.file = StringIO.StringIO(intermediate_certificate_bytes)
|
||||
output = StringIO.StringIO()
|
||||
@@ -254,8 +304,8 @@ class OemCertificateTest(unittest.TestCase):
|
||||
|
||||
def test_get_certificate_chain_info(self):
|
||||
intermediate_key_bytes, intermediate_certificate_bytes = (
|
||||
oem_cert_gen_helper.create_intermediate_certificate_and_key_bytes())
|
||||
args = oem_cert_gen_helper.setup_leaf_cert_args(
|
||||
oem_cert_test_helper.create_intermediate_certificate_and_key_bytes())
|
||||
args = oem_cert_test_helper.setup_leaf_cert_args(
|
||||
intermediate_key_bytes, intermediate_certificate_bytes)
|
||||
oem_certificate.generate_leaf_certificate(args)
|
||||
args.file = StringIO.StringIO(args.output_certificate_file.getvalue())
|
||||
@@ -294,6 +344,92 @@ class OemCertificateTest(unittest.TestCase):
|
||||
Not valid after: 2001-11-17 00:00:00"""
|
||||
self.assertEqual(output.getvalue(), textwrap.dedent(expected_info))
|
||||
|
||||
def test_get_certificate_chain_info_fixed_input(self):
|
||||
# This was generated from args.output_certificate_file in the test above.
|
||||
data_b64 = (
|
||||
'MIIJCQYJKoZIhvcNAQcCoIII+jCCCPYCAQ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R0bUh54FBHM+PttUc49OaNOMTZNFi2KZmYO30vT256'
|
||||
'OSGis06hC6pggIzGA7tKP4ATSOBA1fe27ef0YD6pD2dAdQnhaXguDo3/eHbpUXXpqLr6'
|
||||
'nm0mTbNTgcC673L5YA8qpQkAzk9vLg4UaslMbPfeKM8rqduJFcjTyVY3C4jBC0qxf6z6'
|
||||
'vpWbEO7UpHHdfvWe9DEBODFbyXMxAA==')
|
||||
args = oem_cert_test_helper.ArgParseObject()
|
||||
args.file = StringIO.StringIO(base64.b64decode(data_b64))
|
||||
output = StringIO.StringIO()
|
||||
oem_certificate.get_info(args, output)
|
||||
expected_info = """\
|
||||
Certificate Subject Name:
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.3, name=commonName)>, value=u'2001-leaf')>
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.6, name=countryName)>, value=u'US')>
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.8, name=stateOrProvinceName)>, value=u'WA')>
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.7, name=localityName)>, value=u'Kirkland')>
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.10, name=organizationName)>, value=u'CompanyXYZ')>
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.11, name=organizationalUnitName)>, value=u'ContentProtection')>
|
||||
Issuer Name:
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.6, name=countryName)>, value=u'US')>
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.8, name=stateOrProvinceName)>, value=u'WA')>
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.7, name=localityName)>, value=u'Kirkland')>
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.10, name=organizationName)>, value=u'CompanyXYZ')>
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.11, name=organizationalUnitName)>, value=u'ContentProtection')>
|
||||
Key Size: 1024
|
||||
Widevine System Id: 2001
|
||||
Not valid before: 2001-08-09 00:00:00
|
||||
Not valid after: 2023-07-05 00:00:00
|
||||
|
||||
Certificate Subject Name:
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.6, name=countryName)>, value=u'US')>
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.8, name=stateOrProvinceName)>, value=u'WA')>
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.7, name=localityName)>, value=u'Kirkland')>
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.10, name=organizationName)>, value=u'CompanyXYZ')>
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.11, name=organizationalUnitName)>, value=u'ContentProtection')>
|
||||
Issuer Name:
|
||||
<NameAttribute(oid=<ObjectIdentifier(oid=2.5.4.3, name=commonName)>, value=u'root_cert')>
|
||||
Key Size: 4096
|
||||
Widevine System Id: 2001
|
||||
Not valid before: 2001-08-09 00:00:00
|
||||
Not valid after: 2001-11-17 00:00:00"""
|
||||
self.assertEqual(output.getvalue(), textwrap.dedent(expected_info))
|
||||
|
||||
def test_secure_erase(self):
|
||||
args = ArgParseObject()
|
||||
args.file = tempfile.NamedTemporaryFile(delete=False)
|
||||
|
||||
Reference in New Issue
Block a user