|
| 1 | +// Copyright (c) 2024, 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 com.yubico.internal.util.BinaryUtil |
| 28 | +import com.yubico.internal.util.CertificateParser |
| 29 | +import com.yubico.webauthn.TestAuthenticator |
| 30 | +import com.yubico.webauthn.data.ByteArray |
| 31 | +import com.yubico.webauthn.data.Generators.arbitraryByteArray |
| 32 | +import com.yubico.webauthn.data.Generators.shrinkByteArray |
| 33 | +import org.bouncycastle.asn1.DEROctetString |
| 34 | +import org.junit.runner.RunWith |
| 35 | +import org.scalatest.funspec.AnyFunSpec |
| 36 | +import org.scalatest.matchers.should.Matchers |
| 37 | +import org.scalatestplus.junit.JUnitRunner |
| 38 | +import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks |
| 39 | + |
| 40 | +import java.security.cert.X509Certificate |
| 41 | +import scala.jdk.OptionConverters.RichOptional |
| 42 | + |
| 43 | +@RunWith(classOf[JUnitRunner]) |
| 44 | +class CertificateUtilSpec |
| 45 | + extends AnyFunSpec |
| 46 | + with Matchers |
| 47 | + with ScalaCheckDrivenPropertyChecks { |
| 48 | + describe("parseFidoSerNumExtension") { |
| 49 | + val idFidoGenCeSernum = "1.3.6.1.4.1.45724.1.1.2" |
| 50 | + |
| 51 | + it("correctly parses the id-fido-gen-ce-sernum extension.") { |
| 52 | + forAll( |
| 53 | + // 500-byte long serial numbers are not realistic, but would be valid DER data. |
| 54 | + sizeRange(500) |
| 55 | + ) { |
| 56 | + // Using Array[Byte] here causes an (almost) infinite loop in the shrinker in case of failure. |
| 57 | + // See: https://github.com/typelevel/scalacheck/issues/968#issuecomment-2594018791 |
| 58 | + sernum: ByteArray => |
| 59 | + val (cert, _): (X509Certificate, _) = TestAuthenticator |
| 60 | + .generateAttestationCertificate( |
| 61 | + extensions = List( |
| 62 | + ( |
| 63 | + idFidoGenCeSernum, |
| 64 | + false, |
| 65 | + new DEROctetString(sernum.getBytes), |
| 66 | + ) |
| 67 | + ) |
| 68 | + ) |
| 69 | + |
| 70 | + val result = |
| 71 | + CertificateUtil |
| 72 | + .parseFidoSernumExtension(cert) |
| 73 | + .toScala |
| 74 | + result should equal(Some(sernum)) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + it("returns empty when cert has no id-fido-gen-ce-sernum extension.") { |
| 79 | + val (cert, _): (X509Certificate, _) = |
| 80 | + TestAuthenticator.generateAttestationCertificate(extensions = Nil) |
| 81 | + val result = |
| 82 | + CertificateUtil |
| 83 | + .parseFidoSernumExtension(cert) |
| 84 | + .toScala |
| 85 | + result should be(None) |
| 86 | + } |
| 87 | + |
| 88 | + it("correctly parses the serial number from a real YubiKey enterprise attestation certificate.") { |
| 89 | + val cert = CertificateParser.parsePem("""-----BEGIN CERTIFICATE----- |
| 90 | + |MIIC8zCCAdugAwIBAgIJAKr/KiUzkKrgMA0GCSqGSIb3DQEBCwUAMC8xLTArBgNV |
| 91 | + |BAMMJFl1YmljbyBGSURPIFJvb3QgQ0EgU2VyaWFsIDQ1MDIwMzU1NjAgFw0yNDA1 |
| 92 | + |MDEwMDAwMDBaGA8yMDYwMDQzMDAwMDAwMFowcDELMAkGA1UEBhMCU0UxEjAQBgNV |
| 93 | + |BAoMCVl1YmljbyBBQjEiMCAGA1UECwwZQXV0aGVudGljYXRvciBBdHRlc3RhdGlv |
| 94 | + |bjEpMCcGA1UEAwwgWXViaWNvIEZpZG8gRUUgKFNlcmlhbD0yODI5OTAwMykwWTAT |
| 95 | + |BgcqhkjOPQIBBggqhkjOPQMBBwNCAATImNkI1cwqkW5B3qNrY3pc8zBLhvGyfyfS |
| 96 | + |WCLrODSe8xaRPcZoXYGGwZ0Ua/Hp5nxyD+w1hjS9O9gx8mSDvp+zo4GZMIGWMBMG |
| 97 | + |CisGAQQBgsQKDQEEBQQDBQcBMBUGCysGAQQBguUcAQECBAYEBAGvzvswIgYJKwYB |
| 98 | + |BAGCxAoCBBUxLjMuNi4xLjQuMS40MTQ4Mi4xLjcwEwYLKwYBBAGC5RwCAQEEBAMC |
| 99 | + |AiQwIQYLKwYBBAGC5RwBAQQEEgQQuQ59wTFuT+6iWlamZqZw/jAMBgNVHRMBAf8E |
| 100 | + |AjAAMA0GCSqGSIb3DQEBCwUAA4IBAQAFEMXw1HUDC/TfMFxp2ZrmgQLa5fmzs2Jh |
| 101 | + |C22TUAuY26CYT5dmMUsS5aJd96MtC5gKS57h1auGr2Y4FMxQS9FJHzXAzAtYJfKh |
| 102 | + |j1uS2BSTXf9GULdFKcWvvv50kJ2VmXLge3UgHDBJ8LwrDlZFyISeMZ8jSbmrNu2c |
| 103 | + |8uNBBSfqdor+5H91L1brC9yYneHdxYk6YiEvDBxWjiMa9DQuySh/4a21nasgt0cB |
| 104 | + |prEbfFOLRDm7GDsRTPyefZjZ84yi4Ao+15x+7DM0UwudEVtjOWB2BJtJyxIkXXNF |
| 105 | + |iWFZaxezq0Xt2Kl2sYnMR97ynw/U4TzZDjgb56pN81oKz8Od9B/u |
| 106 | + |-----END CERTIFICATE-----""".stripMargin) |
| 107 | + |
| 108 | + val result = |
| 109 | + CertificateUtil |
| 110 | + .parseFidoSernumExtension(cert) |
| 111 | + .toScala |
| 112 | + |
| 113 | + result should equal(Some(ByteArray.fromHex("01AFCEFB"))) |
| 114 | + |
| 115 | + // For YubiKeys, the sernum octet string represents a big-endian integer |
| 116 | + BinaryUtil.getUint32(result.get.getBytes) should be(28299003) |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments