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);
}
}
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));
}
}
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);
}
}
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;
}
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);
}
}
Aggregations