PrivateKeyManagerImpl extends KeyManagerImpl<PrimitiveT, KeyProtoT> implements PrivateKeyManager<PrimitiveT> { @Override public KeyData getPublicKeyData(ByteString serializedKey) throws GeneralSecurityException { try { KeyProtoT privKeyProto = privateKeyManager.parseKey(serializedKey); privateKeyManager.validateKey(privKeyProto); PublicKeyProtoT publicKeyProto = privateKeyManager.getPublicKey(privKeyProto); publicKeyManager.validateKey(publicKeyProto); return KeyData.newBuilder() .setTypeUrl(publicKeyManager.getKeyType()) .setValue(publicKeyProto.toByteString()) .setKeyMaterialType(publicKeyManager.keyMaterialType()) .build(); } catch (InvalidProtocolBufferException e) { throw new GeneralSecurityException("expected serialized proto of type ", e); } } PrivateKeyManagerImpl(
      PrivateKeyTypeManager<KeyProtoT, PublicKeyProtoT> privateKeyManager,
      KeyTypeManager<PublicKeyProtoT> publicKeyManager,
      Class<PrimitiveT> primitiveClass); @Override KeyData getPublicKeyData(ByteString serializedKey);  }
@Test public void getPublicKeyData_works() throws Exception { TestPrivateKeyTypeManager privateManager = new TestPrivateKeyTypeManager(); TestPublicKeyTypeManager publicManager = new TestPublicKeyTypeManager(); PrivateKeyManager<Void> manager = new PrivateKeyManagerImpl<>(privateManager, publicManager, Void.class); Ed25519PrivateKey privateKey = Ed25519PrivateKey.newBuilder() .setPublicKey( Ed25519PublicKey.newBuilder() .setKeyValue(ByteString.copyFrom(Random.randBytes(32)))) .setKeyValue(ByteString.copyFrom(Random.randBytes(32))) .build(); KeyData keyData = manager.getPublicKeyData(privateKey.toByteString()); assertThat(keyData.getTypeUrl()) .isEqualTo("type.googleapis.com/google.crypto.tink.Ed25519PublicKey"); Ed25519PublicKey publicKey = Ed25519PublicKey.parseFrom(keyData.getValue(), ExtensionRegistryLite.getEmptyRegistry()); assertThat(publicKey).isEqualTo(privateKey.getPublicKey()); assertThat(keyData.getKeyMaterialType()).isEqualTo(KeyMaterialType.ASYMMETRIC_PUBLIC); }
@Test public void getPublicKeyData_invalidPrivateKey_throws() throws Exception { TestPrivateKeyTypeManager privateManager = new TestPrivateKeyTypeManager(); TestPublicKeyTypeManager publicManager = new TestPublicKeyTypeManager(); PrivateKeyManager<Void> manager = new PrivateKeyManagerImpl<>(privateManager, publicManager, Void.class); Ed25519PrivateKey privateKey = Ed25519PrivateKey.newBuilder() .setPublicKey( Ed25519PublicKey.newBuilder() .setKeyValue(ByteString.copyFrom(Random.randBytes(32)))) .setKeyValue(ByteString.copyFrom(Random.randBytes(33))) .build(); ByteString privateKeyByteString = privateKey.toByteString(); try { manager.getPublicKeyData(privateKeyByteString); fail(); } catch (GeneralSecurityException e) { assertExceptionContains(e, "validateKey(Ed25519PrivateKey)"); } }
@Test public void getPublicKeyData_invalidPublicKey_throws() throws Exception { TestPrivateKeyTypeManager privateManager = new TestPrivateKeyTypeManager(); TestPublicKeyTypeManager publicManager = new TestPublicKeyTypeManager(); PrivateKeyManager<Void> manager = new PrivateKeyManagerImpl<>(privateManager, publicManager, Void.class); Ed25519PrivateKey privateKey = Ed25519PrivateKey.newBuilder() .setPublicKey( Ed25519PublicKey.newBuilder() .setKeyValue(ByteString.copyFrom(Random.randBytes(33)))) .setKeyValue(ByteString.copyFrom(Random.randBytes(32))) .build(); ByteString privateKeyByteString = privateKey.toByteString(); try { manager.getPublicKeyData(privateKeyByteString); fail(); } catch (GeneralSecurityException e) { assertExceptionContains(e, "validateKey(Ed25519PublicKey)"); } }