wood burning stoves 2.0*
The moose likes Security and the fly likes NUll pointer exception in XML sig program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Security
Bookmark "NUll pointer exception in XML sig program" Watch "NUll pointer exception in XML sig program" New topic
Author

NUll pointer exception in XML sig program

saurabh raturi
Greenhorn

Joined: Jul 30, 2009
Posts: 11
Hi all,

i have written a progam for xml siganature which is as follows.
i am using smart card for this.null pointer excepton is in red colored line.

please help me.
thanks.


program and out put is as follows.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package sig;

/**
*
* @author cabin6
*/
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.crypto.dsig.*;
import javax.xml.crypto.dsig.keyinfo.*;
import javax.xml.crypto.dsig.spec.*;
import javax.xml.crypto.dsig.dom.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.w3c.dom.Document;
import java.security.cert.*;
import java.security.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.util.Enumeration;
import com.sun.org.apache.xml.internal.security.Init.*;
import java.io.InputStream;
import java.util.Arrays;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import org.w3c.dom.Node;

/**
*
* @author cabin6
*/

public class Main
{
public static void main(String a[])throws Exception{
try{

// Create a DOM XMLSignatureFactory that will be used to
// generate the enveloped signature.

String providerName = System.getProperty("jsr105Provider","org.jcp.xml.dsig.internal.dom.XMLDSigRI");
XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM",(Provider) Class.forName(providerName).newInstance());


// Create a Reference to the enveloped document (in this case,
// you are signing the whole document, so a URI of "" signifies
// that, and also specify the SHA1 digest algorithm and
// the ENVELOPED Transform.

String inputFilename = "c:/result.xml";
InputStream inputStream = new FileInputStream(inputFilename);
DocumentBuilderFactory documentBuilderFactory =DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
Document document = builder.parse(inputStream);
Node node = document.getDocumentElement();


// STEP 4: Prepare the XMLSignatureFactory
// Create DigestMethod
String digestAlgorithm = DigestMethod.SHA1;
DigestMethodParameterSpec digestParams = null;
DigestMethod digestMethod = fac.newDigestMethod(digestAlgorithm, digestParams);

// Create Transform
String transformAlgorithm = Transform.ENVELOPED;
TransformParameterSpec transformParams = null;
Transform transform = fac.newTransform(transformAlgorithm, transformParams);

// Create Reference
String uri = "";
List transforms = Arrays.asList(transform);
String type = null;
String id = null;
Reference reference = fac.newReference(uri, digestMethod,transforms, type, id);

// Create CanonicalizationMethod
String canonAlgorithm = CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS;
C14NMethodParameterSpec canonParams = null;
CanonicalizationMethod canonicalizationMethod =fac.newCanonicalizationMethod(canonAlgorithm, canonParams);

// Create SignatureMethod
String signatureAlgorithm = SignatureMethod.RSA_SHA1;
SignatureMethodParameterSpec signatureParams = null;
SignatureMethod signatureMethod =fac.newSignatureMethod(signatureAlgorithm, signatureParams);

// Create SignedInfo
List references = Arrays.asList(reference);
SignedInfo si = fac.newSignedInfo(canonicalizationMethod,signatureMethod, references);


// Load the KeyStore and get the signing key and certificate.
String configName = "C:/Program Files/Java/jdk1.6.0_05/jre/lib/ext/pkcs11.cfg";
Provider p = new sun.security.pkcs11.SunPKCS11(configName);
Security.addProvider(p);
p.list(System.out);

String password = "PASSWORD";
char[] pin = password.toCharArray();
KeyStore ks = KeyStore.getInstance("PKCS11");
ks.load(null, pin);


Enumeration aliasesEnum = ks.aliases();
String alias = (String) aliasesEnum.nextElement();
System.out.println("Alias: " + alias);
PrivateKey privateKey = (PrivateKey) ks.getKey(alias, null);
System.out.println("Private key: " + privateKey);
X509Certificate cert = (X509Certificate) ks.getCertificate(alias);
System.out.println("Certificate: " + cert);

// Create the KeyInfo containing the X509Data.
KeyInfoFactory kif = fac.getKeyInfoFactory();
List x509Content = new ArrayList();
x509Content.add(cert.getSubjectX500Principal().getName());
x509Content.add(cert);
X509Data xd = kif.newX509Data(x509Content);
KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd));
System.out.println("csl:"+Collections.singletonList(xd));

DOMSignContext signContext = new DOMSignContext(privateKey, node);
// Create the XMLSignature, but don't sign it yet.
XMLSignature signature = fac.newXMLSignature(si,ki);

// Marshal, generate, and sign the enveloped signature.
signature.sign(signContext);


String outputFilename = "C:/Documents and Settings/cabin6.NIC/Desktop/signedPurchaseOrder.xml";
OutputStream outputStream = new FileOutputStream(outputFilename);
Source xmlSource = new DOMSource(document);
Result outputTarget = new StreamResult(outputStream);
TransformerFactory transformerFactory =TransformerFactory.newInstance();
Transformer transformer =transformerFactory.newTransformer();
transformer.transform(xmlSource, outputTarget);

}
catch (FileNotFoundException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}


out put is :

