Search in sources :

Example 1 with UnexpectedCheckedException

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

the class AuthenticatorSelectionCriteriaTest method equals_hashCode_with_serialization_test.

@Test
void equals_hashCode_with_serialization_test() {
    AuthenticatorSelectionCriteria instanceA = new AuthenticatorSelectionCriteria(AuthenticatorAttachment.CROSS_PLATFORM, true, UserVerificationRequirement.REQUIRED);
    byte[] serializedInstanceA;
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos)) {
        oos.writeObject(instanceA);
        serializedInstanceA = baos.toByteArray();
    } catch (IOException e) {
        throw new UnexpectedCheckedException(e);
    }
    AuthenticatorSelectionCriteria instanceB;
    try (ByteArrayInputStream bais = new ByteArrayInputStream(serializedInstanceA);
        ObjectInputStream ois = new ObjectInputStream(bais)) {
        instanceB = (AuthenticatorSelectionCriteria) ois.readObject();
    } catch (IOException | ClassNotFoundException e) {
        throw new UnexpectedCheckedException(e);
    }
    assertAll(() -> assertThat(instanceA).isEqualTo(instanceB), () -> assertThat(instanceA).hasSameHashCodeAs(instanceB));
}
Also used : UnexpectedCheckedException(com.webauthn4j.util.exception.UnexpectedCheckedException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.jupiter.api.Test)

Example 2 with UnexpectedCheckedException

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

the class AttestationCertificateBuilder method build.

public X509Certificate build(PrivateKey issuerPrivateKey) {
    try {
        ContentSigner contentSigner = new JcaContentSignerBuilder("SHA256withECDSA").build(issuerPrivateKey);
        X509CertificateHolder certificateHolder = certificateBuilder.build(contentSigner);
        return new JcaX509CertificateConverter().getCertificate(certificateHolder);
    } catch (CertificateException e) {
        throw new com.webauthn4j.validator.exception.CertificateException(e);
    } catch (OperatorCreationException e) {
        throw new UnexpectedCheckedException(e);
    }
}
Also used : UnexpectedCheckedException(com.webauthn4j.util.exception.UnexpectedCheckedException) JcaContentSignerBuilder(org.bouncycastle.operator.jcajce.JcaContentSignerBuilder) JcaX509CertificateConverter(org.bouncycastle.cert.jcajce.JcaX509CertificateConverter) X509CertificateHolder(org.bouncycastle.cert.X509CertificateHolder) ContentSigner(org.bouncycastle.operator.ContentSigner) CertificateException(java.security.cert.CertificateException) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException)

Example 3 with UnexpectedCheckedException

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

the class CipherUtil method encrypt.

public static byte[] encrypt(byte[] data, byte[] encryptionKey) {
    try {
        final byte[] iv = new byte[IV_SIZE / 8];
        secureRandom.nextBytes(iv);
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptionKey, "AES"), new GCMParameterSpec(TAG_SIZE, iv));
        byte[] cipherBytes = cipher.doFinal(data);
        byte[] output = new byte[iv.length + cipherBytes.length];
        System.arraycopy(iv, 0, output, 0, iv.length);
        System.arraycopy(cipherBytes, 0, output, iv.length, cipherBytes.length);
        return output;
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
        throw new UnexpectedCheckedException(e);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) UnexpectedCheckedException(com.webauthn4j.util.exception.UnexpectedCheckedException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Example 4 with UnexpectedCheckedException

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

the class CipherUtil method decrypt.

public static byte[] decrypt(byte[] encrypted, byte[] encryptionKey) {
    try {
        byte[] iv = new byte[IV_SIZE / 8];
        byte[] cipherBytes = new byte[encrypted.length - iv.length];
        System.arraycopy(encrypted, 0, iv, 0, iv.length);
        System.arraycopy(encrypted, iv.length, cipherBytes, 0, cipherBytes.length);
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(encryptionKey, "AES"), new GCMParameterSpec(TAG_SIZE, iv));
        return cipher.doFinal(cipherBytes);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
        throw new UnexpectedCheckedException(e);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) UnexpectedCheckedException(com.webauthn4j.util.exception.UnexpectedCheckedException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Example 5 with UnexpectedCheckedException

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

the class TestAttestationUtil method createV1DummyCertificate.

public static X509Certificate createV1DummyCertificate() {
    try {
        X509v1CertificateBuilder certificateBuilder = new X509v1CertificateBuilder(new X500Name("O=SharpLab., C=US"), BigInteger.valueOf(1), Date.from(Instant.parse("2000-01-01T00:00:00Z")), Date.from(Instant.parse("2999-12-31T23:59:59Z")), new X500Name("O=SharpLab., C=US"), new SubjectPublicKeyInfo(new DefaultSignatureAlgorithmIdentifierFinder().find("SHA256WITHRSA"), new byte[0]));
        ContentSigner contentSigner = new JcaContentSignerBuilder("SHA256withRSA").build(RSAUtil.createKeyPair().getPrivate());
        X509CertificateHolder certificateHolder = certificateBuilder.build(contentSigner);
        try {
            return new JcaX509CertificateConverter().getCertificate(certificateHolder);
        } catch (CertificateException e) {
            throw new com.webauthn4j.validator.exception.CertificateException(e);
        }
    } catch (OperatorCreationException e) {
        throw new UnexpectedCheckedException(e);
    }
}
Also used : UnexpectedCheckedException(com.webauthn4j.util.exception.UnexpectedCheckedException) JcaContentSignerBuilder(org.bouncycastle.operator.jcajce.JcaContentSignerBuilder) ContentSigner(org.bouncycastle.operator.ContentSigner) CertificateException(java.security.cert.CertificateException) X500Name(org.bouncycastle.asn1.x500.X500Name) SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) DefaultSignatureAlgorithmIdentifierFinder(org.bouncycastle.operator.DefaultSignatureAlgorithmIdentifierFinder) JcaX509CertificateConverter(org.bouncycastle.cert.jcajce.JcaX509CertificateConverter) X509CertificateHolder(org.bouncycastle.cert.X509CertificateHolder) X509v1CertificateBuilder(org.bouncycastle.cert.X509v1CertificateBuilder) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException)

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