[8780cbec] | 1 | package com.nailabs.abac.credential; |
---|
| 2 | |
---|
| 3 | import java.security.*; |
---|
| 4 | import java.security.cert.*; |
---|
| 5 | import org.w3c.dom.*; |
---|
| 6 | import org.apache.xml.security.signature.*; |
---|
| 7 | import org.apache.xml.security.exceptions.*; |
---|
| 8 | import org.apache.xml.security.transforms.Transforms; |
---|
| 9 | import org.apache.xml.security.utils.*; |
---|
| 10 | |
---|
| 11 | public class SignDomain extends Application { |
---|
| 12 | /** signature algorithm is set to DSA */ |
---|
| 13 | protected static String SIG_TYPE = XMLSignature.ALGO_ID_SIGNATURE_DSA; |
---|
| 14 | /** digest algorithm is SHA-1 */ |
---|
| 15 | protected static String DIGEST_TYPE = Constants.ALGO_ID_DIGEST_SHA1; |
---|
| 16 | |
---|
| 17 | protected static String KEY_PAIR_TYPE = "DSA"; |
---|
| 18 | |
---|
| 19 | protected PrivateKey privateKey = null; |
---|
| 20 | |
---|
| 21 | protected PublicKey publicKey = null; |
---|
| 22 | |
---|
| 23 | protected X509Certificate cert = null; |
---|
| 24 | |
---|
| 25 | protected String base64Digest; |
---|
| 26 | |
---|
| 27 | protected String hexDigest; |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * Constructor for the application which reads in an XML document, |
---|
| 31 | * parses the doc into a DOM tree, signs the document, and writes |
---|
| 32 | * the signed tree into an XML file. Cryptographic material is supplied |
---|
| 33 | * by a Java keystore. |
---|
| 34 | */ |
---|
| 35 | public SignDomain(String argv[]) { |
---|
| 36 | super(argv); |
---|
| 37 | name = "SignDomain"; |
---|
| 38 | parseParameters(); |
---|
| 39 | getKeys(); |
---|
| 40 | readDoc(); |
---|
| 41 | signDoc(); |
---|
| 42 | writeDoc(); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | private void signDoc() { |
---|
| 46 | try { |
---|
| 47 | String baseURI = outXML.toURL().toString(); |
---|
| 48 | XMLSignature sig = new XMLSignature( doc, baseURI, SIG_TYPE); |
---|
| 49 | docElement.appendChild( sig.getElement() ); |
---|
| 50 | Transforms transforms = new Transforms( doc ); |
---|
| 51 | transforms.addTransform( Transforms.TRANSFORM_ENVELOPED_SIGNATURE); |
---|
| 52 | transforms.addTransform( Transforms.TRANSFORM_C14N_WITH_COMMENTS); |
---|
| 53 | String referenceURI = noComments ? "" : "#xpointer(/)"; |
---|
| 54 | sig.addDocument(referenceURI,transforms, DIGEST_TYPE); |
---|
| 55 | sig.addKeyInfo( publicKey ); |
---|
| 56 | sig.sign( privateKey ); |
---|
| 57 | |
---|
| 58 | //Element digestValueElem = sig.getSignedInfo().item( 0 ).getChildElementLocalName( 0, Constants.SignatureSpecNS, Constants._TAG_DIGESTVALUE ); |
---|
| 59 | //byte[] elemDig = Base64.decode( digestValueElem ); |
---|
| 60 | byte[] elemDig = sig.getSignedInfo().item(0).getDigestValue(); |
---|
| 61 | base64Digest = Base64.encode( elemDig ); |
---|
| 62 | //hexDigest = HexDump.byteArrayToHexString(elemDig); |
---|
| 63 | hexDigest = ""; |
---|
| 64 | } catch( org.apache.xml.security.exceptions.XMLSecurityException sec) { |
---|
| 65 | sec.printStackTrace(); |
---|
| 66 | } catch( java.net.MalformedURLException murle ) { |
---|
| 67 | murle.printStackTrace(); |
---|
| 68 | } catch( Exception e ) { |
---|
| 69 | e.printStackTrace(); |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | private void printDigest() { |
---|
| 74 | System.out.println( base64Digest ); |
---|
| 75 | System.out.println( hexDigest ); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | private void generateKeyPair() { |
---|
| 79 | try { |
---|
| 80 | KeyPairGenerator kpg = KeyPairGenerator.getInstance( "DSA" ); |
---|
| 81 | kpg.initialize( 512 ); |
---|
| 82 | KeyPair newKeyPair = kpg.generateKeyPair(); |
---|
| 83 | privateKey = newKeyPair.getPrivate(); |
---|
| 84 | publicKey = newKeyPair.getPublic(); |
---|
| 85 | } catch( java.security.NoSuchAlgorithmException nsae ) { |
---|
| 86 | nsae.printStackTrace(); |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | /** standard main routine for launching the application */ |
---|
| 91 | public static void main(String argv[]) { |
---|
| 92 | SignDomain sc = new SignDomain(argv); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | } |
---|