summaryrefslogtreecommitdiffstats
path: root/luni/src/main/java/java/security/cert/CertStore.java
blob: 2e28828c3a3f75e25f7d50658f7487bae9a019c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package java.security.cert;

import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Provider;
import java.security.Security;
import java.util.Collection;
import org.apache.harmony.security.fortress.Engine;

/**
 * This class provides the functionality to retrieve {@code Certificate}s and
 * {@code CRL}s from a read-only repository. This repository may be very large
 * and may store trusted as well as untrusted certificates.
 */
public class CertStore {

    // Store spi implementation service name
    private static final String SERVICE = "CertStore";

    // Used to access common engine functionality
    private static final Engine ENGINE = new Engine(SERVICE);

    // Store default property name
    private static final String PROPERTYNAME = "certstore.type";

    // Default value of CertStore type. It returns if certpathbuild.type
    // property is not defined in java.security file
    private static final String DEFAULTPROPERTY = "LDAP";

    // Store used provider
    private final Provider provider;

    // Store CertStoreSpi implementation
    private final CertStoreSpi spiImpl;

    // Store used type
    private final String type;

    // Store used parameters
    private final CertStoreParameters certStoreParams;

    /**
     * Creates a new {@code CertStore} instance.
     *
     * @param storeSpi
     *            the implementation delegate.
     * @param provider
     *            the security provider.
     * @param type
     *            the certificate store type.
     * @param params
     *            the certificate store parameters (may be {@code null}.
     */
    protected CertStore(CertStoreSpi storeSpi, Provider provider, String type,
            CertStoreParameters params) {
        this.provider = provider;
        this.type = type;
        this.spiImpl = storeSpi;
        this.certStoreParams = params;
    }

    /**
     * Creates a new {@code CertStore} instance with the specified type and
     * initialized with the specified parameters.
     *
     * @param type
     *            the certificate store type.
     * @param params
     *            the certificate store parameters (may be {@code null}).
     * @return the new certificate store instance.
     * @throws NoSuchAlgorithmException
     *             if no provider can provide the specified certificate store
     *             type.
     * @throws InvalidAlgorithmParameterException
     *             if the specified parameters cannot be used to initialize this
     *             certificate store instance.
     * @throws NullPointerException if {@code type == null}
     */
    public static CertStore getInstance(String type, CertStoreParameters params)
            throws InvalidAlgorithmParameterException, NoSuchAlgorithmException {
        if (type == null) {
            throw new NullPointerException("type == null");
        }
        try {
            Engine.SpiAndProvider sap = ENGINE.getInstance(type, params);
            return new CertStore((CertStoreSpi) sap.spi, sap.provider, type, params);
        } catch (NoSuchAlgorithmException e) {
            Throwable th = e.getCause();
            if (th == null) {
                throw e;
            } else {
                throw new InvalidAlgorithmParameterException(e.getMessage(), th);
            }
        }
    }

    /**
     * Creates a new {@code CertStore} instance from the specified provider with
     * the specified type and initialized with the specified parameters.
     *
     * @param type
     *            the certificate store type.
     * @param params
     *            the certificate store parameters (may be {@code null}).
     * @param provider
     *            the name of the provider.
     * @return the new certificate store instance.
     * @throws NoSuchAlgorithmException
     *             if the specified provider cannot provide the requested
     *             certificate store type.
     * @throws NoSuchProviderException
     *             if no provider with the specified name can be found.
     * @throws InvalidAlgorithmParameterException
     *             if the specified parameters cannot be used to initialize this
     *             certificate store instance.
     * @throws IllegalArgumentException if {@code provider == null || provider.isEmpty()}
     * @throws NullPointerException
     *             if {@code type} is {@code null}.
     */
    public static CertStore getInstance(String type,
            CertStoreParameters params, String provider)
            throws InvalidAlgorithmParameterException,
            NoSuchAlgorithmException, NoSuchProviderException {
        if (provider == null || provider.isEmpty()) {
            throw new IllegalArgumentException("provider == null || provider.isEmpty()");
        }
        Provider impProvider = Security.getProvider(provider);
        if (impProvider == null) {
            throw new NoSuchProviderException(provider);
        }
        return getInstance(type, params, impProvider);
    }

    /**
     * Creates a new {@code CertStore} instance from the specified provider with
     * the specified type and initialized with the specified parameters.
     * @param type
     *            the certificate store type.
     * @param params
     *            the certificate store parameters (may be {@code null}).
     * @param provider
     *            the name of the provider.
     * @return the new certificate store instance.
     * @throws NoSuchAlgorithmException
     *             if the specified provider cannot provide the requested
     *             certificate store type.
     * @throws InvalidAlgorithmParameterException
     *             if the specified parameters cannot be used to initialize this
     *             certificate store instance.
     * @throws IllegalArgumentException if {@code provider == null}
     * @throws NullPointerException if {@code type == null}
     */
    public static CertStore getInstance(String type,
            CertStoreParameters params, Provider provider)
            throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
        if (provider == null) {
            throw new IllegalArgumentException("provider == null");
        }
        if (type == null) {
            throw new NullPointerException("type == null");
        }
        try {
            Object spi = ENGINE.getInstance(type, provider, params);
            return new CertStore((CertStoreSpi) spi, provider, type, params);
        } catch (NoSuchAlgorithmException e) {
            Throwable th = e.getCause();
            if (th == null) {
                throw e;
            } else {
                throw new InvalidAlgorithmParameterException(e.getMessage(), th);
            }
        }
    }

    /**
     * Returns the certificate store type.
     *
     * @return the certificate store type.
     */
    public final String getType() {
        return type;
    }

    /**
     * Returns the security provider.
     *
     * @return the security provider.
     */
    public final Provider getProvider() {
        return provider;
    }

    /**
     * Returns a copy of the certificate store parameters that were used to
     * initialize this instance.
     *
     * @return a copy of the certificate store parameters or {@code null} if
     *         none were specified.
     */
    public final CertStoreParameters getCertStoreParameters() {
        if (certStoreParams == null) {
            return null;
        } else {
            return (CertStoreParameters) certStoreParams.clone();
        }
    }

    /**
     * Returns the list of {@code Certificate}s for the specified {@code
     * CertSelector} from this certificate store.
     *
     * @param selector
     *            the selector containing the criteria to search for
     *            certificates in this certificate store.
     * @return the list of {@code Certificate}s that match the criteria of the
     *         specified selector.
     * @throws CertStoreException
     *             if error(s) occur.
     */
    public final Collection<? extends Certificate> getCertificates(CertSelector selector)
            throws CertStoreException {
        return spiImpl.engineGetCertificates(selector);
    }

    /**
     * Returns the list of {@code CRL}s for the specified {@code CRLSelector}
     * from this certificate store.
     *
     * @param selector
     *            the selector containing the criteria to search for certificate
     *            revocation lists in this store.
     * @return the list of {@code CRL}s that match the criteria of the specified
     *         selector
     * @throws CertStoreException
     *             if error(s) occur.
     */
    public final Collection<? extends CRL> getCRLs(CRLSelector selector)
            throws CertStoreException {
        return spiImpl.engineGetCRLs(selector);
    }

    /**
     * Returns the default {@code CertStore} type from the <i>Security
     * Properties</i>.
     *
     * @return the default {@code CertStore} type from the <i>Security
     *         Properties</i>, or the string {@code "LDAP"} if it cannot be
     *         determined.
     */
    public static final String getDefaultType() {
        String defaultType = Security.getProperty(PROPERTYNAME);
        return (defaultType == null ? DEFAULTPROPERTY : defaultType);
    }
}