digital-signature,smartcard,javacard,apdu,ecdsa , Sending signature data in response APDU - Java Card


Sending signature data in response APDU - Java Card

Question:

Tag: digital-signature,smartcard,javacard,apdu,ecdsa

I would like to sign some data (the MESSAGE byte array) on my Java Card and then return the signature in a response APDU. My code works fine (or at least I think it does and it returns 9000) without the line apdu.sendBytes(BAS, sSignLen), but when I uncomment it I get an unknown error (0xC000002B (Unknown error.)).

When I try to send other data in a response APDU it works flawlessly.

apdu.setIncomingAndReceive();
Util.arrayCopyNonAtomic(MESSAGE, (short) 0, buffer, (short) 0, (short) MESSAGE.length);
apdu.setOutgoingAndSend((short) 0, (short) MESSAGE.length);

Here is my code. What am I doing wrong or missing? Thank you!

public class TestApplet extends Applet {

    ...

    private final static byte SIGN = (byte) 0x01;

    ...

    private final static byte[] MESSAGE = new byte[] { 'M', 'e', 's', 's', 'a', 'g', 'e' };

    final static short BAS = 0;

    public void process(APDU apdu) {
        if (this.selectingApplet())
            return;

        byte buffer[] = apdu.getBuffer();

        ...

        switch (buffer[ISO7816.OFFSET_INS]) {
        case SIGN:
            try {
                ECDSAKeyPair = Secp256k1Domain.getKeyPairParameter();
                ECDSAKeyPair.genKeyPair();

                ECDSAPublicKey = (ECPublicKey) ECDSAKeyPair.getPublic();
                ECDSAPrivateKey = (ECPrivateKey) ECDSAKeyPair.getPrivate();

                ECDSASignature = Signature.getInstance(Signature.ALG_ECDSA_SHA, false);

                short signLen = 0;

                byte[] signatureArray = new byte[70];

                ECDSASignature.init(ECDSAPrivateKey, Signature.MODE_SIGN);
                signLen = ECDSASignature.sign(MESSAGE, BAS, (short) MESSAGE.length, signatureArray, BAS);

                apdu.setIncomingAndReceive();
                Util.arrayCopyNonAtomic(signatureArray, (short) 0, buffer, (short) 0, (short) signatureArray.length);
                apdu.setOutgoingAndSend((short) 0, (short) signatureArray.length);
            } catch (CryptoException c) {
                short reason = c.getReason();
                ISOException.throwIt((short) ((short) (0x9C00) | reason));
            }

            break;

        ...

        return;
    }
}

Answer:

It's probably that signLen is larger than the Ne value (incorrectly called Le in the JavaCard specifications). You are also abusing the Le value to mean (short) MESSAGE.length by the way. Ne indicates the maximum number of bytes that are expected to be send back.


Related:


C on smartcards [closed]


c,smartcard
I have the task to write some crypto stuff in C and make it lightweight. The idea behind making it lightweight is, that it could run on a smartcard which doesn't offer much computational power and memory. It won't come to actually running it on a smartcard and it won't...

Sign PDF using an external service and iText


itext,digital-signature,digest
I have this scenario. I have an application that generates a PDF, and that needs to be signed. We have not the certificates to sign the document, because they're in a HSM, and the only way we could make use of the certificates is using a webservice. This webservice, offers...

ISO7816 - Odd INS codes?


smartcard,javacard,apdu
I found these mysterious lines in ISO 7816, (http://www.cardwerk.com/smartcards/smartcard_standard_ISO7816-4_5_basic_organizations.aspx#chap5_4): 5.4.2 Instruction byte The instruction byte INS of a command shall be coded to allow transmission with any of the protocols defined in part 3 of ISO/IEC 7816. Table 10 shows the INS codes that are consequently invalid. Table 10 -...

UID of a NFC/SWP-accessed SIM card


android,nfc,smartcard,sim-card
SIM card is used as a secure element in my project. It is accessed through NFC-SWP contactless interface from a terminal device. I need to identify the SIM card somehow with a unique and permanent identifier and I need to be able to read the identifier through NFC. ICCID seems...

NFC SWP applet selection returns 6999


android,nfc,smartcard,javacard,sim-card
I have a simple JavaCard applet installed on my SIM card. I try to communicate with my applet using Omnikey 5121 CL reader and NFC-enabled Sony Xperia L through NFC/SWP (single wire protocol). The problem is I cannot select the applet - as a status word I get 6999. The...

Sign PDF with smartcard in web context using CAPICOM & iTextSharp


itextsharp,activex,digital-signature,smartcard,capicom
Read through the following references: iText Digital signature white paper, and C# examples. (specifically chapter 4) For those interested, another great and concise summary of the PDF signing process. CAPICOM documentation. Online examples / questions here and on iText mailing list archives, such as here and here. Hashing code: BouncyCastle.X509Certificate[]...

Promag Card Reader PHP Accessing


php,smartcard,pcsc
I have promag 310 card reader. It is connect to PC with com port. I want to access card ID with PHP xampp in windows platform. I add php_pcsc.dll to extension folder. And I update php.ini with it. then I wrote this code: $context = scard_establish_context(); $readers = scard_list_readers($context); print_r($readers);...

Make 2 cardlet Java Card communicate


applet,smartcard,channel,javacard
I have 2 cardlets Java Card on the same smart card that I want to make communicate. I could select an applet then send the corresponding APDU and then deselect it and select the other one and etc... I am wondering if it is possible to do it more properly...

Javacard applet beginner


java,applet,smartcard,javacard
I am new to javacard applet development.How many development tools are there now? Which is the simplest for beginners? As simple as possible... Thanks in advance

iText Stamping - Java [duplicate]


java,pdf,itext,digital-signature,invalidate
This question already has an answer here: how to add blank page in digitally signed pdf using java? 2 answers I am having trouble with stamping PDF documents without invalidating digital signatures. Current, I succeeded stamping a PDF. However, if the document is previously signed the signature is no...

Is it possible to program a “Java Card” - enabled smart card in any other language than Java?


c,embedded,smartcard,javacard
I have a Java Card enabled smart card and a card reader that was given by my Lab-instructor. I am supposed to do a project using Java Card 2.1.1 API. I don't like the Java Card API. Is it possible to program my given smart card using any other language...

How to get signature Value from signed PDF in C#?


c#,pdf,itextsharp,digital-signature
How is it possible to get signature Value from signed PDF file? I can get all other data from signature except its value. Is there any way to get it in C#? PdfPKCS7 pk; PdfReader reader = new PdfReader(PdfFilename); AcroFields af = reader.AcroFields; var names = af.GetSignatureNames(); foreach (string name...

Memory Access Performance in Java Card


performance,memory,smartcard,javacard
Supposing that I instanced a persisant byte array in Java Card with a length of 30 000 (byte[] array = new byte[(short) 0x7530];) that I run through everytime I use my applet. I also instanced an index i that I set everytime I run through my array to 'remember' at...

Two OwnerPIN object in Java Card


security,smartcard,javacard,apdu,pin-code
I am working on a Java Card application where our requirement is to keep some static data and balance in the card. For security I was thinking to make 2 object of OwnerPIN. One object is for terminal authentication (i.e. the terminal needs to send 8 bytes of data to...

how changing master key or other keys can provide security ?how used session key to keep the further communication between DESFire and reader?


security,session,authentication,nfc,smartcard
I am working on the ticket electronik that tickets are DESFire cards. I want communications be safe . i now trying change PICC key . Before changing DESFire master key Authenticate with master key is necessary. In desfire sheet about Authenticate at page 31 was described: "This procedure not only...

ADPU Service in iOS


ios,nfc,smartcard,apdu,contactless-smartcard
I'm looking for an NFC solution for iOS similar to HostApduService for Android. Can someone tell me how can I exchange APDU commands in iOS. Thank you....

How to send a digital signed email or document from iphone ( iOS 8 ) [closed]


ios,iphone,digital-signature
I need a way to create a digital signed document and send using a digital signed e-mail from my iPhone ( iOS 8 ) Any suggestions regarding apps are kindly appreciated Thanx...

number value to byte[6] array card reader


c#,smartcard,smartcard-reader,cardreader
What's the best way to convert a number to a byte[6] in C#? I'm using MagTek Card reader and trying to display desired amount on device screen, it should be 6-byte array. The amount needs to be used and authorized, EMV Tag 9F02, format n12. Function: int requestSmartCard(int cardType, int...

node.js crypto signature and openssl signature does not match


node.js,openssl,rsa,digital-signature,sha256
I wanted to perform RSA-SHA512 on a file in node.js. I could calculate sha512 hash of the given data file which matches with that of openssl's. However when trying to get the digital signature on the same hash, node.js signature differs from openssl signature. Below is an example code snippet:...

Unable to get SignerCertificate from CMSSigned data


java,security,digital-signature,x509certificate,bouncycastle
While trying to extract SignerCertificate (X509) from CMS Signed data, my code is giving error, no certificate found. I am using bouncycastle libraries for this purpose. I think I am unable to sign it properly.The code snippets are attached, Have a look and please correct where I am going wrong....

Secure Box in JCOP card


smartcard,javacard,globalplatform,jcop,nxp-microcontroller
JCOP V2.4.2 Revision 3 Security Target: Page 11-12 A Secure Box concept is implemented within JCOP 2.4.2 R3. The Secure Box is a construct which allows to run non certified third party native code and ensures that this code cannot harm, influence or manipulate the JCOP 2.4.2 R3 operating system...

Send APDU commands to USIM/SIM card in android


android,smartcard,at-command,javacard,sim-card
I was already worked with smart cards and I am familiar with APDU commands (that are defined in ISO/IEC 7816 and Global Platform specifications). Now I want to know if is there any way to send an APDU command to my USIM/SIM card that is inserted to my mobile phone?...

Is checking one randomly chosen digital signature is enough for verifying multiple signed file?


java,certificate,x509certificate,digital-signature,bouncycastle
Imagine, I have multiple detached signatures of some binary file and they stored in different .sig files. When somebody modifies file, all signatures will become invalid. For xml an enveloped signature is widely used. Cheking it is more difficult - one have to extract last ds:Signature element, check signature, remove...

Smart Card Reader T0 T1 communication on APDU level


smartcard,apdu,smartcard-reader
I am struggle to understand what protocol I have to use to communicate with the card T0 or T1? So, correct me if I am wrong, but the reader actually decides by itself what protocol to use to communicate with the card if the card supports both. So my logic...

Ways of generating a digital signature with .NET Framework


.net,visual-c++,c++-cli,smartcard,sign
What's the other way of creating a digital signature using a private key (that's on a smart card, with certificate installed in local certificate store) in .NET Framework other than this one, for I have no clue as to how to find out the key container name (and it seems...

Digitaly link between app and WP


security,windows-phone-8,digital-signature
I have a WebAPI web service which works under https protocol and WP8 app which gets the data from this web service. I have been ask to digitally sign the app with web service. I'm not sure how can I do it or is it even possible but the idea...

Digital Signature in java / android (RSA keys)


java,android,encryption,rsa,digital-signature
I would like to generate a digital signature in my java/android project with a private key(RSA) stored in DB. My 2 keys was generated with the below code (project is in production and I cannot change it): // Get keys pair (RSA) KeyPair rsaKyePair = createKeyPair(); // Get private/ public...

Difference between Sha1WithRSA and Sha1WithRSAEncryption?


java,rsa,digital-signature,sha1,public-key-encryption
I've noticed these two algorithms that have different names, but functionally sound very similar. When I looked further, I've found out Sha1WithRSA is available across many Security Providers, but Sha1WithRSAEncryption is only available if I use the BouncyCastle Provider. Example: JcaContentSignerBuilder("SHA1WithRSAEncryption").setProvider("BC") Is it replacable with JcaContentSignerBuilder("SHA1WithRSA").setProvider("SunJSSE") ? Can someone throw...

SignedXml Compute Signature with SHA256


c#,xml,encryption,digital-signature
I am trying to digitally sign a XML document using SHA256. I am trying to use Security.Cryptography.dll for this. Here is my code - CryptoConfig.AddAlgorithm(typeof(RSAPKCS1SHA256SignatureDescription),"http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"); X509Certificate2 cert = new X509Certificate2(@"location of pks file", "password"); XmlDocument doc = new XmlDocument(); doc.PreserveWhitespace = true; doc.Load(@"input.xml"); SignedXml signedXml = new SignedXml(doc); signedXml.SigningKey =...

Smartcard PKCS11 AES Key Gen Failure


python,aes,smartcard,pkcs11
I am attempting to create an AES 256 key on an ACOS5-64 smartcard and OMNIKEY 3121 card reader, using PKCS11 in python (using the PyKCS11 library). So far, all the "standard" operations seem to work with regards to asymmetric crypto. I have run plenty of code samples and pkcs11-tool commands,...

verifying digital signature in c#


c#,.net,certificate,digital-signature,x509certificate
I have a signed "DLL" file that I want to validate his digital signature in run time ("Before I'm loading it") I have the public key of the certificate embedded in my code, Is there a way to get the "message digest" from the digital signature? or another way to...

Performance measures : Java vs JavaCard [closed]


java,performance,time,smartcard,javacard
I implemented two algorithms in Java. To compare their effectiveness, I call each function 1000 times and compare the execution time (using System.currentTimeMillis()). It needs 2500 ms to execute the first one and 1300 ms for the second one. With these results, I thought have a significative difference (in term...

Extended APDUs and T=0/1 communication protocols


smartcard,javacard,smartcard-reader,globalplatform,pyscard
I have a JCOP V2.4.2 R3 java card that it is mentioned in its datasheet "The card support both T=1 and T=0 communication protocols" I have also an ACR38 smart card reader that it support both T=0 and T=1 protocols. (I have T=0 communication with one card successfully and T=1...

Change PIN of a Gemalto Smartcard through a script


smartcard,gemalto
We have to use the Gemalto IDPrime .Net card Smartcard. We get these USB Dongles and have to change the PIN. Gemalto says via windows: From the Start menu, choose Run and type PINTool. Insert a IDPrime .Net card in the reader as prompted, and click OK. The change PIN...

Working with EVP and OpenSSL, coding in C


c,encryption,openssl,digital-signature,evp-cipher
I've seen many questions on OpenSSL and EVP, but not very many clear answers, but I figured I'd still post my question here and hope for better feedback. The materials given to me are a signed file "symmetrickey.bin", an RSA key set "privatekey_A.pem", "publickey_A.pem", and the other user's public key...

I need to read the values in an XML signed from java


dom,digital-signature
I have a signed XML document, I can validate the signature, what interests me is to get certain fields with which to work: <?xml version="1.0" encoding="UTF-8"?> <inicio> <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"> <ds:SignedInfo> <ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/> <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/> <ds:Reference URI="#1500-Fri Jan 30 19:11:38 BOT 2015"> <ds:Transforms>...

Smart cards and their files


smartcard,javacard
As far as you know, we can list the applets that reside in a java card using tools such as GlobalPlafromPro as follow: GP: gp -list AID: A000000003000000 (|........|) ISD OP_READY: Security Domain, Card lock, Card terminate, Default selected CVM (PIN) management AID: 010203040506 (|......|) App SELECTABLE: (none) AID: 0102030405...

Signing and verifying with java android


java,android,rsa,digital-signature,public-key-encryption
I'm trying to sign an encrypted message with a private key and verify it in Java. This is my first time working with encryption and signatures so I'm not sure how it is supposed to work and I'm kind of stuck here. The verification always returns false. I am posting...

Is this a bug in Transaction mechanism in javacards?


transactions,smartcard,javacard
I wrote the below program and upload it on my card : package transactionMechanismBugCheck; import javacard.framework.APDU; import javacard.framework.Applet; import javacard.framework.ISOException; import javacard.framework.JCSystem; public class TransactionMechanismBugCheck extends Applet { short[] arrayS; byte[] arrayB; private TransactionMechanismBugCheck() { } public static void install(byte bArray[], short bOffset, byte bLength) throws ISOException { new TransactionMechanismBugCheck().register();...

Digital Signature verfication,The selected certificate has errors not valid for usage [closed]


java,digital-signature
Invalid Signature Error when trying to validate signed PDF?

Loading raw 64-byte long ECDSA public key in Java


java,cryptography,digital-signature,public-key,ecdsa
I have a raw (r,s) format ECDSA NIST P-256 public key. It seems that there is no simple way to load it into an object that implements java.security.interfaces.ECPublicKey. What is the cleanest way to load a 64 byte public key so that it can be used to check signatures?...

How to change master key of DESfire cards?What is deciphered key?


smartcard,mifare
I want to change the master key of a DESfire card. I read mifare DESFire datasheet already, but as I am new in this field, I couldn't understand it. It is explained how to Change the Key at page 37 of above document. Can anybody give me an example or...

NDK application Signature Check


java,android,security,android-ndk,digital-signature
I have some security key in an application. I want to store it securly. I like to store it in a native shared library (maybe generated from some code). After that I want it to be returned by a method that will check the signature of the original APK. So...

How can I extract an X509 certificate from a smart card using Java?


java,smartcard
I use the OmniKey 3121 reader and can use the javax.smartcardio API to send APDU commands to the card reader. I'm not sure if there is a standard way to access the certificate stored on the card. Pointers to example code to read the certificate data would greatly help. Also,...

JavaCard applet emulating DESFireEV1


smartcard,javacard,mifare,emulation
My question is simple: is there any existing opensource JavaCard applet emulating the functionality of Mifare DESFireEV1? (the API would have to be a little modified, of course, Select Application 112233 would be for example 80 5A 00 00 03 33 22 11 00 instead of native DESFire command 5A...

How to show digital signature VALUE in signature appearance of PDF file with iTextSharp


c#,itextsharp,digital-signature
Is it possible to append Digital signature VALUE in signature appearance while signing document, because I am using this part of code PdfFilename = txtFile.Text + "\\" + (string)chkLista.Items[i]; DestPdfFilename = txtFile.Text + "\\" + (string)chkLista.Items[i] + "-signed.pdf"; Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser(); Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(cert.RawData)...

Determine Facility Code and Card Number from ATR in C#


c#,.net,bytearray,smartcard,winscard
I have the following card reader HID Omnikey 5325. I have a contact-less card named HIS Proximity. The number written on this card is 133593 42101044091-3. By reading the card, I get the following ATR hex: 3B050002F10673 Using the folowing applications I have managed to see the following information. I...

Sending signature data in response APDU - Java Card


digital-signature,smartcard,javacard,apdu,ecdsa
I would like to sign some data (the MESSAGE byte array) on my Java Card and then return the signature in a response APDU. My code works fine (or at least I think it does and it returns 9000) without the line apdu.sendBytes(BAS, sSignLen), but when I uncomment it I...