001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the License). You may not use this file except in
005: * compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * Header Notice in each file and include the License file
014: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * If applicable, add the following below the CDDL Header,
016: * with the fields enclosed by brackets [] replaced by
017: * you own identifying information:
018: * "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
021: */
022:
023: package com.sun.xml.wss.core.reference;
024:
025: import java.security.MessageDigest;
026: import java.security.NoSuchAlgorithmException;
027: import java.security.cert.CertificateEncodingException;
028: import java.security.cert.X509Certificate;
029: import java.util.logging.Level;
030:
031: import javax.xml.soap.SOAPElement;
032: import org.w3c.dom.Document;
033:
034: import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
035: import com.sun.xml.wss.impl.misc.Base64;
036: import com.sun.xml.wss.impl.MessageConstants;
037: import com.sun.xml.wss.impl.SecurityHeaderException;
038: import com.sun.xml.wss.XWSSecurityException;
039:
040: /**
041: * @author Abhijit Das
042: */
043: public class X509ThumbPrintIdentifier extends KeyIdentifier {
044:
045: /** Defaults */
046: private String encodingType = MessageConstants.BASE64_ENCODING_NS;
047:
048: private String valueType = MessageConstants.ThumbPrintIdentifier_NS;
049:
050: private X509Certificate cert = null;
051:
052: /**
053: * Creates an "empty" KeyIdentifier element with default encoding type
054: * and default value type.
055: */
056: public X509ThumbPrintIdentifier(Document doc)
057: throws XWSSecurityException {
058: super (doc);
059: // Set default attributes
060: setAttribute("EncodingType", encodingType);
061: setAttribute("ValueType", valueType);
062: }
063:
064: public X509ThumbPrintIdentifier(SOAPElement element)
065: throws XWSSecurityException {
066: super (element);
067: }
068:
069: public byte[] getDecodedBase64EncodedValue()
070: throws XWSSecurityException {
071: try {
072: return Base64.decode(getReferenceValue());
073: } catch (Base64DecodingException e) {
074: log.log(Level.SEVERE,
075: "WSS0144.unableto.decode.base64.data",
076: new Object[] { e.getMessage() });
077: throw new SecurityHeaderException(
078: "Unable to decode Base64 encoded data", e);
079: }
080: }
081:
082: /**
083: * @return the SubjectKeyIdentifier from cert or null if cert does not
084: * contain one
085: */
086: public static byte[] getThumbPrintIdentifier(X509Certificate cert)
087: throws XWSSecurityException {
088: byte[] thumbPrintIdentifier = null;
089:
090: try {
091: thumbPrintIdentifier = MessageDigest.getInstance("SHA-1")
092: .digest(cert.getEncoded());
093: } catch (NoSuchAlgorithmException ex) {
094: throw new XWSSecurityException(
095: "Digest algorithm SHA-1 not found");
096: } catch (CertificateEncodingException ex) {
097: throw new XWSSecurityException(
098: "Error while getting certificate's raw content");
099: }
100:
101: return thumbPrintIdentifier;
102: }
103:
104: public void setCertificate(X509Certificate cert) {
105: this .cert = cert;
106: }
107:
108: public X509Certificate getCertificate() {
109: return cert;
110: }
111: }
|