ios - 从证书中获取公钥/私钥

标签 ios objective-c ssl-certificate x509certificate public-key-encryption

我尝试从设备上保存的证书中获取公钥或私钥。 我正在使用这个方法:

    - (SecKeyRef)publicKeyFromFile:(NSString *)path
{
    NSData * certificateData = [[NSData alloc] initWithData:[[NSFileManager defaultManager] contentsAtPath:path]];

    if (certificateData != nil && certificateData.bytes != 0) {

        CFDataRef cfDataPath = CFDataCreate(NULL, [certificateData bytes], [certificateData length]);
        SecCertificateRef certificateFromFile = SecCertificateCreateWithData(NULL, cfDataPath);

        if (certificateFromFile) {
            SecPolicyRef secPolicy = SecPolicyCreateBasicX509();
            SecTrustRef trust;
            SecTrustCreateWithCertificates( certificateFromFile, secPolicy, &trust);
            SecTrustResultType resultType;
            SecTrustEvaluate(trust, &resultType);
            SecKeyRef publicKeyObj = SecTrustCopyPublicKey(trust);

            return publicKeyObj;
        }
    }

    return nil;
}

cfDataPath 中有数据,但 certificateFromFile 始终为 nil...

有谁知道问题出在哪里吗?

最佳答案

Apple 文档引用:

Obtaining a SecKeyRef Object for Public Key Cryptography Extracting Keys from the Keychain If you are using existing public and private keys from your keychain, read Certificate, Key, and Trust Services Programming Guide to learn how to retrieve a SecKeychainItemRef object for that key. Once you have obtained a SecKeychainItemRef, you can cast it to a SecKeyRef for use with this API. Importing Existing Public and Private Keys Importing and exporting public and private key pairs is somewhat more complicated than generating new keys because of the number of different key formats in common use. This example describes how to import and export a key pair in PEM (Privacy Enhanced Mail) format.

了解更多:https://developer.apple.com/library/mac/documentation/Security/Conceptual/SecTransformPG/SigningandVerifying/SigningandVerifying.htmlhttps://developer.apple.com/library/mac/documentation/Security/Conceptual/CertKeyTrustProgGuide/01introduction/introduction.html#//apple_ref/doc/uid/TP40001358

试试这个:

  -(BOOL)trustCertFromChallenge:(NSURLAuthenticationChallenge *)challenge
 {
SecTrustResultType trustResult;
SecTrustRef trust = challenge.protectionSpace.serverTrust;
OSStatus status = SecTrustEvaluate(trust, &trustResult);

//DLog(@"Failed: %@",error.localizedDescription);
//DLog(@"Status: %li | Trust: %@ - %li",(long)status,trust,(long)trustResult);

if (status == 0 && (trustResult == kSecTrustResultUnspecified || trustResult == kSecTrustResultProceed)) {

    SecKeyRef serverKey = SecTrustCopyPublicKey(trust);

    NSString *certPath = [[NSBundle mainBundle] pathForResource:@"MYCert" ofType:@"der"];
    NSData *certData = [NSData dataWithContentsOfFile:certPath];
    SecCertificateRef localCertificate = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)certData);

    SecKeyRef localKey = NULL;
    SecTrustRef localTrust = NULL;
    SecCertificateRef certRefs[1] = {localCertificate};
    CFArrayRef certArray = CFArrayCreate(kCFAllocatorDefault, (void *)certRefs, 1, NULL);
    SecPolicyRef policy = SecPolicyCreateBasicX509();
    OSStatus status = SecTrustCreateWithCertificates(certArray, policy, &localTrust);

    if (status == errSecSuccess)
        localKey = SecTrustCopyPublicKey(localTrust);

    CFRelease(localTrust);
    CFRelease(policy);
    CFRelease(certArray);

     if (serverKey != NULL && localKey != NULL && [(__bridge id)serverKey isEqual:(__bridge id)localKey])
        return YES;
    else
        return NO;
}

//DLog(@"Failed: %@",error.localizedDescription);

return NO;
  }

按照已接受的答案了解更多详情:Objective-C / C pulling private key (modulus) from SecKeyRef

关于ios - 从证书中获取公钥/私钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40175481/

相关文章:

iOS - 使用 AVAudioSessionCategoryAmbient 和 duckOthers 和 mixWithOthers 开启静音

ios - 将导航栏项目按钮设置为新标题

ios - 在 iOS 中使用数组解析 JSON 时出错

ios - 直接发送消息而不显示 MFMessageComposeViewController

apache - 极限挑战失败。来自 http ://service. domain1.com/.well-known/acme-challenge/xWsuGIi0JmuEuDzS5qPkVX3oHuzY2kNl0YGoU6HltRg 的无效响应

ssl - 在 WHM 的主域上使用 SSL

java - 在 tomcat 中安装 GoDaddy SSL 证书...没有证书与私钥匹配

ios - 在应用程序中更改 AVAudioSession 模式

iphone - 如何检测特定区域的触摸

ios - GCD : cancelation of blocks in global concurrent queue