Home | History | Annotate | Download | only in security
      1 /*
      2  * Copyright (C) 2009 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 tests.security;
     18 
     19 import java.security.AlgorithmParameters;
     20 import java.security.InvalidAlgorithmParameterException;
     21 import java.security.InvalidKeyException;
     22 import java.security.KeyPair;
     23 import java.security.KeyPairGenerator;
     24 import java.security.NoSuchAlgorithmException;
     25 import java.security.Signature;
     26 import java.security.SignatureException;
     27 import java.security.spec.AlgorithmParameterSpec;
     28 import java.security.spec.InvalidParameterSpecException;
     29 import junit.framework.Assert;
     30 
     31 public class AlgorithmParameterSignatureHelper<T extends AlgorithmParameterSpec>
     32         extends TestHelper<AlgorithmParameters> {
     33 
     34     private final String algorithmName;
     35     private final String plainData = "some data do sign and verify";
     36     private final Class<T> parameterSpecClass;
     37 
     38     public AlgorithmParameterSignatureHelper(String algorithmName, Class<T> parameterSpecCla1ss) {
     39         this.algorithmName = algorithmName;
     40         this.parameterSpecClass = parameterSpecCla1ss;
     41     }
     42 
     43     @Override
     44     public void test(AlgorithmParameters parameters) {
     45 
     46         Signature signature = null;
     47         try {
     48             signature = Signature.getInstance(algorithmName);
     49         } catch (NoSuchAlgorithmException e) {
     50             Assert.fail(e.getMessage());
     51         }
     52 
     53 
     54         T parameterSpec = null;
     55         try {
     56             parameterSpec = parameters.getParameterSpec(parameterSpecClass);
     57         } catch (InvalidParameterSpecException e) {
     58             Assert.fail(e.getMessage());
     59         }
     60 
     61         KeyPairGenerator generator = null;
     62         try {
     63             generator = KeyPairGenerator.getInstance(algorithmName);
     64         } catch (NoSuchAlgorithmException e) {
     65             Assert.fail(e.getMessage());
     66         }
     67 
     68         try {
     69             generator.initialize(parameterSpec);
     70         } catch (InvalidAlgorithmParameterException e) {
     71             Assert.fail(e.getMessage());
     72         }
     73 
     74         KeyPair keyPair = generator.genKeyPair();
     75 
     76         try {
     77             signature.initSign(keyPair.getPrivate());
     78         } catch (InvalidKeyException e) {
     79             Assert.fail(e.getMessage());
     80         }
     81 
     82         try {
     83             signature.update(plainData.getBytes());
     84         } catch (SignatureException e) {
     85             Assert.fail(e.getMessage());
     86         }
     87 
     88         byte[] signed = null;
     89         try {
     90             signed = signature.sign();
     91         } catch (SignatureException e) {
     92             Assert.fail(e.getMessage());
     93         }
     94 
     95         try {
     96             signature.initVerify(keyPair.getPublic());
     97         } catch (InvalidKeyException e) {
     98             Assert.fail(e.getMessage());
     99         }
    100 
    101         try {
    102             signature.update(plainData.getBytes());
    103         } catch (SignatureException e) {
    104             Assert.fail(e.getMessage());
    105         }
    106 
    107         try {
    108             Assert.assertTrue("signature could not be verified", signature
    109                     .verify(signed));
    110         } catch (SignatureException e) {
    111             Assert.fail(e.getMessage());
    112         }
    113     }
    114 }
    115