]> hydra-www.ietfng.org Git - android-vcpass-oisafe/commitdiff
OI Safe: Change decrypt intent to create unencrypted file directly.
authorpeli0101 <peli0101@72b678ce-9140-0410-bee8-679b907dd61a>
Mon, 27 Apr 2009 20:16:59 +0000 (20:16 +0000)
committerpeli0101 <peli0101@72b678ce-9140-0410-bee8-679b907dd61a>
Mon, 27 Apr 2009 20:16:59 +0000 (20:16 +0000)
git-svn-id: http://openintents.googlecode.com/svn/trunk/Safe@2053 72b678ce-9140-0410-bee8-679b907dd61a

src/org/openintents/safe/CryptoContentProvider.java
src/org/openintents/safe/CryptoHelper.java

index 10ffdcdf66092bafcbd96c9548050d01fdce54b2..6c17a46dc53836ada8ea7101ea09a0f05ae906a4 100644 (file)
@@ -160,7 +160,7 @@ public class CryptoContentProvider extends ContentProvider {
                                        throw new CryptoHelperException("CryptoHelper not available. Are you logged in?");\r
                                }\r
                                Log.d(TAG, "Decrypt..");\r
-                               Uri newuri = ch.decryptFileWithSessionKey(this.getContext(), Uri.parse(originalFile));\r
+                               Uri newuri = ch.decryptFileWithSessionKeyThroughContentProvider(this.getContext(), Uri.parse(originalFile));\r
                                cryptSession = newuri.getPathSegments().get(1);\r
                                sessionFile=SESSION_FILE+"."+cryptSession;\r
                                path += "/"+sessionFile;\r
