BMErEr
BMErEr

Reputation: 151

Smart Card External Authenticate 6982 error

I'm trying to make security communicate with S.A.M.(Secure Access Module) Firstly I send this MSE:SET APDU for External Authenticate:

//83 is my private key's ID. F8 is algorithm identifier 
OutgoingAPDU : 002281A4068001F8840183
ResponseSW1SW2 : 9000 

Before send external auth. documents says encrypt with RSAES-OAEP PKCS #1 so I'm using this openssl command for encryption.

openssl_public_encrypt($dataForEncryption, $output, $publicKey['key'], OPENSSL_PKCS1_OAEP_PADDING);
//$firstPartOfData => first 488 of $output
//$secondPartOfData => last 24 of $output
//total $output is 512

First of all is that true padding for RSAES-OAEP PKCS #1.

And then external auth. APDU commands.

//strlen($firstPartOfData) = 488
OutgoingAPDU : 10820000F4.$firstPartOfData
ResponseSW1SW2 : 9000
//strlen($firstPartOfData) = 24
OutgoingAPDU : 008200000C.$secondPartOfData
ResponseSW1SW2 : 6982

Where am I missing ? Or where is the mistake. I could'n find out the problem.

Upvotes: 0

Views: 1931

Answers (1)

Maarten Bodewes
Maarten Bodewes

Reputation: 93978

RSA / OAEP requires the configuration of a type of Mask Generation Function to create the OAEP padding. This Mask Generation Function type has only one real member: MGF1, so in general this configuration is implicit (i.e. you don't have to configure it yourself). MGF1 itself however allows the user to configure the hash used internally. MGF1 uses SHA-1 by default (in a secure fashion) but it can be configured with SHA-256 or any other hash as well.

In your case OAEP with SHA-256 was used within the smart card, which will result in an error during decryption (the unpadding of the message after modular exponentiation with the private exponent) if you used the SHA-1 default in PHP.

To set the hash correctly you can use phpseclib with the following code:

 $phpsec->setMGFHash('sha256');
 $phpsec->setHash('sha256');
 $phpsec->loadKey($cer["key"]);
 $phpsec->encrypt($plaintext);

The error generated status word 6982 on the smart card. This is a rather badly chosen status word as it means "security conditions not satisfied", which you would expect if the access conditions for the decryption operation are not satisfied. However, ISO/IEC 7816-4 doesn't really specify when the status words should be generated (which is just stupid, but that's how it is).

Upvotes: 1

Related Questions