@@ -58,6 +58,9 @@ private final void resetKeys(final boolean isRegistering) {
5858 keys .getPrivate ().clearKey ();
5959 keys .getPublic ().clearKey ();
6060 keys = null ;
61+ if (!isRegistering ) {
62+ Common .requestDeletion ();
63+ }
6164 }
6265
6366 if (certificate_length > 0 ) {
@@ -205,10 +208,13 @@ protected final ECParams ecParams(final ECCurves ec) {
205208
206209
207210 private final KeyPair generateRSA () {
208- final PrivateKey priv = (PrivateKey )KeyBuilder .buildKey (KeyBuilder .TYPE_RSA_CRT_PRIVATE , rsaModulusBitSize (), false );
209- final RSAPublicKey pub = (RSAPublicKey )KeyBuilder .buildKey (KeyBuilder .TYPE_RSA_PUBLIC , rsaModulusBitSize (), false );
211+ PrivateKey priv = (PrivateKey )KeyBuilder .buildKey (KeyBuilder .TYPE_RSA_CRT_PRIVATE , rsaModulusBitSize (), false );
212+ RSAPublicKey pub = (RSAPublicKey )KeyBuilder .buildKey (KeyBuilder .TYPE_RSA_PUBLIC , rsaModulusBitSize (), false );
210213
211214 if ((priv == null ) || (pub == null )) {
215+ priv = null ;
216+ pub = null ;
217+ Common .requestDeletion ();
212218 return null ;
213219 }
214220
@@ -219,13 +225,16 @@ private final KeyPair generateRSA() {
219225
220226
221227 private final KeyPair generateEC (final ECCurves ec ) {
228+ ECParams params = ecParams (ec );
222229
223- final ECParams params = ecParams (ec );
224-
225- final ECPrivateKey priv = (ECPrivateKey )KeyBuilder .buildKey (KeyBuilder .TYPE_EC_FP_PRIVATE , params .nb_bits , false );
226- final ECPublicKey pub = (ECPublicKey )KeyBuilder .buildKey (KeyBuilder .TYPE_EC_FP_PUBLIC , params .nb_bits , false );
230+ ECPrivateKey priv = (ECPrivateKey )KeyBuilder .buildKey (KeyBuilder .TYPE_EC_FP_PRIVATE , params .nb_bits , false );
231+ ECPublicKey pub = (ECPublicKey )KeyBuilder .buildKey (KeyBuilder .TYPE_EC_FP_PUBLIC , params .nb_bits , false );
227232
228233 if ((priv == null ) || (pub == null )) {
234+ params = null ;
235+ priv = null ;
236+ pub = null ;
237+ Common .requestDeletion ();
229238 return null ;
230239 }
231240
@@ -237,30 +246,29 @@ private final KeyPair generateEC(final ECCurves ec) {
237246
238247
239248 protected final void generate (final ECCurves ec ) {
240-
241- KeyPair nkeys = null ;
249+ resetKeys (false );
242250
243251 if (isRsa ()) {
244- nkeys = generateRSA ();
252+ keys = generateRSA ();
245253 } else if (isEc ()) {
246- nkeys = generateEC (ec );
254+ keys = generateEC (ec );
247255 }
248256
249- if (nkeys == null ) {
257+ if (keys == null ) {
250258 ISOException .throwIt (ISO7816 .SW_CONDITIONS_NOT_SATISFIED );
251259 return ;
252260 }
253261
254- nkeys .genKeyPair ();
262+ keys .genKeyPair ();
255263
256- if (!nkeys .getPublic ().isInitialized () || !nkeys .getPrivate ().isInitialized ()) {
264+ if (!keys .getPublic ().isInitialized () || !keys .getPrivate ().isInitialized ()) {
265+ keys = null ;
266+ Common .requestDeletion ();
257267 ISOException .throwIt (ISO7816 .SW_CONDITIONS_NOT_SATISFIED );
258268 return ;
259269 }
260270
261- resetKeys (false );
262271 has_been_generated = true ;
263- keys = nkeys ;
264272 }
265273
266274
@@ -416,7 +424,6 @@ private final KeyPair importECKey(final ECCurves ec,
416424
417425 protected final void importKey (final ECCurves ec ,
418426 final byte [] buf , final short boff , final short len ) {
419-
420427 short off = boff ;
421428
422429 short template_len = 0 ;
@@ -486,30 +493,28 @@ protected final void importKey(final ECCurves ec,
486493 }
487494 }
488495
489- KeyPair nkeys = null ;
496+ resetKeys ( false ) ;
490497
491498 if (isRsa ()) {
492- nkeys = importRSAKey (buf , data_off , data_len , data_tag_count , data_tag_val , data_tag_len );
499+ keys = importRSAKey (buf , data_off , data_len , data_tag_count , data_tag_val , data_tag_len );
493500 } else if (isEc ()) {
494- nkeys = importECKey (ec , buf , data_off , data_len , data_tag_count , data_tag_val , data_tag_len );
501+ keys = importECKey (ec , buf , data_off , data_len , data_tag_count , data_tag_val , data_tag_len );
495502 }
496503
497- if (nkeys == null ) {
504+ if (keys == null ) {
498505 ISOException .throwIt (ISO7816 .SW_WRONG_DATA );
499506 return ;
500507 }
501508
502- if (!nkeys .getPrivate ().isInitialized () || !nkeys .getPublic ().isInitialized ()) {
509+ if (!keys .getPrivate ().isInitialized () || !keys .getPublic ().isInitialized ()) {
510+ keys = null ;
511+ Common .requestDeletion ();
503512 return ;
504513 }
505-
506- resetKeys (false );
507- keys = nkeys ;
508514 }
509515
510516
511517 protected final short writePublicKeyDo (final byte [] buf , short off ) {
512-
513518 if (!isInitialized ()) {
514519 ISOException .throwIt (Constants .SW_REFERENCE_DATA_NOT_FOUND );
515520 return 0 ;
@@ -520,7 +525,6 @@ protected final short writePublicKeyDo(final byte[] buf, short off) {
520525 off = Util .setShort (buf , off , (short )0x7f49 );
521526
522527 if (isRsa ()) {
523-
524528 final RSAPublicKey rsapub = (RSAPublicKey )pub ;
525529 final short modulus_size = Common .bitsToBytes (rsaModulusBitSize ());
526530 final short exponent_size = Common .bitsToBytes (rsaExponentBitSize ());
@@ -542,9 +546,7 @@ protected final short writePublicKeyDo(final byte[] buf, short off) {
542546 off += rsapub .getExponent (buf , off );
543547
544548 return off ;
545-
546549 } else if (isEc ()) {
547-
548550 final ECPublicKey ecpub = (ECPublicKey )pub ;
549551 final short qsize = (short )(1 + 2 * (short )((ecpub .getSize () / 8 ) + (((ecpub .getSize () % 8 ) == 0 ) ? 0 : 1 )));
550552 short rsize = (short )(1 + qsize );
@@ -564,7 +566,6 @@ protected final short writePublicKeyDo(final byte[] buf, short off) {
564566 off += ecpub .getW (buf , off );
565567
566568 return off ;
567-
568569 }
569570
570571 ISOException .throwIt (ISO7816 .SW_CONDITIONS_NOT_SATISFIED );
@@ -577,7 +578,6 @@ protected final short writePublicKeyDo(final byte[] buf, short off) {
577578 protected final short sign (final Common common ,
578579 final byte [] buf , final short lc ,
579580 final boolean forAuth ) {
580-
581581 if (!isInitialized ()) {
582582 ISOException .throwIt (Constants .SW_REFERENCE_DATA_NOT_FOUND );
583583 return 0 ;
@@ -590,7 +590,6 @@ protected final short sign(final Common common,
590590 byte [] sha_header = null ;
591591
592592 if (isRsa ()) {
593-
594593 if (!forAuth ) {
595594 if (lc == (short )(2 + Constants .DSI_SHA256_HEADER [1 ])) {
596595 sha_header = Constants .DSI_SHA256_HEADER ;
0 commit comments