See inserted code below.
Ramani Rajaram wrote:
>Hi,
>
>I am trying to sign some important data using JSS. I know, I have to
>create SignerInfo and SignedData to achieve this. But SignerInfo needs
>signingKey (private key). Using JSS, I can get the list of certificates
>and all the private keys. But How can I find out the corresponding
>private key to a public key?
>
>Also after creating the SignerInfo, how do I convert it to a SET for
>SignedData. Am I missing something here. I have also placed my piece of
>code here. Any inputs would help me......
>
>
>
>Thanks
>Krish.
>
> public String sign(String nickName, String strBuf)
> {
> IssuerAndSerialNumber issuerAndSerialNumber;
> SET authenticatedAttributes = null;
> SET unauthenticatedAttributes = null;
> SignatureAlgorithm signingAlg =
>SignatureAlgorithm.RSASignatureWithSHA1Digest;
> OBJECT_IDENTIFIER contentType = ContentInfo.DATA;
> byte[] messageDigest;
> org.mozilla.jss.crypto.PrivateKey signingKey = null;
> SET digestAlgorithms = null;
> SET certificates = null;
> SET crls = null;
> SET signers = null;
>
> try
> {
> X509Certificate cert = GetCert(nickName);
> String certData[] = GetCertData(nickName);
>
> Name issuer = new Name();
> issuer.addCommonName("test");
>
> INTEGER serialNumber = new INTEGER(certData[4]);
>
> issuerAndSerialNumber = new IssuerAndSerialNumber(issuer,
>serialNumber);
>
> MessageDigest md = MessageDigest.getInstance("SHA1");
> messageDigest = md.digest(strBuf.getBytes());
>
> String retValue = new String();
> ContentInfo ci = new ContentInfo(strBuf.getBytes());
>
> // how do I get this....
> signingKey = getPrivateKey(nickName);
>
signingKey = CryptoManager.getInstance().findPrivKeyByCert(cert);
>
>
> SignerInfo si = new SignerInfo(issuerAndSerialNumber,
> authenticatedAttributes,
> unauthenticatedAttributes, contentType,
>messageDigest,
> signingAlg, signingKey);
>
> digestAlgorithms = new SET();
> digestAlgorithms.addElement( new PrintableString("SHA1") );
>
> certificates = new SET();
> certificates.addElement( new ANY(cert.getEncoded()) );
>
> signers = new SET();
> // how to convert the si (SignerInfo to SET)
>
signers.addElement(si);
>
>
> SignedData sd = new SignedData(digestAlgorithms, ci,
>certificates, crls, signers);
>
> return retValue;
> }
>
> catch (Exception ex)
> {
> ex.printStackTrace();
> }
>
> return "";
> }
>
>
>