jdk/src/share/classes/sun/security/ec/ECKeyFactory.java
changeset 10336 0bb1999251f8
parent 5506 202f599c92aa
child 13661 7c894680910a
equal deleted inserted replaced
10335:3c7eda3ab2f5 10336:0bb1999251f8
     1 /*
     1 /*
     2  * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2006, 2011, 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
    59     // This can go away once we have EC always available in the SUN provider.
    59     // This can go away once we have EC always available in the SUN provider.
    60     // Used by ECParameters and AlgorithmId.
    60     // Used by ECParameters and AlgorithmId.
    61     public final static Provider ecInternalProvider;
    61     public final static Provider ecInternalProvider;
    62 
    62 
    63     static {
    63     static {
    64         final Provider p = new Provider("SunEC-Internal", 1.0d, null) {};
    64         final Provider p = new Provider("SunEC-Internal", 1.0d, null) {
       
    65             private static final long serialVersionUID = 970685700309471261L;
       
    66         };
    65         AccessController.doPrivileged(new PrivilegedAction<Void>() {
    67         AccessController.doPrivileged(new PrivilegedAction<Void>() {
    66             public Void run() {
    68             public Void run() {
    67                 p.put("KeyFactory.EC", "sun.security.ec.ECKeyFactory");
    69                 p.put("KeyFactory.EC", "sun.security.ec.ECKeyFactory");
    68                 p.put("AlgorithmParameters.EC", "sun.security.ec.ECParameters");
    70                 p.put("AlgorithmParameters.EC", "sun.security.ec.ECParameters");
    69                 p.put("Alg.Alias.AlgorithmParameters.1.2.840.10045.2.1", "EC");
    71                 p.put("Alg.Alias.AlgorithmParameters.1.2.840.10045.2.1", "EC");
   260             throw new InvalidKeySpecException(e);
   262             throw new InvalidKeySpecException(e);
   261         }
   263         }
   262         if (key instanceof ECPublicKey) {
   264         if (key instanceof ECPublicKey) {
   263             ECPublicKey ecKey = (ECPublicKey)key;
   265             ECPublicKey ecKey = (ECPublicKey)key;
   264             if (ECPublicKeySpec.class.isAssignableFrom(keySpec)) {
   266             if (ECPublicKeySpec.class.isAssignableFrom(keySpec)) {
   265                 return (T) new ECPublicKeySpec(
   267                 return keySpec.cast(new ECPublicKeySpec(
   266                     ecKey.getW(),
   268                     ecKey.getW(),
   267                     ecKey.getParams()
   269                     ecKey.getParams()
   268                 );
   270                 ));
   269             } else if (X509EncodedKeySpec.class.isAssignableFrom(keySpec)) {
   271             } else if (X509EncodedKeySpec.class.isAssignableFrom(keySpec)) {
   270                 return (T) new X509EncodedKeySpec(key.getEncoded());
   272                 return keySpec.cast(new X509EncodedKeySpec(key.getEncoded()));
   271             } else {
   273             } else {
   272                 throw new InvalidKeySpecException
   274                 throw new InvalidKeySpecException
   273                         ("KeySpec must be ECPublicKeySpec or "
   275                         ("KeySpec must be ECPublicKeySpec or "
   274                         + "X509EncodedKeySpec for EC public keys");
   276                         + "X509EncodedKeySpec for EC public keys");
   275             }
   277             }
   276         } else if (key instanceof ECPrivateKey) {
   278         } else if (key instanceof ECPrivateKey) {
   277             if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec)) {
   279             if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec)) {
   278                 return (T) new PKCS8EncodedKeySpec(key.getEncoded());
   280                 return keySpec.cast(new PKCS8EncodedKeySpec(key.getEncoded()));
   279             } else if (ECPrivateKeySpec.class.isAssignableFrom(keySpec)) {
   281             } else if (ECPrivateKeySpec.class.isAssignableFrom(keySpec)) {
   280                 ECPrivateKey ecKey = (ECPrivateKey)key;
   282                 ECPrivateKey ecKey = (ECPrivateKey)key;
   281                 return (T) new ECPrivateKeySpec(
   283                 return keySpec.cast(new ECPrivateKeySpec(
   282                     ecKey.getS(),
   284                     ecKey.getS(),
   283                     ecKey.getParams()
   285                     ecKey.getParams()
   284                 );
   286                 ));
   285             } else {
   287             } else {
   286                 throw new InvalidKeySpecException
   288                 throw new InvalidKeySpecException
   287                         ("KeySpec must be ECPrivateKeySpec or "
   289                         ("KeySpec must be ECPrivateKeySpec or "
   288                         + "PKCS8EncodedKeySpec for EC private keys");
   290                         + "PKCS8EncodedKeySpec for EC private keys");
   289             }
   291             }