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