src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Key.java
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47216 71c04702a3d5
child 53257 5170dc2bcf64
equal deleted inserted replaced
56541:92cbbfc996f3 56542:56aaa6cb3693
     1 /*
     1 /*
     2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    36 
    36 
    37 import javax.crypto.*;
    37 import javax.crypto.*;
    38 import javax.crypto.interfaces.*;
    38 import javax.crypto.interfaces.*;
    39 import javax.crypto.spec.*;
    39 import javax.crypto.spec.*;
    40 
    40 
       
    41 import sun.security.rsa.RSAUtil.KeyType;
    41 import sun.security.rsa.RSAPublicKeyImpl;
    42 import sun.security.rsa.RSAPublicKeyImpl;
       
    43 import sun.security.rsa.RSAPrivateCrtKeyImpl;
    42 
    44 
    43 import sun.security.internal.interfaces.TlsMasterSecret;
    45 import sun.security.internal.interfaces.TlsMasterSecret;
    44 
    46 
    45 import sun.security.pkcs11.wrapper.*;
    47 import sun.security.pkcs11.wrapper.*;
    46 import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
    48 import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
   523         synchronized byte[] getEncodedInternal() {
   525         synchronized byte[] getEncodedInternal() {
   524             token.ensureValid();
   526             token.ensureValid();
   525             if (encoded == null) {
   527             if (encoded == null) {
   526                 fetchValues();
   528                 fetchValues();
   527                 try {
   529                 try {
   528                     // XXX make constructor in SunRsaSign provider public
   530                     Key newKey = RSAPrivateCrtKeyImpl.newKey
   529                     // and call it directly
   531                         (KeyType.RSA, null, n, e, d, p, q, pe, qe, coeff);
   530                     KeyFactory factory = KeyFactory.getInstance
       
   531                         ("RSA", P11Util.getSunRsaSignProvider());
       
   532                     Key newKey = factory.translateKey(this);
       
   533                     encoded = newKey.getEncoded();
   532                     encoded = newKey.getEncoded();
   534                 } catch (GeneralSecurityException e) {
   533                 } catch (GeneralSecurityException e) {
   535                     throw new ProviderException(e);
   534                     throw new ProviderException(e);
   536                 }
   535                 }
   537             }
   536             }
   621     }
   620     }
   622 
   621 
   623     private static final class P11RSAPublicKey extends P11Key
   622     private static final class P11RSAPublicKey extends P11Key
   624                                                 implements RSAPublicKey {
   623                                                 implements RSAPublicKey {
   625         private static final long serialVersionUID = -826726289023854455L;
   624         private static final long serialVersionUID = -826726289023854455L;
   626 
       
   627         private BigInteger n, e;
   625         private BigInteger n, e;
   628         private byte[] encoded;
   626         private byte[] encoded;
   629         P11RSAPublicKey(Session session, long keyID, String algorithm,
   627         P11RSAPublicKey(Session session, long keyID, String algorithm,
   630                 int keyLength, CK_ATTRIBUTE[] attributes) {
   628                 int keyLength, CK_ATTRIBUTE[] attributes) {
   631             super(PUBLIC, session, keyID, algorithm, keyLength, attributes);
   629             super(PUBLIC, session, keyID, algorithm, keyLength, attributes);
   650         synchronized byte[] getEncodedInternal() {
   648         synchronized byte[] getEncodedInternal() {
   651             token.ensureValid();
   649             token.ensureValid();
   652             if (encoded == null) {
   650             if (encoded == null) {
   653                 fetchValues();
   651                 fetchValues();
   654                 try {
   652                 try {
   655                     encoded = new RSAPublicKeyImpl(n, e).getEncoded();
   653                     encoded = RSAPublicKeyImpl.newKey
       
   654                         (KeyType.RSA, null, n, e).getEncoded();
   656                 } catch (InvalidKeyException e) {
   655                 } catch (InvalidKeyException e) {
   657                     throw new ProviderException(e);
   656                     throw new ProviderException(e);
   658                 }
   657                 }
   659             }
   658             }
   660             return encoded;
   659             return encoded;