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
18package java.security.cert;
19
20import java.security.GeneralSecurityException;
21
22/**
23 * The exception that is thrown when a certification path (or certificate chain)
24 * cannot be validated.
25 * <p>
26 * A {@code CertPathValidatorException} may optionally include the certification
27 * path instance that failed the validation and the index of the failed
28 * certificate.
29 */
30public class CertPathValidatorException extends GeneralSecurityException {
31
32    private static final long serialVersionUID = -3083180014971893139L;
33
34    /**
35     * the certification path.
36     */
37    private CertPath certPath;
38
39    /**
40     * the index of the certificate.
41     */
42    private int index = -1;
43
44    /**
45     * Creates a new {@code CertPathValidatorException} with the specified
46     * message , cause, certification path and certificate index in the
47     * certification path.
48     *
49     * @param msg
50     *            the detail message for this exception.
51     * @param cause
52     *            the cause.
53     * @param certPath
54     *            the certification path that failed the validation.
55     * @param index
56     *            the index of the failed certificate.
57     * @throws IllegalArgumentException
58     *             if {@code certPath} is {@code null} and index is not {@code
59     *             -1}.
60     * @throws IndexOutOfBoundsException
61     *             if {@code certPath} is not {@code null} and index is not
62     *             referencing an certificate in the certification path.
63     */
64    public CertPathValidatorException(String msg, Throwable cause,
65            CertPath certPath, int index) {
66        super(msg, cause);
67        // check certPath and index parameters
68        if ((certPath == null) && (index != -1)) {
69            throw new IllegalArgumentException("Index should be -1 when CertPath is null");
70        }
71        if ((certPath != null) && ((index < -1) || (index >= certPath.getCertificates().size()))) {
72            throw new IndexOutOfBoundsException();
73        }
74        this.certPath = certPath;
75        this.index = index;
76    }
77
78    /**
79     * Creates a new {@code CertPathValidatorException} with the specified
80     * message and cause.
81     *
82     * @param msg
83     *            the detail message for this exception.
84     * @param cause
85     *            the cause why the path could not be validated.
86     */
87    public CertPathValidatorException(String msg, Throwable cause) {
88        super(msg, cause);
89    }
90
91    /**
92     * Creates a new {@code CertPathValidatorException} with the specified
93     * cause.
94     *
95     * @param cause
96     *            the cause why the path could not be validated.
97     */
98    public CertPathValidatorException(Throwable cause) {
99        super(cause);
100    }
101
102    /**
103     * Creates a new {@code CertPathValidatorException} with the specified
104     * message.
105     *
106     * @param msg
107     *            the detail message for this exception.
108     */
109    public CertPathValidatorException(String msg) {
110        super(msg);
111    }
112
113    /**
114     * Creates a new {@code CertPathValidatorException}.
115     */
116    public CertPathValidatorException() {
117    }
118
119    /**
120     * Returns the certification path that failed validation.
121     *
122     * @return the certification path that failed validation, or {@code null} if
123     *         none was specified.
124     */
125    public CertPath getCertPath() {
126        return certPath;
127    }
128
129    /**
130     * Returns the index of the failed certificate in the certification path.
131     *
132     * @return the index of the failed certificate in the certification path, or
133     *         {@code -1} if none was specified.
134     */
135    public int getIndex() {
136        return index;
137    }
138}
139