Search in sources :

Example 1 with CBORException

use of com.upokecenter.cbor.CBORException 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)

Example 2 with CBORException

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

the class ReceivetestClient method processCBOR.

public static String processCBOR(byte[] payload, String errors, boolean verbose) {
    try {
        StringBuilder statistic = new StringBuilder();
        CBORObject element = CBORObject.DecodeFromBytes(payload);
        if (verbose && element.getType() == CBORType.Array) {
            // expected JSON data
            SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss dd.MM.yyyy");
            try {
                for (CBORObject item : element.getValues()) {
                    if (item.getType() != CBORType.Map) {
                        // unexpected =>
                        // stop application pretty printing
                        statistic.setLength(0);
                        break;
                    }
                    CBORObject value;
                    if ((value = item.get("rid")) != null) {
                        String rid = value.AsString();
                        long time = item.get("time").AsNumber().ToInt64Checked();
                        if (rid.startsWith(REQUEST_ID_PREFIX)) {
                            boolean hit = errors.contains(rid);
                            rid = rid.substring(REQUEST_ID_PREFIX.length());
                            long requestTime = Long.parseLong(rid);
                            statistic.append("Request: ").append(format.format(requestTime));
                            long diff = time - requestTime;
                            if (-MAX_DIFF_TIME_IN_MILLIS < diff && diff < MAX_DIFF_TIME_IN_MILLIS) {
                                statistic.append(", received: ").append(diff).append(" ms");
                            } else {
                                statistic.append(", received: ").append(format.format(time));
                            }
                            if (hit) {
                                statistic.append(" * lost response!");
                            }
                        } else {
                            statistic.append("Request: ").append(rid);
                            statistic.append(", received: ").append(format.format(time));
                        }
                        if ((value = item.get("ep")) != null) {
                            byte[] endpoint = value.GetByteString();
                            int port = item.get("port").AsNumber().ToInt16Checked() & 0xffff;
                            statistic.append(System.lineSeparator());
                            String address = InetAddress.getByAddress(endpoint).getHostAddress();
                            if (address.contains(":")) {
                                address = "[" + address + "]";
                            }
                            statistic.append("    (").append(address).append(":").append(port).append(")");
                        }
                        statistic.append(System.lineSeparator());
                    } else {
                        long time = item.get("systemstart").AsNumber().ToInt64Checked();
                        statistic.append("Server's system start: ").append(format.format(time));
                        statistic.append(System.lineSeparator());
                    }
                }
            } catch (Throwable e) {
                // unexpected => stop application pretty printing
                statistic.setLength(0);
            }
        }
        if (statistic.length() > 0) {
            return statistic.toString();
        } else {
            // CBOR plain pretty printing
            return element.toString();
        }
    } catch (CBORException e) {
        // plain payload
        e.printStackTrace();
        return StringUtil.byteArray2Hex(payload);
    }
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) CBORObject(com.upokecenter.cbor.CBORObject) CBORException(com.upokecenter.cbor.CBORException)

Example 3 with CBORException

use of com.upokecenter.cbor.CBORException 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

CBORException (com.upokecenter.cbor.CBORException)3 CBORObject (com.upokecenter.cbor.CBORObject)3 CBORType (com.upokecenter.cbor.CBORType)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ResourceModel (org.eclipse.leshan.core.model.ResourceModel)1 Type (org.eclipse.leshan.core.model.ResourceModel.Type)1 CodecException (org.eclipse.leshan.core.node.codec.CodecException)1