1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /** 6 * Licensed to the Apache Software Foundation (ASF) under one 7 * or more contributor license agreements. See the NOTICE file 8 * distributed with this work for additional information 9 * regarding copyright ownership. The ASF licenses this file 10 * to you under the Apache License, Version 2.0 (the 11 * "License"); you may not use this file except in compliance 12 * with the License. You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, 17 * software distributed under the License is distributed on an 18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 19 * KIND, either express or implied. See the License for the 20 * specific language governing permissions and limitations 21 * under the License. 22 */ 23 package com.sun.org.apache.xml.internal.security.keys.content.x509; 24 25 import java.security.MessageDigest; 26 import java.security.cert.X509Certificate; 27 28 import com.sun.org.apache.xml.internal.security.algorithms.JCEMapper; 29 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException; 30 import com.sun.org.apache.xml.internal.security.utils.Constants; 31 import com.sun.org.apache.xml.internal.security.utils.Signature11ElementProxy; 32 import org.w3c.dom.Attr; 33 import org.w3c.dom.Document; 34 import org.w3c.dom.Element; 35 36 /** 37 * Provides content model support for the <code>dsig11:X509Digest</code> element. 38 * 39 * @author Brent Putman (putmanb@georgetown.edu) 40 */ 41 public class XMLX509Digest extends Signature11ElementProxy implements XMLX509DataContent { 42 43 /** 44 * Constructor XMLX509Digest 45 * 46 * @param element 47 * @param BaseURI 48 * @throws XMLSecurityException 49 */ 50 public XMLX509Digest(Element element, String BaseURI) throws XMLSecurityException { 51 super(element, BaseURI); 52 } 53 54 /** 55 * Constructor XMLX509Digest 56 * 57 * @param doc 58 * @param digestBytes 59 * @param algorithmURI 60 */ 61 public XMLX509Digest(Document doc, byte[] digestBytes, String algorithmURI) { 62 super(doc); 63 this.addBase64Text(digestBytes); 64 this.constructionElement.setAttributeNS(null, Constants._ATT_ALGORITHM, algorithmURI); 65 } 66 67 /** 68 * Constructor XMLX509Digest 69 * 70 * @param doc 71 * @param x509certificate 72 * @param algorithmURI 73 * @throws XMLSecurityException 74 */ 75 public XMLX509Digest(Document doc, X509Certificate x509certificate, String algorithmURI) throws XMLSecurityException { 76 super(doc); 77 this.addBase64Text(getDigestBytesFromCert(x509certificate, algorithmURI)); 78 this.constructionElement.setAttributeNS(null, Constants._ATT_ALGORITHM, algorithmURI); 79 } 80 81 /** 82 * Method getAlgorithmAttr 83 * 84 * @return the Algorithm attribute 85 */ 86 public Attr getAlgorithmAttr() { 87 return this.constructionElement.getAttributeNodeNS(null, Constants._ATT_ALGORITHM); 88 } 89 90 /** 91 * Method getAlgorithm 92 * 93 * @return Algorithm string 94 */ 95 public String getAlgorithm() { 96 return this.getAlgorithmAttr().getNodeValue(); 97 } 98 99 /** 100 * Method getDigestBytes 101 * 102 * @return the digestbytes 103 * @throws XMLSecurityException 104 */ 105 public byte[] getDigestBytes() throws XMLSecurityException { 106 return this.getBytesFromTextChild(); 107 } 108 109 /** 110 * Method getDigestBytesFromCert 111 * 112 * @param cert 113 * @param algorithmURI 114 * @return digest bytes from the given certificate 115 * 116 * @throws XMLSecurityException 117 */ 118 public static byte[] getDigestBytesFromCert(X509Certificate cert, String algorithmURI) throws XMLSecurityException { 119 String jcaDigestAlgorithm = JCEMapper.translateURItoJCEID(algorithmURI); 120 if (jcaDigestAlgorithm == null) { 121 Object exArgs[] = { algorithmURI }; 122 throw new XMLSecurityException("XMLX509Digest.UnknownDigestAlgorithm", exArgs); 123 } 124 125 try { 126 MessageDigest md = MessageDigest.getInstance(jcaDigestAlgorithm); 127 return md.digest(cert.getEncoded()); 128 } catch (Exception e) { 129 Object exArgs[] = { jcaDigestAlgorithm }; 130 throw new XMLSecurityException("XMLX509Digest.FailedDigest", exArgs); 131 } 132 133 } 134 135 /** @inheritDoc */ 136 public String getBaseLocalName() { 137 return Constants._TAG_X509DIGEST; 138 } 139 }