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.PublicKey;
21
22/**
23 * The implementation of the result of the PKIX certification path validation.
24 *
25 * @see CertPathValidator
26 * @see CertPathValidator#validate(CertPath, CertPathParameters)
27 */
28public class PKIXCertPathValidatorResult implements CertPathValidatorResult {
29    // A trust anchor used during validation of certification path
30    private final TrustAnchor trustAnchor;
31    // Valid policy tree resulting from PKIX
32    // certification path validation algorithm
33    private final PolicyNode policyTree;
34    // Public key of the subject (target) certificate
35    private final PublicKey subjectPublicKey;
36
37    /**
38     * Creates a new {@code PKIXCertPathValidatorResult} with the specified
39     * trust anchor, the valid policy tree and the subject public key.
40     *
41     * @param trustAnchor
42     *            the trust anchor describing the certification authority (CA)
43     *            that served as trust anchor for the certification path.
44     * @param policyTree
45     *            the valid policy tree from the validation.
46     * @param subjectPublicKey
47     *            the subject public key from the validation.
48     */
49    public PKIXCertPathValidatorResult(TrustAnchor trustAnchor,
50            PolicyNode policyTree, PublicKey subjectPublicKey) {
51        this.trustAnchor = trustAnchor;
52        this.policyTree = policyTree;
53        this.subjectPublicKey = subjectPublicKey;
54        if (this.trustAnchor == null) {
55            throw new NullPointerException("trustAnchor == null");
56        }
57        if (this.subjectPublicKey == null) {
58            throw new NullPointerException("subjectPublicKey == null");
59        }
60    }
61
62    /**
63     * Returns the valid policy tree from the validation.
64     *
65     * @return the valid policy tree from the validation.
66     */
67    public PolicyNode getPolicyTree() {
68        return policyTree;
69    }
70
71    /**
72     * Returns the subject public key from the validation.
73     *
74     * @return the subject public key from the validation.
75     */
76    public PublicKey getPublicKey() {
77        return subjectPublicKey;
78    }
79
80    /**
81     * Returns the trust anchor describing the certification authority (CA) that
82     * served as trust anchor for this certification path.
83     *
84     * @return the trust anchor.
85     */
86    public TrustAnchor getTrustAnchor() {
87        return trustAnchor;
88    }
89
90    /**
91     * Clones this {@code PKIXCertPathValidatorResult} instance.
92     *
93     * @return the cloned instance.
94     */
95    public Object clone() {
96        try {
97            return super.clone();
98        } catch (CloneNotSupportedException e) {
99            throw new AssertionError(e);
100        }
101    }
102
103    /**
104     * Returns a string representation for this {@code
105     * PKIXCertPathValidatorResult} instance.
106     *
107     * @return a string representation for this {@code
108     *         PKIXCertPathValidatorResult} instance.
109     */
110    public String toString() {
111        StringBuilder sb = new StringBuilder(super.toString());
112        sb.append(": [\n Trust Anchor: ");
113        sb.append(trustAnchor.toString());
114        sb.append("\n Policy Tree: ");
115        sb.append(policyTree == null ? "no valid policy tree\n"
116                                     : policyTree.toString());
117        sb.append("\n Subject Public Key: ");
118        sb.append(subjectPublicKey.toString());
119        sb.append("\n]");
120        return sb.toString();
121    }
122}
123