Search in sources :

Example 1 with CoseException

use of com.google.cose.exceptions.CoseException in project cose-lib by android.

the class CborUtils method decode.

/**
 * Decodes cbor byte encoding into a CBOR data item.
 * @param data byte array in cbor format.
 * @return DataItem cbor object
 */
public static DataItem decode(final byte[] data) throws CoseException, CborException {
    final ByteArrayInputStream bais = new ByteArrayInputStream(data);
    final List<DataItem> dataItems = new CborDecoder(bais).decode();
    if (dataItems.size() != 1) {
        throw new CoseException("Byte stream cannot be decoded properly. Expected 1 item, found " + dataItems.size());
    }
    return dataItems.get(0);
}
Also used : CborDecoder(co.nstant.in.cbor.CborDecoder) ByteArrayInputStream(java.io.ByteArrayInputStream) DataItem(co.nstant.in.cbor.model.DataItem) CoseException(com.google.cose.exceptions.CoseException)

Example 2 with CoseException

use of com.google.cose.exceptions.CoseException in project cose-lib by android.

the class CoseUtils method getEc2PrivateKeyFromCoordinate.

/**
 * Generates EC2 Private Key from d parameter.
 *
 * Only supports P256 curve currently.
 * @param curve supported curve
 * @param d BigInteger representation for the private key bytes.
 * @return PrivateKey JCA implementation
 * @throws CoseException if unsupported key curve is used.
 */
