abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r541747 - in /incubator/abdera/java/trunk/security/src/main/java/org/apache/abdera/security: Signature.java util/KeyHelper.java xmlsec/XmlSignature.java
Date Fri, 25 May 2007 18:53:11 GMT
Author: jmsnell
Date: Fri May 25 11:53:10 2007
New Revision: 541747

URL: http://svn.apache.org/viewvc?view=rev&rev=541747
Log:
Method for retrieving the KeyInfo of an embedded signature
Helper class for working with java.security.Key stuff

Added:
    incubator/abdera/java/trunk/security/src/main/java/org/apache/abdera/security/util/KeyHelper.java
Modified:
    incubator/abdera/java/trunk/security/src/main/java/org/apache/abdera/security/Signature.java
    incubator/abdera/java/trunk/security/src/main/java/org/apache/abdera/security/xmlsec/XmlSignature.java

Modified: incubator/abdera/java/trunk/security/src/main/java/org/apache/abdera/security/Signature.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/security/src/main/java/org/apache/abdera/security/Signature.java?view=diff&rev=541747&r1=541746&r2=541747
==============================================================================
--- incubator/abdera/java/trunk/security/src/main/java/org/apache/abdera/security/Signature.java
(original)
+++ incubator/abdera/java/trunk/security/src/main/java/org/apache/abdera/security/Signature.java
Fri May 25 11:53:10 2007
@@ -20,6 +20,7 @@
 import java.security.cert.X509Certificate;
 
 import org.apache.abdera.model.Element;
