我一直在尝试使用可为所需功能构建本机代码的插件为混合应用程序实现SSL固定。
在生成的本机项目中,Objective-C代码包括以下功能,用于从X.509 DER .cer
位置读取证书文件(www/certificates
)。
// 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
证书内容硬编码为上述功能。
我该怎么做?
(上面代码的修改版将非常有帮助)
可以在Github上here找到示例证书。
披露:我对移动编程非常陌生,感谢在这方面的各种帮助。
答案 0 :(得分:0)
您可以使用字符串存储证书的base64表示形式(也称为PEM格式)并将其转换为NSData
:
NSString *originalString = [NSString stringWithFormat:@"MIIIHTCCBwWgAwIBAgIIF947UueTr/4wDQYJKoZIhvcNAQELBQAwgcExCzAJBgNVBAYTAlBUMTMwMQYDVQQKDCpJbnN0aXR1dG8gZG9zIFJlZ2lzdG9zIGUgZG8gTm90YXJpYWRvIEkuUC4xHDAaBgNVBAsME0NhcnTDo28gZGUgQ2lkYWTDo28xFDASBgNVBAsMC3N1YkVDRXN0YWRvMUkwRwYDVQQDDEBFQyBkZSBBc3NpbmF0dXJhIERpZ2l0YWwgUXVhbGlmaWNhZGEgZG8gQ2FydMOjbyBkZSBDaWRhZMOjbyAwMDE0MB4XDTE4MDcwOTExNDc1OVoXDTIzMDcwOTIxNTkwMFowggEEMQswCQYDVQQGEwJQVDEcMBoGA1UECgwTQ2FydMOjbyBkZSBDaWRhZMOjbzErMCkGA1UECwwiQXNzaW5hdHVyYSBRdWFsaWZpY2FkYSBkbyBDaWRhZMOjbzEcMBoGA1UECwwTQ2lkYWTDo28gUG9ydHVndcOqczEuMCwGA1UEBAwl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iogckUu5nTv3eARvU8Sfoy5GlNYeYYeIbbODsWiD1M5FRqJEsThKEeVgH+ejDQMls897+fYNE/ZAmIiGzjJM4WPR7huGkjHsBBolgg/Rz0cyMmHeejEHzs="];
NSData *data = [NSData dataFromBase64String:originalString];
答案 1 :(得分:0)
由于上述证书是公开的且格式为X.509 DER .cer
,因此请执行以下操作将其存储为NSData
。
NSURL *url = [NSURL URLWithString:@"https://github.com/ashenwgt/ionic-capacitor-ssl-pinning/blob/master/certificates/reqres-cert.cer?raw=true"];
NSData *data = [NSData dataWithContentsOfURL:url];
要将其转换为有效的字符串,您可以记录NSData
的值并按如下所示复制输出。
NSLog(@"[LOG] CERT data from URL: %@", data);
现在,您可以将证书分配为字符串,并且最终代码将仅包含以下两行(我仅使用上述步骤将证书作为硬编码的有效字符串来获取)。
NSString *certString = @"<308208ea 3082088f a0030201 02021100 c61bedf6 f67f899b 15b2f6ff 73860076 300a0608 2a8648ce 3d040302 30819231 0b300906 03550406 13024742 311b3019 06035504 08131247 72656174 6572204d 616e6368 65737465 72311030 0e060355 04071307 53616c66 6f726431 1a301806 0355040a 1311434f 4d4f444f 20434120 4c696d69 74656431 38303606 03550403 132f434f 4d4f444f 20454343 20446f6d 61696e20 56616c69 64617469 6f6e2053 65637572 65205365 72766572 20434120 32301e17 0d313930 36313930 30303030 305a170d 31393132 32363233 35393539 5a306b31 21301f06 0355040b 1318446f 6d61696e 20436f6e 74726f6c 2056616c 69646174 65643121 301f0603 55040b13 18506f73 69746976 6553534c 204d756c 74692d44 6f6d6169 6e312330 21060355 0403131a 736e6939 36323836 2e636c6f 7564666c 61726573 736c2e63 6f6d3059 30130607 2a8648ce 3d020106 082a8648 ce3d0301 07034200 04d0315a 09030c1e 04fd1553 3c1613a0 6da3e04b fe2d1c44 6050519c 54417349 de043de7 cab599ad bb0dc949 98117112 ae0aef3c d7fae279 8b3ce81a d7020225 51a38206 ea308206 e6301f06 03551d23 04183016 80144009 6167f0bc 83714fde 12082c6f d4d42b76 3d96301d 0603551d 0e041604 141cb795 c12ad6e9 5f06cbd8 c91a72be 57d21f4c 3b300e06 03551d0f 0101ff04 04030207 80300c06 03551d13 0101ff04 02300030 1d060355 1d250416 30140608 2b060105 05070301 06082b06 01050507 0302304f 0603551d 20044830 46303a06 0b2b0601 0401b231 01020207 302b3029 06082b06 01050507 0201161d 68747470 733a2f2f 73656375 72652e63 6f6d6f64 6f2e636f 6d2f4350 53300806 0667810c 01020130 56060355 1d1f044f 304d304b a049a047 86456874 74703a2f 2f63726c 2e636f6d 6f646f63 61342e63 6f6d2f43 4f4d4f44 4f454343 446f6d61 696e5661 6c696461 74696f6e 53656375 72655365 72766572 4341322e 63726c30 81880608 2b060105 05070101 047c307a 30510608 2b060105 05073002 86456874 74703a2f 2f637274 2e636f6d 6f646f63 61342e63 6f6d2f43 4f4d4f44 4f454343 446f6d61 696e5661 6c696461 74696f6e 53656375 72655365 72766572 4341322e 63727430 2506082b 06010505 07300186 19687474 703a2f2f 6f637370 2e636f6d 6f646f63 61342e63 6f6d3082 04290603 551d1104 82042030 82041c82 1a736e69 39363238 362e636c 6f756466 6c617265 73736c2e 636f6d82 0a2a2e61 6c797061 2e636c82 1f2a2e61 72726564 616d656e 746f6261 722e616c 74657276 69737461 2e6f7267 82102a2e 62616c6c 6f6f726e 7373712e 746b8212 2a2e6361 6e64666f 7274636f 6763692e 6d6c820e 2a2e6365 74696f6e 746f702e 746b820b 2a2e6468 636d782e 636f6d82 142a2e64 6f67746b 6c6f6e66 65726376 65722e63 6682112a 2e676574 74686564 6a6f6273 712e6761 820e2a2e 676f6777 616d6972 692e6761 82162a2e 68757274 776f726c 642d7365 72766572 732e7275 82172a2e 696e7374 696e746f 62617369 636f2e63 6f6d2e62 72820b2a 2e6a6564 7a746f2e 6761820d 2a2e6b65 6d697979 72752e63 6682152a 2e6d7572 646f6368 66616d69 6c792e69 642e6175 821d2a2e 70726563 6973696f 6e636c65 616e696e 67686177 6169692e 636f6d82 0e2a2e72 616e616c 656d6469 2e636682 0b2a2e72 65717265 732e696e 82152a2e 72657363 61746576 616c656e 6369612e 6f726782 102a2e72 6f72696d 61712e63 6f6d2e62 72820e2a 2e736965 6d6f6e2e 69642e61 75820b2a 2e746574 6574652e 72758217 2a2e7468 72697665 6e6f7773 656d696e 6172732e 636f6d82 102a2e74 696f7472 6f6d696e 74692e67 6182102a 2e746976 70696865 6c707371 2e636682 102a2e74 6f6e7973 6e61696c 732e636f 6d82122a 2e74726f 63686261 6e6e6f6d 65722e63 6682162a 2e766973 696f6e64 6566656e 6465722e 636f2e75 6b82102a 2e796d79 68796a79 64657879 2e746b82 08616c79 70612e63 6c821d61 72726564 616d656e 746f6261 722e616c 74657276 69737461 2e6f7267 820e6261 6c6c6f6f 726e7373 712e746b 82106361 6e64666f 7274636f 6763692e 6d6c820c 63657469 6f6e746f 702e746b 82096468 636d782e 636f6d82 12646f67 746b6c6f 6e666572 63766572 2e636682 0f676574 74686564 6a6f6273 712e6761 820c676f 6777616d 6972692e 67618214 68757274 776f726c 642d7365 72766572 732e7275 8215696e 7374696e 746f6261 7369636f 2e636f6d 2e627282 096a6564 7a746f2e 6761820b 6b656d69 79797275 2e636682 136d7572 646f6368 66616d69 6c792e69 642e6175 821b7072 65636973 696f6e63 6c65616e 696e6768 61776169 692e636f 6d820c72 616e616c 656d6469 2e636682 09726571 7265732e 696e8213 72657363 61746576 616c656e 6369612e 6f726782 0e726f72 696d6171 2e636f6d 2e627282 0c736965 6d6f6e2e 69642e61 75820974 65746574 652e7275 82157468 72697665 6e6f7773 656d696e 6172732e 636f6d82 0e74696f 74726f6d 696e7469 2e676182 0e746976 70696865 6c707371 2e636682 0e746f6e 79736e61 696c732e 636f6d82 1074726f 63686261 6e6e6f6d 65722e63 66821476 6973696f 6e646566 656e6465 722e636f 2e756b82 0e796d79 68796a79 64657879 2e746b30 82010406 0a2b0601 0401d679 02040204 81f50481 f200f000 7600bbd9 dfbc1f8a 71b59394 2397aa92 7b473857 950aab52 e81a9096 64368e1e d1850000 016b6ffc b6700000 04030047 30450220 75c19e82 18d0f6d1 8dd17a61 318b17de 19e5c1fb c5599592 aca9c03a 1ee1b285 0221008b 0a19bab4 b4edb220 9a3fd494 08995e2b 6bf5c2c0 228425a7 cf03dda2 455e0b00 7600747e da8331ad 33109121 9cce254f 4270c2bf fd5e4220 08c63735 79e6107b cc560000 016b6ffc b6a80000 04030047 30450221 00956fa5 31b98671 82c56123 cf942fac cbd0854a c42afc84 ee2343e1 fbed6ec1 7102207b 22acf340 3c5b2508 c343be43 6f5e48c3 35bd092f 2edc2350 bff5b488 5dffa730 0a06082a 8648ce3d 04030203 49003046 02210090 411d511a 85f2fb4e aeff6fe7 35c4c8e6 7855b0c8 96fb5a8a 6899511e d29ad602 2100f633 d4eeb830 7583721b e8d960bf e5c9fee7 19e9d4af 8626a31d 82d6fb63 a7a6>";
NSData *data = [certString dataUsingEncoding:NSUTF8StringEncoding];