1 // Copyright 2017 Google Inc. 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.internal.MonitoringAnnotations; 20 import com.google.crypto.tink.proto.Keyset; 21 import com.google.protobuf.ExtensionRegistryLite; 22 import com.google.protobuf.InvalidProtocolBufferException; 23 import java.io.IOException; 24 import java.security.GeneralSecurityException; 25 import java.util.Map; 26 27 /** 28 * Static methods for reading or writing cleartext keysets. 29 * 30 * <h3>WARNING</h3> 31 * 32 * <p>Reading or writing cleartext keysets is a bad practice, usage of this API should be 33 * restricted. Users can read encrypted keysets using {@link KeysetHandle#read}. 34 * 35 * @since 1.0.0 36 */ 37 public final class CleartextKeysetHandle { 38 /** 39 * @return a new {@link KeysetHandle} from {@code serialized} that is a serialized {@link Keyset} 40 * @throws GeneralSecurityException 41 * @deprecated Call {@code TinkProtoKeysetFormat.parseKeyset(serialized, 42 * InsecureSecretKeyAccess.get())} which has the same semantics. 43 */ 44 @SuppressWarnings("UnusedException") 45 @Deprecated parseFrom(final byte[] serialized)46 public static final KeysetHandle parseFrom(final byte[] serialized) 47 throws GeneralSecurityException { 48 try { 49 Keyset keyset = Keyset.parseFrom(serialized, ExtensionRegistryLite.getEmptyRegistry()); 50 return KeysetHandle.fromKeyset(keyset); 51 } catch (InvalidProtocolBufferException e) { 52 throw new GeneralSecurityException("invalid keyset"); 53 } 54 } 55 56 /** 57 * @return a new {@link KeysetHandle} from a {@link Keyset} read with {@code reader}. 58 * @throws GeneralSecurityException when the keyset is invalid or can't be read. 59 */ read(KeysetReader reader)60 public static KeysetHandle read(KeysetReader reader) 61 throws GeneralSecurityException, IOException { 62 return KeysetHandle.fromKeyset(reader.read()); 63 } 64 65 /** 66 * Creates a {@link KeysetHandle} from a {@code KeysetReader}. 67 * 68 * <p>The additional {@code monitoringAnnotations} are used for monitoring, and will be passed to 69 * the {@link MonitoringClient}. 70 * 71 * @throws GeneralSecurityException when the keyset is invalid or cannot be read. 72 * @deprecated Instead, use a {@link KeysetHandle.Builder}. 73 */ 74 @Deprecated read( KeysetReader reader, Map<String, String> monitoringAnnotations)75 public static KeysetHandle read( 76 KeysetReader reader, Map<String, String> monitoringAnnotations) 77 throws GeneralSecurityException, IOException { 78 return KeysetHandle.fromKeysetAndAnnotations( 79 reader.read(), MonitoringAnnotations.newBuilder().addAll(monitoringAnnotations).build()); 80 } 81 82 /** 83 * @return the keyset underlying this {@code keysetHandle}. 84 * @deprecated Instead, call "KeysetHandle.getAt()" to get information about individual keys or 85 * TinkProtoKeysetFormat if you need a serialized keyset. 86 */ 87 @Deprecated getKeyset(KeysetHandle keysetHandle)88 public static Keyset getKeyset(KeysetHandle keysetHandle) { 89 return keysetHandle.getKeyset(); 90 } 91 92 /** 93 * Returns a KeysetHandle for {@code keyset}. 94 * 95 * @deprecated Instead, use a {@link KeysetHandle.Builder}. 96 */ 97 @Deprecated fromKeyset(Keyset keyset)98 public static KeysetHandle fromKeyset(Keyset keyset) throws GeneralSecurityException { 99 return KeysetHandle.fromKeyset(keyset); 100 } 101 102 /** 103 * Serializes and writes the {@link Keyset} managed by {@code handle} to {@code keysetWriter}. 104 * 105 * @throws IOException 106 */ write(KeysetHandle handle, KeysetWriter keysetWriter)107 public static void write(KeysetHandle handle, KeysetWriter keysetWriter) throws IOException { 108 keysetWriter.write(handle.getKeyset()); 109 } 110 CleartextKeysetHandle()111 private CleartextKeysetHandle() {} 112 } 113