| 1 |
# Copyright 2016-2018 Yubico AB |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
from yubihsm.defs import ALGORITHM, CAPABILITY, FIPS_STATUS, OBJECT |
| 16 |
from yubihsm.objects import AsymmetricKey, AttestationExtensions |
| 17 |
from yubihsm.exceptions import YubiHsmDeviceError |
| 18 |
from cryptography import x509 |
| 19 |
from cryptography.x509.oid import NameOID |
| 20 |
from cryptography.hazmat.backends import default_backend |
| 21 |
from cryptography.hazmat.primitives import hashes |
| 22 |
from cryptography.hazmat.primitives.asymmetric import ec, rsa, padding |
| 23 |
from cryptography.hazmat.primitives.serialization import Encoding |
| 24 |
from time import sleep |
| 25 |
import datetime |
| 26 |
import uuid |
| 27 |
import pytest |
| 28 |
|
| 29 |
|
| 30 |
def create_pair(session, algorithm): |
| 31 |
if algorithm == ALGORITHM.RSA_2048: |
| 32 |
private_key = rsa.generate_private_key(0x10001, 2048, default_backend()) |
| 33 |
elif algorithm == ALGORITHM.RSA_3072: |
| 34 |
private_key = rsa.generate_private_key(0x10001, 3072, default_backend()) |
| 35 |
elif algorithm == ALGORITHM.RSA_4096: |
| 36 |
private_key = rsa.generate_private_key(0x10001, 4096, default_backend()) |
| 37 |
else: |
| 38 |
ec_curve = ALGORITHM.to_curve(algorithm) |
| 39 |
private_key = ec.generate_private_key(ec_curve, default_backend()) |
| 40 |
|
| 41 |
builder = x509.CertificateBuilder() |
| 42 |
name = x509.Name( |
| 43 |
[x509.NameAttribute(NameOID.COMMON_NAME, "Test Attestation Certificate")] |
| 44 |
) |
| 45 |
builder = builder.subject_name(name) |
| 46 |
builder = builder.issuer_name(name) |
| 47 |
one_day = datetime.timedelta(1, 0, 0) |
| 48 |
builder = builder.not_valid_before(datetime.datetime.today() - one_day) |
| 49 |
builder = builder.not_valid_after(datetime.datetime.today() + one_day) |
| 50 |
builder = builder.serial_number(int(uuid.uuid4())) |
| 51 |
builder = builder.public_key(private_key.public_key()) |
| 52 |
certificate = builder.sign(private_key, hashes.SHA256(), default_backend()) |
| 53 |
|
| 54 |
attkey = AsymmetricKey.put( |
| 55 |
session, |
| 56 |
0, |
| 57 |
"Test Create Pair", |
| 58 |
0xFFFF, |
| 59 |
CAPABILITY.SIGN_ATTESTATION_CERTIFICATE, |
| 60 |
private_key, |
| 61 |
) |
| 62 |
|
| 63 |
certobj = attkey.put_certificate( |
| 64 |
"Test Create Pair", 0xFFFF, CAPABILITY.NONE, certificate |
| 65 |
) |
| 66 |
|
| 67 |
assert certificate.public_bytes(Encoding.DER) == certobj.get() |
| 68 |
return attkey, certobj, certificate |
| 69 |
|
| 70 |
|
| 71 |
ASYM_ALGOS = [ |
| 72 |
ALGORITHM.RSA_2048, |
| 73 |
ALGORITHM.RSA_3072, |
| 74 |
ALGORITHM.RSA_4096, |
| 75 |
ALGORITHM.EC_P256, |
| 76 |
ALGORITHM.EC_P384, |
| 77 |
ALGORITHM.EC_P521, |
| 78 |
ALGORITHM.EC_K256, |
| 79 |
ALGORITHM.EC_P224, |
| 80 |
] |
| 81 |
|
| 82 |
|
| 83 |
class TestAttestationAlgorithms: |
| 84 |
@pytest.fixture(scope="class", params=ASYM_ALGOS) |
| 85 |
def generated_key(self, request, session): |
| 86 |
algorithm = request.param |
| 87 |
key = AsymmetricKey.generate( |
| 88 |
session, |
| 89 |
0, |
| 90 |
"Test Attestation %x" % algorithm, |
| 91 |
0xFFFF, |
| 92 |
CAPABILITY.NONE, |
| 93 |
algorithm, |
| 94 |
) |
| 95 |
yield key |
| 96 |
key.delete() |
| 97 |
|
| 98 |
@pytest.mark.parametrize("algorithm", ASYM_ALGOS) |
| 99 |
def test_attestation(self, session, generated_key, algorithm, info): |
| 100 |
attkey, attcertobj, attcert = create_pair(session, algorithm) |
| 101 |
pubkey = attcert.public_key() |
| 102 |
|
| 103 |
# Verify signatures |
| 104 |
cert = generated_key.attest(attkey.id) |
| 105 |
data = cert.tbs_certificate_bytes |
| 106 |
if isinstance(pubkey, rsa.RSAPublicKey): |
| 107 |
pubkey.verify( |
| 108 |
cert.signature, |
| 109 |
data, |
| 110 |
padding.PKCS1v15(), |
| 111 |
cert.signature_hash_algorithm, |
| 112 |
) |
| 113 |
else: |
| 114 |
pubkey.verify(cert.signature, data, ec.ECDSA(cert.signature_hash_algorithm)) |
| 115 |
|
| 116 |
# Verify certificate extensions |
| 117 |
ext = AttestationExtensions.parse(cert) |
| 118 |
assert info.version == ext.firmware_version |
| 119 |
assert info.serial == ext.serial |
| 120 |
|
| 121 |
obj = generated_key.get_info() |
| 122 |
assert obj.origin == ext.origin |
| 123 |
assert obj.domains == ext.domains |
| 124 |
assert obj.capabilities == ext.capabilities |
| 125 |
assert obj.id == ext.object_id |
| 126 |
assert obj.label == ext.label |
| 127 |
|
| 128 |
# Verify correct public key |
| 129 |
assert cert.public_key() == generated_key.get_public_key() |
| 130 |
|
| 131 |
# Clean up |
| 132 |
attkey.delete() |
| 133 |
attcertobj.delete() |
| 134 |
|
| 135 |
|
| 136 |
def test_fips_approved_attestation(session, connect_hsm): |
| 137 |
try: |
| 138 |
session.get_fips_status() |
| 139 |
except YubiHsmDeviceError: |
| 140 |
pytest.skip("Non-FIPS YubiHSM") |
| 141 |
|
| 142 |
try: |
| 143 |
# Configure into FIPS approved mode |
| 144 |
session.reset_device() |
| 145 |
sleep(5.0) |
| 146 |
hsm = connect_hsm() |
| 147 |
new_session = hsm.create_session_derived(1, "password") |
| 148 |
new_session.set_fips_mode(True) |
| 149 |
assert new_session.get_fips_status() == FIPS_STATUS.PENDING |
| 150 |
|
| 151 |
# Change the default auth key |
| 152 |
authkey = new_session.get_object(1, OBJECT.AUTHENTICATION_KEY) |
| 153 |
authkey.change_password("password2") |
| 154 |
assert new_session.get_fips_status() == FIPS_STATUS.ON |
| 155 |
|
| 156 |
# Generate keys |
| 157 |
key = AsymmetricKey.generate( |
| 158 |
new_session, |
| 159 |
0, |
| 160 |
"Test FIPS Attestation", |
| 161 |
0xFFFF, |
| 162 |
CAPABILITY.NONE, |
| 163 |
ALGORITHM.RSA_2048, |
| 164 |
) |
| 165 |
|
| 166 |
attkey, attcertobj, attcert = create_pair(new_session, ALGORITHM.RSA_2048) |
| 167 |
cert = key.attest(attkey.id) |
| 168 |
ext = AttestationExtensions.parse(cert) |
| 169 |
assert ext.fips_approved in (True, None) |
| 170 |
|
| 171 |
finally: |
| 172 |
# Reset device to get out of FIPS approved mode |
| 173 |
new_session.reset_device() |
| 174 |
sleep(5.0) |