Search in sources :

Example 1 with UdpDataParser

use of org.eclipse.californium.core.network.serialization.UdpDataParser in project californium by eclipse.

the class EncryptorTest method testRequestEncryptor.

/**
 * Tests encryption of a CoAP Request.
 * Test vector is from OSCORE draft. (Test Vector 5)
 *
 * @throws OSException if encryption fails
 */
@Test
public void testRequestEncryptor() throws OSException {
    // Set up OSCORE context
    ctx = new OSCoreCtx(master_secret, true, alg, sid, rid, kdf, 32, null, null, MAX_UNFRAGMENTED_SIZE);
    ctx.setSenderSeq(seq);
    // Create request message from raw byte array
    byte[] requestBytes = new byte[] { 0x44, 0x01, 0x71, (byte) 0xc3, 0x00, 0x00, (byte) 0xb9, 0x32, 0x39, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, (byte) 0x83, 0x74, 0x76, 0x31 };
    UdpDataParser parser = new UdpDataParser();
    Message mess = parser.parseMessage(requestBytes);
    Request r = null;
    if (mess instanceof Request) {
        r = (Request) mess;
    }
    // Encrypt the request message
    HashMapCtxDB db = new HashMapCtxDB();
    db.addContext(r.getURI(), ctx);
    Request encrypted = RequestEncryptor.encrypt(db, r);
    // Check the OSCORE option value
    byte[] predictedOSCoreOption = { 0x09, 0x14, 0x00 };
    assertArrayEquals(predictedOSCoreOption, encrypted.getOptions().getOscore());
    // Check the OSCORE request payload (ciphertext)
    byte[] predictedOSCorePayload = { 0x4e, (byte) 0xd3, 0x39, (byte) 0xa5, (byte) 0xa3, 0x79, (byte) 0xb0, (byte) 0xb8, (byte) 0xbc, 0x73, 0x1f, (byte) 0xff, (byte) 0xb0 };
    assertArrayEquals(predictedOSCorePayload, encrypted.getPayload());
    // Serialize the request message to byte array
    UdpDataSerializer serializer = new UdpDataSerializer();
    byte[] encryptedBytes = serializer.getByteArray(encrypted);
    // Check the whole OSCORE request
    byte[] predictedOSCoreBytes = { 0x44, 0x02, 0x71, (byte) 0xc3, 0x00, 0x00, (byte) 0xb9, 0x32, 0x39, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x63, 0x09, 0x14, 0x00, (byte) 0xff, 0x4e, (byte) 0xd3, 0x39, (byte) 0xa5, (byte) 0xa3, 0x79, (byte) 0xb0, (byte) 0xb8, (byte) 0xbc, 0x73, 0x1f, (byte) 0xff, (byte) 0xb0 };
    assertArrayEquals(predictedOSCoreBytes, encryptedBytes);
}
Also used : UdpDataParser(org.eclipse.californium.core.network.serialization.UdpDataParser) Message(org.eclipse.californium.core.coap.Message) Request(org.eclipse.californium.core.coap.Request) UdpDataSerializer(org.eclipse.californium.core.network.serialization.UdpDataSerializer) Test(org.junit.Test)

Example 2 with UdpDataParser

use of org.eclipse.californium.core.network.serialization.UdpDataParser in project californium by eclipse.

the class EncryptorTest method testResponseEncryptor.

/**
 * Tests encryption of a CoAP Response with partial IV.
 * Test vector is from OSCORE draft. (Test Vector 8)
 *
 * @throws OSException if encryption fails
 */