public static PrivateKey getEc2PrivateKeyFromCoordinate(int curve, BigInteger d) throws CoseException {
    try {
        if (d == null) {
            throw new CoseException("Cannot decode private key. Missing coordinate information.");
        }
        final AlgorithmParameters parameters = AlgorithmParameters.getInstance(EC_PARAMETER_SPEC);
        parameters.init(getEC2ParameterSpecFromCurve(curve));
        final ECParameterSpec ecParameters = parameters.getParameterSpec(ECParameterSpec.class);
        final ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(d, ecParameters);
        return KeyFactory.getInstance(EC_PARAMETER_SPEC).generatePrivate(privateKeySpec);
    } catch (NoSuchAlgorithmException | InvalidParameterSpecException | InvalidKeySpecException e) {
        throw new IllegalStateException("Unexpected error", e);
    }
}
Also used : ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) ECParameterSpec(java.security.spec.ECParameterSpec) CoseException(com.google.cose.exceptions.CoseException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 3 with CoseException

use of com.google.cose.exceptions.CoseException in project cose-lib by android.

the class Ec2SigningKeyTest method testBuilderFailureScenarios.

@Test
public void testBuilderFailureScenarios() throws CborException, CoseException {
    final String xCor = "5A88D182BCE5F42EFA59943F33359D2E8A968FF289D93E5FA444B624343167FE";
    final String yCor = "B16E8CF858DDC7690407BA61D4C338237A8CFCF3DE6AA672FC60A557AA32FC67";
    final String dParam = "5A88D182BCE5F42EFA59943F33359D2E8A968FF289D93E5FA444B624343167FE";
    final byte[] x = TestUtilities.hexStringToByteArray(xCor);
    final byte[] y = TestUtilities.hexStringToByteArray(yCor);
    final byte[] d = TestUtilities.hexStringToByteArray(dParam);
    try {
        Ec2SigningKey.builder().build();
        Assert.fail();
    } catch (CoseException e) {
    // pass
    }
    // Missing curve
    try {
        Ec2SigningKey.builder().withXCoordinate(x).withYCoordinate(y).withDParameter(d).build();
        Assert.fail();
    } catch (CoseException e) {
    // pass
    }
    // Incorrect curve
    try {
        Ec2SigningKey.builder().withCurve(Headers.CURVE_OKP_Ed25519).withXCoordinate(x).withYCoordinate(y).withDParameter(d).build();
        Assert.fail();
    } catch (CoseException e) {
    // pass
    }
    // Missing x
    try {
        Ec2SigningKey.builder().withCurve(Headers.CURVE_EC2_P256).withYCoordinate(y).withDParameter(d).build();
        Assert.fail();
    } catch (CoseException e) {
    // pass
    }
    // Missing y
    try {
        Ec2SigningKey.builder().withCurve(Headers.CURVE_EC2_P256).withXCoordinate(x).withDParameter(d).build();
        Assert.fail();
    } catch (CoseException e) {
    // pass
    }
    // Missing d should pass
    Ec2SigningKey.builder().withCurve(Headers.CURVE_EC2_P256).withXCoordinate(x).withYCoordinate(y).build();
    // Wrong operation
    try {
        Ec2SigningKey.builder().withCurve(Headers.CURVE_EC2_P256).withXCoordinate(x).withYCoordinate(y).withDParameter(d).withOperations(Headers.KEY_OPERATIONS_DECRYPT, Headers.KEY_OPERATIONS_SIGN).build();
        Assert.fail();
    } catch (CoseException e) {
    // pass
    }
}
Also used : CoseException(com.google.cose.exceptions.CoseException) ByteString(co.nstant.in.cbor.model.ByteString) Test(org.junit.Test)

Example 4 with CoseException

use of com.google.cose.exceptions.CoseException in project cose-lib by android.

the class EncryptionKeyTest method testBuilderFailureScenarios.

@Test
public void testBuilderFailureScenarios() throws CborException {
    final String kVal = "65eda5a12577c2bae829437fe338701a10aaa375e1bb5b5de108de439c08551d";
    final byte[] secretKey = TestUtilities.hexStringToByteArray(kVal);
    try {
        EncryptionKey.builder().build();
        Assert.fail();
    } catch (CoseException e) {
    // pass
    }
    try {
        EncryptionKey.builder().withSecretKey(secretKey).withOperations(Headers.KEY_OPERATIONS_SIGN, Headers.KEY_OPERATIONS_DECRYPT).build();
        Assert.fail();
    } catch (CoseException e) {
    // pass
    }
    try {
        EncryptionKey.builder().withSecretKey(new byte[0]).build();
        Assert.fail();
    } catch (CoseException e) {
    // pass
    }
}
Also used : CoseException(com.google.cose.exceptions.CoseException) ByteString(co.nstant.in.cbor.model.ByteString) Test(org.junit.Test)

Example 5 with CoseException

use of com.google.cose.exceptions.CoseException in project cose-lib by android.

the class MacKeyTest method testBuilderFailureScenarios.

@Test
public void testBuilderFailureScenarios() throws CborException {
    final String kVal = "65eda5a12577c2bae829437fe338701a10aaa375e1bb5b5de108de439c08551d";
    final byte[] secretKey = TestUtilities.hexStringToByteArray(kVal);
    try {
        MacKey.builder().build();
        Assert.fail();
    } catch (CoseException e) {
    // pass
    }
    try {
        MacKey.builder().withSecretKey(secretKey).withOperations(Headers.KEY_OPERATIONS_SIGN, Headers.KEY_OPERATIONS_MAC_CREATE).build();
        Assert.fail();
    } catch (CoseException e) {
    // pass
    }
    try {
        MacKey.builder().withSecretKey(new byte[0]).build();
        Assert.fail();
    } catch (CoseException e) {
    // pass
    }
}
Also used : CoseException(com.google.cose.exceptions.CoseException) ByteString(co.nstant.in.cbor.model.ByteString) Test(org.junit.Test)

Aggregations

CoseException (com.google.cose.exceptions.CoseException)17 Test (org.junit.Test)6 ByteString (co.nstant.in.cbor.model.ByteString)5 DataItem (co.nstant.in.cbor.model.DataItem)5 ArrayList (java.util.ArrayList)4 CborBuilder (co.nstant.in.cbor.CborBuilder)3 ArrayBuilder (co.nstant.in.cbor.builder.ArrayBuilder)3 Map (co.nstant.in.cbor.model.Map)2 AlgorithmParameters (java.security.AlgorithmParameters)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 ECParameterSpec (java.security.spec.ECParameterSpec)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 InvalidParameterSpecException (java.security.spec.InvalidParameterSpecException)2 CborDecoder (co.nstant.in.cbor.CborDecoder)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 BigInteger (java.math.BigInteger)1 KeyPair (java.security.KeyPair)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 ECPoint (java.security.spec.ECPoint)1