Search in sources :

Example 1 with CBORObject

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

the class WebAuthnCodecs method importCosePublicKey.

static PublicKey importCosePublicKey(ByteArray key) throws CoseException, IOException, InvalidKeySpecException, NoSuchAlgorithmException {
    CBORObject cose = CBORObject.DecodeFromBytes(key.getBytes());
    final int kty = cose.get(CBORObject.FromObject(1)).AsInt32();
    switch(kty) {
        case 1:
            return importCoseEdDsaPublicKey(cose);
        case 2:
            return importCoseP256PublicKey(cose);
        case 3:
            return importCoseRsaPublicKey(cose);
        default:
            throw new IllegalArgumentException("Unsupported key type: " + kty);
    }
}
Also used : CBORObject(com.upokecenter.cbor.CBORObject)

Example 2 with CBORObject

use of com.upokecenter.cbor.CBORObject in project CovidCertificate-Management-Service by admin-ch.

the class DGCTestJSONGenerator method addCborToJSON.

/**
 * Gets and adds the CBOR encoded certificate JSON.
 */
private static byte[] addCborToJSON(DGCTestJSON dgcTestJSON, byte[] qrCodeContentDecompressedBytes) {
    CBORObject coseCBORObject = CBORObject.DecodeFromBytes(qrCodeContentDecompressedBytes);
    byte[] cosePayloadBytes = coseCBORObject.get(2).GetByteString();
    CBORObject cosePayloadCBORObject = CBORObject.DecodeFromBytes(cosePayloadBytes);
    byte[] hCertCBORBytes = cosePayloadCBORObject.get(-260).get(1).EncodeToBytes();
    String hCertHexString = Hex.encodeHexString(hCertCBORBytes);
    dgcTestJSON.setCbor(hCertHexString);
    return hCertCBORBytes;
}
Also used : CBORObject(com.upokecenter.cbor.CBORObject)

Example 3 with CBORObject

use of com.upokecenter.cbor.CBORObject in project CovidCertificate-Management-Service by admin-ch.

the class CBORService method getSignatureData.

public byte[] getSignatureData(byte[] protectedHeader, byte[] payload) {
    if (protectedHeader == null || protectedHeader.length == 0) {
        throw new IllegalArgumentException("ProtectedHeader must not be empty.");
    }
    if (payload == null || payload.length == 0) {
        throw new IllegalArgumentException("Payload must not be empty.");
    }
    CBORObject cborObject = CBORObject.NewArray();
    cborObject.Add(CONTEXT);
    cborObject.Add(protectedHeader);
    cborObject.Add(EXTERNAL_AAD);
    cborObject.Add(payload);
    return cborObject.EncodeToBytes();
}
Also used : CBORObject(com.upokecenter.cbor.CBORObject)

Example 4 with CBORObject

use of com.upokecenter.cbor.CBORObject in project CovidCertificate-Management-Service by admin-ch.

the class CBORService method getCOSESign1.

public byte[] getCOSESign1(byte[] protectedHeader, byte[] payload, byte[] signature) {
    if (protectedHeader == null || protectedHeader.length == 0) {
        throw new IllegalArgumentException("ProtectedHeader must not be empty.");
    }
    if (payload == null || payload.length == 0) {
        throw new IllegalArgumentException("Payload must not be empty.");
    }
    if (signature == null || signature.length == 0) {
        throw new IllegalArgumentException("Signature must not be empty.");
    }
    CBORObject cborObject = CBORObject.NewArray();
    cborObject.Add(protectedHeader);
    cborObject.Add(UNPROTECTED_HEADER);
    cborObject.Add(payload);
    cborObject.Add(signature);
    cborObject = CBORObject.FromObjectAndTag(cborObject, MESSAGE_TAG);
    return cborObject.EncodeToBytes();
}
Also used : CBORObject(com.upokecenter.cbor.CBORObject)

Example 5 with CBORObject

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

the class LwM2mNodeCborDecoder method decode.

@Override
@SuppressWarnings("unchecked")
public <T extends LwM2mNode> T decode(byte[] content, LwM2mPath path, LwM2mModel model, Class<T> nodeClass) throws CodecException {
    // Support only single value
    if (!path.isResource() && !path.isResourceInstance())
        throw new CodecException("Invalid path %s : CborDecoder decodes resource OR resource instance only", path);
    if (content == null)
        return null;
    // Parse CBOR
    CBORObject cborObject;
    try {
        cborObject = CBORObject.DecodeFromBytes(content);
    } catch (CBORException e) {
        throw new CodecException(e, "Unable to parse CBORD value %s for resource %s", content, path);
    }
    // Find model to know expected type
    Type expectedType;
    ResourceModel rDesc = model.getResourceModel(path.getObjectId(), path.getResourceId());
    if (rDesc != null) {
        expectedType = rDesc.type;
    } else {
        // try to guess type from CBOR ?
        expectedType = guessTypeFromCbor(cborObject, path);
        LOG.debug("Decoding CBOR resource or resource instance without model, type guessed {}", expectedType);
    }
    // Get Node Value
    Object nodeValue = parseCborValue(cborObject, expectedType, path);
    // Create Node
    if (path.isResource()) {
        return (T) LwM2mSingleResource.newResource(path.getResourceId(), nodeValue, expectedType);
    } else {
        return (T) LwM2mResourceInstance.newInstance(path.getResourceInstanceId(), nodeValue, expectedType);
    }
}
Also used : Type(org.eclipse.leshan.core.model.ResourceModel.Type) CBORType(com.upokecenter.cbor.CBORType) ResourceModel(org.eclipse.leshan.core.model.ResourceModel) CodecException(org.eclipse.leshan.core.node.codec.CodecException) CBORObject(com.upokecenter.cbor.CBORObject) 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