@Test
public void testResponseEncryptor() throws OSException {
    // Set up OSCORE context
    // test vector OSCORE draft Appendix C.1.2
    byte[] master_salt = new byte[] { (byte) 0x9e, 0x7c, (byte) 0xa9, 0x22, 0x23, 0x78, 0x63, 0x40 };
    byte[] sid = new byte[] { 0x01 };
    byte[] rid = new byte[0];
    ctx = new OSCoreCtx(master_secret, true, alg, sid, rid, kdf, 32, master_salt, null, MAX_UNFRAGMENTED_SIZE);
    ctx.setSenderSeq(0);
    ctx.setRecipientSeq(seq);
    // Create response message from raw byte array
    byte[] responseBytes = new byte[] { 0x64, 0x45, 0x5d, 0x1f, 0x00, 0x00, 0x39, 0x74, (byte) 0xff, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21 };
    UdpDataParser parser = new UdpDataParser();
    Message mess = parser.parseMessage(responseBytes);
    Response r = null;
    if (mess instanceof Response) {
        r = (Response) mess;
    }
    // Encrypt the response message
    boolean newPartialIV = true;
    Response encrypted = ResponseEncryptor.encrypt(null, r, ctx, newPartialIV, false, seq);
    // Check the OSCORE option value
    byte[] predictedOSCoreOption = { 0x01, 0x00 };
    assertArrayEquals(predictedOSCoreOption, encrypted.getOptions().getOscore());
    // Check the OSCORE response payload (ciphertext)
    byte[] predictedOSCorePayload = { 0x4d, 0x4c, 0x13, 0x66, (byte) 0x93, (byte) 0x84, (byte) 0xb6, 0x73, 0x54, (byte) 0xb2, (byte) 0xb6, 0x17, 0x5f, (byte) 0xf4, (byte) 0xb8, 0x65, (byte) 0x8c, 0x66, 0x6a, 0x6c, (byte) 0xf8, (byte) 0x8e };
    assertArrayEquals(predictedOSCorePayload, encrypted.getPayload());
    // Serialize the response message to byte array
    UdpDataSerializer serializer = new UdpDataSerializer();
    byte[] encryptedBytes = serializer.getByteArray(encrypted);
    // Check the whole OSCORE response
    byte[] predictedOSCoreBytes = { 0x64, 0x44, 0x5d, 0x1f, 0x00, 0x00, 0x39, 0x74, (byte) 0x92, 0x01, 0x00, (byte) 0xff, 0x4d, 0x4c, 0x13, 0x66, (byte) 0x93, (byte) 0x84, (byte) 0xb6, 0x73, 0x54, (byte) 0xb2, (byte) 0xb6, 0x17, 0x5f, (byte) 0xf4, (byte) 0xb8, 0x65, (byte) 0x8c, 0x66, 0x6a, 0x6c, (byte) 0xf8, (byte) 0x8e };
    assertArrayEquals(predictedOSCoreBytes, encryptedBytes);
}
Also used : Response(org.eclipse.californium.core.coap.Response) UdpDataParser(org.eclipse.californium.core.network.serialization.UdpDataParser) Message(org.eclipse.californium.core.coap.Message) UdpDataSerializer(org.eclipse.californium.core.network.serialization.UdpDataSerializer) Test(org.junit.Test)

Example 3 with UdpDataParser

use of org.eclipse.californium.core.network.serialization.UdpDataParser in project californium by eclipse.

the class RequestDecryptor method decrypt.

/**
 * @param db the context database used
 * @param request the request to decrypt
 * @param ctx the OSCore context
 *
 * @return the decrypted request
 *
 * @throws CoapOSException if decryption fails
 */
