Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with Digest Value

843811Dec 2 2009 — edited Dec 2 2009
I am new to creating signatures. My recipient says they can't validate this document. I am posting the beginning XML, my program and the result.
I'm not sure what the issue is so I'm not even sure what question to ask.

I do notice that the DOCTYPE reference is no longer in the XML after adding the signature but I don't know if that matters. I started to create a place to add it (the "do nothing" emit function in the code) but wasn't sure if I was on the right track or, if so, how to properly peck the doctype declaation into the document model prior to signing the document.

BTW: Does the cursor jump around (back to the top) in the input area for anyone else when trying to add several "code" blocks?
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE REQUEST_GROUP SYSTEM "eRegistry_Inquiry_Request_v4_0_0.dtd">
<REQUEST_GROUP MISMOVersionID="2.3.1" _ID="FHA">
 <REQUESTING_PARTY _Identifier="1004550"/>
 <RECEIVING_PARTY _Identifier="1000001"/>
 <REQUEST _Datetime="2009-12-01T18:22:13Z">
  <REQUEST_DATA>
   <EREGISTRY_INQUIRY_REQUEST MISMOVersionID="4.0.0" _ActionType="SummaryInformation">
    <MERS MERS_MINNumber="100052502062008016"/>
   </EREGISTRY_INQUIRY_REQUEST>
  </REQUEST_DATA>
 </REQUEST>
</REQUEST_GROUP>
import java.io.*;

import org.w3c.dom.*;
import org.xml.sax.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.security.KeyStore;
import javax.xml.crypto.dsig.*;
import javax.xml.crypto.dsig.spec.*;
import javax.xml.crypto.dsig.dom.*;
import javax.xml.crypto.dsig.keyinfo.*;
import java.security.cert.X509Certificate;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.parsers.DocumentBuilderFactory;
public class XMLsig {
	public static void main (String argv [])throws Exception {
		}
	Logger logger = Logger.getLogger("eNotes");
	public String getSig(String docIn){
		String myXMLdoc = docIn;
		String signedDoc = "";
		Reference ref;
		SignedInfo si;
		KeyStore ks;
		XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
		try {
			ref = fac.newReference("", fac.newDigestMethod(DigestMethod.SHA1, null),
					Collections.singletonList(fac.newTransform(Transform.ENVELOPED,
							(TransformParameterSpec) null)),null, null);
			si = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE,
					(C14NMethodParameterSpec) null),fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null),
					Collections.singletonList(ref));
			ks = KeyStore.getInstance("JKS");
			String basedir = System.getProperty("user.dir");
			String f1 = basedir + "/hwvauwd233.jks";
			ks.load(new FileInputStream(f1), "hwvauwd233".toCharArray());
			KeyStore.PrivateKeyEntry keyEntry = (KeyStore.PrivateKeyEntry) ks.getEntry("hwvauwd233.hud.gov",new KeyStore.PasswordProtection("hwvauwd233".toCharArray()));
			X509Certificate cert = (X509Certificate) keyEntry.getCertificate();
			KeyInfoFactory kif = fac.getKeyInfoFactory();
			ArrayList x509Content = new ArrayList();
			x509Content.add(cert.getSubjectX500Principal().getName());
			x509Content.add(cert);
			X509Data xd = kif.newX509Data(x509Content);
			KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd));
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			dbf.setNamespaceAware(true);
			StringReader reader = new StringReader(myXMLdoc);
			InputSource xmlSource = new InputSource(reader);
			Document doc = dbf.newDocumentBuilder().parse(xmlSource);
			doc = emit(doc);
			reader.close();
			DOMSignContext dsc = new DOMSignContext(keyEntry.getPrivateKey(), doc.getDocumentElement());
			XMLSignature signature = fac.newXMLSignature(si, ki);
			signature.sign(dsc);
			try
			{
				DOMSource domSource = new DOMSource(doc);
				StringWriter writer = new StringWriter();
				StreamResult result = new StreamResult(writer);
				TransformerFactory tf = TransformerFactory.newInstance();
				Transformer transformer = tf.newTransformer();
				transformer.transform(domSource, result);
				signedDoc = writer.toString();
				}
			catch(TransformerException ex)
			{
				ex.printStackTrace();
				return null;
				}

			} catch (Throwable t) {
				t.printStackTrace();
				logger.log(Level.SEVERE, "eNote error: ", t);
				return null;
				}
			return signedDoc;
			}
	
	   public Document emit(Document doc) throws Exception {
	        return doc;
	    }
	}
<?xml version="1.0" encoding="UTF-8" standalone="no"?><REQUEST_GROUP MISMOVersionID="2.3.1" _ID="FHA">
 <REQUESTING_PARTY _Identifier="1004550"/>
 <RECEIVING_PARTY _Identifier="1000001"/>
 <REQUEST _Datetime="2009-12-01T18:22:13Z">
  <REQUEST_DATA>
   <EREGISTRY_INQUIRY_REQUEST MISMOVersionID="4.0.0" _ActionType="SummaryInformation">
    <MERS MERS_MINNumber="100052502062008016"/>
   </EREGISTRY_INQUIRY_REQUEST>
  </REQUEST_DATA>
 </REQUEST>
 <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
  <SignedInfo>
   <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
   <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
   <Reference URI="">
    <Transforms>
     <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
    </Transforms>
    <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
    <DigestValue>sp9OmFrpCsH8P6moPY0Sk8gCxrw=</DigestValue>
   </Reference>
  </SignedInfo>
  <SignatureValue>DqdL0M9fKga/t/r5jP3dT+WIO8X41L1RX2Y9K0B1PT1atSLQudyPb4osXT0b+Ks
  R3ehO0WmyTZDYInCgcogene4jpY+kAJKJpfNk1/sL9L+qkfkWaKZRprZe7SsWoSz2dODl9iwEKcwua8
  v8LbKYxGj91Ok7VTPxjiQvrzUCeAE=</SignatureValue>
  <KeyInfo>
  <X509Data>
   <X509SubjectName>CN=hwvauwd233.hud.gov,OU=TMD,O=US Department of Housing and Urban Development,L=Washington,ST=District Of Columbia,C=US</X509SubjectName>
   <X509Certificate>MIICvjCCAicCBEsNR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Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 30 2009
Added on Dec 2 2009
2 comments
891 views