jdk/src/share/classes/com/sun/crypto/provider/ConstructKeys.java
changeset 16909 78a1749a43e2
parent 5506 202f599c92aa
equal deleted inserted replaced
16850:f6f6c2182678 16909:78a1749a43e2
     1 /*
     1 /*
     2  * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1999, 2013, 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
    28 import java.security.Key;
    28 import java.security.Key;
    29 import java.security.PublicKey;
    29 import java.security.PublicKey;
    30 import java.security.PrivateKey;
    30 import java.security.PrivateKey;
    31 import java.security.KeyFactory;
    31 import java.security.KeyFactory;
    32 import java.security.InvalidKeyException;
    32 import java.security.InvalidKeyException;
    33 import java.security.NoSuchProviderException;
       
    34 import java.security.NoSuchAlgorithmException;
    33 import java.security.NoSuchAlgorithmException;
    35 import java.security.spec.PKCS8EncodedKeySpec;
    34 import java.security.spec.PKCS8EncodedKeySpec;
    36 import java.security.spec.X509EncodedKeySpec;
    35 import java.security.spec.X509EncodedKeySpec;
    37 import java.security.spec.InvalidKeySpecException;
    36 import java.security.spec.InvalidKeySpecException;
    38 
    37 
    64     {
    63     {
    65         PublicKey key = null;
    64         PublicKey key = null;
    66 
    65 
    67         try {
    66         try {
    68             KeyFactory keyFactory =
    67             KeyFactory keyFactory =
    69                 KeyFactory.getInstance(encodedKeyAlgorithm, "SunJCE");
    68                 KeyFactory.getInstance(encodedKeyAlgorithm,
       
    69                     SunJCE.getInstance());
    70             X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey);
    70             X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey);
    71             key = keyFactory.generatePublic(keySpec);
    71             key = keyFactory.generatePublic(keySpec);
    72         } catch (NoSuchAlgorithmException nsae) {
    72         } catch (NoSuchAlgorithmException nsae) {
    73             // Try to see whether there is another
    73             // Try to see whether there is another
    74             // provider which supports this algorithm
    74             // provider which supports this algorithm
    92         } catch (InvalidKeySpecException ikse) {
    92         } catch (InvalidKeySpecException ikse) {
    93             InvalidKeyException ike =
    93             InvalidKeyException ike =
    94                 new InvalidKeyException("Cannot construct public key");
    94                 new InvalidKeyException("Cannot construct public key");
    95             ike.initCause(ikse);
    95             ike.initCause(ikse);
    96             throw ike;
    96             throw ike;
    97         } catch (NoSuchProviderException nspe) {
       
    98             // Should never happen.
       
    99         }
    97         }
   100 
    98 
   101         return key;
    99         return key;
   102     }
   100     }
   103 
   101 
   116     {
   114     {
   117         PrivateKey key = null;
   115         PrivateKey key = null;
   118 
   116 
   119         try {
   117         try {
   120             KeyFactory keyFactory =
   118             KeyFactory keyFactory =
   121                 KeyFactory.getInstance(encodedKeyAlgorithm, "SunJCE");
   119                 KeyFactory.getInstance(encodedKeyAlgorithm,
       
   120                     SunJCE.getInstance());
   122             PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey);
   121             PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey);
   123             return keyFactory.generatePrivate(keySpec);
   122             return keyFactory.generatePrivate(keySpec);
   124         } catch (NoSuchAlgorithmException nsae) {
   123         } catch (NoSuchAlgorithmException nsae) {
   125             // Try to see whether there is another
   124             // Try to see whether there is another
   126             // provider which supports this algorithm
   125             // provider which supports this algorithm
   144         } catch (InvalidKeySpecException ikse) {
   143         } catch (InvalidKeySpecException ikse) {
   145             InvalidKeyException ike =
   144             InvalidKeyException ike =
   146                 new InvalidKeyException("Cannot construct private key");
   145                 new InvalidKeyException("Cannot construct private key");
   147             ike.initCause(ikse);
   146             ike.initCause(ikse);
   148             throw ike;
   147             throw ike;
   149         } catch (NoSuchProviderException nspe) {
       
   150             // Should never happen.
       
   151         }
   148         }
   152 
   149 
   153         return key;
   150         return key;
   154     }
   151     }
   155 
   152