Title: [1095] trunk/jopenssl: Think that was the final part of the simple enveloping PKCS7 operation.

Diff

Modified: trunk/jopenssl/src/java/org/jruby/ext/openssl/impl/BIO.java (1094 => 1095)


--- trunk/jopenssl/src/java/org/jruby/ext/openssl/impl/BIO.java	2008-08-08 10:54:26 UTC (rev 1094)
+++ trunk/jopenssl/src/java/org/jruby/ext/openssl/impl/BIO.java	2008-08-08 10:54:30 UTC (rev 1095)
@@ -185,39 +185,70 @@
     public void flush() throws IOException {
     }
 
+    private final static byte[] CONTENT_TEXT;
+    static {
+        byte[] val = null;
+        try {
+            val = "Content-Type: text/plain\r\n\r\n".getBytes("ISO8859-1");
+        } catch(Exception e) {
+            val = null;
+        }
+        CONTENT_TEXT = val;
+    }
+
     /** c: SMIME_crlf_copy
      *
      */
     public void crlfCopy(byte[] in, int flags) throws IOException {
-        throw new RuntimeException("TODO: implement");
+        byte[] linebuf = new byte[SMIME.MAX_SMLEN];
+        int[] len = new int[]{0};
+
+        if((flags & PKCS7.BINARY) > 0 ) {
+            write(in, 0, in.length);
+            return;
+        }
+        if((flags & PKCS7.TEXT) > 0) {
+            write(CONTENT_TEXT, 0, CONTENT_TEXT.length);
+        }
+        BIO inBio = memBuf(in);
+        while((len[0] = inBio.gets(linebuf, SMIME.MAX_SMLEN)) > 0) {
+            boolean eol = SMIME.stripEol(linebuf, len);
+            if(len[0] != 0) {
+                write(linebuf, 0, len[0]);
+            }
+            if(eol) {
+                write(SMIME.NEWLINE, 0, 2);
+            }
+
+        }
     }
 
     /** c: BIO_gets
      *
      */
     public int gets(byte[] in, int len) throws IOException {
-        throw new UnsupportedOperationException();
+        throw new UnsupportedOperationException("for " + this.getClass().getName());
     }
 
     /** c: BIO_write
      *
      */
     public int write(byte[] out, int offset, int len) throws IOException {
-        throw new UnsupportedOperationException();
+        throw new UnsupportedOperationException("for " + this.getClass().getName());
     }
 
     /** c: BIO_read
      *
      */
     public int read(byte[] into, int offset, int len) throws IOException {
-        throw new UnsupportedOperationException();
+        throw new UnsupportedOperationException("for " + this.getClass().getName());
     }
 
     /** c: BIO_set_mem_eof_return
      *
      */
     public void setMemEofReturn(int value) {
-        throw new UnsupportedOperationException();
+        throw new UnsupportedOperationException("for " + this.getClass().getName());
     }
 
     /** c: BIO_push

Modified: trunk/jopenssl/src/java/org/jruby/ext/openssl/impl/CipherBIOFilter.java (1094 => 1095)


--- trunk/jopenssl/src/java/org/jruby/ext/openssl/impl/CipherBIOFilter.java	2008-08-08 10:54:26 UTC (rev 1094)
+++ trunk/jopenssl/src/java/org/jruby/ext/openssl/impl/CipherBIOFilter.java	2008-08-08 10:54:30 UTC (rev 1095)
@@ -27,7 +27,10 @@
  ***** END LICENSE BLOCK *****/
 package org.jruby.ext.openssl.impl;
 
