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.proto.EncryptedKeyset; 20 import com.google.crypto.tink.proto.Keyset; 21 import com.google.errorprone.annotations.InlineMe; 22 import com.google.protobuf.ExtensionRegistryLite; 23 import java.io.ByteArrayInputStream; 24 import java.io.File; 25 import java.io.FileInputStream; 26 import java.io.IOException; 27 import java.io.InputStream; 28 29 /** 30 * A {@link KeysetReader} that can read from some source cleartext or encrypted keysets in <a 31 * href="https://developers.google.com/protocol-buffers/docs/encoding">proto binary wire format</a>. 32 * 33 * @since 1.0.0 34 */ 35 public final class BinaryKeysetReader implements KeysetReader { 36 private final InputStream inputStream; 37 38 /** 39 * Static method to create a BinaryKeysetReader from an {@link InputStream}. 40 * 41 * <p>Note: the input stream won't be read until {@link BinaryKeysetReader#read} or {@link 42 * BinaryKeysetReader#readEncrypted} is called, and will be closed immediately after the keyset is 43 * read. 44 */ withInputStream(InputStream stream)45 public static KeysetReader withInputStream(InputStream stream) { 46 return new BinaryKeysetReader(stream); 47 } 48 49 /** Static method to create a BinaryKeysetReader from a byte arrary. */ withBytes(final byte[] bytes)50 public static KeysetReader withBytes(final byte[] bytes) { 51 return new BinaryKeysetReader(new ByteArrayInputStream(bytes)); 52 } 53 54 /** 55 * Static method to create a BinaryKeysetReader from a file. 56 * 57 * <p>Note: the input file won't be read until {@link BinaryKeysetReader#read} or {@link 58 * BinaryKeysetReader#readEncrypted} is called. 59 * 60 * @deprecated Inline the function. 61 */ 62 @InlineMe( 63 replacement = "BinaryKeysetReader.withInputStream(new FileInputStream(file))", 64 imports = {"com.google.crypto.tink.BinaryKeysetReader", "java.io.FileInputStream"}) 65 @Deprecated withFile(File file)66 public static KeysetReader withFile(File file) throws IOException { 67 return withInputStream(new FileInputStream(file)); 68 } 69 BinaryKeysetReader(InputStream stream)70 private BinaryKeysetReader(InputStream stream) { 71 this.inputStream = stream; 72 } 73 74 @Override read()75 public Keyset read() throws IOException { 76 try { 77 return Keyset.parseFrom(inputStream, ExtensionRegistryLite.getEmptyRegistry()); 78 } finally { 79 inputStream.close(); 80 } 81 } 82 83 @Override readEncrypted()84 public EncryptedKeyset readEncrypted() throws IOException { 85 try { 86 return EncryptedKeyset.parseFrom(inputStream, ExtensionRegistryLite.getEmptyRegistry()); 87 } finally { 88 inputStream.close(); 89 } 90 } 91 } 92