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.crypto.tests.support;
24
25import java.security.AlgorithmParameters;
26import java.security.InvalidAlgorithmParameterException;
27import java.security.InvalidKeyException;
28import java.security.Key;
29import java.security.spec.AlgorithmParameterSpec;
30
31import javax.crypto.ExemptionMechanismException;
32import javax.crypto.ExemptionMechanismSpi;
33import javax.crypto.ShortBufferException;
34
35/**
36 * Additional class for verification ExemptionMechanismSpi
37 * and ExemptionMechanism classes
38 *
39 */
40
41public class MyExemptionMechanismSpi  extends ExemptionMechanismSpi {
42
43    private static final int byteArrayLength = 5;
44
45    public static final int getLength() {
46        return byteArrayLength;
47    }
48    @Override
49    protected byte[] engineGenExemptionBlob()
50            throws ExemptionMechanismException {
51        return new byte[byteArrayLength];
52    }
53
54    @Override
55    protected int engineGenExemptionBlob(byte[] output, int outputOffset)
56            throws ShortBufferException, ExemptionMechanismException {
57        return byteArrayLength;
58    }
59
60    @Override
61    protected int engineGetOutputSize(int inputLen) {
62        return 10;
63    }
64
65    @Override
66    protected void engineInit(Key key) throws InvalidKeyException,
67            ExemptionMechanismException {
68        if (key == null) {
69            throw new InvalidKeyException("key is null");
70        }
71        if (!(key instanceof tmpKey)) {
72            throw new ExemptionMechanismException("Incorrect key");
73        }
74    }
75
76    @Override
77    protected void engineInit(Key key, AlgorithmParameters params)
78            throws InvalidKeyException, InvalidAlgorithmParameterException,
79            ExemptionMechanismException {
80        if (key == null) {
81            throw new InvalidKeyException("key is null");
82        }
83        if (!(key instanceof tmpKey)) {
84            throw new ExemptionMechanismException("Incorrect key");
85        }
86        if (params == null) {
87            throw new InvalidAlgorithmParameterException("params is null");
88        }
89    }
90
91    @Override
92    protected void engineInit(Key key, AlgorithmParameterSpec params)
93            throws InvalidKeyException, InvalidAlgorithmParameterException,
94            ExemptionMechanismException {
95        if (key == null) {
96            throw new InvalidKeyException("key is null");
97        }
98        if (!(key instanceof tmpKey)) {
99            throw new ExemptionMechanismException("Incorrect key");
100        }
101        if (params == null) {
102            throw new InvalidAlgorithmParameterException("params is null");
103        }
104    }
105
106    @SuppressWarnings("serial")
107    public class tmpKey implements Key {
108        private String alg;
109        private byte[] enc;
110        public tmpKey(String alg, byte[] enc) {
111            this.alg = alg;
112            this.enc = enc;
113        }
114        public String getFormat() {
115            return "tmpKey";
116        }
117        public String getAlgorithm() {
118            return alg;
119        }
120        public byte[] getEncoded() {
121            return enc;
122        }
123    }
124    @SuppressWarnings("serial")
125    public class tmp1Key implements Key {
126        private byte[] enc;
127        public tmp1Key(String alg, byte[] enc) {
128            this.enc = enc;
129        }
130        public String getAlgorithm() {
131            return "tmp1Key";
132        }
133        public String getFormat() {
134            return "tmp1Key";
135        }
136        public byte[] getEncoded() {
137            return enc;
138        }
139    }
140}
141