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 Vladimir N. Molotkov
20* @version $Revision$
21*/
22
23package org.apache.harmony.security.tests.support.cert;
24
25import java.io.ObjectStreamException;
26import java.security.InvalidKeyException;
27import java.security.NoSuchAlgorithmException;
28import java.security.NoSuchProviderException;
29import java.security.PublicKey;
30import java.security.SignatureException;
31import java.security.cert.Certificate;
32import java.security.cert.CertificateEncodingException;
33import java.security.cert.CertificateException;
34import java.security.cert.X509Extension;
35import java.util.Set;
36
37/**
38 * Stub class for <code>java.security.cert.Certificate</code> tests
39 */
40public class MyCertificate extends Certificate implements X509Extension {
41
42    private static final long serialVersionUID = -1835303280727190066L;
43    // MyCertificate encoding
44    private final byte[] encoding;
45
46    public CertificateRep rep;
47
48    /**
49     * Constructs new object of class <code>MyCertificate</code>
50     *
51     * @param type
52     * @param encoding
53     */
54    public MyCertificate(String type, byte[] encoding) {
55        super(type);
56        // don't copy to allow null parameter in test
57        this.encoding = encoding;
58    }
59
60    /**
61     * Returns <code>MyCertificate</code> encoding
62     */
63    public byte[] getEncoded() throws CertificateEncodingException {
64        // do copy to force NPE in test
65        return encoding.clone();
66    }
67
68    /**
69     * Does nothing
70     */
71    public void verify(PublicKey key) throws CertificateException,
72            NoSuchAlgorithmException, InvalidKeyException,
73            NoSuchProviderException, SignatureException {
74    }
75
76    /**
77     * Does nothing
78     */
79    public void verify(PublicKey key, String sigProvider)
80            throws CertificateException, NoSuchAlgorithmException,
81            InvalidKeyException, NoSuchProviderException, SignatureException {
82    }
83
84    /**
85     * Returns formatted <code>String</code>
86     * describing <code>MyCertificate</code> object
87     */
88    public String toString() {
89        return "[My test Certificate, type: " + getType() + "]";
90    }
91
92    public Object writeReplace() throws ObjectStreamException {
93        return super.writeReplace();
94    }
95
96    /**
97     * Returns public key (stub) from <code>MyCertificate</code> object
98     */
99    public PublicKey getPublicKey() {
100        return new PublicKey() {
101           private static final long serialVersionUID = 788077928335589816L;
102            public String getAlgorithm() {
103                return "TEST";
104            }
105            public byte[] getEncoded() {
106                return new byte[] {(byte)1, (byte)2, (byte)3};
107            }
108            public String getFormat() {
109                return "TEST_FORMAT";
110            }
111        };
112    }
113
114    public Certificate.CertificateRep getCertificateRep()
115            throws ObjectStreamException {
116        Object obj = super.writeReplace();
117        return (MyCertificateRep) obj;
118    }
119
120    public class MyCertificateRep extends Certificate.CertificateRep {
121
122        private static final long serialVersionUID = -3474284043994635553L;
123
124        private String type;
125        private byte[] data;
126
127        public MyCertificateRep(String type, byte[] data) {
128            super(type, data);
129            this.data = data;
130            this.type = type;
131        }
132
133        public Object readResolve() throws ObjectStreamException {
134            return super.readResolve();
135        }
136
137        public String getType() {
138            return type;
139        }
140
141        public byte[] getData() {
142            return data;
143        }
144    }
145    public Set<String> getNonCriticalExtensionOIDs() {
146        return null;
147    }
148
149    public Set<String> getCriticalExtensionOIDs() {
150        return null;
151    }
152
153    public byte[] getExtensionValue(String oid) {
154        return null;
155    }
156
157    public boolean hasUnsupportedCriticalExtension() {
158        return false;
159    }
160}
161