MyExemptionMechanismSpi.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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 */
39
40public class MyExemptionMechanismSpi  extends ExemptionMechanismSpi {
41
42    private static final int byteArrayLength = 5;
43
44    public static final int getLength() {
45        return byteArrayLength;
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        public tmpKey(String alg, byte[] enc) {
104            this.alg = alg;
105            this.enc = enc;
106        }
107        public String getFormat() {
108            return "tmpKey";
109        }
110        public String getAlgorithm() {
111            return alg;
112        }
113        public byte[] getEncoded() {
114            return enc;
115        }
116    }
117    @SuppressWarnings("serial")
118    public class tmp1Key implements Key {
119        private byte[] enc;
120        public tmp1Key(String alg, byte[] enc) {
121            this.enc = enc;
122        }
123        public String getAlgorithm() {
124            return "tmp1Key";
125        }
126        public String getFormat() {
127            return "tmp1Key";
128        }
129        public byte[] getEncoded() {
130            return enc;
131        }
132    }
133}
134