Search in sources :

Example 6 with U2FException

use of com.google.u2f.U2FException in project OpenUnison by TremoloSecurity.

the class RawMessageCodec method decodeRegisterResponse.

public static RegisterResponse decodeRegisterResponse(byte[] data) throws U2FException {
    try {
        DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(data));
        byte reservedByte = inputStream.readByte();
        byte[] userPublicKey = new byte[65];
        inputStream.readFully(userPublicKey);
        byte[] keyHandle = new byte[inputStream.readUnsignedByte()];
        inputStream.readFully(keyHandle);
        X509Certificate attestationCertificate = (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(inputStream);
        byte[] signature = new byte[inputStream.available()];
        inputStream.readFully(signature);
        if (inputStream.available() != 0) {
            throw new U2FException("Message ends with unexpected data");
        }
        if (reservedByte != REGISTRATION_RESERVED_BYTE_VALUE) {
            throw new U2FException(String.format("Incorrect value of reserved byte. Expected: %d. Was: %d", REGISTRATION_RESERVED_BYTE_VALUE, reservedByte));
        }
        return new RegisterResponse(userPublicKey, keyHandle, attestationCertificate, signature);
    } catch (IOException e) {
        throw new U2FException("Error when parsing raw RegisterResponse", e);
    } catch (CertificateException e) {
        throw new U2FException("Error when parsing attestation certificate", e);
    }
}
Also used : RegisterResponse(com.google.u2f.key.messages.RegisterResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) CertificateException(java.security.cert.CertificateException) U2FException(com.google.u2f.U2FException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) X509Certificate(java.security.cert.X509Certificate)

Example 7 with U2FException

use of com.google.u2f.U2FException in project OpenUnison by TremoloSecurity.

the class SerialCodec method parseRequest.

public static U2FRequest parseRequest(InputStream inputStream) throws U2FException, IOException {
    DataInputStream dataInputStream = new DataInputStream(inputStream);
    byte version = dataInputStream.readByte();
    if (version != VERSION) {
        throw new U2FException(String.format("Unsupported message version: %d", version));
    }
    byte command = dataInputStream.readByte();
    switch(command) {
        case COMMAND_REGISTER:
            return RawMessageCodec.decodeRegisterRequest(parseMessage(dataInputStream));
        case COMMAND_AUTHENTICATE:
            return RawMessageCodec.decodeAuthenticateRequest(parseMessage(dataInputStream));
        default:
            throw new U2FException(String.format("Unsupported command: %d", command));
    }
}
Also used : U2FException(com.google.u2f.U2FException) DataInputStream(java.io.DataInputStream)

Example 8 with U2FException

use of com.google.u2f.U2FException in project OpenUnison by TremoloSecurity.

the class RawMessageCodec method decodeAuthenticateRequest.

public static AuthenticateRequest decodeAuthenticateRequest(byte[] data) throws U2FException {
    try {
        DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(data));
        byte controlByte = inputStream.readByte();
        byte[] challengeSha256 = new byte[32];
        inputStream.readFully(challengeSha256);
        byte[] appIdSha256 = new byte[32];
        inputStream.readFully(appIdSha256);
        byte[] keyHandle = new byte[inputStream.readUnsignedByte()];
        inputStream.readFully(keyHandle);
        if (inputStream.available() != 0) {
            throw new U2FException("Message ends with unexpected data");
        }
        return new AuthenticateRequest(controlByte, challengeSha256, appIdSha256, keyHandle);
    } catch (IOException e) {
        throw new U2FException("Error when parsing raw AuthenticateRequest", e);
    }
}
Also used : AuthenticateRequest(com.google.u2f.key.messages.AuthenticateRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) U2FException(com.google.u2f.U2FException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 9 with U2FException

use of com.google.u2f.U2FException in project OpenUnison by TremoloSecurity.

the class RawMessageCodec method encodeRegisterResponse.

public static byte[] encodeRegisterResponse(RegisterResponse registerResponse) throws U2FException {
    byte[] userPublicKey = registerResponse.getUserPublicKey();
    byte[] keyHandle = registerResponse.getKeyHandle();
    X509Certificate attestationCertificate = registerResponse.getAttestationCertificate();
    byte[] signature = registerResponse.getSignature();
    byte[] attestationCertificateBytes;
    try {
        attestationCertificateBytes = attestationCertificate.getEncoded();
    } catch (CertificateEncodingException e) {
        throw new U2FException("Error when encoding attestation certificate.", e);
    }
    if (keyHandle.length > 255) {
        throw new U2FException("keyHandle length cannot be longer than 255 bytes!");
    }
    byte[] result = new byte[1 + userPublicKey.length + 1 + keyHandle.length + attestationCertificateBytes.length + signature.length];
    ByteBuffer.wrap(result).put(REGISTRATION_RESERVED_BYTE_VALUE).put(userPublicKey).put((byte) keyHandle.length).put(keyHandle).put(attestationCertificateBytes).put(signature);
    return result;
}
Also used : CertificateEncodingException(java.security.cert.CertificateEncodingException) U2FException(com.google.u2f.U2FException) X509Certificate(java.security.cert.X509Certificate)

Example 10 with U2FException

use of com.google.u2f.U2FException in project OpenUnison by TremoloSecurity.

the class RawMessageCodec method decodeRegisterRequest.

public static RegisterRequest decodeRegisterRequest(byte[] data) throws U2FException {
    try {
        DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(data));
        byte[] appIdSha256 = new byte[32];
        byte[] challengeSha256 = new byte[32];
        inputStream.readFully(challengeSha256);
        inputStream.readFully(appIdSha256);
        if (inputStream.available() != 0) {
            throw new U2FException("Message ends with unexpected data");
        }
        return new RegisterRequest(appIdSha256, challengeSha256);
    } catch (IOException e) {
        throw new U2FException("Error when parsing raw RegisterRequest", e);
    }
}
Also used : RegisterRequest(com.google.u2f.key.messages.RegisterRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) U2FException(com.google.u2f.U2FException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Aggregations

U2FException (com.google.u2f.U2FException)21 SecurityKeyData (com.google.u2f.server.data.SecurityKeyData)6 IOException (java.io.IOException)6 DataInputStream (java.io.DataInputStream)5 JsonParser (com.google.gson.JsonParser)4 RegisterResponse (com.google.u2f.key.messages.RegisterResponse)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 X509Certificate (java.security.cert.X509Certificate)4 AuthenticateResponse (com.google.u2f.key.messages.AuthenticateResponse)3 U2FServer (com.google.u2f.server.U2FServer)3 AuthController (com.tremolosecurity.proxy.auth.AuthController)3 AuthInfo (com.tremolosecurity.proxy.auth.AuthInfo)3 Attribute (com.tremolosecurity.saml.Attribute)3 InvalidKeyException (java.security.InvalidKeyException)3 SignatureException (java.security.SignatureException)3 CertificateEncodingException (java.security.cert.CertificateEncodingException)3 HashMap (java.util.HashMap)3 JsonObject (com.google.gson.JsonObject)2 EnrollSessionData (com.google.u2f.server.data.EnrollSessionData)2