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 static java.nio.charset.StandardCharsets.UTF_8; 20 import static org.junit.Assert.assertThrows; 21 22 import com.google.common.truth.Expect; 23 import com.google.crypto.tink.config.TinkConfig; 24 import com.google.crypto.tink.proto.KeyStatusType; 25 import com.google.crypto.tink.proto.Keyset; 26 import com.google.crypto.tink.proto.OutputPrefixType; 27 import com.google.crypto.tink.testing.TestUtil; 28 import java.io.IOException; 29 import java.security.GeneralSecurityException; 30 import org.junit.BeforeClass; 31 import org.junit.Rule; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.junit.runners.JUnit4; 35 36 /** Tests for NoSecretKeysetHandle. */ 37 @RunWith(JUnit4.class) 38 public class NoSecretKeysetHandleTest { 39 @Rule public final Expect expect = Expect.create(); 40 41 @BeforeClass setUp()42 public static void setUp() throws GeneralSecurityException { 43 TinkConfig.register(); 44 } 45 46 @SuppressWarnings("deprecation") // This is a test for deprecated functions 47 @Test withTypeAsymmetricPublic_deprecated_noSecretKeysetHandle_sameAs_readNoSecret()48 public void withTypeAsymmetricPublic_deprecated_noSecretKeysetHandle_sameAs_readNoSecret() 49 throws Exception { 50 KeysetHandle privateHandle = KeysetHandle.generateNew(KeyTemplates.get("ECDSA_P256")); 51 Keyset keyset = privateHandle.getPublicKeysetHandle().getKeyset(); 52 53 Keyset keyset2 = NoSecretKeysetHandle.parseFrom(keyset.toByteArray()).getKeyset(); 54 Keyset keyset3 = 55 NoSecretKeysetHandle.read(BinaryKeysetReader.withBytes(keyset.toByteArray())).getKeyset(); 56 57 Keyset keyset4 = KeysetHandle.readNoSecret(keyset.toByteArray()).getKeyset(); 58 Keyset keyset5 = 59 KeysetHandle.readNoSecret(BinaryKeysetReader.withBytes(keyset.toByteArray())).getKeyset(); 60 61 expect.that(keyset).isEqualTo(keyset2); 62 expect.that(keyset).isEqualTo(keyset3); 63 expect.that(keyset).isEqualTo(keyset4); 64 expect.that(keyset).isEqualTo(keyset5); 65 } 66 67 @SuppressWarnings("deprecation") // This is a test for the deprecated functions 68 @Test withTypeSymmetric_deprecated_noSecretKeysetHandle_sameAs_readNoSecret()69 public void withTypeSymmetric_deprecated_noSecretKeysetHandle_sameAs_readNoSecret() 70 throws Exception { 71 String keyValue = "01234567890123456"; 72 Keyset keyset = 73 TestUtil.createKeyset( 74 TestUtil.createKey( 75 TestUtil.createHmacKeyData(keyValue.getBytes(UTF_8), 16), 76 42, 77 KeyStatusType.ENABLED, 78 OutputPrefixType.TINK)); 79 80 assertThrows( 81 GeneralSecurityException.class, () -> NoSecretKeysetHandle.parseFrom(keyset.toByteArray())); 82 assertThrows( 83 GeneralSecurityException.class, 84 () -> NoSecretKeysetHandle.read(BinaryKeysetReader.withBytes(keyset.toByteArray()))); 85 86 assertThrows( 87 GeneralSecurityException.class, () -> KeysetHandle.readNoSecret(keyset.toByteArray())); 88 assertThrows( 89 GeneralSecurityException.class, 90 () -> KeysetHandle.readNoSecret(BinaryKeysetReader.withBytes(keyset.toByteArray()))); 91 } 92 93 @SuppressWarnings("deprecation") // This is a test for deprecated functions 94 @Test withTypeAsymmetricPrivate_deprecated_noSecretKeysetHandle_sameAs_readNoSecret()95 public void withTypeAsymmetricPrivate_deprecated_noSecretKeysetHandle_sameAs_readNoSecret() 96 throws Exception { 97 Keyset keyset = KeysetHandle.generateNew(KeyTemplates.get("ECDSA_P256")).getKeyset(); 98 99 assertThrows( 100 GeneralSecurityException.class, () -> NoSecretKeysetHandle.parseFrom(keyset.toByteArray())); 101 assertThrows( 102 GeneralSecurityException.class, 103 () -> NoSecretKeysetHandle.read(BinaryKeysetReader.withBytes(keyset.toByteArray()))); 104 assertThrows( 105 GeneralSecurityException.class, 106 () -> NoSecretKeysetHandle.read(BinaryKeysetReader.withBytes(keyset.toByteArray()))); 107 108 assertThrows( 109 GeneralSecurityException.class, () -> KeysetHandle.readNoSecret(keyset.toByteArray())); 110 assertThrows( 111 GeneralSecurityException.class, 112 () -> KeysetHandle.readNoSecret(BinaryKeysetReader.withBytes(keyset.toByteArray()))); 113 } 114 115 @SuppressWarnings("deprecation") // This is a test for deprecated functions 116 @Test withEmptyKeyset_deprecated_noSecretKeysetHandle_sameAs_readNoSecret()117 public void withEmptyKeyset_deprecated_noSecretKeysetHandle_sameAs_readNoSecret() 118 throws Exception { 119 assertThrows(GeneralSecurityException.class, () -> NoSecretKeysetHandle.parseFrom(new byte[0])); 120 assertThrows( 121 GeneralSecurityException.class, 122 () -> NoSecretKeysetHandle.read(BinaryKeysetReader.withBytes(new byte[0]))); 123 124 assertThrows(GeneralSecurityException.class, () -> KeysetHandle.readNoSecret(new byte[0])); 125 assertThrows( 126 GeneralSecurityException.class, 127 () -> KeysetHandle.readNoSecret(BinaryKeysetReader.withBytes(new byte[0]))); 128 } 129 130 @SuppressWarnings("deprecation") // This is a test for the deprecated function 131 @Test withInvalidKeyset_deprecated_noSecretKeysetHandle_almostSameAs_readNoSecret()132 public void withInvalidKeyset_deprecated_noSecretKeysetHandle_almostSameAs_readNoSecret() 133 throws Exception { 134 byte[] invalidSerializedProto = new byte[] {0x00, 0x01, 0x02}; 135 assertThrows( 136 GeneralSecurityException.class, 137 () -> NoSecretKeysetHandle.parseFrom(invalidSerializedProto)); 138 assertThrows( 139 IOException.class, // This is inconsistent 140 () -> NoSecretKeysetHandle.read(BinaryKeysetReader.withBytes(invalidSerializedProto))); 141 142 assertThrows( 143 GeneralSecurityException.class, () -> KeysetHandle.readNoSecret(invalidSerializedProto)); 144 assertThrows( 145 GeneralSecurityException.class, 146 () -> KeysetHandle.readNoSecret(BinaryKeysetReader.withBytes(invalidSerializedProto))); 147 } 148 } 149