public Key(K2Context context, KeyData data)
throws UnregisteredKeyVersionException, InvalidKeyDataException {
// NOTE: lower-level exceptions take precedence by design
KeyVersionRegistry registry = context.getKeyVersionRegistry();
ExtensionRegistry protoRegistry = registry.getProtoExtensions();
// Retain the core
if (!data.hasCore()) {
// Core field is required
throw new InvalidKeyDataException(
InvalidKeyDataException.Reason.PROTO_PARSE, null);
}
coreBytes = data.getCore();
// Parse the core, containing the security/usage constraints
KeyCore core;
try {
core = KeyCore.parseFrom(coreBytes, protoRegistry);
} catch (InvalidProtocolBufferException ex) {
throw new InvalidKeyDataException(
InvalidKeyDataException.Reason.PROTO_PARSE, ex);
}
// TODO(darylseah): extract security properties from core
// Extract the key version list
final int kvCount = data.getKeyVersionCount();
keyVersions.ensureCapacity(kvCount);
UnregisteredKeyVersionException unregisteredException = null;
InvalidKeyDataException buildException = null;
for (KeyVersionData kvData : data.getKeyVersionList()) {
if (!kvData.hasType()) {
// Type field is required
throw new InvalidKeyDataException(
InvalidKeyDataException.Reason.PROTO_PARSE, null);
}
try {
KeyVersion kv = registry.newBuilder(kvData.getType())
.withData(kvData, protoRegistry).build();
keyVersions.add(kv);
} catch (InvalidProtocolBufferException ex) {
// Throw proto parsing exceptions immediately
throw new InvalidKeyDataException(