Search in sources :

Example 21 with CBORObject

use of com.upokecenter.cbor.CBORObject in project californium by eclipse.

the class EncryptCommon method decryptWithKey.

protected byte[] decryptWithKey(byte[] rgbKey) throws CoseException {
    CBORObject algX = findAttribute(HeaderKeys.Algorithm);
    AlgorithmID alg = AlgorithmID.FromCBOR(algX);
    if (rgbEncrypt == null)
        throw new CoseException("No Encrypted Content Specified");
    if (!isSupportedAesCcm(alg)) {
        throw new CoseException("Unsupported Algorithm Specified");
    }
    AES_CCM_Decrypt(alg, rgbKey);
    return rgbContent;
}
Also used : CBORObject(com.upokecenter.cbor.CBORObject)

Example 22 with CBORObject

use of com.upokecenter.cbor.CBORObject in project californium by eclipse.

the class EncryptCommon method encryptWithKey.

void encryptWithKey(byte[] rgbKey) throws CoseException, IllegalStateException {
    CBORObject algX = findAttribute(HeaderKeys.Algorithm);
    AlgorithmID alg = AlgorithmID.FromCBOR(algX);
    if (rgbContent == null)
        throw new CoseException("No Content Specified");
    if (!isSupportedAesCcm(alg)) {
        throw new CoseException("Unsupported Algorithm Specified");
    }
    AES_CCM_Encrypt(alg, rgbKey);
}
Also used : CBORObject(com.upokecenter.cbor.CBORObject)

Example 23 with CBORObject

use of com.upokecenter.cbor.CBORObject in project californium by eclipse.

the class Message method EncodeToCBORObject.

/**
 * Encode the COSE message object to a CBORObject tree.  This function call will force cryptographic operations to be executed as needed.
 *
 * @return CBORObject representing the message.
 * @throws CoseException Internal COSE Exception
 */
public CBORObject EncodeToCBORObject() throws CoseException {
    CBORObject obj;
    obj = EncodeCBORObject();
    if (emitTag) {
        obj = CBORObject.FromObjectAndTag(obj, messageTag.value);
    }
    return obj;
}
Also used : CBORObject(com.upokecenter.cbor.CBORObject)

Example 24 with CBORObject

use of com.upokecenter.cbor.CBORObject in project java-webauthn-server by Yubico.

the class WebAuthnCodecs method getCoseKeyAlg.

static Optional<COSEAlgorithmIdentifier> getCoseKeyAlg(ByteArray key) {
    CBORObject cose = CBORObject.DecodeFromBytes(key.getBytes());
    final int alg = cose.get(CBORObject.FromObject(3)).AsInt32();
    return COSEAlgorithmIdentifier.fromId(alg);
}
Also used : CBORObject(com.upokecenter.cbor.CBORObject)

Example 25 with CBORObject

use of com.upokecenter.cbor.CBORObject in project java-webauthn-server by Yubico.

the class AuthenticatorData method parseAttestedCredentialData.

private static VariableLengthParseResult parseAttestedCredentialData(AuthenticatorDataFlags flags, byte[] bytes) {
    final int AAGUID_INDEX = 0;
    final int AAGUID_END = AAGUID_INDEX + 16;
    final int CREDENTIAL_ID_LENGTH_INDEX = AAGUID_END;
    final int CREDENTIAL_ID_LENGTH_END = CREDENTIAL_ID_LENGTH_INDEX + 2;
    ExceptionUtil.assure(bytes.length >= CREDENTIAL_ID_LENGTH_END, "Attested credential data must contain at least %d bytes, was %d: %s", CREDENTIAL_ID_LENGTH_END, bytes.length, new ByteArray(bytes).getHex());
    byte[] credentialIdLengthBytes = Arrays.copyOfRange(bytes, CREDENTIAL_ID_LENGTH_INDEX, CREDENTIAL_ID_LENGTH_END);
    final int L;
    try {
        L = BinaryUtil.getUint16(credentialIdLengthBytes);
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException("Invalid credential ID length bytes: " + Arrays.asList(credentialIdLengthBytes), e);
    }
    final int CREDENTIAL_ID_INDEX = CREDENTIAL_ID_LENGTH_END;
    final int CREDENTIAL_ID_END = CREDENTIAL_ID_INDEX + L;
    final int CREDENTIAL_PUBLIC_KEY_INDEX = CREDENTIAL_ID_END;
    final int CREDENTIAL_PUBLIC_KEY_AND_EXTENSION_DATA_END = bytes.length;
    ExceptionUtil.assure(bytes.length >= CREDENTIAL_ID_END, "Expected credential ID of length %d, but attested credential data and extension data is only %d bytes: %s", CREDENTIAL_ID_END, bytes.length, new ByteArray(bytes).getHex());
    ByteArrayInputStream indefiniteLengthBytes = new ByteArrayInputStream(Arrays.copyOfRange(bytes, CREDENTIAL_PUBLIC_KEY_INDEX, CREDENTIAL_PUBLIC_KEY_AND_EXTENSION_DATA_END));
    final CBORObject credentialPublicKey = CBORObject.Read(indefiniteLengthBytes);
    final CBORObject extensions;
    if (indefiniteLengthBytes.available() > 0) {
        if (flags.ED) {
            try {
                extensions = CBORObject.Read(indefiniteLengthBytes);
            } catch (CBORException e) {
                throw new IllegalArgumentException("Failed to parse extension data", e);
            }
        } else {
            throw new IllegalArgumentException(String.format("Flags indicate no extension data, but %d bytes remain after attested credential data.", indefiniteLengthBytes.available()));
        }
    } else {
        if (flags.ED) {
            throw new IllegalArgumentException("Flags indicate there should be extension data, but no bytes remain after attested credential data.");
        } else {
            extensions = null;
        }
    }
    return new VariableLengthParseResult(AttestedCredentialData.builder().aaguid(new ByteArray(Arrays.copyOfRange(bytes, AAGUID_INDEX, AAGUID_END))).credentialId(new ByteArray(Arrays.copyOfRange(bytes, CREDENTIAL_ID_INDEX, CREDENTIAL_ID_END))).credentialPublicKey(new ByteArray(credentialPublicKey.EncodeToBytes())).build(), extensions);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CBORObject(com.upokecenter.cbor.CBORObject) CBORException(com.upokecenter.cbor.CBORException)

Aggregations

CBORObject (com.upokecenter.cbor.CBORObject)30 CBORException (com.upokecenter.cbor.CBORException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 CBORNumber (com.upokecenter.cbor.CBORNumber)2 CBORType (com.upokecenter.cbor.CBORType)2 BigInteger (java.math.BigInteger)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 SecretKeySpec (javax.crypto.spec.SecretKeySpec)2 OptionSet (org.eclipse.californium.core.coap.OptionSet)2 Request (org.eclipse.californium.core.coap.Request)2 UdpDataParser (org.eclipse.californium.core.network.serialization.UdpDataParser)2 Encrypt0Message (org.eclipse.californium.cose.Encrypt0Message)2 DatagramReader (org.eclipse.californium.elements.util.DatagramReader)2 SenMLException (org.eclipse.leshan.senml.SenMLException)2 SenMLRecord (org.eclipse.leshan.senml.SenMLRecord)2 RecoveryCertificateQrCode (ch.admin.bag.covidcertificate.service.domain.RecoveryCertificateQrCode)1 VaccinationCertificateQrCode (ch.admin.bag.covidcertificate.service.domain.VaccinationCertificateQrCode)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1