com.webauthn4j.data.attestation.authenticator.COSEKey Java Examples

The following examples show how to use com.webauthn4j.data.attestation.authenticator.COSEKey. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AttestedCredentialDataConverter.java    From webauthn4j with Apache License 2.0 6 votes vote down vote up
public AttestedCredentialData convert(ByteBuffer attestedCredentialData) {
    byte[] aaguidBytes = new byte[AAGUID_LENGTH];
    attestedCredentialData.get(aaguidBytes, 0, AAGUID_LENGTH);
    AAGUID aaguid = new AAGUID(aaguidBytes);
    int length = UnsignedNumberUtil.getUnsignedShort(attestedCredentialData);
    byte[] credentialId = new byte[length];
    attestedCredentialData.get(credentialId, 0, length);
    byte[] remaining = new byte[attestedCredentialData.remaining()];
    attestedCredentialData.get(remaining);
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(remaining);
    COSEKeyEnvelope coseKeyEnvelope = convertToCredentialPublicKey(byteArrayInputStream);
    COSEKey coseKey = coseKeyEnvelope.getCOSEKey();
    AttestedCredentialData result = new AttestedCredentialData(aaguid, credentialId, coseKey);
    int extensionsBufferLength = remaining.length - coseKeyEnvelope.getLength();
    attestedCredentialData.position(attestedCredentialData.position() - extensionsBufferLength);
    return result;
}
 
Example #2
Source File: BeanAssertUtil.java    From webauthn4j with Apache License 2.0 6 votes vote down vote up
public static void validate(AttestedCredentialData attestedCredentialData) {
    if (attestedCredentialData == null) {
        throw new ConstraintViolationException("attestedCredentialData must not be null");
    }

    AAGUID aaguid = attestedCredentialData.getAaguid();
    if (aaguid == null) {
        throw new ConstraintViolationException("aaguid must not be null");
    }

    if (attestedCredentialData.getCredentialId() == null) {
        throw new ConstraintViolationException("credentialId must not be null");
    }

    COSEKey coseKey = attestedCredentialData.getCOSEKey();
    validate(coseKey);
}
 
Example #3
Source File: PackedAttestationStatementValidator.java    From webauthn4j with Apache License 2.0 5 votes vote down vote up
private AttestationType validateSelfAttestation(RegistrationObject registrationObject, byte[] sig, COSEAlgorithmIdentifier alg, byte[] attrToBeSigned) {
    COSEKey coseKey =
            registrationObject.getAttestationObject().getAuthenticatorData().getAttestedCredentialData().getCOSEKey();
    // Validate that alg matches the algorithm of the coseKey in authenticatorData.
    COSEAlgorithmIdentifier credentialPublicKeyAlgorithm = coseKey.getAlgorithm();
    if (!alg.equals(credentialPublicKeyAlgorithm)) {
        throw new BadAlgorithmException("`alg` in attestation statement doesn't match the algorithm of the coseKey in authenticatorData.");
    }
    // Verify that sig is a valid signature over the concatenation of authenticatorData and clientDataHash using the credential public key with alg.
    if (!verifySignature(coseKey.getPublicKey(), alg, sig, attrToBeSigned)) {
        throw new BadSignatureException("`sig` in attestation statement is not valid signature over the concatenation of authenticatorData and clientDataHash.");
    }
    // If successful, return attestation type Self and empty attestation trust path.
    return AttestationType.SELF;
}
 
Example #4
Source File: COSEKeyEnvelopeDeserializer.java    From webauthn4j with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public COSEKeyEnvelope deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    COSEKey coseKey = ctxt.readValue(p, COSEKey.class);
    int length = (int) p.getCurrentLocation().getByteOffset();
    return new COSEKeyEnvelope(coseKey, length);
}
 
Example #5
Source File: AssertionSignatureValidator.java    From webauthn4j with Apache License 2.0 5 votes vote down vote up
public void validate(AuthenticationData authenticationData, COSEKey coseKey) {
    byte[] signedData = getSignedData(authenticationData);
    byte[] signature = authenticationData.getSignature();
    if (!verifySignature(coseKey, signature, signedData)) {
        throw new BadSignatureException("Assertion signature is not valid.");
    }
}
 
Example #6
Source File: COSEKeyConverter.java    From webauthn4j-spring-security with Apache License 2.0 4 votes vote down vote up
@Override
public COSEKey convertToEntityAttribute(String dbData) {
    byte[] data = Base64UrlUtil.decode(dbData);
    return cborConverter.readValue(data, COSEKey.class);
}
 