public static Request decrypt(OSCoreCtxDB db, Request request, OSCoreCtx ctx) throws CoapOSException {
    discardEOptions(request);
    byte[] protectedData = request.getPayload();
    Encrypt0Message enc;
    OptionSet uOptions = request.getOptions();
    try {
        enc = decompression(protectedData, request);
    } catch (OSException e) {
        LOGGER.error(ErrorDescriptions.FAILED_TO_DECODE_COSE);
        throw new CoapOSException(ErrorDescriptions.FAILED_TO_DECODE_COSE, ResponseCode.BAD_OPTION);
    }
    CBORObject kid = enc.findAttribute(HeaderKeys.KID);
    if (kid == null || !kid.getType().equals(CBORType.ByteString)) {
        LOGGER.error(ErrorDescriptions.MISSING_KID);
        throw new CoapOSException(ErrorDescriptions.FAILED_TO_DECODE_COSE, ResponseCode.BAD_OPTION);
    }
    byte[] rid = kid.GetByteString();
    // Retrieve Context ID (kid context)
    CBORObject kidContext = enc.findAttribute(CBORObject.FromObject(10));
    byte[] contextID = null;
    if (kidContext != null) {
        contextID = kidContext.GetByteString();
    }
    // Perform context re-derivation procedure if triggered or ongoing
    try {
        ctx = ContextRederivation.incomingRequest(db, ctx, contextID, rid);
    } catch (OSException e) {
        LOGGER.error(ErrorDescriptions.CONTEXT_REGENERATION_FAILED);
        throw new CoapOSException(ErrorDescriptions.CONTEXT_REGENERATION_FAILED, ResponseCode.BAD_REQUEST);
    }
    if (ctx == null) {
        LOGGER.error(ErrorDescriptions.CONTEXT_NOT_FOUND);
        throw new CoapOSException(ErrorDescriptions.CONTEXT_NOT_FOUND, ResponseCode.UNAUTHORIZED);
    }
    byte[] plaintext;
    try {
        plaintext = decryptAndDecode(enc, request, ctx, null);
    } catch (OSException e) {
        // First check for replay exceptions
        if (e.getMessage().equals(ErrorDescriptions.REPLAY_DETECT)) {
            LOGGER.error(ErrorDescriptions.REPLAY_DETECT);
            throw new CoapOSException(ErrorDescriptions.REPLAY_DETECT, ResponseCode.UNAUTHORIZED);
        }
        // Otherwise return generic error message
        LOGGER.error(ErrorDescriptions.DECRYPTION_FAILED);
        throw new CoapOSException(ErrorDescriptions.DECRYPTION_FAILED, ResponseCode.BAD_REQUEST);
    }
    // Check if parsing of request plaintext succeeds
    try {
        DatagramReader reader = new DatagramReader(new ByteArrayInputStream(plaintext));
        ctx.setCoAPCode(Code.valueOf(reader.read(CoAP.MessageFormat.CODE_BITS)));
        // resets option so eOptions gets priority during parse
        request.setOptions(EMPTY);
        new UdpDataParser().parseOptionsAndPayload(reader, request);
    } catch (Exception e) {
        LOGGER.error(ErrorDescriptions.DECRYPTION_FAILED);
        throw new CoapOSException(ErrorDescriptions.DECRYPTION_FAILED, ResponseCode.BAD_REQUEST);
    }
    OptionSet eOptions = request.getOptions();
    eOptions = OptionJuggle.merge(eOptions, uOptions);
    request.setOptions(eOptions);
    // We need the kid value on layer level
    request.getOptions().setOscore(rid);
    // Associate the Token with the context used
    db.addContext(request.getToken(), ctx);
    // Set information about the OSCORE context used in the endpoint context of this request
    OSCoreEndpointContextInfo.receivingRequest(ctx, request);
    return OptionJuggle.setRealCodeRequest(request, ctx.getCoAPCode());
}
Also used : Encrypt0Message(org.eclipse.californium.cose.Encrypt0Message) UdpDataParser(org.eclipse.californium.core.network.serialization.UdpDataParser) ByteArrayInputStream(java.io.ByteArrayInputStream) DatagramReader(org.eclipse.californium.elements.util.DatagramReader) OptionSet(org.eclipse.californium.core.coap.OptionSet) CBORObject(com.upokecenter.cbor.CBORObject)

Example 4 with UdpDataParser

use of org.eclipse.californium.core.network.serialization.UdpDataParser in project californium by eclipse.

the class DecryptorTest method testRequestDecryptor.

/**
 * Tests decryption of a CoAP Request.
 * Test vector is from OSCORE draft. (Test Vector 5)
 *
 * @throws OSException if decryption fails
 */
@Test
public void testRequestDecryptor() throws OSException {
    // Set up OSCORE context
    ctx = new OSCoreCtx(master_secret, true, alg, sid, rid, kdf, 32, null, null, MAX_UNFRAGMENTED_SIZE);
    OSCoreCtxDB db = new HashMapCtxDB();
    db.addContext(ctx);
    // Create the encrypted request message from raw byte array
    byte[] encryptedRequestBytes = new byte[] { 0x44, 0x02, 0x71, (byte) 0xc3, 0x00, 0x00, (byte) 0xb9, 0x32, 0x39, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x63, 0x09, 0x14, 0x00, (byte) 0xff, 0x4e, (byte) 0xd3, 0x39, (byte) 0xa5, (byte) 0xa3, 0x79, (byte) 0xb0, (byte) 0xb8, (byte) 0xbc, 0x73, 0x1f, (byte) 0xff, (byte) 0xb0 };
    UdpDataParser parser = new UdpDataParser();
    Message mess = parser.parseMessage(encryptedRequestBytes);
    Request r = null;
    if (mess instanceof Request) {
        r = (Request) mess;
    }
    // Decrypt the request message
    Request decrypted = RequestDecryptor.decrypt(db, r, ctx);
    decrypted.getOptions().removeOscore();
    // Serialize the request message to byte array
    UdpDataSerializer serializer = new UdpDataSerializer();
    byte[] decryptedBytes = serializer.getByteArray(decrypted);
    // Check the whole decrypted request
    byte[] predictedBytes = { 0x44, 0x01, 0x71, (byte) 0xc3, 0x00, 0x00, (byte) 0xb9, 0x32, 0x39, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, (byte) 0x83, 0x74, 0x76, 0x31 };
    assertArrayEquals(predictedBytes, decryptedBytes);
}
Also used : UdpDataParser(org.eclipse.californium.core.network.serialization.UdpDataParser) Message(org.eclipse.californium.core.coap.Message) Request(org.eclipse.californium.core.coap.Request) UdpDataSerializer(org.eclipse.californium.core.network.serialization.UdpDataSerializer) Test(org.junit.Test)