index 5c3313421cb038363a9b1a8e937ff932e4983904..36b4ef5bec34ac182d787b63dfd6e2ed9b4957b4 100644 (file)
@@ -68,6 +68,9 @@ public class CryptoHelper {
 \r
        private static final boolean debug = true;\r
     private static String TAG = "CryptoHelper";\r
+    \r
+    public static final String OISAFE_EXTENSION = ".oisafe";\r
+    \r
     protected static PBEKeySpec pbeKeySpec;\r
     protected static PBEParameterSpec pbeParamSpec;\r
     protected static SecretKeyFactory keyFac;\r
@@ -618,11 +621,10 @@ public class CryptoHelper {
                        InputStream is;\r
                        if (fileUri.getScheme().equals("file")) {\r
                                is = new java.io.FileInputStream(fileUri.getPath());\r
-                               outputPath = fileUri.getPath() + ".oisafe";\r
+                               outputPath = fileUri.getPath() + OISAFE_EXTENSION;\r
                        } else {\r
                                is = contentResolver.openInputStream(fileUri);\r
-                               outputPath = Environment\r
-                               .getExternalStorageDirectory().toString() + "/tmp.oisafe";\r
+                               outputPath = getTemporaryFileName();\r
                        }\r
                        \r
                        FileOutputStream os = new FileOutputStream(outputPath);\r
@@ -721,26 +723,118 @@ public class CryptoHelper {
                if (status==false) {\r
                        return null;\r
                }\r
-               return Uri.parse("file://" + outputPath); // TODO: UUEncode\r
+               return Uri.fromFile(new File(outputPath)); //Uri.parse("file://" + outputPath); // TODO: UUEncode\r
     }\r
+       /**\r
+        * @return\r
+        */\r
+       private String getTemporaryFileName() throws CryptoHelperException {\r
+               String randomPart;\r
+               try {\r
+                       // create a random session name\r
+                       randomPart=generateSalt();\r
+               } catch (NoSuchAlgorithmException e1) {\r
+                       e1.printStackTrace();\r
+                   String msg = "Decrypt error: "+e1.getLocalizedMessage();\r
+                   throw new CryptoHelperException(msg);\r
+               }\r
+               \r
+               return Environment\r
+               .getExternalStorageDirectory().toString() + "/tmp-" + randomPart;\r
+       }\r
 \r
     /**\r
-     * Unencrypt a file previously encrypted with\r
+     * Dencrypt a file previously encrypted with\r
      * encryptFileWithSessionKey().\r
      * \r
+     * Creates a new file without the .oisafe extension.\r
+     * \r
      * @author Peli\r
      * \r
      * @param ctx Context of activity in order to store temp file\r
      * @param fileUri Uri to either a stream or a file to read from\r
-     * @param useContentProvider true for using Content Provider,\r
-     *        false for creating a file without ".oisafe" extension and\r
-     *        deleting the original file.\r
      * @return If decryption is successful, returns Uri of a content \r
      *                 provider to read the plaintext file.  Upon failure,\r
      *                 return null.\r
      * @throws Exception\r
      */\r
     public Uri decryptFileWithSessionKey(Context ctx, Uri fileUri) throws CryptoHelperException {\r
+       Log.d(TAG, "fileUri="+fileUri.toString());\r
+       ContentResolver contentResolver = ctx.getContentResolver();\r
+\r
+               String outputPath = null;\r
+               Uri resultUri = null;\r
+       boolean result = false;\r
+       \r
+       try {\r
+               InputStream is;\r
+                       if (fileUri.getScheme().equals("file")) {\r
+                               String inputPath = fileUri.getPath();\r
+                               is = new java.io.FileInputStream(inputPath);\r
+                               if (debug) Log.d(TAG, "Decrypt: Input from " + inputPath);\r
+                               if (inputPath.endsWith(OISAFE_EXTENSION)) {\r
+                                       outputPath = inputPath.substring(0, inputPath.length() - OISAFE_EXTENSION.length());\r
+                               }\r
+                       } else {\r
+                               is = contentResolver.openInputStream(fileUri);\r
+                               if (debug) Log.d(TAG, "Decrypt: Input from " + fileUri.toString());\r
+                       }\r
+\r
+                       if (outputPath == null) {\r
+                               outputPath = getTemporaryFileName();\r
+                       }\r
+                       \r
+                       FileOutputStream os = new FileOutputStream(outputPath);\r
+       \r
+                       // after writing the decrypted content to a temporary file,\r
+                       // pass back a Uri that can be used to read back the contents\r
+                       resultUri = Uri.fromFile(new File(outputPath)); //Uri.parse("file://" + outputPath); // TODO: UUEncode?\r
+                       \r
+                       result = decryptStreamWithSessionKey(ctx, is, os);\r
+       \r
+                       // Close the input stream\r
+                       is.close();\r
+                       os.close();\r
+\r
+               } catch (FileNotFoundException e) {\r
+                       Log.e(TAG, "File not found", e);\r
+               } catch (IOException e) {\r
+                       Log.e(TAG, "IOException", e);\r
+               }\r
+\r
+\r
+               if (result == true) {\r
+                       // Successful\r
+\r
+                       // Securely delete the original file:\r
+                       \r
+                       SecureDelete.delete(new File(fileUri.getPath()));\r
+               } else {\r
+                       resultUri = null;\r
+                       \r
+                       // Unsuccessful. Clean up\r
+                       //ctx.deleteFile(sessionFile);\r
+               }\r
+               \r
+       return resultUri;\r
+    }\r
+    \r
+    /**\r
+     * Dencrypt a file previously encrypted with\r
+     * encryptFileWithSessionKey().\r
+     * \r
+     * The original file is not modified\r
+     * \r
+     * @author Peli\r
+     * \r
+     * @param ctx Context of activity in order to store temp file\r
+     * @param fileUri Uri to either a stream or a file to read from\r
+     * @return If decryption is successful, returns Uri of a content \r
+     *                 provider to read the plaintext file.  Upon failure,\r
+     *                 return null.\r
+     * @throws Exception\r
+     */\r
+    public Uri decryptFileWithSessionKeyThroughContentProvider(Context ctx, Uri fileUri) throws CryptoHelperException {\r
        Log.d(TAG, "fileUri="+fileUri.toString());\r
        ContentResolver contentResolver = ctx.getContentResolver();\r
        \r