-1

I want to verify my signature . my signature is a byte array. I use spongy castle I get error "org.spongycastle.cms.CMSException: Malformed content." this is my code:

   String base64 = Base64.toBase64String(signedchallenge);
   CMSSignedData cms = new CMSSignedData(Base64.decode(base64));
   Store store = cms.getCertificates();
   SignerInformationStore signers = cms.getSignerInfos();
   Collection c = signers.getSigners();

I get error in line " CMSSignedData cms = new CMSSignedData(Base64.decode(base64));"

also I used this method for signed challenge generation. It did in smart cart

          Signature signature=Signature.getInstance(Signature.ALG_RSA_SHA_PKCS1,false);
      signature.init(thePrivateKey,Signature.MODE_SIGN);
      signLength=signature.sign(buffer,(short)(ISO7816.OFFSET_CDATA & 0xFF), inputlength, buffer, (short)(0));
      apdu.setOutgoingAndSend((short)0,signLength);
Fatemeh
  • 35
  • 8
  • The first snippet of code is not a signature verification. I guess the signature is generated into `buffer`. What is the relationship between `buffer` and `signedchallenge`? Show the full code. Where is the public key? Do You want a simple PKCS#1 verification? – pedrofb Nov 21 '16 at 18:02
  • Thank you for answer me. yes I edited my code. signed challenge is in buffer. I don't write complete code for verification.publickey is not important. I get it from another method . yes It is my code for signe generation . I want to signe a challenge with my private ke. what is standard? do I make mistake for sign generation? if it is good yes I want to use this method – Fatemeh Nov 21 '16 at 18:09
  • You will need the public key to verify the PKCS#1 signature. This is a standard digital signature, but you have post code about 'CMS' format which embeds a pkcs1. I provide an answer to verify the PKCS#1 signature – pedrofb Nov 21 '16 at 18:39
  • does it mean I should generate a cms signe format? – Fatemeh Nov 21 '16 at 18:56
  • CMS encapsulates digital signature including other attributes like signing time or the used X509 certificates, and CMS values are encoded in ASN.1 syntax. Usage depends on the purpose of your use case. Without knowing the details is not possible to advise. I suggest to post your question here http://security.stackexchange.com/ – pedrofb Nov 22 '16 at 08:10

1 Answers1

1

According to javacard documentation

ALG_RSA_SHA_PKCS1 generates a 20-byte SHA digest, pads the digest according to the PKCS#1 (v1.5) scheme, and encrypts it using RSA

To verify the signature in Android side use this code

Signature sig = Signature.getInstance("SHA1withRSA");
sig.initVerify(publicKey);
sig.update(challenge);
boolean verifies = sig.verify(signedchallenge);

Where signedchallenge is the signature available on buffer from (short)(ISO7816.OFFSET_CDATA & 0xFF) to signLength and challenge is the original data to sign

pedrofb
  • 37,271
  • 5
  • 94
  • 142
  • thank you very much dear Pedrofb.can I use same code in C# .net too? I use this code (in my question) in C# and bouncycastle and I have this problem in c# too. don't use bouncycastle in your code ? can you give me a code with bouncy castle? – Fatemeh Nov 21 '16 at 18:47
  • You do not need 'bouncycastle' to verify a RSA signature because Java and C# have native support.( But have not for CMS ) In this link http://stackoverflow.com/questions/8437288/signing-and-verifying-signatures-with-rsa-c-sharp you have a similar example with C# – pedrofb Nov 21 '16 at 19:02
  • Excuse me dear Pedrofb. I have problem for convert byte array public key to rsa parameter . I wrote this post "http://stackoverflow.com/questions/40734380/convert-byte-array-to-rsa-parameters" can I use boncycastle for pkcs#1 in c#? – Fatemeh Nov 22 '16 at 06:11
  • I have no experience with C#. There are some links in SO. Check this http://stackoverflow.com/questions/11506891/how-to-load-the-rsa-public-key-from-file-in-c-sharp – pedrofb Nov 22 '16 at 08:12