title: how to generate digital signature with x509 certificate?
body: <p>How do we get and append x509data and x509certificate tag to the xml produced by the following code  </p>
  
  <pre><code> String providerName = System.getProperty("jsr105Provider",
     "org.jcp.xml.dsig.internal.dom.XMLDSigRI");
  
   XMLSignatureFactory fac =
     XMLSignatureFactory.getInstance("DOM",
     (Provider) Class.forName(providerName).newInstance());
  
   Reference ref =
     fac.newReference("",
         fac.newDigestMethod(DigestMethod.SHA1, null),
             Collections.singletonList(
                 fac.newTransform(Transform.ENVELOPED,(XMLStructure) null)), 
         null, null);
  
     SignedInfo si = fac.newSignedInfo
         (fac.newCanonicalizationMethod
           (CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS, 
              (XMLStructure) null), 
          fac.newSignatureMethod(SignatureMethod.RSA_SHA1, 
              null),
          Collections.singletonList(ref));
  
     KeyPairGenerator kpg = 
         KeyPairGenerator.getInstance("RSA");
     kpg.initialize(512);
     KeyPair kp = kpg.generateKeyPair();
  
     KeyInfoFactory kif = fac.getKeyInfoFactory();
     KeyValue kv = kif.newKeyValue(kp.getPublic());
  
     KeyInfo ki = 
         kif.newKeyInfo(Collections.singletonList(kv));
  
     DocumentBuilderFactory dbf =
         DocumentBuilderFactory.newInstance();
     dbf.setNamespaceAware(true);
     Document doc1 = 
         dbf.newDocumentBuilder().
         parse(new FileInputStream("C:/Documents and Settings/sbtho/Desktop/downloads/samp.xml"));
  
     DOMSignContext dsc = new DOMSignContext
      (kp.getPrivate(), doc.getDocumentElement());
  
  
     XMLSignature signature = fac.newXMLSignature(si, ki);
        signature.sign(dsc);
  
     TransformerFactory tf = TransformerFactory.newInstance();
     Transformer trans = tf.newTransformer();
  
     trans.transform(
         new DOMSource(doc),
         new StreamResult(
             new FileOutputStream("C:/Documents and Settings/sbtho/Desktop/downloads/signedsamp.xml")));
  </code></pre>
  
  <p>the output of the above code looks like this and i want ti insert x509 tags inside the keyinfo tag.</p>
  
  <pre><code>   &lt;?xml version="1.0" encoding="UTF-8" standalone="no" ?&gt; 
    &lt;questionset&gt;
     &lt;question category="graph" /&gt; 
    &lt;Signature xmlns="http://www.w3.org/2000/09/xmldsig#"&gt;
    &lt;SignedInfo&gt;
     &lt;CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments" /&gt; 
     &lt;SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /&gt; 
      &lt;Reference URI=""&gt;
      &lt;Transforms&gt;
      &lt;Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /&gt; 
      &lt;/Transforms&gt;
      &lt;DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /&gt; 
      &lt;DigestValue&gt;Kjgj/nVt41Q8gfDwSdfTGW42FQ8=&lt;/DigestValue&gt; 
      &lt;/Reference&gt;
      &lt;/SignedInfo&gt;
        &lt;SignatureValue&gt;nhdbvODcXYvc5w65todyDBkVJJW/VgN3sxMjILO+qavIln0np57qSYvC6CjavLEdD5KZ0uLoD7r/ o07X9k3I5Q==&lt;/SignatureValue&gt; 
   &lt;KeyInfo&gt;
   &lt;KeyValue&gt;
   &lt;RSAKeyValue&gt;
     &lt;Modulus&gt;qc/XQnBZ2/waPw+wUmdFiYUEY8RDLpaDn+Xmm56WoHn9jKKB0BCrYxz33q+z4O7VwQdv1eAdv9cK eTHEEpJpIQ==&lt;/Modulus&gt; 
    &lt;Exponent&gt;AQAB&lt;/Exponent&gt; 
    &lt;/RSAKeyValue&gt;
    &lt;/KeyValue&gt;
    &lt;/KeyInfo&gt;
    &lt;/Signature&gt;
    &lt;/questionset&gt;
  </code></pre>
  
  <p>and how is the x509certificate created ?</p>