incubator-amber-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r963074 - in /incubator/amber/trunk/signature-api/src/main: java/org/apache/amber/signature/rsa/ javacc/
Date Sun, 11 Jul 2010 13:57:47 GMT
Author: simonetripodi
Date: Sun Jul 11 13:57:47 2010
New Revision: 963074

URL: http://svn.apache.org/viewvc?rev=963074&view=rev
Log:
the parser returns directly the private/public RSA key

Modified:
    incubator/amber/trunk/signature-api/src/main/java/org/apache/amber/signature/rsa/AbstractRsaSha1Key.java
    incubator/amber/trunk/signature-api/src/main/java/org/apache/amber/signature/rsa/PemRsaSha1SigningKey.java
    incubator/amber/trunk/signature-api/src/main/java/org/apache/amber/signature/rsa/PemRsaSha1VeifyingKey.java
    incubator/amber/trunk/signature-api/src/main/javacc/PemCertificateParser.jj

Modified: incubator/amber/trunk/signature-api/src/main/java/org/apache/amber/signature/rsa/AbstractRsaSha1Key.java
URL: http://svn.apache.org/viewvc/incubator/amber/trunk/signature-api/src/main/java/org/apache/amber/signature/rsa/AbstractRsaSha1Key.java?rev=963074&r1=963073&r2=963074&view=diff
==============================================================================
--- incubator/amber/trunk/signature-api/src/main/java/org/apache/amber/signature/rsa/AbstractRsaSha1Key.java
(original)
+++ incubator/amber/trunk/signature-api/src/main/java/org/apache/amber/signature/rsa/AbstractRsaSha1Key.java
Sun Jul 11 13:57:47 2010
@@ -24,7 +24,6 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLConnection;
 
-import org.apache.amber.signature.Key;
 import org.apache.amber.signature.SignatureException;
 
 /**
@@ -32,11 +31,11 @@ import org.apache.amber.signature.Signat
  *
  * @version $Id$
  */
