Java Code Examples for com.fasterxml.jackson.dataformat.cbor.CBORParser#nextToken()

The following examples show how to use com.fasterxml.jackson.dataformat.cbor.CBORParser#nextToken() . 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: AbstractDDiApiIntegrationTest.java    From hawkbit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Convert CBOR to JSON equivalent.
 * 
 * @param input
 *            CBOR data to convert
 * @return Equivalent JSON string
 * @throws IOException
 *             Invalid CBOR input
 */
protected static String cborToJson(byte[] input) throws IOException {
    CBORFactory cborFactory = new CBORFactory();
    CBORParser cborParser = cborFactory.createParser(input);
    JsonFactory jsonFactory = new JsonFactory();
    StringWriter stringWriter = new StringWriter();
    JsonGenerator jsonGenerator = jsonFactory.createGenerator(stringWriter);
    while (cborParser.nextToken() != null) {
        jsonGenerator.copyCurrentEvent(cborParser);
    }
    jsonGenerator.flush();
    return stringWriter.toString();
}
 
Example 2
Source File: FIDO2Extensions.java    From fido2 with GNU Lesser General Public License v2.1 5 votes vote down vote up
public int decodeExtensions(byte[] extensionBytes) throws IOException {
    CBORFactory f = new CBORFactory();
    ObjectMapper mapper = new ObjectMapper(f);
    CBORParser parser = f.createParser(extensionBytes);
    extensionMap = mapper.readValue(parser, new TypeReference<Map<String, Object>>() {});

    //Return size of AttestedCredentialData
    int numRemainingBytes = 0;
    JsonToken leftoverCBORToken;
    while ((leftoverCBORToken = parser.nextToken()) != null) {
        numRemainingBytes += leftoverCBORToken.asByteArray().length;
    }
    return extensionBytes.length - numRemainingBytes;
}
 
Example 3
Source File: FIDO2Extensions.java    From fido2 with GNU Lesser General Public License v2.1 5 votes vote down vote up
public int decodeExtensions(byte[] extensionBytes) throws IOException
{
    CBORFactory cbf = new CBORFactory();
    CBORParser cbp = cbf.createParser(extensionBytes);
    extensionMap = (new ObjectMapper(cbf)).readValue(cbp, new TypeReference<Map<String, Object>>() {});
    int numRemainingBytes = 0;
    JsonToken leftoverCBORToken;
    while ((leftoverCBORToken = cbp.nextToken()) != null) {
        numRemainingBytes += leftoverCBORToken.asByteArray().length;
    }
    return extensionBytes.length - numRemainingBytes;
}
 
Example 4
Source File: CborFactory.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private static JsonObject parseObject(final CBORParser parser, final ByteBuffer byteBuffer) throws IOException {
    final LinkedHashMap<String, JsonField> map = new LinkedHashMap<>();
    final long startOffset = parser.getTokenLocation().getByteOffset();
    while (parser.nextToken() == JsonToken.FIELD_NAME) {
        final String key = parser.currentName();
        final JsonField jsonField = JsonField.newInstance(key, parseValue(parser, byteBuffer));
        map.put(key, jsonField);
    }
    final long endOffset = parser.getTokenLocation().getByteOffset();
    return ImmutableJsonObject.of(map, getBytesFromInputSource(startOffset, endOffset, byteBuffer));
}
 
Example 5
Source File: CborFactory.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private static JsonArray parseArray(final CBORParser parser, final ByteBuffer byteBuffer) throws IOException {
    final LinkedList<JsonValue> list = new LinkedList<>();
    final long startOffset = parser.getTokenLocation().getByteOffset();
    while (parser.nextToken() != JsonToken.END_ARRAY) {
        final JsonValue jsonValue = parseValue(parser, byteBuffer, parser.currentToken());
        list.add(jsonValue);
    }
    final long endOffset = parser.getTokenLocation().getByteOffset();
    return ImmutableJsonArray.of(list, getBytesFromInputSource(startOffset, endOffset, byteBuffer));
}
 
Example 6
Source File: FIDO2AttestationObject.java    From fido2 with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void decodeAttestationObject(String attestationObject) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, InvalidParameterSpecException {
    CBORFactory f = new CBORFactory();
    ObjectMapper mapper = new ObjectMapper(f);
    byte[] authenticatorData = null;
    Object attestationStmt = null;
    CBORParser parser = f.createParser(org.apache.commons.codec.binary.Base64.decodeBase64(attestationObject));
    Map<String, Object> attObjectMap = mapper.readValue(parser, new TypeReference<Map<String, Object>>() {
    });

    //Verify cbor is properly formatted cbor (no extra bytes)
    if(parser.nextToken() != null){
        throw new IllegalArgumentException("FIDO2AttestationObject contains invalid CBOR");
    }

    for (String key : attObjectMap.keySet()) {
        if (key.equalsIgnoreCase("fmt")) {
            attFormat = attObjectMap.get(key).toString();
        } else if (key.equalsIgnoreCase("authData")) {
            authenticatorData = (byte[]) attObjectMap.get(key);
        } else if (key.equalsIgnoreCase("attStmt")) {
            attestationStmt = attObjectMap.get(key);
        }
    }
    authData = new FIDO2AuthenticatorData();
    authData.decodeAuthData(authenticatorData);

    skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.FINE, "FIDO-MSG-2001",
                "ATTFORMAT = "  +attFormat);
    switch (attFormat) {
        case "fido-u2f":
            attStmt = new U2FAttestationStatment();
            attStmt.decodeAttestationStatement(attestationStmt);
            break;

        case "packed":
            attStmt = new PackedAttestationStatement();
            attStmt.decodeAttestationStatement(attestationStmt);
            break;

        case "tpm":
            attStmt = new TPMAttestationStatement();
            attStmt.decodeAttestationStatement(attestationStmt);
            break;

        case "android-key":
            attStmt = new AndroidKeyAttestationStatement();
            attStmt.decodeAttestationStatement(attestationStmt);
            break;

        case "android-safetynet":
            attStmt = new AndroidSafetynetAttestationStatement();
            attStmt.decodeAttestationStatement(attestationStmt);
            break;

        case "none":
            attStmt = new NoneAttestationStatement();
            attStmt.decodeAttestationStatement(attestationStmt);
            break;

        default:
            throw new IllegalArgumentException("Invalid attestation format");
    }

}