package com.nailabs.abac.credential; import java.security.*; import java.security.cert.*; import org.w3c.dom.*; import org.apache.xml.security.signature.*; import org.apache.xml.security.exceptions.*; import org.apache.xml.security.transforms.Transforms; import org.apache.xml.security.utils.*; public class SignDomain extends Application { /** signature algorithm is set to DSA */ protected static String SIG_TYPE = XMLSignature.ALGO_ID_SIGNATURE_DSA; /** digest algorithm is SHA-1 */ protected static String DIGEST_TYPE = Constants.ALGO_ID_DIGEST_SHA1; protected static String KEY_PAIR_TYPE = "DSA"; protected PrivateKey privateKey = null; protected PublicKey publicKey = null; protected X509Certificate cert = null; protected String base64Digest; protected String hexDigest; /** * Constructor for the application which reads in an XML document, * parses the doc into a DOM tree, signs the document, and writes * the signed tree into an XML file. Cryptographic material is supplied * by a Java keystore. */ public SignDomain(String argv[]) { super(argv); name = "SignDomain"; parseParameters(); getKeys(); readDoc(); signDoc(); writeDoc(); } private void signDoc() { try { String baseURI = outXML.toURL().toString(); XMLSignature sig = new XMLSignature( doc, baseURI, SIG_TYPE); docElement.appendChild( sig.getElement() ); Transforms transforms = new Transforms( doc ); transforms.addTransform( Transforms.TRANSFORM_ENVELOPED_SIGNATURE); transforms.addTransform( Transforms.TRANSFORM_C14N_WITH_COMMENTS); String referenceURI = noComments ? "" : "#xpointer(/)"; sig.addDocument(referenceURI,transforms, DIGEST_TYPE); sig.addKeyInfo( publicKey ); sig.sign( privateKey ); //Element digestValueElem = sig.getSignedInfo().item( 0 ).getChildElementLocalName( 0, Constants.SignatureSpecNS, Constants._TAG_DIGESTVALUE ); //byte[] elemDig = Base64.decode( digestValueElem ); byte[] elemDig = sig.getSignedInfo().item(0).getDigestValue(); base64Digest = Base64.encode( elemDig ); //hexDigest = HexDump.byteArrayToHexString(elemDig); hexDigest = ""; } catch( org.apache.xml.security.exceptions.XMLSecurityException sec) { sec.printStackTrace(); } catch( java.net.MalformedURLException murle ) { murle.printStackTrace(); } catch( Exception e ) { e.printStackTrace(); } } private void printDigest() { System.out.println( base64Digest ); System.out.println( hexDigest ); } private void generateKeyPair() { try { KeyPairGenerator kpg = KeyPairGenerator.getInstance( "DSA" ); kpg.initialize( 512 ); KeyPair newKeyPair = kpg.generateKeyPair(); privateKey = newKeyPair.getPrivate(); publicKey = newKeyPair.getPublic(); } catch( java.security.NoSuchAlgorithmException nsae ) { nsae.printStackTrace(); } } /** standard main routine for launching the application */ public static void main(String argv[]) { SignDomain sc = new SignDomain(argv); } }