1 // Copyright 2022 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 java.io.ByteArrayOutputStream; 20 import java.io.IOException; 21 import java.security.GeneralSecurityException; 22 23 /** Functions to parse and serialize Keyset in Tink's binary format based on Protobufs. */ 24 public final class TinkProtoKeysetFormat { 25 26 @SuppressWarnings("UnusedException") parseKeyset(byte[] serializedKeyset, SecretKeyAccess access)27 public static KeysetHandle parseKeyset(byte[] serializedKeyset, SecretKeyAccess access) 28 throws GeneralSecurityException { 29 if (access == null) { 30 throw new NullPointerException("SecretKeyAccess cannot be null"); 31 } 32 try { 33 return CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(serializedKeyset)); 34 } catch (IOException e) { 35 throw new GeneralSecurityException("Parse keyset failed"); 36 } 37 } 38 39 @SuppressWarnings("UnusedException") serializeKeyset(KeysetHandle keysetHandle, SecretKeyAccess access)40 public static byte[] serializeKeyset(KeysetHandle keysetHandle, SecretKeyAccess access) 41 throws GeneralSecurityException { 42 if (access == null) { 43 throw new NullPointerException("SecretKeyAccess cannot be null"); 44 } 45 try { 46 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 47 CleartextKeysetHandle.write(keysetHandle, BinaryKeysetWriter.withOutputStream(outputStream)); 48 return outputStream.toByteArray(); 49 } catch (IOException e) { 50 throw new GeneralSecurityException("Serialize keyset failed"); 51 } 52 } 53 54 @SuppressWarnings("UnusedException") parseKeysetWithoutSecret(byte[] serializedKeyset)55 public static KeysetHandle parseKeysetWithoutSecret(byte[] serializedKeyset) 56 throws GeneralSecurityException { 57 return KeysetHandle.readNoSecret(serializedKeyset); 58 } 59 60 @SuppressWarnings("UnusedException") serializeKeysetWithoutSecret(KeysetHandle keysetHandle)61 public static byte[] serializeKeysetWithoutSecret(KeysetHandle keysetHandle) 62 throws GeneralSecurityException { 63 try { 64 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 65 keysetHandle.writeNoSecret(BinaryKeysetWriter.withOutputStream(outputStream)); 66 return outputStream.toByteArray(); 67 } catch (IOException e) { 68 throw new GeneralSecurityException("Serialize keyset failed"); 69 } 70 } 71 72 @SuppressWarnings("UnusedException") parseEncryptedKeyset( byte[] serializedEncryptedKeyset, Aead keysetEncryptionAead, byte[] associatedData)73 public static KeysetHandle parseEncryptedKeyset( 74 byte[] serializedEncryptedKeyset, Aead keysetEncryptionAead, byte[] associatedData) 75 throws GeneralSecurityException { 76 try { 77 return KeysetHandle.readWithAssociatedData( 78 BinaryKeysetReader.withBytes(serializedEncryptedKeyset), 79 keysetEncryptionAead, 80 associatedData); 81 } catch (IOException e) { 82 throw new GeneralSecurityException("Parse keyset failed"); 83 } 84 } 85 86 @SuppressWarnings("UnusedException") serializeEncryptedKeyset( KeysetHandle keysetHandle, Aead keysetEncryptionAead, byte[] associatedData)87 public static byte[] serializeEncryptedKeyset( 88 KeysetHandle keysetHandle, Aead keysetEncryptionAead, byte[] associatedData) 89 throws GeneralSecurityException { 90 try { 91 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 92 keysetHandle.writeWithAssociatedData( 93 BinaryKeysetWriter.withOutputStream(outputStream), keysetEncryptionAead, associatedData); 94 return outputStream.toByteArray(); 95 } catch (IOException e) { 96 throw new GeneralSecurityException("Serialize keyset failed"); 97 } 98 } 99 TinkProtoKeysetFormat()100 private TinkProtoKeysetFormat() {} 101 } 102