views:

300

answers:

3

I'm writing an OS X client for a software that is written in PHP. This software uses a simple RPC interface to receive and execute commands. The RPC client has to sign the commands he sends to ensure that no MITM can modify any of them.

However, as the server was not accepting the signatures I sent from my OS X client, I started investigating and found out that PHP's openssl_sign function generates a different signature for a given private key/data combination than the Objective-C SSCrypto framework (which is only a wrapper for the openssl lib):

SSCrypto *crypto = [[SSCrypto alloc] initWithPrivateKey:self.localPrivKey];
NSData *shaed = [self sha1:@"hello"];
[crypto setClearTextWithData:shaed];
NSData *data = [crypto sign];

generates a signature like CtbkSxvqNZ+mAN...

The PHP code

openssl_sign("hello", $signature, $privateKey);

generates a signature like 6u0d2qjFiMbZ+... (For my certain key, of course. base64 encoded)

I'm not quite shure why this is happening and I unsuccessfully experimented with different hash-algorithms. As the PHP documentation states SHA1 is used by default.

So why do these two functions generate different signatures and how can I get my Objective-C part to generate a signature that PHPs openssl_verify will accept?

Note: I double checked that the keys and the data is correct!

A: 

It looks to me like your PHP is signing "hello", while your Objective-C is signing sha1("hello"). That is, the way I read the docs, PHP's openssl_sign is using sha1 by default in the internal mechanics of signing, as opposed to applying sha1 to the data before signing.

Isaac
I removed the sha1 in my Objective-C code but I still get a different (third) signature: `iaQ+f1PJi`...Note: The signatures that are generated with SSCrypto (Objective-C) match with the ones I can generate using openssl directly on the command line.
Pascal
Does `openssl_verify()` still fail on the new signature? Also (based on the sample code with `SSCrypto`) are you using something like `[signedData encodeBase64]`?
Isaac
Yes, PHP's `openssl_verify()` still fails (returns 0). I only use `[signedData encodeBase64]` for the human readable output (e.g. `NSLog()`), anywhere else I use `NSData` or `NSString`. I think the problem is on the PHP side as SSCrypto generates valid signatures that work with the normal openssl binary.
Pascal
If either side is doing some padding on the string, they may be padding with different characters and thereby throwing off the hash. You could try trivially extending the string length one character at a time and see if they match at some point, which'd be your block size.
Marc B
+1  A: 

Okay, it took quite a few hours. Here's what's happening:

When you call the openssl_sign function, PHP internally uses the EVP API that is provided by the openssl lib. EVP is a "high level" API to the underlying functions like RSA_private_encrypt. So when you call base64_encode(openssl_sign('hello', $signature, $privKey)) that's similar to do something like this on the command line using the openssl binary:

echo -n "hello"| openssl dgst -sha1 -sign priv.key | openssl enc -base64

and NOT

echo -n "hello" | openssl dgst -sha1 | openssl rsautl -encrypt priv.key | openssl enc -base64

I don't know why this produces different output , but it does. If someone has an idea why they differ: please share!
However, as I'm using the SSCrypto framework I rewrote the -sign (and -verify) function with EVP calls (abstract):

EVP_MD_CTX_init(&md_ctx);
EVP_SignInit(&md_ctx, mdtype);
EVP_SignUpdate(&md_ctx, input, inlen);
if (EVP_SignFinal(&md_ctx, (unsigned char*) outbuf, (unsigned int *)&outlen, pkey)) {
    NSLog(@"signed successfully.");
}

And voila: I get the same signatures as I got from PHP. Oh and for the record: PHP uses the PKCS padding.

Thank you guys for pointing me in the right direction!

Pascal
A: 

Not that it fixes, but your use of rsautl seems to be incorrect. You probably should be using rsautl -sign instead of rsautl -encrypt