1 /*
   2  * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.pkcs11;
  27 
  28 import java.security.*;
  29 import java.security.spec.AlgorithmParameterSpec;
  30 import java.security.spec.*;
  31 
  32 import java.util.Locale;
  33 
  34 import javax.crypto.*;
  35 import javax.crypto.spec.*;
  36 
  37 import static sun.security.pkcs11.TemplateManager.*;
  38 import sun.security.pkcs11.wrapper.*;
  39 import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
  40 import sun.security.internal.spec.TlsRsaPremasterSecretParameterSpec;
  41 import sun.security.util.KeyUtil;
  42 
  43 /**
  44  * RSA Cipher implementation class. We currently only support
  45  * PKCS#1 v1.5 padding on top of CKM_RSA_PKCS.
  46  *
  47  * @author  Andreas Sterbenz
  48  * @since   1.5
  49  */
  50 final class P11RSACipher extends CipherSpi {
  51 
  52     // minimum length of PKCS#1 v1.5 padding
  53     private final static int PKCS1_MIN_PADDING_LENGTH = 11;
  54 
  55     // constant byte[] of length 0
  56     private final static byte[] B0 = new byte[0];
  57 
  58     // mode constant for public key encryption
  59     private final static int MODE_ENCRYPT = 1;
  60     // mode constant for private key decryption
  61     private final static int MODE_DECRYPT = 2;
  62     // mode constant for private key encryption (signing)
  63     private final static int MODE_SIGN    = 3;
  64     // mode constant for public key decryption (verifying)
  65     private final static int MODE_VERIFY  = 4;
  66 
  67     // padding type constant for NoPadding
  68     private final static int PAD_NONE = 1;
  69     // padding type constant for PKCS1Padding
  70     private final static int PAD_PKCS1 = 2;
  71 
  72     // token instance
  73     private final Token token;
  74 
  75     // algorithm name (always "RSA")
  76     private final String algorithm;
  77 
  78     // mechanism id
  79     private final long mechanism;
  80 
  81     // associated session, if any
  82     private Session session;
  83 
  84     // mode, one of MODE_* above
  85     private int mode;
  86 
  87     // padding, one of PAD_* above
  88     private int padType;
  89 
  90     private byte[] buffer;
  91     private int bufOfs;
  92 
  93     // key, if init() was called
  94     private P11Key p11Key;
  95 
  96     // flag indicating whether an operation is initialized
  97     private boolean initialized;
  98 
  99     // maximum input data size allowed
 100     // for decryption, this is the length of the key
 101     // for encryption, length of the key minus minimum padding length
 102     private int maxInputSize;
 103 
 104     // maximum output size. this is the length of the key
 105     private int outputSize;
 106 
 107     // cipher parameter for TLS RSA premaster secret
 108     private AlgorithmParameterSpec spec = null;
 109 
 110     // the source of randomness
 111     private SecureRandom random;
 112 
 113     P11RSACipher(Token token, String algorithm, long mechanism)
 114             throws PKCS11Exception {
 115         super();
 116         this.token = token;
 117         this.algorithm = "RSA";
 118         this.mechanism = mechanism;
 119     }
 120 
 121     // modes do not make sense for RSA, but allow ECB
 122     // see JCE spec
 123     protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
 124         if (mode.equalsIgnoreCase("ECB") == false) {
 125             throw new NoSuchAlgorithmException("Unsupported mode " + mode);
 126         }
 127     }
 128 
 129     protected void engineSetPadding(String padding)
 130             throws NoSuchPaddingException {
 131         String lowerPadding = padding.toLowerCase(Locale.ENGLISH);
 132         if (lowerPadding.equals("pkcs1padding")) {
 133             padType = PAD_PKCS1;
 134         } else if (lowerPadding.equals("nopadding")) {
 135             padType = PAD_NONE;
 136         } else {
 137             throw new NoSuchPaddingException("Unsupported padding " + padding);
 138         }
 139     }
 140 
 141     // return 0 as block size, we are not a block cipher
 142     // see JCE spec
 143     protected int engineGetBlockSize() {
 144         return 0;
 145     }
 146 
 147     // return the output size
 148     // see JCE spec
 149     protected int engineGetOutputSize(int inputLen) {
 150         return outputSize;
 151     }
 152 
 153     // no IV, return null
 154     // see JCE spec
 155     protected byte[] engineGetIV() {
 156         return null;
 157     }
 158 
 159     // no parameters, return null
 160     // see JCE spec
 161     protected AlgorithmParameters engineGetParameters() {
 162         return null;
 163     }
 164 
 165     // see JCE spec
 166     protected void engineInit(int opmode, Key key, SecureRandom random)
 167             throws InvalidKeyException {
 168         implInit(opmode, key);
 169     }
 170 
 171     // see JCE spec
 172     @SuppressWarnings("deprecation")
 173     protected void engineInit(int opmode, Key key,
 174             AlgorithmParameterSpec params, SecureRandom random)
 175             throws InvalidKeyException, InvalidAlgorithmParameterException {
 176         if (params != null) {
 177             if (!(params instanceof TlsRsaPremasterSecretParameterSpec)) {
 178                 throw new InvalidAlgorithmParameterException(
 179                         "Parameters not supported");
 180             }
 181             spec = params;
 182             this.random = random;   // for TLS RSA premaster secret
 183         }
 184         implInit(opmode, key);
 185     }
 186 
 187     // see JCE spec
 188     protected void engineInit(int opmode, Key key, AlgorithmParameters params,
 189             SecureRandom random)
 190             throws InvalidKeyException, InvalidAlgorithmParameterException {
 191         if (params != null) {
 192             throw new InvalidAlgorithmParameterException(
 193                         "Parameters not supported");
 194         }
 195         implInit(opmode, key);
 196     }
 197 
 198     private void implInit(int opmode, Key key) throws InvalidKeyException {
 199         cancelOperation();
 200         p11Key = P11KeyFactory.convertKey(token, key, algorithm);
 201         boolean encrypt;
 202         if (opmode == Cipher.ENCRYPT_MODE) {
 203             encrypt = true;
 204         } else if (opmode == Cipher.DECRYPT_MODE) {
 205             encrypt = false;
 206         } else if (opmode == Cipher.WRAP_MODE) {
 207             if (p11Key.isPublic() == false) {
 208                 throw new InvalidKeyException
 209                                 ("Wrap has to be used with public keys");
 210             }
 211             // No further setup needed for C_Wrap(). We'll initialize later if
 212             // we can't use C_Wrap().
 213             return;
 214         } else if (opmode == Cipher.UNWRAP_MODE) {
 215             if (p11Key.isPrivate() == false) {
 216                 throw new InvalidKeyException
 217                                 ("Unwrap has to be used with private keys");
 218             }
 219             // No further setup needed for C_Unwrap(). We'll initialize later
 220             // if we can't use C_Unwrap().
 221             return;
 222         } else {
 223             throw new InvalidKeyException("Unsupported mode: " + opmode);
 224         }
 225         if (p11Key.isPublic()) {
 226             mode = encrypt ? MODE_ENCRYPT : MODE_VERIFY;
 227         } else if (p11Key.isPrivate()) {
 228             mode = encrypt ? MODE_SIGN : MODE_DECRYPT;
 229         } else {
 230             throw new InvalidKeyException("Unknown key type: " + p11Key);
 231         }
 232         int n = (p11Key.length() + 7) >> 3;
 233         outputSize = n;
 234         buffer = new byte[n];
 235         maxInputSize = ((padType == PAD_PKCS1 && encrypt) ?
 236                             (n - PKCS1_MIN_PADDING_LENGTH) : n);
 237         try {
 238             initialize();
 239         } catch (PKCS11Exception e) {
 240             throw new InvalidKeyException("init() failed", e);
 241         }
 242     }
 243 
 244     private void cancelOperation() {
 245         token.ensureValid();
 246         if (initialized == false) {
 247             return;
 248         }
 249         initialized = false;
 250         if ((session == null) || (token.explicitCancel == false)) {
 251             return;
 252         }
 253         if (session.hasObjects() == false) {
 254             session = token.killSession(session);
 255             return;
 256         }
 257         try {
 258             PKCS11 p11 = token.p11;
 259             int inLen = maxInputSize;
 260             int outLen = buffer.length;
 261             switch (mode) {
 262             case MODE_ENCRYPT:
 263                 p11.C_Encrypt
 264                         (session.id(), buffer, 0, inLen, buffer, 0, outLen);
 265                 break;
 266             case MODE_DECRYPT:
 267                 p11.C_Decrypt
 268                         (session.id(), buffer, 0, inLen, buffer, 0, outLen);
 269                 break;
 270             case MODE_SIGN:
 271                 byte[] tmpBuffer = new byte[maxInputSize];
 272                 p11.C_Sign
 273                         (session.id(), tmpBuffer);
 274                 break;
 275             case MODE_VERIFY:
 276                 p11.C_VerifyRecover
 277                         (session.id(), buffer, 0, inLen, buffer, 0, outLen);
 278                 break;
 279             default:
 280                 throw new ProviderException("internal error");
 281             }
 282         } catch (PKCS11Exception e) {
 283             // XXX ensure this always works, ignore error
 284         }
 285     }
 286 
 287     private void ensureInitialized() throws PKCS11Exception {
 288         token.ensureValid();
 289         if (initialized == false) {
 290             initialize();
 291         }
 292     }
 293 
 294     private void initialize() throws PKCS11Exception {
 295         if (session == null) {
 296             session = token.getOpSession();
 297         }
 298         PKCS11 p11 = token.p11;
 299         CK_MECHANISM ckMechanism = new CK_MECHANISM(mechanism);
 300         switch (mode) {
 301         case MODE_ENCRYPT:
 302             p11.C_EncryptInit(session.id(), ckMechanism, p11Key.keyID);
 303             break;
 304         case MODE_DECRYPT:
 305             p11.C_DecryptInit(session.id(), ckMechanism, p11Key.keyID);
 306             break;
 307         case MODE_SIGN:
 308             p11.C_SignInit(session.id(), ckMechanism, p11Key.keyID);
 309             break;
 310         case MODE_VERIFY:
 311             p11.C_VerifyRecoverInit(session.id(), ckMechanism, p11Key.keyID);
 312             break;
 313         default:
 314             throw new AssertionError("internal error");
 315         }
 316         bufOfs = 0;
 317         initialized = true;
 318     }
 319 
 320     private void implUpdate(byte[] in, int inOfs, int inLen) {
 321         try {
 322             ensureInitialized();
 323         } catch (PKCS11Exception e) {
 324             throw new ProviderException("update() failed", e);
 325         }
 326         if ((inLen == 0) || (in == null)) {
 327             return;
 328         }
 329         if (bufOfs + inLen > maxInputSize) {
 330             bufOfs = maxInputSize + 1;
 331             return;
 332         }
 333         System.arraycopy(in, inOfs, buffer, bufOfs, inLen);
 334         bufOfs += inLen;
 335     }
 336 
 337     private int implDoFinal(byte[] out, int outOfs, int outLen)
 338             throws BadPaddingException, IllegalBlockSizeException {
 339         if (bufOfs > maxInputSize) {
 340             throw new IllegalBlockSizeException("Data must not be longer "
 341                 + "than " + maxInputSize + " bytes");
 342         }
 343         try {
 344             ensureInitialized();
 345             PKCS11 p11 = token.p11;
 346             int n;
 347             switch (mode) {
 348             case MODE_ENCRYPT:
 349                 n = p11.C_Encrypt
 350                         (session.id(), buffer, 0, bufOfs, out, outOfs, outLen);
 351                 break;
 352             case MODE_DECRYPT:
 353                 n = p11.C_Decrypt
 354                         (session.id(), buffer, 0, bufOfs, out, outOfs, outLen);
 355                 break;
 356             case MODE_SIGN:
 357                 byte[] tmpBuffer = new byte[bufOfs];
 358                 System.arraycopy(buffer, 0, tmpBuffer, 0, bufOfs);
 359                 tmpBuffer = p11.C_Sign(session.id(), tmpBuffer);
 360                 if (tmpBuffer.length > outLen) {
 361                     throw new BadPaddingException("Output buffer too small");
 362                 }
 363                 System.arraycopy(tmpBuffer, 0, out, outOfs, tmpBuffer.length);
 364                 n = tmpBuffer.length;
 365                 break;
 366             case MODE_VERIFY:
 367                 n = p11.C_VerifyRecover
 368                         (session.id(), buffer, 0, bufOfs, out, outOfs, outLen);
 369                 break;
 370             default:
 371                 throw new ProviderException("internal error");
 372             }
 373             return n;
 374         } catch (PKCS11Exception e) {
 375             throw (BadPaddingException)new BadPaddingException
 376                 ("doFinal() failed").initCause(e);
 377         } finally {
 378             initialized = false;
 379             session = token.releaseSession(session);
 380         }
 381     }
 382 
 383     // see JCE spec
 384     protected byte[] engineUpdate(byte[] in, int inOfs, int inLen) {
 385         implUpdate(in, inOfs, inLen);
 386         return B0;
 387     }
 388 
 389     // see JCE spec
 390     protected int engineUpdate(byte[] in, int inOfs, int inLen,
 391             byte[] out, int outOfs) throws ShortBufferException {
 392         implUpdate(in, inOfs, inLen);
 393         return 0;
 394     }
 395 
 396     // see JCE spec
 397     protected byte[] engineDoFinal(byte[] in, int inOfs, int inLen)
 398             throws IllegalBlockSizeException, BadPaddingException {
 399         implUpdate(in, inOfs, inLen);
 400         int n = implDoFinal(buffer, 0, buffer.length);
 401         byte[] out = new byte[n];
 402         System.arraycopy(buffer, 0, out, 0, n);
 403         return out;
 404     }
 405 
 406     // see JCE spec
 407     protected int engineDoFinal(byte[] in, int inOfs, int inLen,
 408             byte[] out, int outOfs) throws ShortBufferException,
 409             IllegalBlockSizeException, BadPaddingException {
 410         implUpdate(in, inOfs, inLen);
 411         return implDoFinal(out, outOfs, out.length - outOfs);
 412     }
 413 
 414     private byte[] doFinal() throws BadPaddingException,
 415             IllegalBlockSizeException {
 416         byte[] t = new byte[2048];
 417         int n = implDoFinal(t, 0, t.length);
 418         byte[] out = new byte[n];
 419         System.arraycopy(t, 0, out, 0, n);
 420         return out;
 421     }
 422 
 423     // see JCE spec
 424     protected byte[] engineWrap(Key key) throws InvalidKeyException,
 425             IllegalBlockSizeException {
 426         String keyAlg = key.getAlgorithm();
 427         P11Key sKey = null;
 428         try {
 429             // The conversion may fail, e.g. trying to wrap an AES key on
 430             // a token that does not support AES, or when the key size is
 431             // not within the range supported by the token.
 432             sKey = P11SecretKeyFactory.convertKey(token, key, keyAlg);
 433         } catch (InvalidKeyException ike) {
 434             byte[] toBeWrappedKey = key.getEncoded();
 435             if (toBeWrappedKey == null) {
 436                 throw new InvalidKeyException
 437                         ("wrap() failed, no encoding available", ike);
 438             }
 439             // Directly encrypt the key encoding when key conversion failed
 440             implInit(Cipher.ENCRYPT_MODE, p11Key);
 441             implUpdate(toBeWrappedKey, 0, toBeWrappedKey.length);
 442             try {
 443                 return doFinal();
 444             } catch (BadPaddingException bpe) {
 445                 // should not occur
 446                 throw new InvalidKeyException("wrap() failed", bpe);
 447             } finally {
 448                 // Restore original mode
 449                 implInit(Cipher.WRAP_MODE, p11Key);
 450             }
 451         }
 452         Session s = null;
 453         try {
 454             s = token.getOpSession();
 455             return token.p11.C_WrapKey(s.id(), new CK_MECHANISM(mechanism),
 456                 p11Key.keyID, sKey.keyID);
 457         } catch (PKCS11Exception e) {
 458             throw new InvalidKeyException("wrap() failed", e);
 459         } finally {
 460             token.releaseSession(s);
 461         }
 462     }
 463 
 464     // see JCE spec
 465     @SuppressWarnings("deprecation")
 466     protected Key engineUnwrap(byte[] wrappedKey, String algorithm,
 467             int type) throws InvalidKeyException, NoSuchAlgorithmException {
 468 
 469         boolean isTlsRsaPremasterSecret =
 470                 algorithm.equals("TlsRsaPremasterSecret");
 471         Exception failover = null;
 472 
 473         SecureRandom secureRandom = random;
 474         if (secureRandom == null && isTlsRsaPremasterSecret) {
 475             secureRandom = new SecureRandom();
 476         }
 477 
 478         // Should C_Unwrap be preferred for non-TLS RSA premaster secret?
 479         if (token.supportsRawSecretKeyImport()) {
 480             // XXX implement unwrap using C_Unwrap() for all keys
 481             implInit(Cipher.DECRYPT_MODE, p11Key);
 482             if (wrappedKey.length > maxInputSize) {
 483                 throw new InvalidKeyException("Key is too long for unwrapping");
 484             }
 485 
 486             byte[] encoded = null;
 487             implUpdate(wrappedKey, 0, wrappedKey.length);
 488             try {
 489                 encoded = doFinal();
 490             } catch (BadPaddingException e) {
 491                 if (isTlsRsaPremasterSecret) {
 492                     failover = e;
 493                 } else {
 494                     throw new InvalidKeyException("Unwrapping failed", e);
 495                 }
 496             } catch (IllegalBlockSizeException e) {
 497                 // should not occur, handled with length check above
 498                 throw new InvalidKeyException("Unwrapping failed", e);
 499             }
 500 
 501             if (isTlsRsaPremasterSecret) {
 502                 if (!(spec instanceof TlsRsaPremasterSecretParameterSpec)) {
 503                     throw new IllegalStateException(
 504                             "No TlsRsaPremasterSecretParameterSpec specified");
 505                 }
 506 
 507                 // polish the TLS premaster secret
 508                 TlsRsaPremasterSecretParameterSpec psps =
 509                         (TlsRsaPremasterSecretParameterSpec)spec;
 510                 encoded = KeyUtil.checkTlsPreMasterSecretKey(
 511                         psps.getClientVersion(), psps.getServerVersion(),
 512                         secureRandom, encoded, (failover != null));
 513             }
 514 
 515             return ConstructKeys.constructKey(encoded, algorithm, type);
 516         } else {
 517             Session s = null;
 518             SecretKey secretKey = null;
 519             try {
 520                 try {
 521                     s = token.getObjSession();
 522                     long keyType = CKK_GENERIC_SECRET;
 523                     CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
 524                             new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY),
 525                             new CK_ATTRIBUTE(CKA_KEY_TYPE, keyType),
 526                         };
 527                     attributes = token.getAttributes(
 528                             O_IMPORT, CKO_SECRET_KEY, keyType, attributes);
 529                     long keyID = token.p11.C_UnwrapKey(s.id(),
 530                             new CK_MECHANISM(mechanism), p11Key.keyID,
 531                             wrappedKey, attributes);
 532                     secretKey = P11Key.secretKey(s, keyID,
 533                             algorithm, 48 << 3, attributes);
 534                 } catch (PKCS11Exception e) {
 535                     if (isTlsRsaPremasterSecret) {
 536                         failover = e;
 537                     } else {
 538                         throw new InvalidKeyException("unwrap() failed", e);
 539                     }
 540                 }
 541 
 542                 if (isTlsRsaPremasterSecret) {
 543                     byte[] replacer = new byte[48];
 544                     if (failover == null) {
 545                         // Does smart compiler dispose this operation?
 546                         secureRandom.nextBytes(replacer);
 547                     }
 548 
 549                     TlsRsaPremasterSecretParameterSpec psps =
 550                             (TlsRsaPremasterSecretParameterSpec)spec;
 551 
 552                     // Please use the tricky failover and replacer byte array
 553                     // as the parameters so that smart compiler won't dispose
 554                     // the unused variable .
 555                     secretKey = polishPreMasterSecretKey(token, s,
 556                             failover, replacer, secretKey,
 557                             psps.getClientVersion(), psps.getServerVersion());
 558                 }
 559 
 560                 return secretKey;
 561             } finally {
 562                 token.releaseSession(s);
 563             }
 564         }
 565     }
 566 
 567     // see JCE spec
 568     protected int engineGetKeySize(Key key) throws InvalidKeyException {
 569         int n = P11KeyFactory.convertKey(token, key, algorithm).length();
 570         return n;
 571     }
 572 
 573     private static SecretKey polishPreMasterSecretKey(
 574             Token token, Session session,
 575             Exception failover, byte[] replacer, SecretKey secretKey,
 576             int clientVersion, int serverVersion) {
 577 
 578         if (failover != null) {
 579             CK_VERSION version = new CK_VERSION(
 580                     (clientVersion >>> 8) & 0xFF, clientVersion & 0xFF);
 581             try {
 582                 CK_ATTRIBUTE[] attributes = token.getAttributes(
 583                         O_GENERATE, CKO_SECRET_KEY,
 584                         CKK_GENERIC_SECRET, new CK_ATTRIBUTE[0]);
 585                 long keyID = token.p11.C_GenerateKey(session.id(),
 586                     // new CK_MECHANISM(CKM_TLS_PRE_MASTER_KEY_GEN, version),
 587                         new CK_MECHANISM(CKM_SSL3_PRE_MASTER_KEY_GEN, version),
 588                         attributes);
 589                 return P11Key.secretKey(session,
 590                         keyID, "TlsRsaPremasterSecret", 48 << 3, attributes);
 591             } catch (PKCS11Exception e) {
 592                 throw new ProviderException(
 593                         "Could not generate premaster secret", e);
 594             }
 595         }
 596 
 597         return secretKey;
 598     }
 599 
 600 }
 601 
 602 final class ConstructKeys {
 603     /**
 604      * Construct a public key from its encoding.
 605      *
 606      * @param encodedKey the encoding of a public key.
 607      *
 608      * @param encodedKeyAlgorithm the algorithm the encodedKey is for.
 609      *
 610      * @return a public key constructed from the encodedKey.
 611      */
 612     private static final PublicKey constructPublicKey(byte[] encodedKey,
 613             String encodedKeyAlgorithm)
 614             throws InvalidKeyException, NoSuchAlgorithmException {
 615         try {
 616             KeyFactory keyFactory =
 617                 KeyFactory.getInstance(encodedKeyAlgorithm);
 618             X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey);
 619             return keyFactory.generatePublic(keySpec);
 620         } catch (NoSuchAlgorithmException nsae) {
 621             throw new NoSuchAlgorithmException("No installed providers " +
 622                                                "can create keys for the " +
 623                                                encodedKeyAlgorithm +
 624                                                "algorithm", nsae);
 625         } catch (InvalidKeySpecException ike) {
 626             throw new InvalidKeyException("Cannot construct public key", ike);
 627         }
 628     }
 629 
 630     /**
 631      * Construct a private key from its encoding.
 632      *
 633      * @param encodedKey the encoding of a private key.
 634      *
 635      * @param encodedKeyAlgorithm the algorithm the wrapped key is for.
 636      *
 637      * @return a private key constructed from the encodedKey.
 638      */
 639     private static final PrivateKey constructPrivateKey(byte[] encodedKey,
 640             String encodedKeyAlgorithm) throws InvalidKeyException,
 641             NoSuchAlgorithmException {
 642         try {
 643             KeyFactory keyFactory =
 644                 KeyFactory.getInstance(encodedKeyAlgorithm);
 645             PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey);
 646             return keyFactory.generatePrivate(keySpec);
 647         } catch (NoSuchAlgorithmException nsae) {
 648             throw new NoSuchAlgorithmException("No installed providers " +
 649                                                "can create keys for the " +
 650                                                encodedKeyAlgorithm +
 651                                                "algorithm", nsae);
 652         } catch (InvalidKeySpecException ike) {
 653             throw new InvalidKeyException("Cannot construct private key", ike);
 654         }
 655     }
 656 
 657     /**
 658      * Construct a secret key from its encoding.
 659      *
 660      * @param encodedKey the encoding of a secret key.
 661      *
 662      * @param encodedKeyAlgorithm the algorithm the secret key is for.
 663      *
 664      * @return a secret key constructed from the encodedKey.
 665      */
 666     private static final SecretKey constructSecretKey(byte[] encodedKey,
 667             String encodedKeyAlgorithm) {
 668         return new SecretKeySpec(encodedKey, encodedKeyAlgorithm);
 669     }
 670 
 671     static final Key constructKey(byte[] encoding, String keyAlgorithm,
 672             int keyType) throws InvalidKeyException, NoSuchAlgorithmException {
 673         switch (keyType) {
 674         case Cipher.SECRET_KEY:
 675             return constructSecretKey(encoding, keyAlgorithm);
 676         case Cipher.PRIVATE_KEY:
 677             return constructPrivateKey(encoding, keyAlgorithm);
 678         case Cipher.PUBLIC_KEY:
 679             return constructPublicKey(encoding, keyAlgorithm);
 680         default:
 681             throw new InvalidKeyException("Unknown keytype " + keyType);
 682         }
 683     }
 684 }