use of co.nstant.in.cbor.builder.ArrayBuilder in project identity-credential by google.
the class Util method coseSign1Sign.
@NonNull
static DataItem coseSign1Sign(@NonNull Signature s, @Nullable byte[] data, @Nullable byte[] detachedContent, @Nullable Collection<X509Certificate> certificateChain) {
int dataLen = (data != null ? data.length : 0);
int detachedContentLen = (detachedContent != null ? detachedContent.length : 0);
if (dataLen > 0 && detachedContentLen > 0) {
throw new IllegalArgumentException("data and detachedContent cannot both be non-empty");
}
int keySize;
int alg;
if (s.getAlgorithm().equals("SHA256withECDSA")) {
keySize = 32;
alg = COSE_ALG_ECDSA_256;
} else if (s.getAlgorithm().equals("SHA384withECDSA")) {
keySize = 48;
alg = COSE_ALG_ECDSA_384;
} else if (s.getAlgorithm().equals("SHA512withECDSA")) {
keySize = 64;
alg = COSE_ALG_ECDSA_512;
} else {
throw new IllegalArgumentException("Unsupported algorithm " + s.getAlgorithm());
}
CborBuilder protectedHeaders = new CborBuilder();
MapBuilder<CborBuilder> protectedHeadersMap = protectedHeaders.addMap();
protectedHeadersMap.put(COSE_LABEL_ALG, alg);
byte[] protectedHeadersBytes = cborEncode(protectedHeaders.build().get(0));
byte[] toBeSigned = coseBuildToBeSigned(protectedHeadersBytes, data, detachedContent);
byte[] coseSignature = null;
try {
s.update(toBeSigned);
byte[] derSignature = s.sign();
coseSignature = signatureDerToCose(derSignature, keySize);
} catch (SignatureException e) {
throw new IllegalStateException("Error signing data", e);
}
CborBuilder builder = new CborBuilder();
ArrayBuilder<CborBuilder> array = builder.addArray();
array.add(protectedHeadersBytes);
MapBuilder<ArrayBuilder<CborBuilder>> unprotectedHeaders = array.addMap();
try {
if (certificateChain != null && certificateChain.size() > 0) {
if (certificateChain.size() == 1) {
X509Certificate cert = certificateChain.iterator().next();
unprotectedHeaders.put(COSE_LABEL_X5CHAIN, cert.getEncoded());
} else {
ArrayBuilder<MapBuilder<ArrayBuilder<CborBuilder>>> x5chainsArray = unprotectedHeaders.putArray(COSE_LABEL_X5CHAIN);
for (X509Certificate cert : certificateChain) {
x5chainsArray.add(cert.getEncoded());
}
}
}
} catch (CertificateEncodingException e) {
throw new IllegalStateException("Error encoding certificate", e);
}
if (data == null || data.length == 0) {
array.add(new SimpleValue(SimpleValueType.NULL));
} else {
array.add(data);
}
array.add(coseSignature);
return builder.build().get(0);
}
use of co.nstant.in.cbor.builder.ArrayBuilder in project cose-lib by android.
the class Recipient method encode.
@Override
public DataItem encode() throws CborException {
ArrayBuilder<CborBuilder> arrayBuilder = new CborBuilder().addArray();
arrayBuilder.add(CoseUtils.serializeProtectedHeaders(getProtectedHeaders())).add(getUnprotectedHeaders()).add(getCiphertext());
if (recipients != null && !recipients.isEmpty()) {
ArrayBuilder<ArrayBuilder<CborBuilder>> recipientArrayBuilder = arrayBuilder.addArray();
for (Recipient recipient : recipients) {
recipientArrayBuilder.add(recipient.encode());
}
recipientArrayBuilder.end();
}
return arrayBuilder.end().build().get(0);
}
use of co.nstant.in.cbor.builder.ArrayBuilder in project cose-lib by android.
the class SignMessage method encode.
@Override
public DataItem encode() throws CoseException, CborException {
if (signatures == null || signatures.size() == 0) {
throw new CoseException("Error while serializing SignMessage. Signatures not found.");
}
ArrayBuilder<CborBuilder> messageBuilder = new CborBuilder().addArray();
messageBuilder.add(CoseUtils.serializeProtectedHeaders(getProtectedHeaders())).add(getUnprotectedHeaders()).add(message);
ArrayBuilder<ArrayBuilder<CborBuilder>> signArrayBuilder = messageBuilder.addArray();
for (Signature signature : signatures) {
signArrayBuilder.add(signature.encode());
}
signArrayBuilder.end();
return messageBuilder.end().build().get(0);
}
use of co.nstant.in.cbor.builder.ArrayBuilder in project cose-lib by android.
the class EncryptMessage method encode.
@Override
public DataItem encode() throws CborException, CoseException {
ArrayBuilder<CborBuilder> encryptArrayBuilder = new CborBuilder().addArray();
encryptArrayBuilder.add(CoseUtils.serializeProtectedHeaders(getProtectedHeaders())).add(getUnprotectedHeaders()).add(ciphertext);
ArrayBuilder<ArrayBuilder<CborBuilder>> recipientArrayBuilder = encryptArrayBuilder.addArray();
if (recipients == null) {
throw new CoseException("Error while serializing EncryptMessage. Recipient field not found.");
}
for (Recipient recipient : recipients) {
recipientArrayBuilder.add(recipient.encode());
}
recipientArrayBuilder.end();
return encryptArrayBuilder.end().build().get(0);
}
use of co.nstant.in.cbor.builder.ArrayBuilder in project cose-lib by android.
the class MacMessage method encode.
@Override
public DataItem encode() throws CoseException, CborException {
if (recipients == null) {
throw new CoseException("Error while serializing MacMessage. Recipient field not found.");
}
ArrayBuilder<CborBuilder> macArrayBuilder = new CborBuilder().addArray();
macArrayBuilder.add(CoseUtils.serializeProtectedHeaders(getProtectedHeaders())).add(getUnprotectedHeaders()).add(message).add(tag);
ArrayBuilder<ArrayBuilder<CborBuilder>> recipientArrayBuilder = macArrayBuilder.addArray();
for (Recipient recipient : recipients) {
recipientArrayBuilder.add(recipient.encode());
}
recipientArrayBuilder.end();
return macArrayBuilder.end().build().get(0);
}
Aggregations