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