All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yubico.yubikit.fido.webauthn.AttestationObject Maven / Gradle / Ivy

There is a newer version: 2.7.0
Show newest version
/*
 * Copyright (C) 2023 Yubico.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yubico.yubikit.fido.webauthn;

import com.yubico.yubikit.fido.Cbor;
import com.yubico.yubikit.fido.ctap.Ctap2Session;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import javax.annotation.Nullable;

/**
 * Webauthn AttestationObject which exposes attestation authenticator data.
 *
 * @see WebAuthn Attestation
 */
public class AttestationObject {
    public static final String KEY_FORMAT = "fmt";
    public static final String KEY_AUTHENTICATOR_DATA = "authData";
    public static final String KEY_ATTESTATION_STATEMENT = "attStmt";
    public static final String KEY_EP_ATT = "epAtt";
    public static final String KEY_LARGE_BLOB_KEY = "largeBlobKey";

    private final String format;
    private final AuthenticatorData authenticatorData;
    private final Map attestationStatement;
    @Nullable
    private final Boolean enterpriseAttestation;
    @Nullable
    private final byte[] largeBlobKey;

    public AttestationObject(
            String format,
            AuthenticatorData authenticatorData,
            Map attestationStatement,
            @Nullable Boolean enterpriseAttestation,
            @Nullable byte[] largeBlobKey
    ) {
        this.format = format;
        this.authenticatorData = authenticatorData;
        this.attestationStatement = attestationStatement;
        this.enterpriseAttestation = enterpriseAttestation;
        this.largeBlobKey = largeBlobKey;
    }

    static public AttestationObject fromCredential(Ctap2Session.CredentialData credential) {
        return new AttestationObject(
                credential.getFormat(),
                AuthenticatorData.parseFrom(ByteBuffer.wrap(credential.getAuthenticatorData())),
                credential.getAttestationStatement(),
                credential.getEnterpriseAttestation(),
                credential.getLargeBlobKey()
        );
    }

    @SuppressWarnings("unused")
    public String getFormat() {
        return format;
    }

    public AuthenticatorData getAuthenticatorData() {
        return authenticatorData;
    }

    @SuppressWarnings("unused")
    public Map getAttestationStatement() {
        return attestationStatement;
    }

    @SuppressWarnings("unused")
    @Nullable
    public Boolean getEnterpriseAttestation() {
        return enterpriseAttestation;
    }

    @SuppressWarnings("unused")
    @Nullable
    public byte[] getLargeBlobKey() {
        return largeBlobKey;
    }

    public byte[] toBytes() {
        Map attestationObject = new HashMap<>();
        attestationObject.put(AttestationObject.KEY_FORMAT, format);
        attestationObject.put(AttestationObject.KEY_AUTHENTICATOR_DATA, authenticatorData.getBytes());
        attestationObject.put(AttestationObject.KEY_ATTESTATION_STATEMENT, attestationStatement);
        if (enterpriseAttestation != null) {
            attestationObject.put(AttestationObject.KEY_EP_ATT, enterpriseAttestation);
        }
        if (largeBlobKey != null) {
            attestationObject.put(AttestationObject.KEY_LARGE_BLOB_KEY, largeBlobKey);
        }
        return Cbor.encode(attestationObject);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        AttestationObject that = (AttestationObject) o;

        if (!format.equals(that.format)) return false;
        if (!authenticatorData.equals(that.authenticatorData)) return false;
        if (!Objects.equals(enterpriseAttestation, that.enterpriseAttestation)) return false;
        if (!Arrays.equals(largeBlobKey, that.largeBlobKey)) return false;
        return Arrays.equals(
                Cbor.encode(attestationStatement),
                Cbor.encode(that.attestationStatement));
    }

    @Override
    public int hashCode() {
        int result = format.hashCode();
        result = 31 * result + authenticatorData.hashCode();
        result = 31 * result + Arrays.hashCode(Cbor.encode(attestationStatement));
        result = 31 * result + (enterpriseAttestation != null ? enterpriseAttestation.hashCode() : 0);
        result = 31 * result + Arrays.hashCode(largeBlobKey);
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy