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.security.keystore2; 18 19 import android.annotation.NonNull; 20 import android.security.KeyStoreSecurityLevel; 21 import android.security.keystore.KeyProperties; 22 import android.system.keystore2.KeyDescriptor; 23 import android.system.keystore2.KeyMetadata; 24 25 import java.security.interfaces.ECPublicKey; 26 import java.security.spec.ECParameterSpec; 27 import java.security.spec.ECPoint; 28 29 /** 30 * {@link ECPublicKey} backed by keystore. 31 * 32 * @hide 33 */ 34 public class AndroidKeyStoreECPublicKey extends AndroidKeyStorePublicKey implements ECPublicKey { 35 36 private final ECParameterSpec mParams; 37 private final ECPoint mW; 38 AndroidKeyStoreECPublicKey(@onNull KeyDescriptor descriptor, @NonNull KeyMetadata metadata, @NonNull byte[] x509EncodedForm, @NonNull KeyStoreSecurityLevel securityLevel, @NonNull ECParameterSpec params, @NonNull ECPoint w)39 public AndroidKeyStoreECPublicKey(@NonNull KeyDescriptor descriptor, 40 @NonNull KeyMetadata metadata, 41 @NonNull byte[] x509EncodedForm, 42 @NonNull KeyStoreSecurityLevel securityLevel, 43 @NonNull ECParameterSpec params, @NonNull ECPoint w) { 44 super(descriptor, metadata, x509EncodedForm, KeyProperties.KEY_ALGORITHM_EC, securityLevel); 45 mParams = params; 46 mW = w; 47 } 48 AndroidKeyStoreECPublicKey(@onNull KeyDescriptor descriptor, @NonNull KeyMetadata metadata, @NonNull KeyStoreSecurityLevel securityLevel, @NonNull ECPublicKey info)49 public AndroidKeyStoreECPublicKey(@NonNull KeyDescriptor descriptor, 50 @NonNull KeyMetadata metadata, 51 @NonNull KeyStoreSecurityLevel securityLevel, @NonNull ECPublicKey info) { 52 this(descriptor, metadata, info.getEncoded(), securityLevel, info.getParams(), info.getW()); 53 if (!"X.509".equalsIgnoreCase(info.getFormat())) { 54 throw new IllegalArgumentException( 55 "Unsupported key export format: " + info.getFormat()); 56 } 57 } 58 59 @Override getPrivateKey()60 public AndroidKeyStorePrivateKey getPrivateKey() { 61 return new AndroidKeyStoreECPrivateKey( 62 getUserKeyDescriptor(), getKeyIdDescriptor().nspace, getAuthorizations(), 63 getSecurityLevel(), mParams); 64 } 65 66 @Override getParams()67 public ECParameterSpec getParams() { 68 return mParams; 69 } 70 71 @Override getW()72 public ECPoint getW() { 73 return mW; 74 } 75 } 76