|
| 1 | +// Copyright (c) 2018, Yubico AB |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// Redistribution and use in source and binary forms, with or without |
| 5 | +// modification, are permitted provided that the following conditions are met: |
| 6 | +// |
| 7 | +// 1. Redistributions of source code must retain the above copyright notice, this |
| 8 | +// list of conditions and the following disclaimer. |
| 9 | +// |
| 10 | +// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | +// this list of conditions and the following disclaimer in the documentation |
| 12 | +// and/or other materials provided with the distribution. |
| 13 | +// |
| 14 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 15 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 17 | +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 18 | +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 | +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 20 | +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 21 | +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 22 | +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | + |
| 25 | +package com.yubico.webauthn.attestation |
| 26 | + |
| 27 | +import java.util.Collections |
| 28 | + |
| 29 | +import com.yubico.internal.util.CertificateParser |
| 30 | +import com.yubico.internal.util.JacksonCodecs |
| 31 | +import com.yubico.webauthn.attestation.resolver.SimpleAttestationResolver |
| 32 | +import com.yubico.webauthn.attestation.resolver.SimpleTrustResolver |
| 33 | +import com.yubico.webauthn.test.RealExamples |
| 34 | +import com.yubico.webauthn.FinishRegistrationOptions |
| 35 | +import com.yubico.webauthn.RelyingParty |
| 36 | +import com.yubico.webauthn.attestation.Transport.LIGHTNING |
| 37 | +import com.yubico.webauthn.attestation.Transport.NFC |
| 38 | +import com.yubico.webauthn.attestation.Transport.USB |
| 39 | +import com.yubico.webauthn.data.PublicKeyCredentialCreationOptions |
| 40 | +import com.yubico.webauthn.data.PublicKeyCredentialParameters |
| 41 | +import com.yubico.webauthn.test.Helpers |
| 42 | +import org.junit.runner.RunWith |
| 43 | +import org.scalatest.FunSpec |
| 44 | +import org.scalatest.Matchers |
| 45 | +import org.scalatest.junit.JUnitRunner |
| 46 | + |
| 47 | +import scala.collection.JavaConverters._ |
| 48 | + |
| 49 | + |
| 50 | +@RunWith(classOf[JUnitRunner]) |
| 51 | +class DeviceIdentificationSpec extends FunSpec with Matchers { |
| 52 | + |
| 53 | + def metadataService(metadataJson: String): StandardMetadataService = { |
| 54 | + val metadata = Collections.singleton(JacksonCodecs.json().readValue(metadataJson, classOf[MetadataObject])) |
| 55 | + new StandardMetadataService( |
| 56 | + new SimpleAttestationResolver(metadata, SimpleTrustResolver.fromMetadata(metadata)) |
| 57 | + ) |
| 58 | + } |
| 59 | + |
| 60 | + describe("A RelyingParty with the default StandardMetadataService") { |
| 61 | + |
| 62 | + describe("correctly identifies") { |
| 63 | + def check(expectedName: String, testData: RealExamples.Example, transports: Set[Transport]) { |
| 64 | + val rp = RelyingParty.builder() |
| 65 | + .identity(testData.rp) |
| 66 | + .credentialRepository(Helpers.CredentialRepository.empty) |
| 67 | + .metadataService(new StandardMetadataService()) |
| 68 | + .build() |
| 69 | + |
| 70 | + val result = rp.finishRegistration(FinishRegistrationOptions.builder() |
| 71 | + .request(PublicKeyCredentialCreationOptions.builder() |
| 72 | + .rp(testData.rp) |
| 73 | + .user(testData.user) |
| 74 | + .challenge(testData.attestation.challenge) |
| 75 | + .pubKeyCredParams(List(PublicKeyCredentialParameters.ES256).asJava) |
| 76 | + .build()) |
| 77 | + .response(testData.attestation.credential) |
| 78 | + .build()); |
| 79 | + |
| 80 | + result.isAttestationTrusted should be (true) |
| 81 | + result.getAttestationMetadata.isPresent should be (true) |
| 82 | + result.getAttestationMetadata.get.getDeviceProperties.isPresent should be (true) |
| 83 | + result.getAttestationMetadata.get.getDeviceProperties.get().get("displayName") should equal (expectedName) |
| 84 | + result.getAttestationMetadata.get.getTransports.isPresent should be (true) |
| 85 | + result.getAttestationMetadata.get.getTransports.get.asScala should equal (transports) |
| 86 | + } |
| 87 | + |
| 88 | + it("a YubiKey NEO.") { |
| 89 | + check("YubiKey NEO/NEO-n", RealExamples.YubiKeyNeo, Set(USB, NFC)) |
| 90 | + } |
| 91 | + it("a YubiKey 4.") { |
| 92 | + check("YubiKey 4/YubiKey 4 Nano", RealExamples.YubiKey4, Set(USB)) |
| 93 | + } |
| 94 | + it("a YubiKey 5 NFC.") { |
| 95 | + check("YubiKey 5 NFC", RealExamples.YubiKey5, Set(USB, NFC)) |
| 96 | + } |
| 97 | + it("a YubiKey 5 Nano.") { |
| 98 | + check("YubiKey 5 Series security key", RealExamples.YubiKey5Nano, Set(USB)) |
| 99 | + } |
| 100 | + it("a YubiKey 5Ci.") { |
| 101 | + check("YubiKey 5Ci", RealExamples.YubiKey5Ci, Set(USB, LIGHTNING)) |
| 102 | + } |
| 103 | + it("a Security Key by Yubico.") { |
| 104 | + check("Security Key by Yubico", RealExamples.SecurityKey, Set(USB)) |
| 105 | + } |
| 106 | + it("a Security Key 2 by Yubico.") { |
| 107 | + check("Security Key by Yubico", RealExamples.SecurityKey2, Set(USB)) |
| 108 | + } |
| 109 | + it("a Security Key NFC by Yubico.") { |
| 110 | + check("Security Key NFC by Yubico", RealExamples.SecurityKeyNfc, Set(USB, NFC)) |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + describe("The default AttestationResolver") { |
| 116 | + describe("successfully identifies") { |
| 117 | + def check(expectedName: String, testData: RealExamples.Example, transports: Set[Transport]) { |
| 118 | + val cert = CertificateParser.parseDer(testData.attestationCert.getBytes) |
| 119 | + val resolved = StandardMetadataService.createDefaultAttestationResolver().resolve(cert) |
| 120 | + resolved.isPresent should be (true) |
| 121 | + resolved.get.getDeviceProperties.isPresent should be (true) |
| 122 | + resolved.get.getDeviceProperties.get.get("displayName") should equal (expectedName) |
| 123 | + resolved.get.getTransports.isPresent should be (true) |
| 124 | + resolved.get.getTransports.get.asScala should equal (transports) |
| 125 | + } |
| 126 | + |
| 127 | + it("a YubiKey NEO.") { |
| 128 | + check("YubiKey NEO/NEO-n", RealExamples.YubiKeyNeo, Set(USB, NFC)) |
| 129 | + } |
| 130 | + it("a YubiKey 4.") { |
| 131 | + check("YubiKey 4/YubiKey 4 Nano", RealExamples.YubiKey4, Set(USB)) |
| 132 | + } |
| 133 | + it("a YubiKey 5 NFC.") { |
| 134 | + check("YubiKey 5 NFC", RealExamples.YubiKey5, Set(USB, NFC)) |
| 135 | + } |
| 136 | + it("a YubiKey 5 Nano.") { |
| 137 | + check("YubiKey 5 Series security key", RealExamples.YubiKey5Nano, Set(USB)) |
| 138 | + } |
| 139 | + it("a YubiKey 5Ci.") { |
| 140 | + check("YubiKey 5Ci", RealExamples.YubiKey5Ci, Set(USB, LIGHTNING)) |
| 141 | + } |
| 142 | + it("a Security Key by Yubico.") { |
| 143 | + check("Security Key by Yubico", RealExamples.SecurityKey, Set(USB)) |
| 144 | + } |
| 145 | + it("a Security Key 2 by Yubico.") { |
| 146 | + check("Security Key by Yubico", RealExamples.SecurityKey2, Set(USB)) |
| 147 | + } |
| 148 | + it("a Security Key NFC by Yubico.") { |
| 149 | + check("Security Key NFC by Yubico", RealExamples.SecurityKeyNfc, Set(USB, NFC)) |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | +} |
0 commit comments