+import org.apache.xml.security.keys.KeyInfo;
 
 /**
  * Interface used for digitally signing and verifying Abdera elements
@@ -45,6 +46,8 @@
    * Returns a listing of X.509 certificates of valid digital signatures in the element
    */
   <T extends Element>X509Certificate[] getValidSignatureCertificates(T element, SignatureOptions
options) throws SecurityException;
+  
+  <T extends Element>KeyInfo getSignatureKeyInfo(T element, SignatureOptions options)
throws SecurityException;
   
   /**
    * Returns the default signing options

Added: incubator/abdera/java/trunk/security/src/main/java/org/apache/abdera/security/util/KeyHelper.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/security/src/main/java/org/apache/abdera/security/util/KeyHelper.java?view=auto&rev=541747
==============================================================================
--- incubator/abdera/java/trunk/security/src/main/java/org/apache/abdera/security/util/KeyHelper.java
(added)
+++ incubator/abdera/java/trunk/security/src/main/java/org/apache/abdera/security/util/KeyHelper.java
Fri May 25 11:53:10 2007
@@ -0,0 +1,149 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  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.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.security.util;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.cert.Certificate;
+import java.security.Key;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.SecureRandom;
+import java.security.UnrecoverableKeyException;
+import java.security.cert.CertificateException;
+
+import javax.crypto.KeyGenerator;
+import javax.crypto.SecretKey;
+
+public class KeyHelper {
+  
+  public static void saveKeystore(
+    KeyStore ks, 
+    String file,
+    String password) 
+      throws KeyStoreException, 
+             NoSuchAlgorithmException, 
+             CertificateException, 
+             FileNotFoundException, 
+             IOException {
+      ks.store(new FileOutputStream(file), password.toCharArray());
+  }
+  
+  public static KeyStore loadKeystore(
+      String file, 
+      String pass) 
+        throws KeyStoreException, 
+               NoSuchAlgorithmException, 
+               CertificateException, 
+               IOException {
+      KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
+      InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(file);
+      if (in == null) in = new FileInputStream(file);
+      ks.load(in, pass.toCharArray());
+      return ks;
+    }
+  
+  public static KeyStore loadKeystore(
+    String type, 
+    String file, 
+    String pass) 
+      throws KeyStoreException, 
+             NoSuchAlgorithmException, 
+             CertificateException, 
+             IOException {
+    KeyStore ks = KeyStore.getInstance(type);
+    InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(file);
+    if (in == null) in = new FileInputStream(file);
+    ks.load(in, pass.toCharArray());
+    return ks;
+  }
+  
+  @SuppressWarnings("unchecked")
+  public static <T extends Key>T getKey(
+    KeyStore ks, 
+    String alias, 
+    String pass) 
+      throws KeyStoreException, 
+             NoSuchAlgorithmException, 
+             UnrecoverableKeyException {
+    return (T)ks.getKey(
+        alias,
+        pass.toCharArray());
+  }
+  
+  @SuppressWarnings("unchecked")
+  public static <T extends Certificate>T getCertificate(
+    KeyStore ks, 
+    String alias) 
+      throws KeyStoreException {
+    return (T)ks.getCertificate(alias);
+  }
+  
+  public static KeyPair generateKeyPair(
+      String type, 
+      int size) 
+        throws NoSuchAlgorithmException, 
+               NoSuchProviderException {
+      KeyPairGenerator keyGen = KeyPairGenerator.getInstance(type);
+      SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
+      keyGen.initialize(size, random);
+      random.setSeed(System.currentTimeMillis());
+      return keyGen.generateKeyPair();
+    }
+  
+  public static KeyPair generateKeyPair(
+    String type, 
+    int size,
+    String provider) 
+      throws NoSuchAlgorithmException, 
+             NoSuchProviderException {
+    KeyPairGenerator keyGen = KeyPairGenerator.getInstance(type, provider);
+    SecureRandom random = SecureRandom.getInstance("SHA1PRNG", provider);
+    keyGen.initialize(size, random);
+    random.setSeed(System.currentTimeMillis());
+    return keyGen.generateKeyPair();
+  }
+  
+  public static SecretKey generateSecretKey(
+      String type, 
+      int size) 
+        throws NoSuchAlgorithmException, 
+               NoSuchProviderException {
+      KeyGenerator keyGenerator = KeyGenerator.getInstance(type);
+      keyGenerator.init(size);
+      return keyGenerator.generateKey();
+    }
+  
+  public static SecretKey generateSecretKey(
+    String type, 
+    int size,
+    String provider) 
+      throws NoSuchAlgorithmException, 
+             NoSuchProviderException {
+    KeyGenerator keyGenerator = KeyGenerator.getInstance(type, provider);
+    keyGenerator.init(size);
+    return keyGenerator.generateKey();
+  }
+}

Modified: incubator/abdera/java/trunk/security/src/main/java/org/apache/abdera/security/xmlsec/XmlSignature.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/security/src/main/java/org/apache/abdera/security/xmlsec/XmlSignature.java?view=diff&rev=541747&r1=541746&r2=541747
==============================================================================
--- incubator/abdera/java/trunk/security/src/main/java/org/apache/abdera/security/xmlsec/XmlSignature.java
(original)
+++ incubator/abdera/java/trunk/security/src/main/java/org/apache/abdera/security/xmlsec/XmlSignature.java
Fri May 25 11:53:10 2007
@@ -188,6 +188,32 @@
     return null;
   }
   
+  public <T extends Element> KeyInfo getSignatureKeyInfo(
+    T element, 
+    SignatureOptions options) 
+      throws SecurityException {
+    KeyInfo ki = null;
+    org.w3c.dom.Element dom = fomToDom((Element)element, options);
+    NodeList children = dom.getChildNodes();
+    for (int n = 0; n < children.getLength(); n++) {
+      try {
+        Node node = children.item(n);
+        if (node.getNodeType() == Node.ELEMENT_NODE) {
+          org.w3c.dom.Element el = (org.w3c.dom.Element) node;
+          if (Constants.DSIG_NS.equals(el.getNamespaceURI()) &&
+              Constants.LN_SIGNATURE.equals(el.getLocalName())) {
+            IRI baseUri = element.getResolvedBaseUri();
+            XMLSignature sig = 
+              new XMLSignature(
+                el, (baseUri != null) ? baseUri.toString() : "");
+            ki = sig.getKeyInfo();
+          }
+        }
+      } catch (Exception e) {}
+    }
+    return ki;
+  }
+  
   private boolean _verify(
     Element element, 
     SignatureOptions options) 



Mime
View raw message