Example 5 with UdpDataParser

use of org.eclipse.californium.core.network.serialization.UdpDataParser in project californium by eclipse.

the class DecryptorTest method testResponseDecryptor.

/**
 * Tests decryption of a CoAP Response with partial IV.
 * Test vector is from OSCORE draft. (Test Vector 8)
 *
 * @throws OSException if decryption fails
 */
@Test
public void testResponseDecryptor() throws OSException {
    // Set up OSCORE context
    // test vector OSCORE draft Appendix C.1.1
    byte[] master_salt = new byte[] { (byte) 0x9e, 0x7c, (byte) 0xa9, 0x22, 0x23, 0x78, 0x63, 0x40 };
    byte[] sid = new byte[0];
    byte[] rid = new byte[] { 0x01 };
    int seq = 20;
    ctx = new OSCoreCtx(master_secret, true, alg, sid, rid, kdf, 32, master_salt, null, MAX_UNFRAGMENTED_SIZE);
    // Create the encrypted response message from raw byte array
    byte[] encryptedResponseBytes = new byte[] { 0x64, 0x44, 0x5d, 0x1f, 0x00, 0x00, 0x39, 0x74, (byte) 0x92, 0x01, 0x00, (byte) 0xff, 0x4d, 0x4c, 0x13, 0x66, (byte) 0x93, (byte) 0x84, (byte) 0xb6, 0x73, 0x54, (byte) 0xb2, (byte) 0xb6, 0x17, 0x5f, (byte) 0xf4, (byte) 0xb8, 0x65, (byte) 0x8c, 0x66, 0x6a, 0x6c, (byte) 0xf8, (byte) 0x8e };
    UdpDataParser parser = new UdpDataParser();
    Message mess = parser.parseMessage(encryptedResponseBytes);
    Response r = null;
    if (mess instanceof Response) {
        r = (Response) mess;
    }
    // Set up some state information simulating the original outgoing request
    OSCoreCtxDB db = new HashMapCtxDB();
    db.addContext(r.getToken(), ctx);
    // Decrypt the response message
    Response decrypted = ResponseDecryptor.decrypt(db, r, seq);
    decrypted.getOptions().removeOscore();
    // Check the decrypted response payload
    String predictedPayload = "Hello World!";
    assertEquals(predictedPayload, decrypted.getPayloadString());
    // Serialize the response message to byte array
    UdpDataSerializer serializer = new UdpDataSerializer();
    byte[] decryptedBytes = serializer.getByteArray(decrypted);
    // Check the whole decrypted response
    byte[] predictedBytes = { 0x64, 0x45, 0x5d, 0x1f, 0x00, 0x00, 0x39, 0x74, (byte) 0xff, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21 };
    assertArrayEquals(predictedBytes, decryptedBytes);
}
Also used : Response(org.eclipse.californium.core.coap.Response) UdpDataParser(org.eclipse.californium.core.network.serialization.UdpDataParser) Message(org.eclipse.californium.core.coap.Message) UdpDataSerializer(org.eclipse.californium.core.network.serialization.UdpDataSerializer) Test(org.junit.Test)

Aggregations

UdpDataParser (org.eclipse.californium.core.network.serialization.UdpDataParser)6 Message (org.eclipse.californium.core.coap.Message)4 UdpDataSerializer (org.eclipse.californium.core.network.serialization.UdpDataSerializer)4 Test (org.junit.Test)4 CBORObject (com.upokecenter.cbor.CBORObject)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 OptionSet (org.eclipse.californium.core.coap.OptionSet)2 Request (org.eclipse.californium.core.coap.Request)2 Response (org.eclipse.californium.core.coap.Response)2 Encrypt0Message (org.eclipse.californium.cose.Encrypt0Message)2 DatagramReader (org.eclipse.californium.elements.util.DatagramReader)2 Token (org.eclipse.californium.core.coap.Token)1