Search in sources :

Example 6 with UnexpectedCheckedException

use of com.webauthn4j.util.exception.UnexpectedCheckedException in project webauthn4j by webauthn4j.

the class PackedAttestationStatementValidatorTest method generateCertPath.

private static AttestationCertificatePath generateCertPath(KeyPair pair, String signAlg) {
    try {
        Provider bcProvider = new BouncyCastleProvider();
        // Security.addProvider(bcProvider);
        long now = System.currentTimeMillis();
        Date from = new Date(now);
        Date to = new Date(from.getTime() + TimeUnit.DAYS.toMillis(1));
        X500Name dnName = new X500Name("C=ORG, O=Dummy Org, OU=Authenticator Attestation, CN=Dummy");
        BigInteger certSerialNumber = BigInteger.ZERO;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(from);
        calendar.add(Calendar.YEAR, 1);
        ContentSigner contentSigner = new JcaContentSignerBuilder(signAlg).build(pair.getPrivate());
        JcaX509v3CertificateBuilder certBuilder = new JcaX509v3CertificateBuilder(dnName, certSerialNumber, from, to, dnName, pair.getPublic());
        BasicConstraints basicConstraints = new BasicConstraints(false);
        certBuilder.addExtension(new ASN1ObjectIdentifier("2.5.29.19"), true, basicConstraints);
        X509Certificate certificate = new JcaX509CertificateConverter().setProvider(bcProvider).getCertificate(certBuilder.build(contentSigner));
        return new AttestationCertificatePath(Collections.singletonList(certificate));
    } catch (OperatorCreationException | CertificateException | CertIOException e) {
        throw new UnexpectedCheckedException(e);
    }
}
Also used : UnexpectedCheckedException(com.webauthn4j.util.exception.UnexpectedCheckedException) AttestationCertificatePath(com.webauthn4j.data.attestation.statement.AttestationCertificatePath) JcaContentSignerBuilder(org.bouncycastle.operator.jcajce.JcaContentSignerBuilder) Calendar(java.util.Calendar) ContentSigner(org.bouncycastle.operator.ContentSigner) CertificateException(java.security.cert.CertificateException) X500Name(org.bouncycastle.asn1.x500.X500Name) CertIOException(org.bouncycastle.cert.CertIOException) Date(java.util.Date) X509Certificate(java.security.cert.X509Certificate) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) JcaX509CertificateConverter(org.bouncycastle.cert.jcajce.JcaX509CertificateConverter) JcaX509v3CertificateBuilder(org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder) BigInteger(java.math.BigInteger) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) BasicConstraints(org.bouncycastle.asn1.x509.BasicConstraints) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 7 with UnexpectedCheckedException

use of com.webauthn4j.util.exception.UnexpectedCheckedException in project webauthn4j by webauthn4j.

the class ECUtil method createPublicKey.

@NonNull
private static PublicKey createPublicKey(@NonNull byte[] x, @NonNull byte[] y) {
    try {
        byte[] encodedPublicKey = ByteBuffer.allocate(1 + x.length + y.length).put(new byte[] { 0x04 }).put(x).put(y).array();
        ECPoint point = createECPoint(encodedPublicKey);
        return KeyFactory.getInstance("ECDSA").generatePublic(new ECPublicKeySpec(point, ECUtil.P_256_SPEC));
    } catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
        throw new UnexpectedCheckedException(e);
    }
}
Also used : UnexpectedCheckedException(com.webauthn4j.util.exception.UnexpectedCheckedException) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Example 8 with UnexpectedCheckedException

use of com.webauthn4j.util.exception.UnexpectedCheckedException in project webauthn4j by webauthn4j.

the class HKDFUtil method createMac.

@NonNull
private static Mac createMac(@NonNull byte[] key) {
    Mac mac;
    try {
        mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(key, "HmacSHA256"));
        return mac;
    } catch (NoSuchAlgorithmException | InvalidKeyException e) {
        throw new UnexpectedCheckedException(e);
    }
}
Also used : UnexpectedCheckedException(com.webauthn4j.util.exception.UnexpectedCheckedException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Example 9 with UnexpectedCheckedException

use of com.webauthn4j.util.exception.UnexpectedCheckedException in project webauthn4j by webauthn4j.

the class CertPathSerializer method serialize.

/**
 * {@inheritDoc}
 */
@Override
public void serialize(@NonNull CertPath value, @NonNull JsonGenerator gen, @NonNull SerializerProvider provider) throws IOException {
    try {
        gen.writeStartArray();
        for (Certificate certificate : value.getCertificates()) {
            gen.writeBinary(certificate.getEncoded());
        }
        gen.writeEndArray();
    } catch (CertificateEncodingException e) {
        throw new UnexpectedCheckedException(e);
    }
}
Also used : UnexpectedCheckedException(com.webauthn4j.util.exception.UnexpectedCheckedException) CertificateEncodingException(java.security.cert.CertificateEncodingException) Certificate(java.security.cert.Certificate)

Example 10 with UnexpectedCheckedException

use of com.webauthn4j.util.exception.UnexpectedCheckedException in project webauthn4j by webauthn4j.

the class DeviceCheckManagerTest method getAppleAppAttestCertFileTrustAnchorsProvider.

private CertFileTrustAnchorsProvider getAppleAppAttestCertFileTrustAnchorsProvider() {
    CertFileTrustAnchorsProvider certFileTrustAnchorsProvider = new CertFileTrustAnchorsProvider();
    try {
        Path path = Paths.get(ClassLoader.getSystemResource("apple-app-attest/Apple_App_Attestation_Root_CA.pem").toURI());
        certFileTrustAnchorsProvider.setCertificates(Collections.singletonList(path));
        return certFileTrustAnchorsProvider;
    } catch (URISyntaxException e) {
        throw new UnexpectedCheckedException(e);
    }
}
Also used : Path(java.nio.file.Path) CertFileTrustAnchorsProvider(com.webauthn4j.anchor.CertFileTrustAnchorsProvider) UnexpectedCheckedException(com.webauthn4j.util.exception.UnexpectedCheckedException) URISyntaxException(java.net.URISyntaxException)

Aggregations

UnexpectedCheckedException (com.webauthn4j.util.exception.UnexpectedCheckedException)18 NonNull (org.checkerframework.checker.nullness.qual.NonNull)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 InvalidKeyException (java.security.InvalidKeyException)4 SecretKeySpec (javax.crypto.spec.SecretKeySpec)4 CertificateEncodingException (java.security.cert.CertificateEncodingException)3 CertificateException (java.security.cert.CertificateException)3 JcaX509CertificateConverter (org.bouncycastle.cert.jcajce.JcaX509CertificateConverter)3 ContentSigner (org.bouncycastle.operator.ContentSigner)3 OperatorCreationException (org.bouncycastle.operator.OperatorCreationException)3 JcaContentSignerBuilder (org.bouncycastle.operator.jcajce.JcaContentSignerBuilder)3 CertFileTrustAnchorsProvider (com.webauthn4j.anchor.CertFileTrustAnchorsProvider)2 URISyntaxException (java.net.URISyntaxException)2 Path (java.nio.file.Path)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 Certificate (java.security.cert.Certificate)2 BadPaddingException (javax.crypto.BadPaddingException)2 Cipher (javax.crypto.Cipher)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 Mac (javax.crypto.Mac)2