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 package tests.targets.security;
     17 
     18 import java.security.InvalidKeyException;
     19 import java.security.InvalidParameterException;
     20 import java.security.NoSuchAlgorithmException;
     21 import java.security.PrivateKey;
     22 import java.security.Provider;
     23 import java.security.PublicKey;
     24 import java.security.Security;
     25 import java.security.Signature;
     26 import java.security.SignatureException;
     27 import junit.framework.TestCase;
     28 
     29 public class SignatureTestMD2withRSA extends TestCase {
     30 
     31     public void testSignature() {
     32 
     33         // MD2 must not be part of android.
     34 
     35         try {
     36             Signature signature = Signature.getInstance("MD2withRSA");
     37             fail("MD2withRSA for signature verification must not be supported");
     38         } catch (NoSuchAlgorithmException e) {
     39             // expected
     40         }
     41 
     42         try {
     43             Signature signature = Signature.getInstance("MD2WithRSA");
     44             fail("MD2withRSA for signature verification must not be supported");
     45         } catch (NoSuchAlgorithmException e) {
     46             // expected
     47         }
     48 
     49         try {
     50             Signature signature = Signature.getInstance("MD2WITHRSA");
     51             fail("MD2withRSA for signature verification must not be supported");
     52         } catch (NoSuchAlgorithmException e) {
     53             // expected
     54         }
     55 
     56         try {
     57             Signature signature = Signature.getInstance("MD2withRSAEncryption");
     58             fail("MD2withRSA for signature verification must not be supported");
     59         } catch (NoSuchAlgorithmException e) {
     60             // expected
     61         }
     62 
     63         try {
     64             Signature signature = Signature.getInstance("MD2WithRSAEncryption");
     65             fail("MD2withRSA for signature verification must not be supported");
     66         } catch (NoSuchAlgorithmException e) {
     67             // expected
     68         }
     69 
     70         try {
     71             Signature signature = Signature.getInstance("MD2WITHRSAENCRYPTION");
     72             fail("MD2withRSA for signature verification must not be supported");
     73         } catch (NoSuchAlgorithmException e) {
     74             // expected
     75         }
     76 
     77         try {
     78             Signature signature = Signature.getInstance("MD2/RSA");
     79             fail("MD2withRSA for signature verification must not be supported");
     80         } catch (NoSuchAlgorithmException e) {
     81             // expected
     82         }
     83 
     84         try {
     85             Signature signature = Signature.getInstance("1.2.840.113549.1.1.2");
     86             fail("MD2withRSA for signature verification must not be supported");
     87         } catch (NoSuchAlgorithmException e) {
     88             // expected
     89         }
     90     }
     91 
     92     public void testSignature2() throws Exception{
     93 
     94         Provider provider = new MyProvider();
     95         Security.addProvider(provider);
     96 
     97         Signature signature = Signature.getInstance("MD2withRSA");
     98         signature = Signature.getInstance("MD2WithRSA");
     99         signature = Signature.getInstance("MD2WITHRSA");
    100         signature = Signature.getInstance("MD2withRSAEncryption");
    101         signature = Signature.getInstance("MD2WithRSAEncryption");
    102         signature = Signature.getInstance("MD2WITHRSAENCRYPTION");
    103         signature = Signature.getInstance("MD2/RSA");
    104         signature = Signature.getInstance("1.2.840.113549.1.1.2");
    105 
    106         Security.removeProvider(provider.getName());
    107     }
    108 
    109     public final class MyProvider extends Provider {
    110         public MyProvider()
    111         {
    112             super("SignatureMD2WithRSATest", 1.00, "TestProvider");
    113             put("Signature.MD2WithRSAEncryption",
    114                     "tests.targets.security.SignatureTestMD2withRSA$MD2withRSA");
    115             put("Alg.Alias.Signature.MD2WITHRSAENCRYPTION",
    116                     "MD2WithRSAEncryption");
    117             put("Alg.Alias.Signature.MD2WithRSA", "MD2WithRSAEncryption");
    118             put("Alg.Alias.Signature.MD2withRSA", "MD2WithRSAEncryption");
    119             put("Alg.Alias.Signature.MD2/RSA", "MD2WithRSAEncryption");
    120             put("Alg.Alias.Signature.1.2.840.113549.1.1.2",
    121                     "MD2WithRSAEncryption");
    122         }
    123     }
    124 
    125     public static class MD2withRSA extends Signature {
    126 
    127         public MD2withRSA() {
    128             super("MD2WithRSA");
    129         }
    130 
    131         public MD2withRSA(String algorithm) {
    132             super(algorithm);
    133         }
    134 
    135         @Override
    136         protected Object engineGetParameter(String param)
    137                 throws InvalidParameterException {
    138             return null;
    139         }
    140 
    141         @Override
    142         protected void engineInitSign(PrivateKey privateKey)
    143                 throws InvalidKeyException {
    144         }
    145 
    146         @Override
    147         protected void engineInitVerify(PublicKey publicKey)
    148                 throws InvalidKeyException {
    149         }
    150 
    151         @Override
    152         protected void engineSetParameter(String param, Object value)
    153                 throws InvalidParameterException {
    154         }
    155 
    156         @Override
    157         protected byte[] engineSign() throws SignatureException {
    158             return null;
    159         }
    160 
    161         @Override
    162         protected void engineUpdate(byte b) throws SignatureException {
    163         }
    164 
    165         @Override
    166         protected void engineUpdate(byte[] b, int off, int len)
    167                 throws SignatureException {
    168         }
    169 
    170         @Override
    171         protected boolean engineVerify(byte[] sigBytes)
    172                 throws SignatureException {
    173             return false;
    174         }
    175     }
    176 }
    177