6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1)
authorvinnie
Thu, 21 Jan 2010 23:59:41 +0000
changeset 4809 c00eed67999d
parent 4682 2aebbe5c08a7
child 4810 8edeb96d627b
6763530: Cannot decode PublicKey (Proider SunPKCS11, curve prime256v1) Reviewed-by: andrew
jdk/src/share/classes/sun/security/pkcs11/P11ECKeyFactory.java
jdk/src/share/classes/sun/security/pkcs11/P11Key.java
--- a/jdk/src/share/classes/sun/security/pkcs11/P11ECKeyFactory.java	Tue Jan 19 11:43:45 2010 +0800
+++ b/jdk/src/share/classes/sun/security/pkcs11/P11ECKeyFactory.java	Thu Jan 21 23:59:41 2010 +0000
@@ -40,6 +40,8 @@
 import sun.security.pkcs11.wrapper.*;
 import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
 
+import sun.security.util.DerValue;
+
 /**
  * EC KeyFactory implemenation.
  *
@@ -201,7 +203,16 @@
 
     private PublicKey generatePublic(ECPoint point, ECParameterSpec params) throws PKCS11Exception {
         byte[] encodedParams = ECParameters.encodeParameters(params);
-        byte[] encodedPoint = ECParameters.encodePoint(point, params.getCurve());
+        byte[] encodedPoint = null;
+        DerValue pkECPoint = new DerValue(DerValue.tag_OctetString,
+            ECParameters.encodePoint(point, params.getCurve()));
+
+        try {
+            encodedPoint = pkECPoint.toByteArray();
+        } catch (IOException e) {
+            throw new IllegalArgumentException("Could not DER encode point", e);
+        }
+
         CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
             new CK_ATTRIBUTE(CKA_CLASS, CKO_PUBLIC_KEY),
             new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_EC),
--- a/jdk/src/share/classes/sun/security/pkcs11/P11Key.java	Tue Jan 19 11:43:45 2010 +0800
+++ b/jdk/src/share/classes/sun/security/pkcs11/P11Key.java	Thu Jan 21 23:59:41 2010 +0000
@@ -45,6 +45,8 @@
 import sun.security.pkcs11.wrapper.*;
 import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
 
+import sun.security.util.DerValue;
+
 /**
  * Key implementation classes.
  *
@@ -1000,8 +1002,16 @@
             try {
                 params = P11ECKeyFactory.decodeParameters
                             (attributes[1].getByteArray());
+                DerValue wECPoint = new DerValue(attributes[0].getByteArray());
+                if (wECPoint.getTag() != DerValue.tag_OctetString)
+                    throw new IOException("Unexpected tag: " +
+                        wECPoint.getTag());
+                params = P11ECKeyFactory.decodeParameters
+                            (attributes[1].getByteArray());
                 w = P11ECKeyFactory.decodePoint
-                            (attributes[0].getByteArray(), params.getCurve());
+                    (wECPoint.getDataBytes(), params.getCurve());
+
+
             } catch (Exception e) {
                 throw new RuntimeException("Could not parse key values", e);
             }