Implement writing certificate PEM data

This commit is contained in:
Sergey Abramchuk
2017-09-06 23:29:06 +03:00
parent 18a41d88d1
commit 7cbb69fed5
2 changed files with 28 additions and 0 deletions

View File

@@ -18,4 +18,6 @@
- (nonnull instancetype) __unavailable init;
- (nullable NSData *)pemData:(out NSError * __nullable * __nullable)error;
@end

View File

@@ -7,6 +7,7 @@
//
#import <mbedtls/x509_crt.h>
#import <mbedtls/pem.h>
#import "NSError+Message.h"
#import "OpenVPNError.h"
@@ -70,6 +71,31 @@
return certificate;
}
- (NSData *)pemData:(out NSError **)error {
NSString *header = @"-----BEGIN CERTIFICATE-----\n";
NSString *footer = @"-----END CERTIFICATE-----\n";
size_t buffer_length = self.crt->raw.len * 2;
unsigned char *pem_buffer = malloc(buffer_length);
size_t output_length = 0;
int result = mbedtls_pem_write_buffer(header.UTF8String, footer.UTF8String, self.crt->raw.p, self.crt->raw.len, pem_buffer, buffer_length, &output_length);
if (result < 0) {
if (error) {
NSString *reason = [NSError reasonFromResult:result];
*error = [NSError errorWithDomain:OpenVPNIdentityErrorDomain code:result userInfo:@{
NSLocalizedDescriptionKey: @"Failed to write PEM data.",
NSLocalizedFailureReasonErrorKey: reason
}];
}
return nil;
}
return [NSData dataWithBytes:pem_buffer length:output_length];
}
- (void)dealloc {
mbedtls_x509_crt_free(self.crt);
free(self.crt);