-2

I wrote this code and I debug it with jcIDE. I have a error 0x6f00 in line signature.sign('''). I sent apdu "00 80 00 00 04 01 02 03 04" for signing operation
. My key is RSA -1024

RSAPrivateKey thePrivateKey = (RSAPrivateKey) KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PRIVATE, KeyBuilder.LENGTH_RSA_1024, NO_EXTERNAL_ACCESS);
    RSAPublicKey thePublickKey = (RSAPublicKey) KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC, KeyBuilder.LENGTH_RSA_1024, NO_EXTERNAL_ACCESS);

    public void  generatesignature(APDU apdu)
   {

    if(!Pin.isValidated())
          ISOException.throwIt (ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);


    byte[] buffer=apdu.getBuffer();


    // data field of the command APDU
    short numdata=(short) buffer[ISO7816.OFFSET_LC];

    byte p1=(byte)buffer[ISO7816.OFFSET_P1];

    thePrivateKey=(RSAPrivateKey)PrivateKeyArray[p1];
    thePublickKey=(RSAPublicKey)PublicKeyArray[p1];


    // receive data starting from the offset
    // ISO.OFFSET_CDATA
    short inputlength= (short) apdu.setIncomingAndReceive();

     // it is an error if the number of data bytes
     // read does not match the number in Lc byte
    if (inputlength == 0)
       ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

    try
    {         
    //convert input to hash

    MessageDigest digest=MessageDigest.getInstance(MessageDigest.ALG_SHA,false );

  short hashlength=digest.doFinal(buffer,ISO7816.OFFSET_CDATA,numdata,Input_Hash,(short)0);


    Signature    signature=Signature.getInstance(Signature.ALG_RSA_SHA_PKCS1,false);
    signature.init(thePrivateKey,Signature.MODE_SIGN);

     short hashlength=signature.sign(Input_Hash,(short)0,hashlength,Input_Sign, (short)0);



      Util.arrayCopy(Input_Sign,(short)0, buffer, (short)0, signLength);  
      apdu.setOutgoingAndSend((short)0, ((short)signLength));


    }
    catch (CryptoException c) {
     short reason = c.getReason();
     ISOException.throwIt(reason);       // for check
     }  
  }   

Can everybody help me?exactly in line short hashlength=signature.sign(Input_Hash,(short)0,hashlength,Input_Sign, (short)0); I have problem. I use catch but code never go in catch. enter image description here

my result in apdutool on real gemalto 2.2.1 card enter image description here

Ftm
  • 99
  • 1
  • 11
  • first: the code is incomplete, so its hard to say anything – Paul Bastian Aug 31 '16 at 13:17
  • second: `short signature.sign(...` is not valid. after short you must provide a variable name. – Paul Bastian Aug 31 '16 at 13:18
  • 1
    When and how do you initialize the object `signature`? Is `signature != null` when you execute the method `generatesignature()`? Are `Input_Hash` and `Input_Sign` `!= null`? And do they have at least `hashlength` elements? Finally, where does you code actually fail? The line `short signature.sign...` is not a valid construct and won't even compile! So what code did you actually use? – Michael Roland Aug 31 '16 at 13:27
  • Furthermore you should try to catch every Exception, not just CryptoException and then try to find out which exception was thrown – Paul Bastian Aug 31 '16 at 13:55
  • thanks for answer.I test it and debug with jcide. I attached my result image.yes Mr Roland signature is not null and input hash and input sign is not null – Ftm Aug 31 '16 at 15:45
  • Do you get the status code 0x6F00 in a simulator/emulator or on a real card? – Michael Roland Aug 31 '16 at 21:26
  • I get this error in jcide environment and for simulator.my code never go in catch – Ftm Sep 01 '16 at 02:00
  • Before signature . Sign input_hash has data but input_sign is empty. – Ftm Sep 01 '16 at 02:09
  • I test my code on real gemalto 2.2.1 . bu I get error 6f00 in step generate signature. I attached result image – Ftm Sep 01 '16 at 04:13
  • I has a problem. I use try ,cath and I add breakpoint in catch but when I debug my code ,breakpoint in catch is cleared and can't add breackpoin in these situation . It is a problem in my whole code and I can not use catch at all – Ftm Sep 01 '16 at 05:06
  • 1
    ` short numdata=(short) buffer[ISO7816.OFFSET_LC];` that short value may be negative, use `& 0xFF` if unsure. Please try and format your code better and use the Sun Code Conventions as minimum. Compare smart card development with watch making, not house building. – Maarten Bodewes Sep 02 '16 at 09:12

1 Answers1

0
      byte[] buffer=apdu.getBuffer();

     short numdata=(short) buffer[ISO7816.OFFSET_LC];


     short inputlength= (short) apdu.setIncomingAndReceive();

     if (inputlength == 0)
       ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);



      //sign
      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);
Ftm
  • 99
  • 1
  • 11