source: fedd/abac-src/ttg/credential/SignDomain.java @ 7321a43

version-1.30
Last change on this file since 7321a43 was 8780cbec, checked in by Jay Jacobs <Jay.Jacobs@…>, 15 years ago

ABAC sources from Cobham

  • Property mode set to 100644
File size: 3.0 KB
Line 
1package com.nailabs.abac.credential;
2
3import java.security.*;
4import java.security.cert.*;
5import org.w3c.dom.*;
6import org.apache.xml.security.signature.*;
7import org.apache.xml.security.exceptions.*;
8import org.apache.xml.security.transforms.Transforms;
9import org.apache.xml.security.utils.*;
10
11public 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}
Note: See TracBrowser for help on using the repository browser.