Home | History | Annotate | Download | only in conscrypt
      1 /*
      2  * Copyright (C) 2017 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 org.conscrypt;
     18 
     19 import java.security.InvalidKeyException;
     20 import java.security.spec.InvalidKeySpecException;
     21 import java.security.spec.KeySpec;
     22 import javax.crypto.SecretKey;
     23 import javax.crypto.SecretKeyFactorySpi;
     24 import javax.crypto.spec.DESedeKeySpec;
     25 import javax.crypto.spec.SecretKeySpec;
     26 
     27 /**
     28  * An implementation of {@link javax.crypto.SecretKeyFactory} for use with DESEDE keys.  This
     29  * class supports {@link SecretKeySpec} and {@link DESedeKeySpec} for key specs.
     30  *
     31  * @hide
     32  */
     33 @Internal
     34 public class DESEDESecretKeyFactory extends SecretKeyFactorySpi {
     35     @Override
     36     protected SecretKey engineGenerateSecret(KeySpec keySpec) throws InvalidKeySpecException {
     37         if (keySpec == null) {
     38             throw new InvalidKeySpecException("Null KeySpec");
     39         }
     40         if (keySpec instanceof SecretKeySpec) {
     41             SecretKeySpec key = (SecretKeySpec) keySpec;
     42             try {
     43                 if (!DESedeKeySpec.isParityAdjusted(key.getEncoded(), 0)) {
     44                     throw new InvalidKeySpecException(
     45                             "SecretKeySpec is not a parity-adjusted DESEDE key");
     46                 }
     47             } catch (InvalidKeyException e) {
     48                 throw new InvalidKeySpecException(e);
     49             }
     50             return key;
     51         } else if (keySpec instanceof DESedeKeySpec) {
     52             DESedeKeySpec desKeySpec = (DESedeKeySpec) keySpec;
     53             return new SecretKeySpec(desKeySpec.getKey(), "DESEDE");
     54         } else {
     55             throw new InvalidKeySpecException(
     56                     "Unsupported KeySpec class: " + keySpec.getClass().getName());
     57         }
     58     }
     59 
     60     @Override
     61     protected KeySpec engineGetKeySpec(SecretKey secretKey,
     62             @SuppressWarnings("rawtypes") Class aClass) throws InvalidKeySpecException {
     63         if (secretKey == null) {
     64             throw new InvalidKeySpecException("Null SecretKey");
     65         }
     66         if (aClass == SecretKeySpec.class) {
     67             try {
     68                 if (!DESedeKeySpec.isParityAdjusted(secretKey.getEncoded(), 0)) {
     69                     throw new InvalidKeySpecException("SecretKey is not a parity-adjusted DESEDE key");
     70                 }
     71             } catch (InvalidKeyException e) {
     72                 throw new InvalidKeySpecException(e);
     73             }
     74             if (secretKey instanceof SecretKeySpec) {
     75                 return (KeySpec) secretKey;
     76             } else {
     77                 return new SecretKeySpec(secretKey.getEncoded(), "DESEDE");
     78             }
     79         } else if (aClass == DESedeKeySpec.class) {
     80             try {
     81                 return new DESedeKeySpec(secretKey.getEncoded());
     82             } catch (InvalidKeyException e) {
     83                 throw new InvalidKeySpecException(e);
     84             }
     85         } else {
     86             throw new InvalidKeySpecException("Unsupported KeySpec class: " + aClass);
     87         }
     88     }
     89 
     90     @Override
     91     protected SecretKey engineTranslateKey(SecretKey secretKey) throws InvalidKeyException {
     92         if (secretKey == null) {
     93             throw new InvalidKeyException("Null SecretKey");
     94         }
     95         return new SecretKeySpec(secretKey.getEncoded(), secretKey.getAlgorithm());
     96     }
     97 }
     98