01: /*
02: * DigestCertSelector.java
03: *
04: * Created on February 26, 2007, 6:20 PM
05: *
06: * To change this template, choose Tools | Template Manager
07: * and open the template in the editor.
08: */
09:
10: package com.sun.xml.wss.impl.misc;
11:
12: import java.security.cert.CertSelector;
13: import java.security.cert.Certificate;
14:
15: import java.util.logging.Level;
16: import java.util.logging.Logger;
17: import com.sun.xml.wss.logging.LogDomainConstants;
18: import java.security.cert.X509Certificate;
19: import java.util.Arrays;
20:
21: import com.sun.xml.wss.XWSSecurityException;
22:
23: import java.security.MessageDigest;
24: import java.security.NoSuchAlgorithmException;
25: import java.security.PrivilegedAction;
26: import java.security.cert.CertificateEncodingException;
27:
28: /**
29: *
30: * @author Kumar Jayanti
31: */
32: public class DigestCertSelector implements CertSelector {
33:
34: private final byte[] keyId;
35: private final String algorithm;
36: /** logger */
37: protected static final Logger log = Logger.getLogger(
38: LogDomainConstants.WSS_API_DOMAIN,
39: LogDomainConstants.WSS_API_DOMAIN_BUNDLE);
40:
41: /** Creates a new instance of KeyIdentifierCertSelector */
42: public DigestCertSelector(byte[] keyIdValue, String algo) {
43: this .keyId = keyIdValue;
44: this .algorithm = algo;
45: }
46:
47: public boolean match(Certificate cert) {
48: if (cert instanceof X509Certificate) {
49: byte[] thumbPrintIdentifier = null;
50:
51: try {
52: thumbPrintIdentifier = MessageDigest.getInstance(
53: this .algorithm).digest(cert.getEncoded());
54: } catch (NoSuchAlgorithmException ex) {
55: log.log(Level.SEVERE, "WSS0708.no.digest.algorithm");
56: throw new RuntimeException(
57: "Digest algorithm SHA-1 not found");
58: } catch (CertificateEncodingException ex) {
59: log.log(Level.SEVERE,
60: "WSS0709.error.getting.rawContent");
61: throw new RuntimeException(
62: "Error while getting certificate's raw content");
63: }
64:
65: if (Arrays.equals(thumbPrintIdentifier, keyId)) {
66: return true;
67: }
68: }
69: return false;
70: }
71:
72: public Object clone() {
73: return new DigestCertSelector(this.keyId, this.algorithm);
74: }
75: }
|