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);
}
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);
}
}
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
}
}
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
}
}
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
}
}
Aggregations