-abstract class AbstractRsaSha1Key implements Key {
+abstract class AbstractRsaSha1Key implements org.apache.amber.signature.Key {
 
     private static final String[] METHODS = { "RSA-SHA1" };
 
-    private final byte[] byteValue;
+    private final java.security.Key keyValue;
 
     /**
      * Loads a key from a certificate located in the classpath.
@@ -63,7 +62,7 @@ abstract class AbstractRsaSha1Key implem
                     + "' not found, please make sure it exists in the classpath");
         }
 
-        this.byteValue = this.readCertificate(certificateURL);
+        this.keyValue = this.readCertificate(certificateURL);
     }
 
     /**
@@ -83,7 +82,7 @@ abstract class AbstractRsaSha1Key implem
         }
 
         try {
-            this.byteValue = this.readCertificate(certificateFileLocation.toURI().toURL());
+            this.keyValue = this.readCertificate(certificateFileLocation.toURI().toURL());
         } catch (MalformedURLException e) {
             throw new SignatureException("Impossible to read the certificate from '"
                     + certificateFileLocation
@@ -102,10 +101,10 @@ abstract class AbstractRsaSha1Key implem
             throw new SignatureException("parameter 'certificateURL' must not be null");
         }
 
-        this.byteValue = this.readCertificate(certificateURL);
+        this.keyValue = this.readCertificate(certificateURL);
     }
 
-    private byte[] readCertificate(URL certificateURL) throws SignatureException {
+    private java.security.Key readCertificate(URL certificateURL) throws SignatureException
{
         URLConnection urlConnection = null;
         InputStream input = null;
 
@@ -133,16 +132,16 @@ abstract class AbstractRsaSha1Key implem
         }
     }
 
-    public final byte[] getByteValue() {
-        return this.byteValue;
+    public java.security.Key getKeyValue() {
+        return this.keyValue;
     }
 
     @Override
     public final String getValue() {
-        return new String(this.byteValue);
+        return String.valueOf(this.keyValue);
     }
 
-    protected abstract byte[] readCertificate(InputStream input) throws Exception;
+    protected abstract java.security.Key readCertificate(InputStream input) throws Exception;
 
     /**
      * {@inheritDoc}

Modified: incubator/amber/trunk/signature-api/src/main/java/org/apache/amber/signature/rsa/PemRsaSha1SigningKey.java
URL: http://svn.apache.org/viewvc/incubator/amber/trunk/signature-api/src/main/java/org/apache/amber/signature/rsa/PemRsaSha1SigningKey.java?rev=963074&r1=963073&r2=963074&view=diff
==============================================================================
--- incubator/amber/trunk/signature-api/src/main/java/org/apache/amber/signature/rsa/PemRsaSha1SigningKey.java
(original)
+++ incubator/amber/trunk/signature-api/src/main/java/org/apache/amber/signature/rsa/PemRsaSha1SigningKey.java
Sun Jul 11 13:57:47 2010
@@ -19,6 +19,7 @@ package org.apache.amber.signature.rsa;
 import java.io.File;
 import java.io.InputStream;
 import java.net.URL;
+import java.security.Key;
 
 import org.apache.amber.signature.SignatureException;
 import org.apache.amber.signature.SigningKey;
@@ -50,7 +51,7 @@ public final class PemRsaSha1SigningKey
      * {@inheritDoc}
      */
     @Override
-    protected byte[] readCertificate(InputStream input) throws Exception {
+    protected Key readCertificate(InputStream input) throws Exception {
         PemCertificateParser pemCertificateParser = new PemCertificateParser(input);
         return pemCertificateParser.parsePrivateKey();
     }

Modified: incubator/amber/trunk/signature-api/src/main/java/org/apache/amber/signature/rsa/PemRsaSha1VeifyingKey.java
URL: http://svn.apache.org/viewvc/incubator/amber/trunk/signature-api/src/main/java/org/apache/amber/signature/rsa/PemRsaSha1VeifyingKey.java?rev=963074&r1=963073&r2=963074&view=diff
==============================================================================
--- incubator/amber/trunk/signature-api/src/main/java/org/apache/amber/signature/rsa/PemRsaSha1VeifyingKey.java
(original)
+++ incubator/amber/trunk/signature-api/src/main/java/org/apache/amber/signature/rsa/PemRsaSha1VeifyingKey.java
Sun Jul 11 13:57:47 2010
@@ -19,6 +19,7 @@ package org.apache.amber.signature.rsa;
 import java.io.File;
 import java.io.InputStream;
 import java.net.URL;
+import java.security.Key;
 
 import org.apache.amber.signature.SignatureException;
 import org.apache.amber.signature.SigningKey;
@@ -50,7 +51,7 @@ public final class PemRsaSha1VeifyingKey
      * {@inheritDoc}
      */
     @Override
-    protected byte[] readCertificate(InputStream input) throws Exception {
+    protected Key readCertificate(InputStream input) throws Exception {
         PemCertificateParser pemCertificateParser = new PemCertificateParser(input);
         return pemCertificateParser.parsePublicCertificate();
     }

Modified: incubator/amber/trunk/signature-api/src/main/javacc/PemCertificateParser.jj
URL: http://svn.apache.org/viewvc/incubator/amber/trunk/signature-api/src/main/javacc/PemCertificateParser.jj?rev=963074&r1=963073&r2=963074&view=diff
==============================================================================
--- incubator/amber/trunk/signature-api/src/main/javacc/PemCertificateParser.jj (original)
+++ incubator/amber/trunk/signature-api/src/main/javacc/PemCertificateParser.jj Sun Jul 11
13:57:47 2010
@@ -19,6 +19,11 @@ package org.apache.amber.signature.rsa;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.security.KeyFactory;
+import java.security.interfaces.RSAPrivateKey;
+import java.security.interfaces.RSAPublicKey;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.security.spec.X509EncodedKeySpec;
 
 import org.apache.commons.codec.binary.Base64OutputStream;
 
@@ -29,6 +34,18 @@ import org.apache.commons.codec.binary.B
  */
 final class PemCertificateParser {
 
+    private static final String RSA_NAME = "RSA";
+
+    private static KeyFactory getRsaKeyFactory() {
+        try {
+            return KeyFactory.getInstance(RSA_NAME);
+        } catch (Exception e) {
+            throw new RuntimeException("Impossible to instantiate the RSA KeyFactory", e);
+        }
+    }
+
+    private final KeyFactory keyFactory = getRsaKeyFactory();
+
     private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
     private final Base64OutputStream output = new Base64OutputStream(baos, false);
@@ -49,28 +66,40 @@ TOKEN : {
     | <CERTIFICATE_CONTENT : (["a"-"z","A"-"Z","0"-"9","+","/","="])+>
 }
 
-public byte[] parsePrivateKey() : { }
+public RSAPrivateKey parsePrivateKey() throws Exception : { }
 {
     <BEGIN_PRIVATE_KEY> [ certificateContent() ]  <END_PRIVATE_KEY> <EOF>
{
         try {
-            this.output.close();
-            this.baos.close();
-            return this.baos.toByteArray();
-        } catch (IOException e) {
-            throw new ParseException("Fatal protocol error: " + e.getMessage());
+            PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(this.baos.toByteArray());
+            return (RSAPrivateKey) this.keyFactory.generatePrivate(privSpec);
+        } catch (Exception e) {
+            throw e;
+        } finally {
+            try {
+                this.output.close();
+                this.baos.close();
+            } catch (IOException e) {
+                // close quietly
+            }
         }
     }
 }
 
-public byte[] parsePublicCertificate() : { }
+public RSAPublicKey parsePublicCertificate() throws Exception : { }
 {
     <BEGIN_CERTIFICATE> [ certificateContent() ]  <END_CERTIFICATE> <EOF>
{
         try {
-            this.output.close();
-            this.baos.close();
-            return this.baos.toByteArray();
-        } catch (IOException e) {
-            throw new ParseException("Fatal protocol error: " + e.getMessage());
+            X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(this.baos.toByteArray());
+            return (RSAPublicKey) this.keyFactory.generatePublic(pubSpec);
+        } catch (Exception e) {
+            throw e;
+        } finally {
+            try {
+                this.output.close();
+                this.baos.close();
+            } catch (IOException e) {
+                // close quietly
+            }
         }
     }
 }



Mime
View raw message