Refactor generation of mbedTLS errors

This commit is contained in:
Sergey Abramchuk
2018-01-18 16:39:41 +03:00
parent 761564a028
commit eff0bccfef
7 changed files with 54 additions and 115 deletions
-15
View File
@@ -1,15 +0,0 @@
//
// NSError+Message.h
// OpenVPN Adapter
//
// Created by Sergey Abramchuk on 06.09.17.
//
//
#import <Foundation/Foundation.h>
@interface NSError (Message)
+ (NSString *)reasonFromResult:(NSInteger)result;
@end
-28
View File
@@ -1,28 +0,0 @@
//
// NSError+Message.m
// OpenVPN Adapter
//
// Created by Sergey Abramchuk on 06.09.17.
//
//
#import <mbedtls/error.h>
#import "NSError+Message.h"
@implementation NSError (Message)
+ (NSString *)reasonFromResult:(NSInteger)result {
size_t length = 1024;
char *buffer = malloc(length);
mbedtls_strerror(result, buffer, length);
NSString *reason = [NSString stringWithUTF8String:buffer];
free(buffer);
return reason;
}
@end
+6
View File
@@ -24,4 +24,10 @@ typedef NS_ERROR_ENUM(OpenVPNAdapterErrorDomain, OpenVPNAdapterError);
@end
@interface NSError (OpenVPNMbedTLSErrorGeneration)
+ (NSError *)ovpn_errorObjectForMbedTLSError:(NSInteger)errorCode description:(NSString *)description;
@end
NS_ASSUME_NONNULL_END
+22
View File
@@ -7,6 +7,8 @@
#import "NSError+OpenVPNError.h"
#import <mbedtls/error.h>
#import "OpenVPNError.h"
@implementation NSError (OpenVPNAdapterErrorGeneration)
@@ -179,3 +181,23 @@
}
@end
@implementation NSError (OpenVPNMbedTLSErrorGeneration)
+ (NSError *)ovpn_errorObjectForMbedTLSError:(NSInteger)errorCode description:(NSString *)description {
size_t length = 1024;
char *buffer = malloc(length);
mbedtls_strerror(errorCode, buffer, length);
NSString *reason = [NSString stringWithUTF8String:buffer];
free(buffer);
return [NSError errorWithDomain:OpenVPNIdentityErrorDomain code:errorCode userInfo:@{
NSLocalizedDescriptionKey: description,
NSLocalizedFailureReasonErrorKey: reason
}];
}
@end
+9 -24
View File
@@ -5,13 +5,12 @@
// Created by Sergey Abramchuk on 06.09.17.
//
//
#import "OpenVPNCertificate.h"
#import <mbedtls/x509_crt.h>
#import <mbedtls/pem.h>
#import "NSError+Message.h"
#import "OpenVPNError.h"
#import "OpenVPNCertificate.h"
#import "NSError+OpenVPNError.h"
@interface OpenVPNCertificate ()
@@ -39,11 +38,7 @@
int result = mbedtls_x509_crt_parse(certificate.crt, (const unsigned char *)pemString.UTF8String, pemData.length + 1);
if (result < 0) {
if (error) {
NSString *reason = [NSError reasonFromResult:result];
*error = [NSError errorWithDomain:OpenVPNIdentityErrorDomain code:result userInfo:@{
NSLocalizedDescriptionKey: @"Failed to read PEM data.",
NSLocalizedFailureReasonErrorKey: reason
}];
*error = [NSError ovpn_errorObjectForMbedTLSError:result description:@"Failed to read PEM data"];
}
return nil;
@@ -58,11 +53,7 @@
int result = mbedtls_x509_crt_parse_der(certificate.crt, derData.bytes, derData.length);
if (result < 0) {
if (error) {
NSString *reason = [NSError reasonFromResult:result];
*error = [NSError errorWithDomain:OpenVPNIdentityErrorDomain code:result userInfo:@{
NSLocalizedDescriptionKey: @"Failed to read DER data.",
NSLocalizedFailureReasonErrorKey: reason
}];
*error = [NSError ovpn_errorObjectForMbedTLSError:result description:@"Failed to read DER data"];
}
return nil;
@@ -80,14 +71,11 @@
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);
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
}];
*error = [NSError ovpn_errorObjectForMbedTLSError:result description: @"Failed to write PEM data"];
}
free(pem_buffer);
@@ -103,11 +91,8 @@
- (NSData *)derData:(out NSError **)error {
if (self.crt->raw.p == NULL || self.crt->raw.len == 0) {
if (error) {
NSString *reason = [NSError reasonFromResult:MBEDTLS_ERR_X509_BAD_INPUT_DATA];
*error = [NSError errorWithDomain:OpenVPNIdentityErrorDomain code:MBEDTLS_ERR_X509_BAD_INPUT_DATA userInfo:@{
NSLocalizedDescriptionKey: @"Failed to write DER data.",
NSLocalizedFailureReasonErrorKey:reason
}];
*error = [NSError ovpn_errorObjectForMbedTLSError:MBEDTLS_ERR_X509_BAD_INPUT_DATA
description: @"Failed to write DER data"];
}
return nil;
+15 -26
View File
@@ -6,11 +6,11 @@
//
//
#import "OpenVPNPrivateKey.h"
#import <mbedtls/pk.h>
#import "NSError+Message.h"
#import "OpenVPNError.h"
#import "OpenVPNPrivateKey.h"
#import "NSError+OpenVPNError.h"
@interface OpenVPNPrivateKey ()
@@ -45,14 +45,12 @@
size_t pem_length = strlen(pemString.UTF8String) + 1;
size_t password_length = password != nil ? strlen(password.UTF8String) : 0;
int result = mbedtls_pk_parse_key(key.ctx, (const unsigned char *)pemString.UTF8String, pem_length, (const unsigned char *)password.UTF8String, password_length);
int result = mbedtls_pk_parse_key(key.ctx, (const unsigned char *)pemString.UTF8String,
pem_length, (const unsigned char *)password.UTF8String, password_length);
if (result < 0) {
if (error) {
NSString *reason = [NSError reasonFromResult:result];
*error = [NSError errorWithDomain:OpenVPNIdentityErrorDomain code:result userInfo:@{
NSLocalizedDescriptionKey: @"Failed to read PEM data.",
NSLocalizedFailureReasonErrorKey: reason
}];
*error = [NSError ovpn_errorObjectForMbedTLSError:result description:@"Failed to read PEM data"];
}
return nil;
@@ -66,14 +64,12 @@
size_t password_length = password != nil ? strlen(password.UTF8String) : 0;
int result = mbedtls_pk_parse_key(key.ctx, derData.bytes, derData.length, (const unsigned char *)password.UTF8String, password_length);
int result = mbedtls_pk_parse_key(key.ctx, derData.bytes,
derData.length, (const unsigned char *)password.UTF8String, password_length);
if (result < 0) {
if (error) {
NSString *reason = [NSError reasonFromResult:result];
*error = [NSError errorWithDomain:OpenVPNIdentityErrorDomain code:result userInfo:@{
NSLocalizedDescriptionKey: @"Failed to read DER data.",
NSLocalizedFailureReasonErrorKey: reason
}];
*error = [NSError ovpn_errorObjectForMbedTLSError:result description:@"Failed to read DER data"];
}
return nil;
@@ -89,18 +85,15 @@
int result = mbedtls_pk_write_key_pem(self.ctx, pem_buffer, buffer_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
}];
*error = [NSError ovpn_errorObjectForMbedTLSError:result description:@"Failed to write PEM data"];
}
free(pem_buffer);
return nil;
}
NSData *pemData = [[NSString stringWithCString:(const char *)pem_buffer encoding:NSUTF8StringEncoding] dataUsingEncoding:NSUTF8StringEncoding];
NSData *pemData = [[NSString stringWithCString:(const char *)pem_buffer
encoding:NSUTF8StringEncoding] dataUsingEncoding:NSUTF8StringEncoding];
free(pem_buffer);
return pemData;
@@ -113,11 +106,7 @@
int result = mbedtls_pk_write_key_der(self.ctx, der_buffer, buffer_length);
if (result < 0) {
if (error) {
NSString *reason = [NSError reasonFromResult:result];
*error = [NSError errorWithDomain:OpenVPNIdentityErrorDomain code:result userInfo:@{
NSLocalizedDescriptionKey: @"Failed to write DER data.",
NSLocalizedFailureReasonErrorKey: reason
}];
*error = [NSError ovpn_errorObjectForMbedTLSError:result description:@"Failed to write DER data"];
}
free(der_buffer);