1 // Copyright 2020 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 //////////////////////////////////////////////////////////////////////////////// 16 17 package com.google.crypto.tink; 18 19 import com.google.crypto.tink.annotations.Alpha; 20 import com.google.crypto.tink.internal.KeyTypeManager; 21 import com.google.crypto.tink.internal.PrivateKeyTypeManager; 22 import com.google.crypto.tink.proto.KeyData; 23 import com.google.protobuf.ByteString; 24 import com.google.protobuf.InvalidProtocolBufferException; 25 import com.google.protobuf.MessageLite; 26 import java.security.GeneralSecurityException; 27 28 /** 29 * Implementation of the {@link PrivateKeyManager} interface based on an {@link 30 * PrivateKeyTypeManager} and the corresponding public key manager, implemented by an {@link 31 * KeyTypeManager}. 32 * 33 * <p>Choosing {@code PrimitiveT} equal to {@link java.lang.Void} is valid; in this case the 34 * functions {@link #getPrimitive} will throw if invoked. 35 */ 36 @Alpha 37 class PrivateKeyManagerImpl< 38 PrimitiveT, KeyProtoT extends MessageLite, PublicKeyProtoT extends MessageLite> 39 extends KeyManagerImpl<PrimitiveT, KeyProtoT> implements PrivateKeyManager<PrimitiveT> { 40 41 private final PrivateKeyTypeManager<KeyProtoT, PublicKeyProtoT> privateKeyManager; 42 private final KeyTypeManager<PublicKeyProtoT> publicKeyManager; 43 PrivateKeyManagerImpl( PrivateKeyTypeManager<KeyProtoT, PublicKeyProtoT> privateKeyManager, KeyTypeManager<PublicKeyProtoT> publicKeyManager, Class<PrimitiveT> primitiveClass)44 public PrivateKeyManagerImpl( 45 PrivateKeyTypeManager<KeyProtoT, PublicKeyProtoT> privateKeyManager, 46 KeyTypeManager<PublicKeyProtoT> publicKeyManager, 47 Class<PrimitiveT> primitiveClass) { 48 super(privateKeyManager, primitiveClass); 49 this.privateKeyManager = privateKeyManager; 50 this.publicKeyManager = publicKeyManager; 51 } 52 53 @Override getPublicKeyData(ByteString serializedKey)54 public KeyData getPublicKeyData(ByteString serializedKey) throws GeneralSecurityException { 55 try { 56 KeyProtoT privKeyProto = privateKeyManager.parseKey(serializedKey); 57 privateKeyManager.validateKey(privKeyProto); 58 PublicKeyProtoT publicKeyProto = privateKeyManager.getPublicKey(privKeyProto); 59 publicKeyManager.validateKey(publicKeyProto); 60 return KeyData.newBuilder() 61 .setTypeUrl(publicKeyManager.getKeyType()) 62 .setValue(publicKeyProto.toByteString()) 63 .setKeyMaterialType(publicKeyManager.keyMaterialType()) 64 .build(); 65 } catch (InvalidProtocolBufferException e) { 66 throw new GeneralSecurityException("expected serialized proto of type ", e); 67 } 68 } 69 } 70