Search in sources :

Example 1 with SecurityProviderSymmetricKey

use of com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey in project azure-iot-sdk-java by Azure.

the class SecurityProviderSymmetricKeyTest method testSignDataThrowsSecurityProviderExceptionOnEmptySignature.

@Test(expected = SecurityProviderException.class)
public void testSignDataThrowsSecurityProviderExceptionOnEmptySignature() throws SecurityProviderException {
    final String TEST_SIGNATURE = "testSignature";
    final String TEST_BASE64_DECODED_KEY = "";
    final String HMAC_SHA_256 = "HmacSHA256";
    // arrange
    SecurityProviderSymmetricKey securityProviderSymmetricKey = new SecurityProviderSymmetricKey(testSymKey, testRegId);
    // act
    securityProviderSymmetricKey.HMACSignData("".getBytes(StandardCharsets.UTF_8), TEST_BASE64_DECODED_KEY.getBytes(StandardCharsets.UTF_8));
    // assert
    new Verifications() {

        {
            new SecretKeySpec((byte[]) any, HMAC_SHA_256);
            times = 1;
            mockedMac.doFinal(TEST_SIGNATURE.getBytes(StandardCharsets.UTF_8));
            times = 1;
        }
    };
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecurityProviderSymmetricKey(com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey) Test(org.junit.Test)

Example 2 with SecurityProviderSymmetricKey

use of com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey in project azure-iot-sdk-java by Azure.

the class SecurityProviderSymmetricKeyTest method testSignData.

@Test
public void testSignData() throws SecurityProviderException {
    final String TEST_SIGNATURE = "testSignature";
    // Semmle flags this as sensitive call, but it is a false positive since it is for test purposes
    // lgtm
    final String TEST_BASE64_DECODED_KEY = "base64DecodedKey";
    final String HMAC_SHA_256 = "HmacSHA256";
    // arrange
    SecurityProviderSymmetricKey securityProviderSymmetricKey = new SecurityProviderSymmetricKey(testSymKey, testRegId);
    // act
    securityProviderSymmetricKey.HMACSignData(TEST_SIGNATURE.getBytes(StandardCharsets.UTF_8), TEST_BASE64_DECODED_KEY.getBytes(StandardCharsets.UTF_8));
    // assert
    new Verifications() {

        {
            new SecretKeySpec((byte[]) any, HMAC_SHA_256);
            times = 1;
            mockedMac.doFinal(TEST_SIGNATURE.getBytes(StandardCharsets.UTF_8));
            times = 1;
        }
    };
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecurityProviderSymmetricKey(com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey) Test(org.junit.Test)

Example 3 with SecurityProviderSymmetricKey

use of com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey in project azure-iot-sdk-java by Azure.

the class SecurityProviderSymmetricKeyTest method testSignDataThrowsSecurityProviderExceptionOnInvalidKey.

@Test(expected = SecurityProviderException.class)
public void testSignDataThrowsSecurityProviderExceptionOnInvalidKey() throws SecurityProviderException, InvalidKeyException {
    final String TEST_SIGNATURE = "testSignature";
    // Semmle flags this as sensitive call, but it is a false positive since it is for test purposes
    // lgtm
    final String TEST_BASE64_DECODED_KEY = "InvalidKey";
    final String HMAC_SHA_256 = "HmacSHA256";
    new Expectations() {

        {
            mockedMac.init((Key) any);
            result = new InvalidKeyException();
        }
    };
    // arrange
    SecurityProviderSymmetricKey securityProviderSymmetricKey = new SecurityProviderSymmetricKey(testSymKey, testRegId);
    // act
    securityProviderSymmetricKey.HMACSignData(TEST_SIGNATURE.getBytes(StandardCharsets.UTF_8), TEST_BASE64_DECODED_KEY.getBytes(StandardCharsets.UTF_8));
    // assert
    new Verifications() {

        {
            new SecretKeySpec((byte[]) any, HMAC_SHA_256);
            times = 1;
            mockedMac.doFinal(TEST_SIGNATURE.getBytes(StandardCharsets.UTF_8));
            times = 0;
        }
    };
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecurityProviderSymmetricKey(com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey) Test(org.junit.Test)

Example 4 with SecurityProviderSymmetricKey

use of com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey in project azure-iot-sdk-java by Azure.

the class SecurityProviderSymmetricKeyTest method testConstructorSucceeds.

@Test
public void testConstructorSucceeds() throws SecurityProviderException {
    // act
    SecurityProviderSymmetricKey securityProviderSymmetricKey = new SecurityProviderSymmetricKey(testSymKey, testRegId);
    // assert
    assertEquals(securityProviderSymmetricKey.getSymmetricKey(), testSymKey);
    assertEquals(securityProviderSymmetricKey.getRegistrationId(), testRegId);
}
Also used : SecurityProviderSymmetricKey(com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey) Test(org.junit.Test)

Example 5 with SecurityProviderSymmetricKey

use of com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey in project azure-iot-sdk-java by Azure.

the class SecurityProviderSymmetricKeyTest method testSignDataThrowsSecurityProviderExceptionOnNullSignature.

@Test(expected = SecurityProviderException.class)
public void testSignDataThrowsSecurityProviderExceptionOnNullSignature() throws SecurityProviderException {
    final String TEST_SIGNATURE = "testSignature";
    final String TEST_BASE64_DECODED_KEY = "";
    final String HMAC_SHA_256 = "HmacSHA256";
    // arrange
    SecurityProviderSymmetricKey securityProviderSymmetricKey = new SecurityProviderSymmetricKey(testSymKey, testRegId);
    // act
    securityProviderSymmetricKey.HMACSignData(null, TEST_BASE64_DECODED_KEY.getBytes(StandardCharsets.UTF_8));
    // assert
    new Verifications() {

        {
            new SecretKeySpec((byte[]) any, HMAC_SHA_256);
            times = 1;
            mockedMac.doFinal(TEST_SIGNATURE.getBytes(StandardCharsets.UTF_8));
            times = 1;
        }
    };
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecurityProviderSymmetricKey(com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey) Test(org.junit.Test)

Aggregations

SecurityProviderSymmetricKey (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey)17 Test (org.junit.Test)10 SecretKeySpec (javax.crypto.spec.SecretKeySpec)6 ProvisioningDeviceClientException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException)4 SecurityProviderTpm (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderTpm)3 IOException (java.io.IOException)3 ProvisioningDeviceSecurityException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceSecurityException)2 Scanner (java.util.Scanner)2 UrlPathBuilder (com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder)1 SecurityProvider (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProvider)1 SecurityProviderX509 (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderX509)1 SecurityProviderException (com.microsoft.azure.sdk.iot.provisioning.security.exceptions.SecurityProviderException)1 SecurityProviderTPMEmulator (com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator)1 SecurityProviderX509Cert (com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderX509Cert)1 KeyManager (javax.net.ssl.KeyManager)1 SSLContext (javax.net.ssl.SSLContext)1 TrustManager (javax.net.ssl.TrustManager)1