run:
-- listing properties --
Alg.Alias.MessageDigest.SHA=SHA1
KeyPairGenerator.DH=sun.security.pkcs11.P11KeyPairGenerator
KeyGenerator.DESede=sun.security.pkcs11.P11KeyGenerator
Alg.Alias.KeyAgreement.DiffieHellman=DH
Alg.Alias.KeyPairGenerator.DiffieHellman=DH
Provider.id name=SunPKCS11-SunPKCS11-rainbow_token
KeyGenerator.ARCFOUR=sun.security.pkcs11.P11KeyGenerator
Signature.SHA512withRSA=sun.security.pkcs11.P11Signature
Signature.MD2withRSA=sun.security.pkcs11.P11Signature
Alg.Alias.Cipher.RC4=ARCFOUR
KeyFactory.DH=sun.security.pkcs11.P11DHKeyFactory
Cipher.DESede/CBC/NoPadding=sun.security.pkcs11.P11Cipher
Provider.id version=1.6
KeyFactory.RSA=sun.security.pkcs11.P11RSAKeyFactory
SecretKeyFactory.ARCFOUR=sun.security.pkcs11.P11SecretKeyFactory
Alg.Alias.Signature.NONEwithDSA=RawDSA
Signature.MD5withRSA=sun.security.pkcs11.P11Signature
Cipher.DES/CBC/NoPadding=sun.security.pkcs11.P11Cipher
MessageDigest.SHA1=sun.security.pkcs11.P11Digest
SecretKeyFactory.DESede=sun.security.pkcs11.P11SecretKeyFactory
Alg.Alias.KeyGenerator.RC4=ARCFOUR
KeyGenerator.DES=sun.security.pkcs11.P11KeyGenerator
Alg.Alias.KeyStore.PKCS11-SunPKCS11-rainbow_token=PKCS11
Cipher.ARCFOUR=sun.security.pkcs11.P11Cipher
Signature.SHA256withRSA=sun.security.pkcs11.P11Signature
KeyStore.PKCS11=sun.security.pkcs11.P11KeyStore
MessageDigest.MD5=sun.security.pkcs11.P11Digest
KeyPairGenerator.DSA=sun.security.pkcs11.P11KeyPairGenerator
SecretKeyFactory.DES=sun.security.pkcs11.P11SecretKeyFactory
MessageDigest.MD2=sun.security.pkcs11.P11Digest
Alg.Alias.SecretKeyFactory.RC4=ARCFOUR
SecureRandom.PKCS11=sun.security.pkcs11.P11SecureRandom
KeyAgreement.DH=sun.security.pkcs11.P11KeyAgreement
Alg.Alias.MessageDigest.SHA-1=SHA1
Provider.id info=SunPKCS11-SunPKCS11-rainbow_token usi...
Signature.DSA=sun.security.pkcs11.P11Signature
Signature.SHA1withRSA=sun.security.pkcs11.P11Signature
Signature.SHA384withRSA=sun.security.pkcs11.P11Signature
KeyFactory.DSA=sun.security.pkcs11.P11DSAKeyFactory
Provider.id className=sun.security.pkcs11.SunPKCS11
KeyPairGenerator.RSA=sun.security.pkcs11.P11KeyPairGenerator
Alg.Alias.Signature.SHA1withDSA=DSA
Signature.RawDSA=sun.security.pkcs11.P11Signature
Alg.Alias.KeyFactory.DiffieHellman=DH
Cipher.RSA/ECB/PKCS1Padding=sun.security.pkcs11.P11RSACipher
Alias: 26094dc8-70b4-491c-9c6f-4e9e1f4f86bc

Aug 7, 2009 12:45:11 PM com.sun.org.apache.xml.internal.security.Init init
SEVERE: Bad:
java.lang.NullPointerException
at org.apache.crimson.tree.ElementNode.getAttributeNodeNS(ElementNode.java:415)
at org.apache.crimson.tree.ElementNode.getAttributeNS(ElementNode.java:392)
at com.sun.org.apache.xml.internal.security.Init.init(Init.java:187)
at org.jcp.xml.dsig.internal.dom.DOMXMLSignature.<clinit>(DOMXMLSignature.java:63)
at org.jcp.xml.dsig.internal.dom.DOMXMLSignatureFactory.newXMLSignature(DOMXMLSignatureFactory.java:35)
at sig.Main.main(Main.java:193)
java.lang.NullPointerException
at org.apache.crimson.tree.ElementNode.getAttributeNodeNS(ElementNode.java:415)
at org.apache.crimson.tree.ElementNode.getAttributeNS(ElementNode.java:392)
at com.sun.org.apache.xml.internal.security.Init.init(Init.java:187)
at org.jcp.xml.dsig.internal.dom.DOMXMLSignature.<clinit>(DOMXMLSignature.java:63)
at org.jcp.xml.dsig.internal.dom.DOMXMLSignatureFactory.newXMLSignature(DOMXMLSignatureFactory.java:35)
at sig.Main.main(Main.java:193)
Exception in thread "main" java.lang.NullPointerException
at org.apache.crimson.tree.ElementNode.getAttributeNodeNS(ElementNode.java:415)
at org.jcp.xml.dsig.internal.dom.DOMReference.marshal(DOMReference.java:271)
at org.jcp.xml.dsig.internal.dom.DOMSignedInfo.marshal(DOMSignedInfo.java:216)
at org.jcp.xml.dsig.internal.dom.DOMXMLSignature.marshal(DOMXMLSignature.java:194)
at org.jcp.xml.dsig.internal.dom.DOMXMLSignature.sign(DOMXMLSignature.java:304)
at sig.Main.main(Main.java:196)
Java Result: 1
BUILD SUCCESSFUL (total time: 16 seconds)


 
It is sorta covered in the JavaRanch Style Guide.
 
subject: NUll pointer exception in XML sig program