1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.keystore.cts; 18 19 import java.security.Key; 20 import java.security.KeyPair; 21 22 import javax.crypto.SecretKey; 23 24 public class ImportedKey { 25 private final boolean mSymmetric; 26 private final String mAlias; 27 private final KeyPair mOriginalKeyPair; 28 private final KeyPair mKeystoreBackedKeyPair; 29 private final SecretKey mOriginalSecretKey; 30 private final SecretKey mKeystoreBackedSecretKey; 31 ImportedKey(String alias, KeyPair original, KeyPair keystoreBacked)32 public ImportedKey(String alias, KeyPair original, KeyPair keystoreBacked) { 33 mAlias = alias; 34 mSymmetric = false; 35 mOriginalKeyPair = original; 36 mKeystoreBackedKeyPair = keystoreBacked; 37 mOriginalSecretKey = null; 38 mKeystoreBackedSecretKey = null; 39 } 40 ImportedKey(String alias, SecretKey original, SecretKey keystoreBacked)41 public ImportedKey(String alias, SecretKey original, SecretKey keystoreBacked) { 42 mAlias = alias; 43 mSymmetric = true; 44 mOriginalKeyPair = null; 45 mKeystoreBackedKeyPair = null; 46 mOriginalSecretKey = original; 47 mKeystoreBackedSecretKey = keystoreBacked; 48 } 49 getAlias()50 public String getAlias() { 51 return mAlias; 52 } 53 getOriginalEncryptionKey()54 public Key getOriginalEncryptionKey() { 55 if (mSymmetric) { 56 return mOriginalSecretKey; 57 } else { 58 return mOriginalKeyPair.getPublic(); 59 } 60 } 61 getOriginalDecryptionKey()62 public Key getOriginalDecryptionKey() { 63 if (mSymmetric) { 64 return mOriginalSecretKey; 65 } else { 66 return mOriginalKeyPair.getPrivate(); 67 } 68 } 69 getOriginalSigningKey()70 public Key getOriginalSigningKey() { 71 if (mSymmetric) { 72 return mOriginalSecretKey; 73 } else { 74 return mOriginalKeyPair.getPrivate(); 75 } 76 } 77 getOriginalVerificationKey()78 public Key getOriginalVerificationKey() { 79 if (mSymmetric) { 80 return mOriginalSecretKey; 81 } else { 82 return mOriginalKeyPair.getPublic(); 83 } 84 } 85 getKeystoreBackedEncryptionKey()86 public Key getKeystoreBackedEncryptionKey() { 87 if (mSymmetric) { 88 return mKeystoreBackedSecretKey; 89 } else { 90 return mKeystoreBackedKeyPair.getPublic(); 91 } 92 } 93 getKeystoreBackedDecryptionKey()94 public Key getKeystoreBackedDecryptionKey() { 95 if (mSymmetric) { 96 return mKeystoreBackedSecretKey; 97 } else { 98 return mKeystoreBackedKeyPair.getPrivate(); 99 } 100 } 101 getKeystoreBackedSigningKey()102 public Key getKeystoreBackedSigningKey() { 103 if (mSymmetric) { 104 return mKeystoreBackedSecretKey; 105 } else { 106 return mKeystoreBackedKeyPair.getPrivate(); 107 } 108 } 109 getKeystoreBackedVerificationKey()110 public Key getKeystoreBackedVerificationKey() { 111 if (mSymmetric) { 112 return mKeystoreBackedSecretKey; 113 } else { 114 return mKeystoreBackedKeyPair.getPublic(); 115 } 116 } 117 118 getOriginalKeyPair()119 public KeyPair getOriginalKeyPair() { 120 checkIsKeyPair(); 121 return mOriginalKeyPair; 122 } 123 getKeystoreBackedKeyPair()124 public KeyPair getKeystoreBackedKeyPair() { 125 checkIsKeyPair(); 126 return mKeystoreBackedKeyPair; 127 } 128 getOriginalSecretKey()129 public SecretKey getOriginalSecretKey() { 130 checkIsSecretKey(); 131 return mOriginalSecretKey; 132 } 133 getKeystoreBackedSecretKey()134 public SecretKey getKeystoreBackedSecretKey() { 135 checkIsSecretKey(); 136 return mKeystoreBackedSecretKey; 137 } 138 checkIsKeyPair()139 private void checkIsKeyPair() { 140 if (mSymmetric) { 141 throw new IllegalStateException("Not a KeyPair"); 142 } 143 } 144 checkIsSecretKey()145 private void checkIsSecretKey() { 146 if (!mSymmetric) { 147 throw new IllegalStateException("Not a SecretKey"); 148 } 149 } 150 } 151