Any1 who can help out integrating commercial edition of SQLCipher into a Rubymotion application.

98 views
Skip to first unread message

Ashish Upadhyay

unread,
May 9, 2013, 6:09:14 PM5/9/13
to sqlc...@googlegroups.com
I encrypt a plaintext.db to an encrypted db using sqlite3 binary shipped in the zip package #sqlcipher-export .

sqlite> ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'testkey';
sqlite> SELECT sqlcipher_export('encrypted');
sqlite> DETACH DATABASE encrypted;


I add this encrypted.db to my resources folder and add the following configuration to my Rakefile

app.vendor_project('vendor/sqlcipher',
:static,
:products => ['./binaries/libsqlcipher.a'],
:source_files => ['./include'],
:c_flags => "-DSQLITE_HAS_CODEC"
)

In my application I have

      @connection = FMDatabase.databaseWithPath((File.join App.documents_path, 'encrypted.db'))
      @connection.traceExecution = true
      @connection.open
      @connection.setKey('testkey')


The last line returns me a false.

Is the method to include and compile SQLCipher files wrong ?

Please help.

Thanks

Stephen Lombardo

unread,
May 9, 2013, 6:28:43 PM5/9/13
to sqlc...@googlegroups.com
Hi Ashish,

Just to rule out a path issue, could you try to move libsqlcipher.a and sqlite3.h into the root of vendor/sqlcipher, then try

  app.vendor_project('vendor/sqlcipher',
                     :static,
                     :products => ['libsqlcipher.a']
                     :c_flags => "-DSQLITE_HAS_CODEC"
                     )

Cheers,
Stephen


--
 
---
You received this message because you are subscribed to the Google Groups "SQLCipher Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlcipher+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ashish Upadhyay

unread,
May 9, 2013, 6:46:41 PM5/9/13
to sqlc...@googlegroups.com
Did not help.

I have a question why would setKey return me a false ?

- (BOOL)setKey:(NSString*)key {
#ifdef SQLITE_HAS_CODEC
    if (!key) {
        return NO;
    }
   
    int rc = sqlite3_key(_db, [key UTF8String], (int)strlen([key UTF8String]));
   
    return (rc == SQLITE_OK);
#else
    return NO;
#endif
}

 Is SQLITE_HAS_CODEC not getting set ?


Nick Parker

unread,
May 10, 2013, 9:09:31 AM5/10/13
to sqlc...@googlegroups.com
Hi Ashish,

I have a couple of questions to run by you.  With your definition of a vendor_project of SQLCipher, can you verify that your application is properly building SQLCipher from source by verifying the resulting archive file is generated based on the path in your products key?  Are you able to modify the source and/or attach a debugger to the setKey method to further understand the execution flow at runtime?

Nick Parker

Nick Parker

Stephen Lombardo

unread,
May 10, 2013, 4:56:39 PM5/10/13
to sqlc...@googlegroups.com
Hello Ashish,

FMDB will only set the key on the database if SQLCipher is properly linked, and the FMDB classes are compiled with SQLITE_HAS_CODEC defined. The default behavior for FMDB is to return false on setKey if there is an error, or SQLITE_HAS_CODEC is not defined. Therefore, I would like you to try these two steps:

1. Once you have opened the connection, execute a query for "PRAGMA cipher_version;". If SQLCipher is indeed properly linked into your application it should return "2.1.1". If it doesn't return that string, then it's likely the static linking isn't occurring

2. Post a gist of the full build log, which will allow us to see whether -DSQLITE_HAS_CODEC is actually being passed in when fmdb is compiled.

Please let us know what you find. Thanks!

Cheers,
Stephen

Ashish Upadhyay

unread,
May 11, 2013, 9:57:56 AM5/11/13
to sqlc...@googlegroups.com
Hi Stephen,

I have posted a gist with the above info .
Please take a look.

https://gist.github.com/upadhyay-ashish/93418d5b2a6cb6a5369f

Ashish Upadhyay

unread,
May 11, 2013, 10:00:01 AM5/11/13
to sqlc...@googlegroups.com
Hi Nick ,

I have attached a gist with the build log  https://gist.github.com/upadhyay-ashish/93418d5b2a6cb6a5369f

Also I was able to attach a debugger in the setKey method in FMDatabase.m class, it seems SQLITE_HAS_CODEC is not defined.

Thanks

Billy Gray

unread,
May 11, 2013, 4:10:28 PM5/11/13
to sqlc...@googlegroups.com
Hi Ashish,

Thanks for posting that build log. I think that while SQLCIPHER_HAS_CODEC is being defined properly when your project is built, it's not being defined as a build flag as the FM sources are being built. Take a look at the build command used for FMResultSet.m in that gist for example. You'll see that the flag -DCOCOA_PODS is defined, but there's no -DSQLCIPHER_HAS_CODEC flag in that build command. This suggests that setKey: will always return false even though SQLCipher is properly linked to your app and returning the correct cipher version, because the #ifdef statement in that method isn't getting the flag.

I'm not too familiar with building CocoaPods, so I'm not sure how to fix the build issue myself. You could work around it temporarily by using the key PRAGMA directly instead of setKey:, but it's probably best to fix the issue with the build configuration. Hopefully someone else here will chime in with regard to that. If you work it out yourself, please let us know!

Regards,
Billy Gray

Team Zetetic
http://zetetic.net

Ashish Upadhyay

unread,
May 11, 2013, 8:18:15 PM5/11/13
to sqlc...@googlegroups.com
Got it working with the workaround +1

Will try to figure out why the flags are not being set properly.

Thanks for the quick help :)

Stephen Lombardo

unread,
May 13, 2013, 10:49:50 AM5/13/13
to sqlc...@googlegroups.com
Hi Ashish,

It might be worth inquiring with the rubymotion folks whether :c_flags should be appending specified flags globally, i.e. to other PODS.

Another alternative workaround would be to create a local copy of FMDB.podspec, and adjust it to define SQLITE_HAS_CODEC, either by adding #define SQLITE_HAS_CODEC to prefix_header_contents, or by adjusting the xcconfig, i.e.

    s.xcconfig = {'OTHER_CFLAGS' => '$(inherited) -DSQLITE_HAS_CODEC'}

Can you try one or both of those and let us know if it resolves the problem without the PRAGMA key workaround?

Thanks!

Cheers,
Stephen

Ashish Upadhyay

unread,
May 18, 2013, 2:06:53 AM5/18/13
to sqlc...@googlegroups.com
HI SJ,

I got some info on this, the fact is that headers are not used with a precompiled .a static library , and in this case we need to compile FMDB with the header, somebody from RM or Cocoapods will look into it

Thanks
Reply all
Reply to author
Forward
0 new messages