3
我試圖從iOS中的各種URL讀取證書。然而,我的代碼工作不正常 - 應該返回我需要的信息的數組總是返回null
。在iOS上閱讀證書問題
我錯過了什麼?
- (void)findCertificate:(NSString *)url
{
NSInputStream*input = [[NSInputStream inputStreamWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://store.writeitstudios.com"]]] retain];
[input setDelegate:self];
[input scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[input open];
NSLog(@"Status: %i",[input streamStatus]);
}
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
NSLog(@"handle Event: %i",eventCode);
if (eventCode == NSStreamStatusOpen)
{
NSArray *certificates = (NSArray*)CFReadStreamCopyProperty((CFReadStreamRef)aStream, kCFStreamPropertySSLPeerCertificates);
NSLog(@"Certs: %@",CFReadStreamCopyProperty((CFReadStreamRef)aStream, kCFStreamPropertySSLPeerCertificates));
if ([certificates count] > 0) {
SecCertificateRef certificate = (SecCertificateRef)[certificates objectAtIndex:0];
NSString *description = (NSString*)SecCertificateCopySubjectSummary(certificate);
NSData *data = (NSData *)SecCertificateCopyData(certificate);
NSLog(@"Description: %@",description);
}
}
}
是的,我知道我在泄漏記憶。這只是一個片段。
這個腳本非常適用於網站,如貝寶,但不返回https://store.writeitstudios.com的結果。這是否只承認最常見的證書頒發機構頒發的證書? – Pripyat
它在我的網站上工作。你確定你使用的是https網址嗎?委託方法是否被調用? – puzzle
確實調用了委託方法。它基本上只是調用didStartConnection和didFinishConnection。它適用於大多數網址,但出於某種原因不適用於該網址。 – Pripyat