1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18/** 19* @author Vera Y. Petrashkova 20* @version $Revision$ 21*/ 22 23package org.apache.harmony.security.tests.support; 24 25import java.security.InvalidAlgorithmParameterException; 26import java.security.InvalidParameterException; 27import java.security.KeyPair; 28import java.security.KeyPairGenerator; 29import java.security.PrivateKey; 30import java.security.PublicKey; 31import java.security.SecureRandom; 32import java.security.spec.AlgorithmParameterSpec; 33 34/** 35 * Additional class extends KeyPairGenerator 36 * 37 */ 38 39public class MyKeyPairGenerator1 extends KeyPairGenerator { 40 public int keySize; 41 42 public SecureRandom secureRandom; 43 44 public AlgorithmParameterSpec paramSpec; 45 46 public MyKeyPairGenerator1() { 47 super("MyKeyPairGenerator1"); 48 } 49 50 public MyKeyPairGenerator1(String pp) { 51 super(pp); 52 } 53 54 public String getAlgorithm() { 55 return "MyKeyPairGenerator1"; 56 } 57 58 public static final String getResAlgorithm() { 59 return "MyKeyPairGenerator1"; 60 } 61 62 public void initialize(int keysize, SecureRandom random) { 63 if ((keysize < 0) || ((keysize % 100) != 0)) { 64 throw new InvalidParameterException("Incorrect keysize parameter"); 65 } 66 if (random == null) { 67 throw new InvalidParameterException("Incorrect random"); 68 } 69 keySize = keysize; 70 secureRandom = random; 71 } 72 73 public KeyPair generateKeyPair() { 74 try { 75 return new KeyPair(new PubKey(), new PrivKey()); 76 } catch (Exception e) { 77 e.printStackTrace(); 78 return null; 79 } 80 } 81 82 public void initialize(AlgorithmParameterSpec param, SecureRandom random) 83 throws InvalidAlgorithmParameterException { 84 if (random == null) { 85 throw new InvalidParameterException("Incorrect random"); 86 } 87 if (param == null) { 88 throw new InvalidAlgorithmParameterException("Incorrect param"); 89 } 90 paramSpec = param; 91 secureRandom = random; 92 } 93 94 public class PubKey implements PublicKey { 95 private String algorithm; 96 97 private String format; 98 99 private byte[] encoded; 100 101 public PubKey() { 102 this.algorithm = "MyKeyPairGenerator1"; 103 this.format = "test1"; 104 this.encoded = new byte[10]; 105 } 106 107 public String getAlgorithm() { 108 return algorithm; 109 } 110 111 public String getFormat() { 112 return format; 113 } 114 115 public byte[] getEncoded() { 116 return encoded; 117 } 118 } 119 120 public class PrivKey implements PrivateKey { 121 private String algorithm; 122 123 private String format; 124 125 private byte[] encoded; 126 127 public PrivKey() { 128 this.algorithm = "MyKeyPairGenerator1"; 129 this.format = "test1"; 130 this.encoded = new byte[10]; 131 } 132 133 public String getAlgorithm() { 134 return algorithm; 135 } 136 137 public String getFormat() { 138 return format; 139 } 140 141 public byte[] getEncoded() { 142 return encoded; 143 } 144 } 145 146}