gpt4 book ai didi

ios - 如何在 Objective-C 中硬编码 .cer 证书?

转载 作者:行者123 更新时间:2023-11-29 13:55:12 24 4
gpt4 key购买 nike

我一直在尝试使用为所需功能构建 native 代码的插件为混合应用程序实现 SSL Pinning。

在生成的 native 项目中,Objective-C 代码包含以下函数,用于从 www/certificates 位置读取证书文件 (X.509 DER .cer) .

// AFSecurityPolicy.m    
+ (NSSet *)certificatesInBundle:(NSBundle *)bundle {
NSArray *paths = [bundle pathsForResourcesOfType:@"cer" inDirectory:@"www/certificates"];
NSMutableSet *certificates = [NSMutableSet setWithCapacity:[paths count]];

for (NSString *path in paths) {
NSData *certificateData = [NSData dataWithContentsOfFile:path];
[certificates addObject:certificateData];
}

return [NSSet setWithSet:certificates];
}

但由于插件中的一个已知错误,此位置不会自动创建(理想情况下应该自动创建)。作为解决方法,我想将 .cer 证书内容硬编码到上述函数中。

我该怎么做?

(以上代码的修改版本会很有帮助)

可以找到样本证书here在 Github 上。

披露:我是移动编程的新手,非常感谢这方面的任何帮助。

最佳答案

您可以使用字符串来存储证书的 base64 表示形式(也称为 PEM 格式)并将其转换为 NSData:

NSString *originalString = [NSString stringWithFormat:@"MIIIHTCCBwWgAwIBAgIIF947UueTr/4wDQYJKoZIhvcNAQELBQAwgcExCzAJBgNVBAYTAlBUMTMwMQYDVQQKDCpJbnN0aXR1dG8gZG9zIFJlZ2lzdG9zIGUgZG8gTm90YXJpYWRvIEkuUC4xHDAaBgNVBAsME0NhcnTDo28gZGUgQ2lkYWTDo28xFDASBgNVBAsMC3N1YkVDRXN0YWRvMUkwRwYDVQQDDEBFQyBkZSBBc3NpbmF0dXJhIERpZ2l0YWwgUXVhbGlmaWNhZGEgZG8gQ2FydMOjbyBkZSBDaWRhZMOjbyAwMDE0MB4XDTE4MDcwOTExNDc1OVoXDTIzMDcwOTIxNTkwMFowggEEMQswCQYDVQQGEwJQVDEcMBoGA1UECgwTQ2FydMOjbyBkZSBDaWRhZMOjbzErMCkGA1UECwwiQXNzaW5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r3rqhlt8sN5vgOl1jzZg7jnPfU/9RQtNqKJm6E/Vwbi5r9KVVFQijULzt92JdTGHzrD3WoCoZfKfZzMsevJW2sSHQAWkJiogckUu5nTv3eARvU8Sfoy5GlNYeYYeIbbODsWiD1M5FRqJEsThKEeVgH+ejDQMls897+fYNE/ZAmIiGzjJM4WPR7huGkjHsBBolgg/Rz0cyMmHeejEHzs="]; 
NSData *data = [NSData dataFromBase64String:originalString];

关于ios - 如何在 Objective-C 中硬编码 .cer 证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56899559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com