+import java.io.IOException;
+import javax.crypto.BadPaddingException;
 import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
 
 /**
  *
@@ -40,6 +43,29 @@
         this.cipher = cipher;
     }
 
+    public void flush() throws IOException {
+        try {
+            byte[] result = cipher.doFinal();
+            if(result == null) {
+                return;
+            }
+            next().write(result, 0, result.length);
+        } catch(IllegalBlockSizeException e) {
+            throw new PKCS7Exception(-1, -1, e.toString());
+        } catch(BadPaddingException e) {
+            throw new PKCS7Exception(-1, -1, e.toString());
+        }
+    }
+
+    public int write(byte[] out, int offset, int len) throws IOException {
+        byte[] result = cipher.update(out, offset, len);
+        if(result == null) {
+            return len;
+        }
+        next().write(result, 0, result.length);
+        return len;
+    }
+
     public int getType() {
         return TYPE_CIPHER;
     }

Modified: trunk/jopenssl/src/java/org/jruby/ext/openssl/impl/PKCS7.java (1094 => 1095)


--- trunk/jopenssl/src/java/org/jruby/ext/openssl/impl/PKCS7.java	2008-08-08 10:54:26 UTC (rev 1094)
+++ trunk/jopenssl/src/java/org/jruby/ext/openssl/impl/PKCS7.java	2008-08-08 10:54:30 UTC (rev 1095)
@@ -292,6 +292,7 @@
         Cipher evpCipher = null;
         BIO out = null;
         BIO btmp = null;
+        EncContent enc = null;
 
         switch(i) {
         case ASN1Registry.NID_pkcs7_signed:
@@ -301,6 +302,7 @@
         case ASN1Registry.NID_pkcs7_signedAndEnveloped:
             rsk = getSignedAndEnveloped().getRecipientInfo();
             mdSk = getSignedAndEnveloped().getMdAlgs();
+            enc = getSignedAndEnveloped().getEncData();
             xalg = getSignedAndEnveloped().getEncData().getAlgorithm();
             evpCipher = getSignedAndEnveloped().getEncData().getCipher();
             if(null == evpCipher) {
@@ -309,6 +311,7 @@
             break;
         case ASN1Registry.NID_pkcs7_enveloped:
             rsk = getEnveloped().getRecipientInfo();
+            enc = getEnveloped().getEncData();
             xalg = getEnveloped().getEncData().getAlgorithm();
             evpCipher = getEnveloped().getEncData().getCipher();
             if(null == evpCipher) {
@@ -348,16 +351,18 @@
 //             }
 // 		}
         if(evpCipher != null) {
-            int keylen, ivlen;
-            int jj, max;
             byte[] tmp;
-            
+            String algorithm = evpCipher.getAlgorithm();
+
             btmp = BIO.cipherFilter(evpCipher);
 
+            int klen = -1;
+
             try {
                 KeyGenerator gen = KeyGenerator.getInstance(evpCipher.getAlgorithm());
                 gen.init(new SecureRandom());
                 SecretKey key = gen.generateKey();
+                klen = ((SecretKeySpec)key).getEncoded().length*8;
                 evpCipher.init(Cipher.ENCRYPT_MODE, key);
 
                 if(null != rsk) {
@@ -373,6 +378,23 @@
                 e.printStackTrace();
             }
 
+            DERObjectIdentifier encAlgo = ASN1Registry.sym2oid(algorithm);
+            if(encAlgo == null) {
+                String name = algorithm;
+                String block = "CBC";
+                if(name.indexOf('/') != -1) {
+                    String[] nameParts = name.split("/");
+                    name = nameParts[0];
+                    block = nameParts[1];
+                }
+                encAlgo = ASN1Registry.sym2oid(name + "-" + klen + "-" + block);
+                if(null == encAlgo) {
+                    throw new PKCS7Exception(-1, -1, "Couldn't find algorithm " + algorithm + ". Tried: " + (name + "-" + klen + "-" + block));
+                }
+            }
+
+            enc.setAlgorithm(new AlgorithmIdentifier(encAlgo));
+
             if(out == null) {
                 out = btmp;
             } else {

Modified: trunk/jopenssl/src/java/org/jruby/ext/openssl/impl/SMIME.java (1094 => 1095)


--- trunk/jopenssl/src/java/org/jruby/ext/openssl/impl/SMIME.java	2008-08-08 10:54:26 UTC (rev 1094)
+++ trunk/jopenssl/src/java/org/jruby/ext/openssl/impl/SMIME.java	2008-08-08 10:54:30 UTC (rev 1095)
@@ -37,8 +37,8 @@
  * @author <a href="" PROTECTED]">Ola Bini</a>
  */
 public class SMIME {
-    private final static int MAX_SMLEN = 1024;
-    private final static byte[] NEWLINE = new byte[]{'\r','\n'};
+    public final static int MAX_SMLEN = 1024;
+    public final static byte[] NEWLINE = new byte[]{'\r','\n'};
 
     private Mime mime;
 
@@ -71,7 +71,7 @@
     /* c: static strip_eol
      *
      */
-    private boolean stripEol(byte[] linebuf, int[] plen) {
+    public static boolean stripEol(byte[] linebuf, int[] plen) {
         int len = plen[0];
         boolean isEol = false;
         

Modified: trunk/jopenssl/test/test_java_pkcs7.rb (1094 => 1095)


--- trunk/jopenssl/test/test_java_pkcs7.rb	2008-08-08 10:54:26 UTC (rev 1094)
+++ trunk/jopenssl/test/test_java_pkcs7.rb	2008-08-08 10:54:30 UTC (rev 1095)
@@ -688,7 +688,7 @@
       certs = [X509Cert]
       cipher = Cipher.get_instance("AES", BCP.new)
       data = ""
-      p PKCS7::encrypt(certs, data, cipher, 0)
+      PKCS7::encrypt(certs, data, cipher, PKCS7::BINARY)
     end
   end
 end
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to