【发布时间】:2016-01-15 08:26:37
【问题描述】:
在我的静态库中,我有一个许可证文件。我想确保它是由我自己生成的(并且没有被改变)。所以这个想法是使用我读过的 RSA 签名。
我在网上查了一下,结果是这样的:
首先:使用我找到的信息生成私钥和自签名证书here。
// Generate private key
openssl genrsa -out private_key.pem 2048 -sha256
// Generate certificate request
openssl req -new -key private_key.pem -out certificate_request.pem -sha256
// Generate public certificate
openssl x509 -req -days 2000 -in certificate_request.pem -signkey private_key.pem -out certificate.pem -sha256
// Convert it to cer format so iOS kan work with it
openssl x509 -outform der -in certificate.pem -out certificate.cer -sha256
之后,我创建一个许可证文件(以日期和应用标识符作为内容)并根据找到的信息为该文件生成签名 here:
// Store the sha256 of the licence in a file
openssl dgst -sha256 licence.txt > hash
// And generate a signature file for that hash with the private key generated earlier
openssl rsautl -sign -inkey private_key.pem -keyform PEM -in hash > signature.sig
我认为一切正常。我没有收到任何错误,并按预期获得了密钥和证书以及其他文件。
接下来我将certificate.cer、signature.sig 和license.txt 复制到我的应用程序中。
现在我想检查签名是否由我签名并且对 license.txt 有效。我发现很难找到任何好的例子,但这是我目前所拥有的:
我发现的Seucyrity.Framework 使用SecKeyRef 来引用RSA 密钥/证书并使用SecKeyRawVerify 来验证签名。
我有以下方法从文件中加载公钥。
- (SecKeyRef)publicKeyFromFile:(NSString *) path
{
NSData *myCertData = [[NSFileManager defaultManager] contentsAtPath:path];
CFDataRef myCertDataRef = (__bridge CFDataRef) myCertData;
SecCertificateRef cert = SecCertificateCreateWithData (kCFAllocatorDefault, myCertDataRef);
CFArrayRef certs = CFArrayCreate(kCFAllocatorDefault, (const void **) &cert, 1, NULL);
SecPolicyRef policy = SecPolicyCreateBasicX509();
SecTrustRef trust;
SecTrustCreateWithCertificates(certs, policy, &trust);
SecTrustResultType trustResult;
SecTrustEvaluate(trust, &trustResult);
SecKeyRef pub_key_leaf = SecTrustCopyPublicKey(trust);
if (trustResult == kSecTrustResultRecoverableTrustFailure)
{
NSLog(@"I think this is the problem");
}
return pub_key_leaf;
}
这是基于this SO 帖子。
对于签名验证,我找到了以下函数
BOOL PKCSVerifyBytesSHA256withRSA(NSData* plainData, NSData* signature, SecKeyRef publicKey)
{
size_t signedHashBytesSize = SecKeyGetBlockSize(publicKey);
const void* signedHashBytes = [signature bytes];
size_t hashBytesSize = CC_SHA256_DIGEST_LENGTH;
uint8_t* hashBytes = malloc(hashBytesSize);
if (!CC_SHA256([plainData bytes], (CC_LONG)[plainData length], hashBytes)) {
return nil;
}
OSStatus status = SecKeyRawVerify(publicKey,
kSecPaddingPKCS1SHA256,
hashBytes,
hashBytesSize,
signedHashBytes,
signedHashBytesSize);
return status == errSecSuccess;
}
取自here
在我的项目中,我这样调用代码:
// Get the licence data
NSString *licencePath = [[NSBundle mainBundle] pathForResource:@"licence" ofType:@"txt"];
NSData *data = [[NSFileManager defaultManager] contentsAtPath:licencePath];
// Get the signature data
NSString *signaturePath = [[NSBundle mainBundle] pathForResource:@"signature" ofType:@"sig"];
NSData *signature = [[NSFileManager defaultManager] contentsAtPath:signaturePath];
// Get the public key
NSString *publicKeyPath = [[NSBundle mainBundle] pathForResource:@"certificate" ofType:@"cer"];
SecKeyRef publicKey = [self publicKeyFromFile:publicKeyPath];
// Check if the signature is valid with this public key for this data
BOOL result = PKCSVerifyBytesSHA256withRSA(data, signature, publicKey);
if (result)
{
NSLog(@"Alright All good!");
}
else
{
NSLog(@"Something went wrong!");
}
目前它总是说:“出了点问题!”虽然我不确定是什么。我发现获取公钥的方法中的信任结果等于kSecTrustResultRecoverableTrustFailure,我认为这是问题所在。在Apple documentation 中,我发现这可能是证书已过期的结果。尽管这里似乎并非如此。但也许我生成证书的方式有问题?
我的问题归结为,我做错了什么,我该如何解决?我发现这方面的文档非常稀少且难以阅读。
我有 uploaded 一个带有生成证书和此处引用的代码的 iOS 项目。也许这会派上用场。
【问题讨论】:
-
如果您查看您指向的文档中的清单 3-5,您会看到它列出了“
AllStatusBits”。当您遇到该错误时,您能弄清楚状态位是什么吗? -
嗨,当我尝试合并该代码时,第一个错误是
AllStatusBits的类型CSSM_TP_APPLE_CERT_STATUS未知,我似乎找不到要包含的工作头文件得到那种类型。在互联网上,我发现它可能是#import <Security/cssmapple.h>,但在 iOS 上不存在(不再存在?)。 - 我有 uploaded 我的项目代码和证书,也许这有帮助。 -
是的,我只是仔细检查了一遍,但我确实 already 添加了安全框架。也许它已在 iOS 版本中移到其他地方?
-
对不起,它不在 iOS Security.framework 中,我在看一个 OS X 项目。
-
您好,安全要求有多严格?我想知道为什么你不想使用像 md5/sha 哈希这样简单的东西,而只是将哈希值硬编码到你的应用程序中?
标签: ios objective-c cocoa openssl digital-signature