Best & funniest photo that I ever took :] #LoveBirds
That was at the time when I discovered that pigeons truly love french fries - And that's my hand on the picture :-)
#include <openssl/pem.h>
#include <openssl/pkcs12.h>
#include <openssl/pkcs7.h>
#include <openssl/err.h>
+(BOOL)signManifest:(NSData *)manifest toPath:(NSString *)signaturePath withPKCS12FilePath:(NSString *)pkcs12 andAdditionalCACertPath:(NSString *)intermediateCertPath {
NSLog(@"%s", __FUNCTION__);
BOOL result = NO;
FILE *fp;
BIO *in = NULL, *out = NULL;
PKCS12 *p12;
X509 *scert = NULL, *caCert = NULL;
STACK_OF(X509) *ca = NULL;
EVP_PKEY *skey = NULL;
PKCS7 *p7 = NULL;
/* For simple S/MIME signing use PKCS7_DETACHED.
* On OpenSSL 0.9.9 only:
* for streaming detached set PKCS7_DETACHED|PKCS7_STREAM
* for streaming non-detached set PKCS7_STREAM
*/
int flags = PKCS7_DETACHED | PKCS7_BINARY;
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
/* Read in signer certificate and private key */
if (!(fp = fopen([pkcs12 UTF8String], "rb"))) {
NSLog(@"%s Error opening file %@", __FUNCTION__, pkcs12);
goto end;
}
p12 = d2i_PKCS12_fp(fp, NULL);
fclose (fp);
if (!p12) {
NSLog(@"%s Error reading PKCS#12 file", __FUNCTION__);
ERR_print_errors_fp(stderr);
goto end;
}
if (!PKCS12_parse(p12, "", &skey, &scert, &ca)) {
NSLog(@"%s Error parsing PKCS#12 file", __FUNCTION__);
ERR_print_errors_fp(stderr);
goto end;
}
PKCS12_free(p12);
if (!scert || !skey)
goto end;
/* Read intermediate CA root certificates */
if (!(fp = fopen([intermediateCertPath UTF8String], "rb"))) {
NSLog(@"%s Error opening file %@", __FUNCTION__, intermediateCertPath);
goto end;
}
caCert = d2i_X509_fp(fp, NULL);
fclose (fp);
if (!caCert) {
NSLog(@"%s Error reading X509 Certificate file", __FUNCTION__);
ERR_print_errors_fp(stderr);
goto end;
}
//Add the intermediate CA certificate to the signing stack
if (ca == NULL) {
ca = sk_X509_new_null();
}
sk_X509_push(ca, caCert);
/* Open content being signed */
in = BIO_new_mem_buf((void *)[manifest bytes], [manifest length]);
if (!in)
goto end;
/* Sign content */
p7 = PKCS7_sign(scert, skey, ca, in, flags);
if (!p7)
goto end;
//create a file handle to where the signature will be saved
out = BIO_new_file([signaturePath UTF8String], "w");
if (!out)
goto end;
//if (!(flags & PKCS7_STREAM))
// BIO_reset(in);
/* Write out S/MIME message */
if (!i2d_PKCS7_bio(out, p7))
goto end;
result = YES;
end:
if (result == NO)
{
NSLog(@"%s Error Signing Data", __FUNCTION__);
ERR_print_errors_fp(stderr);
}
if (ca) {
sk_X509_free(ca);
}
if (p7)
PKCS7_free(p7);
if (scert)
X509_free(scert);
if (skey)
EVP_PKEY_free(skey);
if (in)
BIO_free(in);
if (out)
BIO_free(out);
return result;
}