Example #7
Source File: WebAuthnCredentialModelInput.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public String toString() {
    StringBuilder sb = new StringBuilder("Credential Type = " + credentialType + ",");
    if (credentialDBId != null)
        sb.append("Credential DB Id = ")
          .append(credentialDBId)
          .append(",");
    if (attestationStatement != null) {
        sb.append("Attestation Statement Format = ")
          .append(attestationStatement.getFormat())
          .append(",");
    } else if (attestationStatementFormat != null) {
        sb.append("Attestation Statement Format = ")
          .append(attestationStatementFormat)
          .append(",");
    }
    if (attestedCredentialData != null) {
        sb.append("AAGUID = ")
          .append(attestedCredentialData.getAaguid().toString())
          .append(",");
        sb.append("CREDENTIAL_ID = ")
          .append(Base64.encodeBytes(attestedCredentialData.getCredentialId()))
          .append(",");
        COSEKey credPubKey = attestedCredentialData.getCOSEKey();
        byte[] keyId = credPubKey.getKeyId();
        if (keyId != null)
            sb.append("CREDENTIAL_PUBLIC_KEY.key_id = ")
              .append(Base64.encodeBytes(keyId))
              .append(",");
        sb.append("CREDENTIAL_PUBLIC_KEY.algorithm = ")
          .append(String.valueOf(credPubKey.getAlgorithm().getValue()))
          .append(",");
        sb.append("CREDENTIAL_PUBLIC_KEY.key_type = ")
          .append(credPubKey.getKeyType().name())
          .append(",");
    }
    if (authenticationRequest != null) {
        // only set on Authentication
        sb.append("Credential Id = ")
          .append(Base64.encodeBytes(authenticationRequest.getCredentialId()))
          .append(",");
    }
    if (sb.length() > 0)
        sb.deleteCharAt(sb.lastIndexOf(","));
    return sb.toString();
}
 
Example #8
Source File: CredentialPublicKeyConverter.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public COSEKey convertToEntityAttribute(String s) {
    return cborConverter.readValue(Base64Url.decode(s), COSEKey.class);
}
 
Example #9
Source File: CredentialPublicKeyConverter.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public String convertToDatabaseColumn(COSEKey credentialPublicKey) {
    return Base64Url.encode(cborConverter.writeValueAsBytes(credentialPublicKey));
}
 
Example #10
Source File: BeanAssertUtil.java    From webauthn4j with Apache License 2.0 4 votes vote down vote up
public static void validate(COSEKey coseKey) {
    if (coseKey == null) {
        throw new ConstraintViolationException("coseKey must not be null");
    }
    coseKey.validate();
}
 
Example #11
Source File: COSEKeyEnvelope.java    From webauthn4j with Apache License 2.0 4 votes vote down vote up
public COSEKey getCOSEKey() {
    return coseKey;
}
 
Example #12
Source File: COSEKeyEnvelope.java    From webauthn4j with Apache License 2.0 4 votes vote down vote up
COSEKeyEnvelope(COSEKey coseKey, int length) {
    this.coseKey = coseKey;
    this.length = length;
}
 
Example #13
Source File: AttestedCredentialDataConverter.java    From webauthn4j with Apache License 2.0 4 votes vote down vote up
byte[] convert(COSEKey coseKey) {
    return cborConverter.writeValueAsBytes(coseKey);
}
 
Example #14
Source File: COSEKeyConverter.java    From webauthn4j-spring-security with Apache License 2.0 4 votes vote down vote up
@Override
public COSEKey convertToEntityAttribute(String dbData) {
    byte[] data = Base64UrlUtil.decode(dbData);
    return cborConverter.readValue(data, COSEKey.class);
}
 
Example #15
Source File: COSEKeyConverter.java    From webauthn4j-spring-security with Apache License 2.0 4 votes vote down vote up
@Override
public String convertToDatabaseColumn(COSEKey attribute) {
    return Base64UrlUtil.encodeToString(cborConverter.writeValueAsBytes(attribute));
}
 
Example #16
Source File: COSEKeyConverter.java    From webauthn4j-spring-security with Apache License 2.0 4 votes vote down vote up
@Override
public String convertToDatabaseColumn(COSEKey attribute) {
    return Base64UrlUtil.encodeToString(cborConverter.writeValueAsBytes(attribute));
}
 
Example #17
Source File: WebAuthnCredentialProvider.java    From keycloak with Apache License 2.0 3 votes vote down vote up
/**
 * Convert WebAuthnCredentialModel, which was usually retrieved from DB, to the CredentialInput, which contains data in the webauthn4j specific format
 */
private WebAuthnCredentialModelInput getCredentialInputFromCredentialModel(CredentialModel credential) {
    WebAuthnCredentialModel webAuthnCredential = getCredentialFromModel(credential);

    WebAuthnCredentialData credData = webAuthnCredential.getWebAuthnCredentialData();

    WebAuthnCredentialModelInput auth = new WebAuthnCredentialModelInput(getType());

    byte[] credentialId = null;
    try {
        credentialId = Base64.decode(credData.getCredentialId());
    } catch (IOException ioe) {
        // NOP
    }

    AAGUID aaguid = new AAGUID(credData.getAaguid());

    COSEKey pubKey = credentialPublicKeyConverter.convertToEntityAttribute(credData.getCredentialPublicKey());

    AttestedCredentialData attrCredData = new AttestedCredentialData(aaguid, credentialId, pubKey);

    auth.setAttestedCredentialData(attrCredData);

    long count = credData.getCounter();
    auth.setCount(count);

    auth.setCredentialDBId(credential.getId());

    auth.setAttestationStatementFormat(credData.getAttestationStatementFormat());

    return auth;
}