| 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 CAPABILITY, ALGORITHM |
| 16 |
from yubihsm.objects import Opaque |
| 17 |
from yubihsm.exceptions import YubiHsmInvalidRequestError |
| 18 |
from cryptography import x509 |
| 19 |
from cryptography.hazmat.backends import default_backend |
| 20 |
from cryptography.hazmat.primitives import hashes |
| 21 |
from cryptography.hazmat.primitives.asymmetric import ec |
| 22 |
import os |
| 23 |
import uuid |
| 24 |
import datetime |
| 25 |
import pytest |
| 26 |
|
| 27 |
|
| 28 |
def get_test_cert(): |
| 29 |
private_key = ec.generate_private_key( |
| 30 |
ALGORITHM.EC_P256.to_curve(), default_backend() |
| 31 |
) |
| 32 |
name = x509.Name( |
| 33 |
[x509.NameAttribute(x509.oid.NameOID.COMMON_NAME, "Test Certificate")] |
| 34 |
) |
| 35 |
one_day = datetime.timedelta(1, 0, 0) |
| 36 |
certificate = ( |
| 37 |
x509.CertificateBuilder() |
| 38 |
.subject_name(name) |
| 39 |
.issuer_name(name) |
| 40 |
.not_valid_before(datetime.datetime.today() - one_day) |
| 41 |
.not_valid_after(datetime.datetime.today() + one_day) |
| 42 |
.serial_number(int(uuid.uuid4())) |
| 43 |
.public_key(private_key.public_key()) |
| 44 |
.sign(private_key, hashes.SHA256(), default_backend()) |
| 45 |
) |
| 46 |
return certificate |
| 47 |
|
| 48 |
|
| 49 |
def test_put_empty(session): |
| 50 |
# Can't put an empty object |
| 51 |
with pytest.raises(ValueError): |
| 52 |
Opaque.put( |
| 53 |
session, |
| 54 |
0, |
| 55 |
"Test PUT empty Opaque", |
| 56 |
1, |
| 57 |
CAPABILITY.NONE, |
| 58 |
ALGORITHM.OPAQUE_DATA, |
| 59 |
b"", |
| 60 |
) |
| 61 |
|
| 62 |
|
| 63 |
def test_data(session): |
| 64 |
for size in (1, 256, 1234, 1968): |
| 65 |
data = os.urandom(size) |
| 66 |
|
| 67 |
opaque = Opaque.put( |
| 68 |
session, |
| 69 |
0, |
| 70 |
"Test data Opaque", |
| 71 |
1, |
| 72 |
CAPABILITY.NONE, |
| 73 |
ALGORITHM.OPAQUE_DATA, |
| 74 |
data, |
| 75 |
) |
| 76 |
|
| 77 |
assert data == opaque.get() |
| 78 |
opaque.delete() |
| 79 |
|
| 80 |
|
| 81 |
def test_put_too_big(session): |
| 82 |
with pytest.raises(YubiHsmInvalidRequestError): |
| 83 |
Opaque.put( |
| 84 |
session, |
| 85 |
0, |
| 86 |
"Test large Opaque", |
| 87 |
1, |
| 88 |
CAPABILITY.NONE, |
| 89 |
ALGORITHM.OPAQUE_DATA, |
| 90 |
os.urandom(3064), |
| 91 |
) |
| 92 |
|
| 93 |
# Make sure our session is still working |
| 94 |
assert len(session.get_pseudo_random(123)) == 123 |
| 95 |
|
| 96 |
|
| 97 |
def test_certificate(session): |
| 98 |
certificate = get_test_cert() |
| 99 |
certobj = Opaque.put_certificate( |
| 100 |
session, 0, "Test certificate Opaque", 1, CAPABILITY.NONE, certificate |
| 101 |
) |
| 102 |
|
| 103 |
assert certificate == certobj.get_certificate() |
| 104 |
certobj.delete() |
| 105 |
|
| 106 |
|
| 107 |
def test_compressed_certificate(session): |
| 108 |
certificate = get_test_cert() |
| 109 |
|
| 110 |
certobj = Opaque.put_certificate( |
| 111 |
session, |
| 112 |
0, |
| 113 |
"Test certificate Opaque", |
| 114 |
1, |
| 115 |
CAPABILITY.NONE, |
| 116 |
certificate, |
| 117 |
) |
| 118 |
compressed_certobj = Opaque.put_certificate( |
| 119 |
session, |
| 120 |
0, |
| 121 |
"Test certificate Opaque Compressed", |
| 122 |
1, |
| 123 |
CAPABILITY.NONE, |
| 124 |
certificate, |
| 125 |
compress=True, |
| 126 |
) |
| 127 |
assert certobj.get_certificate() == compressed_certobj.get_certificate() |
| 128 |
assert certobj.get() != compressed_certobj.get() |
| 129 |
assert len(certobj.get()) > len(compressed_certobj.get()) |