22
33import static com .google .common .primitives .Bytes .concat ;
44
5- import android .util .Pair ;
65import android .util .SparseArray ;
76
8- import com .google .common .base .Charsets ;
97import com .google .common .primitives .Bytes ;
108
119import java .io .ByteArrayOutputStream ;
1210import java .io .IOException ;
11+ import java .nio .charset .StandardCharsets ;
1312import java .util .Arrays ;
1413import java .util .HashMap ;
1514import java .util .Map ;
15+ import java .util .Objects ;
1616
1717import ee .ria .DigiDoc .smartcardreader .ApduResponseException ;
1818import ee .ria .DigiDoc .smartcardreader .SmartCardReader ;
1919import ee .ria .DigiDoc .smartcardreader .SmartCardReaderException ;
2020
2121class ID1 implements Token {
22-
23- private static final Map <CertificateType , Pair <Byte , Byte >> CERT_MAP = new HashMap <>();
22+ private static final Map <CertificateType , byte []> CERT_MAP = new HashMap <>();
2423 static {
25- CERT_MAP .put (CertificateType .AUTHENTICATION , new Pair <>(( byte ) 0xF1 , (byte ) 0x01 ) );
26- CERT_MAP .put (CertificateType .SIGNING , new Pair <>( (byte ) 0xF2 , (byte ) 0x1F ) );
24+ CERT_MAP .put (CertificateType .AUTHENTICATION , new byte [] {( byte ) 0xAD , (byte ) 0xF1 , 0x34 , 0x01 } );
25+ CERT_MAP .put (CertificateType .SIGNING , new byte [] {( byte ) 0xAD , (byte ) 0xF2 , 0x34 , (byte ) 0x1F } );
2726 }
2827
2928 private static final Map <CodeType , Byte > PIN_MAP = new HashMap <>();
@@ -52,19 +51,18 @@ public PersonalData personalData() throws SmartCardReaderException {
5251 reader .transmit (0x00 , 0xA4 , 0x01 , 0x0C , new byte [] {0x50 , 0x00 }, null );
5352 SparseArray <String > data = new SparseArray <>();
5453 for (int i = 1 ; i <= 8 ; i ++) {
55- reader .transmit (0x00 , 0xA4 , 0x01 , 0x0C , new byte [] {0x50 , (byte ) i }, null );
54+ reader .transmit (0x00 , 0xA4 , 0x02 , 0x0C , new byte [] {0x50 , (byte ) i }, null );
5655 byte [] record = reader .transmit (0x00 , 0xB0 , 0x00 , 0x00 , null , 0x00 );
57- data .put (i , new String (record , Charsets .UTF_8 ).trim ());
56+ data .put (i , new String (record , StandardCharsets .UTF_8 ).trim ());
5857 }
5958 return ID1PersonalDataParser .parse (data );
6059 }
6160
6261 @ Override
6362 public byte [] certificate (CertificateType type ) throws SmartCardReaderException {
6463 selectMainAid ();
65- reader .transmit (0x00 , 0xA4 , 0x00 , 0x0C , null , null );
66- reader .transmit (0x00 , 0xA4 , 0x01 , 0x0C , new byte [] {(byte ) 0xAD , CERT_MAP .get (type ).first }, null );
67- reader .transmit (0x00 , 0xA4 , 0x01 , 0x0C , new byte [] {0x34 , CERT_MAP .get (type ).second }, null );
64+ reader .transmit (0x00 , 0xA4 , 0x09 , 0x0C , CERT_MAP .get (type ), null );
65+
6866 ByteArrayOutputStream stream = new ByteArrayOutputStream ();
6967 while (true ) {
7068 try {
@@ -89,65 +87,54 @@ public int codeRetryCounter(CodeType type) throws SmartCardReaderException {
8987 } else {
9088 selectMainAid ();
9189 }
92- return reader .transmit (0x00 , 0xCB , 0x3F , 0xFF , new byte [] {0x4D , 0x08 , 0x70 , 0x06 , (byte ) 0xBF , (byte ) 0x81 , PIN_MAP .get (type ), 0x02 , (byte ) 0xA0 , (byte ) 0x80 }, 0x00 )[13 ];
90+ return reader .transmit (0x00 , 0xCB , 0x3F , 0xFF , new byte [] {0x4D , 0x08 , 0x70 , 0x06 , (byte ) 0xBF , (byte ) 0x81 , Objects . requireNonNull ( PIN_MAP .get (type ) ), 0x02 , (byte ) 0xA0 , (byte ) 0x80 }, 0x00 )[13 ];
9391 }
9492
9593 @ Override
9694 public void changeCode (CodeType type , byte [] currentCode , byte [] newCode ) throws SmartCardReaderException {
97- verifyCode (type , currentCode );
9895 if (type .equals (CodeType .PIN2 )) {
9996 selectQSCDAid ();
10097 } else {
10198 selectMainAid ();
10299 }
103- reader .transmit (0x00 , 0x24 , 0x00 , VERIFY_PIN_MAP .get (type ), Bytes .concat (code (currentCode ), code (newCode )), null );
100+ verifyCode (type , currentCode );
101+ reader .transmit (0x00 , 0x24 , 0x00 , Objects .requireNonNull (VERIFY_PIN_MAP .get (type )), Bytes .concat (code (currentCode ), code (newCode )), null );
104102 }
105103
106104 @ Override
107105 public void unblockAndChangeCode (byte [] pukCode , CodeType type , byte [] newCode ) throws SmartCardReaderException {
108106 verifyCode (CodeType .PUK , pukCode );
109- // block code if not yet blocked
110- while (codeRetryCounter (type ) != 0 ) {
111- try {
112- verifyCode (type , new byte [] {(byte ) 0xFF });
113- } catch (CodeVerificationException ignored ) {}
114- }
115107 if (type .equals (CodeType .PIN2 )) {
116108 selectQSCDAid ();
117109 }
118- reader .transmit (0x00 , 0x2C , 0x02 , VERIFY_PIN_MAP .get (type ), code (newCode ), null );
110+ reader .transmit (0x00 , 0x2C , 0x02 , Objects . requireNonNull ( VERIFY_PIN_MAP .get (type ) ), code (newCode ), null );
119111 }
120112
121113 @ Override
122- public byte [] calculateSignature (byte [] pin2 , byte [] hash , boolean ecc ) throws SmartCardReaderException , IllegalStateException {
114+ public byte [] calculateSignature (byte [] pin2 , byte [] hash , boolean ecc ) throws SmartCardReaderException {
115+ selectQSCDAid ();
123116 if (null != pin2 && pin2 .length > 0 ) {
124- verifyCode (CodeType .PIN2 , pin2 );
117+ verifyCode (CodeType .PIN2 , pin2 );
125118 Arrays .fill (pin2 , (byte ) 0 );
126119 } else {
127120 throw new IllegalStateException ("PIN2 is null or empty" );
128121 }
129- selectQSCDAid ();
130122 reader .transmit (0x00 , 0x22 , 0x41 , 0xB6 , new byte [] {(byte ) 0x80 , 0x04 , (byte ) 0xFF , 0x15 , 0x08 , 0x00 , (byte ) 0x84 , 0x01 , (byte ) 0x9F }, null );
131123 return reader .transmit (0x00 , 0x2A , 0x9E , 0x9A , padWithZeroes (hash ), 0x00 );
132124 }
133125
134126 @ Override
135127 public byte [] decrypt (byte [] pin1 , byte [] data , boolean ecc ) throws SmartCardReaderException {
128+ selectOberthurAid ();
136129 byte [] prefix = new byte [] {0x00 };
137130 verifyCode (CodeType .PIN1 , pin1 );
138- selectOberthurAid ();
139131 reader .transmit (0x00 , 0x22 , 0x41 , 0xB8 , new byte [] {(byte ) 0x80 , 0x04 , (byte ) 0xFF , 0x30 , 0x04 , 0x00 , (byte ) 0x84 , 0x01 , (byte ) 0x81 }, null );
140132 return reader .transmit (0x00 , 0x2A , 0x80 , 0x86 , concat (prefix , data ), 0x00 );
141133 }
142134
143135 private void verifyCode (CodeType type , byte [] code ) throws SmartCardReaderException {
144- if (type .equals (CodeType .PIN2 )) {
145- selectQSCDAid ();
146- } else {
147- selectMainAid ();
148- }
149136 try {
150- reader .transmit (0x00 , 0x20 , 0x00 , VERIFY_PIN_MAP .get (type ), code (code ), null );
137+ reader .transmit (0x00 , 0x20 , 0x00 , Objects . requireNonNull ( VERIFY_PIN_MAP .get (type ) ), code (code ), null );
151138 } catch (ApduResponseException e ) {
152139 if (e .sw1 == 0x63 || (e .sw1 == 0x69 && e .sw2 == (byte ) 0x83 )) {
153140 throw new CodeVerificationException (type );
0 commit comments