You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by sm...@apache.org on 2007/03/20 05:51:40 UTC
svn commit: r520274 - in
/harmony/enhanced/classlib/trunk/modules/security/src:
main/java/common/org/apache/harmony/security/x509/
test/api/java/org/apache/harmony/security/tests/java/security/cert/
Author: smishura
Date: Mon Mar 19 21:51:39 2007
New Revision: 520274
URL: http://svn.apache.org/viewvc?view=rev&rev=520274
Log:
Fix bug revealed after HARMONY-3384 evaluation:
([classlib][security]java.security.cert.X509Certificate.toString() throws NullPointerException)
SubjectPublicKeyInfo.getPublicKey() returned null and printed an exception stack trace if key's algorithm is unknown. Now in this case key object (that contains parsed data:oid,key bytes) is returned.
Added:
harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/x509/X509PublicKey.java (with props)
Modified:
harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/x509/SubjectPublicKeyInfo.java
harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/cert/X509CertSelectorTest.java
Modified: harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/x509/SubjectPublicKeyInfo.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/x509/SubjectPublicKeyInfo.java?view=diff&rev=520274&r1=520273&r2=520274
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/x509/SubjectPublicKeyInfo.java (original)
+++ harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/x509/SubjectPublicKeyInfo.java Mon Mar 19 21:51:39 2007
@@ -23,7 +23,9 @@
package org.apache.harmony.security.x509;
import java.security.KeyFactory;
+import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
+import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import org.apache.harmony.security.asn1.ASN1BitString;
@@ -140,8 +142,8 @@
*/
public PublicKey getPublicKey() {
if (publicKey == null) {
+ String alg_oid = algorithmID.getAlgorithm();
try {
- String alg_oid = algorithmID.getAlgorithm();
String alg =
AlgNameMapper.map2AlgName(alg_oid);
@@ -150,8 +152,12 @@
}
publicKey = KeyFactory.getInstance(alg)
.generatePublic(new X509EncodedKeySpec(getEncoded()));
- } catch (Exception e) {
- e.printStackTrace();
+ } catch (InvalidKeySpecException e) {
+ } catch (NoSuchAlgorithmException e) {
+ }
+ if (publicKey == null) {
+ publicKey = new X509PublicKey(alg_oid, getEncoded(),
+ subjectPublicKey);
}
}
return publicKey;
Added: harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/x509/X509PublicKey.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/x509/X509PublicKey.java?view=auto&rev=520274
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/x509/X509PublicKey.java (added)
+++ harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/x509/X509PublicKey.java Mon Mar 19 21:51:39 2007
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.security.x509;
+
+import java.security.PublicKey;
+
+public class X509PublicKey implements PublicKey {
+
+ private final String algorithm;
+
+ private final byte[] encoded;
+
+ private final byte[] keyBytes;
+
+ public X509PublicKey(String algorithm, byte[] encoded, byte[] keyBytes) {
+ this.algorithm = algorithm;
+ this.encoded = encoded;
+ this.keyBytes = keyBytes;
+ }
+
+ public String getAlgorithm() {
+ return algorithm;
+ }
+
+ public String getFormat() {
+ return "X.509"; // $NON-NLS-1$
+ }
+
+ public byte[] getEncoded() {
+ return encoded;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder buf = new StringBuilder("algorithm = "); // $NON-NLS-1$
+ buf.append(algorithm);
+ buf.append(", params unparsed, unparsed keybits = \n"); // $NON-NLS-1$
+ // TODO: implement compatible toString method()
+ // buf.append(Arrays.toString(keyBytes));
+
+ return buf.toString();
+ }
+}
Propchange: harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/x509/X509PublicKey.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/cert/X509CertSelectorTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/cert/X509CertSelectorTest.java?view=diff&rev=520274&r1=520273&r2=520274
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/cert/X509CertSelectorTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/cert/X509CertSelectorTest.java Mon Mar 19 21:51:39 2007
@@ -18,7 +18,9 @@
package org.apache.harmony.security.tests.java.security.cert;
import java.io.IOException;
+import java.security.PublicKey;
import java.security.cert.X509CertSelector;
+import java.util.Arrays;
import junit.framework.TestCase;
@@ -87,5 +89,30 @@
// expected
}
}
+ }
+
+ /**
+ * @tests java.security.cert.X509CertSelector#setSubjectPublicKey(byte[])
+ */
+ public void test_setSubjectPublicKeyLB$() throws Exception {
+
+ //SubjectPublicKeyInfo ::= SEQUENCE {
+ // algorithm AlgorithmIdentifier,
+ // subjectPublicKey BIT STRING }
+ byte[] enc = { 0x30, 0x0E, // SEQUENCE
+ 0x30, 0x07, // SEQUENCE
+ 0x06, 0x02, 0x03, 0x05,//OID
+ 0x01, 0x01, 0x07, //ANY
+ 0x03, 0x03, 0x01, 0x01, 0x06, // subjectPublicKey
+ };
+
+ X509CertSelector selector = new X509CertSelector();
+
+ selector.setSubjectPublicKey(enc);
+ PublicKey key = selector.getSubjectPublicKey();
+ assertEquals("0.3.5", key.getAlgorithm());
+ assertEquals("X.509", key.getFormat());
+ assertTrue(Arrays.equals(enc, key.getEncoded()));
+ assertNotNull(key.toString());
}
}