/
SignatureTrustValidator.java
128 lines (115 loc) · 4.93 KB
/
SignatureTrustValidator.java
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
/**
* 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 org.apache.wss4j.dom.validate;
import java.security.PublicKey;
import java.security.cert.X509Certificate;
import java.util.Collection;
import java.util.regex.Pattern;
import org.apache.wss4j.common.crypto.Crypto;
import org.apache.wss4j.common.ext.WSSecurityException;
import org.apache.wss4j.dom.handler.RequestData;
/**
* This class verifies trust in a credential used to verify a signature, which is extracted
* from the Credential passed to the validate method.
*/
public class SignatureTrustValidator implements Validator {
private static final org.slf4j.Logger LOG =
org.slf4j.LoggerFactory.getLogger(SignatureTrustValidator.class);
/**
* Validate the credential argument. It must contain a non-null X509Certificate chain
* or a PublicKey. A Crypto implementation is also required to be set.
*
* This implementation first attempts to verify trust on the certificate (chain). If
* this is not successful, then it will attempt to verify trust on the Public Key.
*
* @param credential the Credential to be validated
* @param data the RequestData associated with the request
* @throws WSSecurityException on a failed validation
*/
public Credential validate(Credential credential, RequestData data) throws WSSecurityException {
if (credential == null) {
throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, "noCredential");
}
X509Certificate[] certs = credential.getCertificates();
PublicKey publicKey = credential.getPublicKey();
Crypto crypto = getCrypto(data);
if (certs != null && certs.length > 0) {
validateCertificates(certs);
if (crypto == null) {
throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, "noSigCryptoFile");
}
verifyTrustInCerts(certs, crypto, data, data.isRevocationEnabled());
return credential;
}
if (publicKey != null) {
if (crypto == null) {
throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, "noSigCryptoFile");
}
validatePublicKey(publicKey, crypto);
return credential;
}
throw new WSSecurityException(WSSecurityException.ErrorCode.FAILED_AUTHENTICATION);
}
protected Crypto getCrypto(RequestData data) {
return data.getSigVerCrypto();
}
/**
* Validate the certificates by checking the validity of each cert
* @throws WSSecurityException
*/
protected void validateCertificates(X509Certificate[] certificates)
throws WSSecurityException {
// Nothing
}
/**
* Evaluate whether the given certificate chain should be trusted.
*
* @param certificates the certificate chain that should be validated against the keystore
* @param crypto A Crypto instance
* @param data A RequestData instance
* @param enableRevocation Whether revocation is enabled or not
* @throws WSSecurityException if the certificate chain is not trusted
*/
protected void verifyTrustInCerts(
X509Certificate[] certificates,
Crypto crypto,
RequestData data,
boolean enableRevocation
) throws WSSecurityException {
//
// Use the validation method from the crypto to check whether the subjects'
// certificate was really signed by the issuer stated in the certificate
//
Collection<Pattern> subjectCertConstraints = data.getSubjectCertConstraints();
Collection<Pattern> issuerCertConstraints = data.getIssuerDNPatterns();
crypto.verifyTrust(certificates, enableRevocation, subjectCertConstraints, issuerCertConstraints);
String subjectString = certificates[0].getSubjectX500Principal().getName();
LOG.debug(
"Certificate path has been verified for certificate with subject {}", subjectString
);
}
/**
* Validate a public key
* @throws WSSecurityException
*/
protected void validatePublicKey(PublicKey publicKey, Crypto crypto)
throws WSSecurityException {
crypto.verifyTrust(publicKey);
}
}