Java Code Examples for com.fasterxml.jackson.dataformat.cbor.CBORFactory#createParser()

The following examples show how to use com.fasterxml.jackson.dataformat.cbor.CBORFactory#createParser() . 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: RSAKeyObject.java    From fido2 with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void decode(byte[] cbor) throws IOException {

        CBORFactory f = new CBORFactory();
        ObjectMapper mapper = new ObjectMapper(f);
        CBORParser parser = f.createParser(cbor);

        Map<String, Object> pkObjectMap = mapper.readValue(parser, new TypeReference<Map<String, Object>>() {
        });

        for (String key : pkObjectMap.keySet()) {
            skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.FINE, "FIDO-MSG-2001",
                    "key : " + key + ", Value : " + pkObjectMap.get(key).toString());
            switch (key) {
                case "1":
                    kty = (int) pkObjectMap.get(key);
                    break;
                case "3":
                    alg = (int) pkObjectMap.get(key);
                    break;
                case "-1":
                    n = (byte[]) pkObjectMap.get(key);
                    break;
                case "-2":
                    e = (byte[]) pkObjectMap.get(key);
                    break;
            }
        }
    }
 
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 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 4
Source File: ECKeyObject.java    From fido2 with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void decode(byte[] cbor) throws IOException {

        CBORFactory f = new CBORFactory();
        ObjectMapper mapper = new ObjectMapper(f);
        CBORParser parser = f.createParser(cbor);

        Map<String, Object> pkObjectMap = mapper.readValue(parser, new TypeReference<Map<String, Object>>() {
        });

        for (String key : pkObjectMap.keySet()) {
            skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.FINE, "FIDO-MSG-2001",
                    "key : " + key);
            switch (key) {
                case "1":
                    kty = (int) pkObjectMap.get(key);
                    break;
                case "-1":
                    crv = (int) pkObjectMap.get(key);
                    break;
                case "3":
                    alg = (int) pkObjectMap.get(key);
                    break;
                case "-2":
                    x = (byte[]) pkObjectMap.get(key);
                    break;
                case "-3":
                    y = (byte[]) pkObjectMap.get(key);
                    break;
            }
        }
    }
 
Example 5
Source File: RSAKeyObject.java    From fido2 with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void decode(byte[] cbor) throws IOException
    {
        CBORFactory f = new CBORFactory();
        ObjectMapper mapper = new ObjectMapper(f);
        CBORParser parser = f.createParser(cbor);

        Map<String, Object> pkobjectsmap = mapper.readValue(parser, new TypeReference<Map<String, Object>>() {
        });

        for (String key : pkobjectsmap.keySet()) {
//            System.out.println("key : " + key + ", Value : " + pkobjectsmap.get(key).toString());
            switch (key) {
                case "1":
                    kty = (int) pkobjectsmap.get(key);
                    break;
                case "3":
                    alg = (int) pkobjectsmap.get(key);
                    break;
                case "-1":
                    n = (byte[]) pkobjectsmap.get(key);
                    break;
                case "-2":
                    e = (byte[]) pkobjectsmap.get(key);
                    break;
            }
        }
    }
 
Example 6
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 7
Source File: ECKeyObject.java    From fido2 with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void decode(byte[] cbor) throws IOException {

        CBORFactory f = new CBORFactory();
        ObjectMapper mapper = new ObjectMapper(f);
        CBORParser parser = f.createParser(cbor);

        Map<String, Object> pkobjectsmap = mapper.readValue(parser, new TypeReference<Map<String, Object>>() {
        });

        for (String key : pkobjectsmap.keySet()) {
//            System.out.println("key : " + key);
            switch (key) {
                case "1":
                    kty = (int) pkobjectsmap.get(key);
                    break;
                case "-1":
                    crv = (int) pkobjectsmap.get(key);
                    break;
                case "3":
                    alg = (int) pkobjectsmap.get(key);
                    break;
                case "-2":
                    x = (byte[]) pkobjectsmap.get(key);
                    break;
                case "-3":
                    y = (byte[]) pkobjectsmap.get(key);
                    break;
            }
        }
    }
 
Example 8
Source File: FIDO2AttestedCredentialData.java    From fido2 with GNU Lesser General Public License v2.1 4 votes vote down vote up
public int decodeAttCredData(byte[] data) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, InvalidParameterSpecException {
    int remainingDataIndex = 0;

    aaguid = new byte[16];
    credentialId = new byte[]{};
    System.arraycopy(data, 0, aaguid, 0, 16);
    remainingDataIndex += 16;

    skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.FINE, "FIDO-MSG-2001",
                "AAGUID : " + Base64.toBase64String(aaguid));

    byte[] lengthValue = new byte[2];
    System.arraycopy(data, remainingDataIndex, lengthValue, 0, 2);
    remainingDataIndex += 2;
    length = ByteBuffer.wrap(lengthValue).getShort();

    skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.FINE, "FIDO-MSG-2001",
                "length : " + length);

    credentialId = new byte[length];
    System.arraycopy(data, remainingDataIndex, credentialId, 0, length);
    remainingDataIndex += length;

    byte[] cbor = new byte[data.length - remainingDataIndex];
    System.arraycopy(data, remainingDataIndex, cbor, 0, data.length - remainingDataIndex);

    skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.FINE, "FIDO-MSG-2001",
                "cbor (hex): \n" + bytesToHexString(cbor, cbor.length));
    CBORFactory f = new CBORFactory();
    ObjectMapper mapper = new ObjectMapper(f);
    int kty = 0;
    CBORParser parser = f.createParser(cbor);
    Map<String, Object> pkObjectMap = mapper.readValue(parser, new TypeReference<Map<String, Object>>() {
    });

    skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.FINE, "FIDO-MSG-2001",
                "pkObjectMap: ");
    for(String key: pkObjectMap.keySet()){
        skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.FINE, "FIDO-MSG-2001",
                "Key: " + key + ", Object: " + pkObjectMap.get(key).toString());
    }
    kty = (int) pkObjectMap.get("1");
    skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.FINE, "FIDO-MSG-2001",
                "KTY = " + kty);
    if (kty == 2) {
        ECKeyObject eck = new ECKeyObject();
        eck.decode(cbor);

        int crv = eck.getCrv();
        skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.FINE, "FIDO-MSG-2001",
                "crv = " + crv);
        String curveString = skfsCommon.getCurveFromFIDOECCCurveID(crv);
        skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.FINE, "FIDO-MSG-2001",
                "curveString = " + curveString);
        skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.FINE, "FIDO-MSG-2001",
                "X = " + org.bouncycastle.util.encoders.Hex.toHexString(eck.getX()));
        skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.FINE, "FIDO-MSG-2001",
                "Y = " + org.bouncycastle.util.encoders.Hex.toHexString(eck.getY()));
        publicKey = cryptoCommon.getUserECPublicKey(eck.getX(), eck.getY(), curveString);

        fko = eck;
    } else {
        RSAKeyObject rko = new RSAKeyObject();
        rko.decode(cbor);


        RSAPublicKeySpec spec = new RSAPublicKeySpec(new BigInteger(1,rko.getN()), new BigInteger(1,rko.getE()));
        publicKey = KeyFactory.getInstance("RSA").generatePublic(spec);

        fko = rko;
    }

    //Return size of AttestedCredentialData
    skfsLogger.log(skfsConstants.SKFE_LOGGER, Level.FINE, "FIDO-MSG-2001",
                "FIDO2AttestedCredentialData size (bytes: " + parser.getCurrentLocation().getByteOffset());
    return remainingDataIndex + (int) parser.getCurrentLocation().getByteOffset();
}
 
Example 9
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");
    }

}