From 3aec00cfeaeb5ae4d16d03d2c7512ad011a1843a Mon Sep 17 00:00:00 2001 From: Ben Toews Date: Thu, 2 Feb 2017 18:27:08 -0700 Subject: [PATCH] run clang-format --- .clang-format | 3 + SoftU2F | 2 +- SoftU2FTool/SelfSignedCertificate.h | 17 +- SoftU2FTool/SelfSignedCertificate.m | 276 +++++++++++----------- SoftU2FTool/SoftU2FTool-Bridging-Header.h | 6 +- 5 files changed, 152 insertions(+), 152 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..94fd78e --- /dev/null +++ b/.clang-format @@ -0,0 +1,3 @@ +BasedOnStyle: LLVM +ColumnLimit: 0 +BreakBeforeBraces: Attach diff --git a/SoftU2F b/SoftU2F index ba2682d..ff64767 160000 --- a/SoftU2F +++ b/SoftU2F @@ -1 +1 @@ -Subproject commit ba2682d64b5b9a6fbb163f743d9ddcb3c1f18925 +Subproject commit ff64767d1a559183695e64e1fa2c3e340a44e27c diff --git a/SoftU2FTool/SelfSignedCertificate.h b/SoftU2FTool/SelfSignedCertificate.h index a523773..5db1b43 100644 --- a/SoftU2FTool/SelfSignedCertificate.h +++ b/SoftU2FTool/SelfSignedCertificate.h @@ -8,21 +8,22 @@ #import #undef I // complex.h defines some crazy `I` macro... -#import +#import #import #import #import #import -#import #import +#import -@interface SelfSignedCertificate : NSObject; -@property EVP_PKEY* pkey; -@property X509* x509; +@interface SelfSignedCertificate : NSObject + +@property EVP_PKEY *pkey; +@property X509 *x509; - (id)init; -- (NSData*)toDer; -- (NSData*)signData:(NSData*)msg; -+ (bool)parseX509:(NSData*)data consumed:(NSInteger *)consumed; +- (NSData *)toDer; +- (NSData *)signData:(NSData *)msg; ++ (bool)parseX509:(NSData *)data consumed:(NSInteger *)consumed; @end diff --git a/SoftU2FTool/SelfSignedCertificate.m b/SoftU2FTool/SelfSignedCertificate.m index 8c1b011..c026494 100644 --- a/SoftU2FTool/SelfSignedCertificate.m +++ b/SoftU2FTool/SelfSignedCertificate.m @@ -12,148 +12,144 @@ @implementation SelfSignedCertificate -- (id)init -{ - self = [super init]; - if (self) { - if ([self generateKeyPair] && [self generateX509]) { - printf("SelfSignedCertificate initialized\n"); - } else { - printf("Error initializing SelfSignedCertificate\n"); - } - } - return self; -} - -- (int)generateX509 -{ - self.x509 = X509_new(); - if (self.x509 == NULL) { - printf("failed to init x509\n"); - return 0; - } - - X509_set_version(self.x509, 2); - ASN1_INTEGER_set(X509_get_serialNumber(self.x509), 1); - X509_gmtime_adj(X509_get_notBefore(self.x509), 0); - X509_gmtime_adj(X509_get_notAfter(self.x509),(long)60*60*24*1); - - X509_NAME* name = X509_get_subject_name(self.x509); - X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (const unsigned char*)"mastahyeti", -1, -1, 0); - - X509_set_issuer_name(self.x509, name); - - if (!X509_set_pubkey(self.x509, self.pkey)) { - printf("failed to set public key.\n"); - return 0; - } - - if (!X509_sign(self.x509, self.pkey, EVP_sha256())) { - printf("failed to sign cert\n"); - return 0; - } - - return 1; -} - -- (int)generateKeyPair -{ - self.pkey = EVP_PKEY_new(); - if (self.pkey == NULL) { - printf("failed to init pkey\n"); - return 0; - } - - EC_KEY *ec = EC_KEY_new(); - if (ec == NULL) { - printf("EC_KEY_new failed\n"); - return 0; - } - - EC_GROUP *ecg = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); - if (ecg == NULL) { - printf("EC_GROUP_new_by_curve_name failed\n"); - return 0; - } - - EC_GROUP_set_asn1_flag(ecg, NID_X9_62_prime256v1); - EC_KEY_set_group(ec, ecg); - - if (EC_KEY_generate_key(ec) != 1) { - printf("couldn't generate ec key\n"); - return 0; - } - - if (EC_KEY_check_key(ec) != 1) { - printf("error checking key\n"); - return 0; - } - - if (EVP_PKEY_assign_EC_KEY(self.pkey, ec) != 1) { - printf("failed to assing ec to pkey\n"); - EC_KEY_free(ec); - return 0; - } - - return 1; -} - -- (NSData*)toDer -{ - unsigned char* buf = NULL; - unsigned int len = i2d_X509(self.x509, &buf); - return [[NSData alloc] initWithBytes: buf length: len]; -} - -- (NSData*)signData:(NSData*)msg -{ - EVP_MD_CTX ctx; - const unsigned char* cmsg = (const unsigned char*)[msg bytes]; - unsigned char* sig = (unsigned char*)malloc(EVP_PKEY_size(self.pkey)); - unsigned int len; - - if (EVP_SignInit(&ctx, EVP_sha256()) != 1) { - free(sig); - printf("failed to init signing context\n"); - return nil; - }; - - if (EVP_SignUpdate(&ctx, cmsg, (unsigned int)[msg length]) != 1) { - free(sig); - printf("failed to update digest\n"); - return nil; - } - - if (EVP_SignFinal(&ctx, sig, &len, self.pkey) != 1) { - free(sig); - printf("failed to finalize digest\n"); - return nil; - } - - return [[NSData alloc] initWithBytes:sig length:len]; -} - -- (void)dealloc -{ - X509_free(self.x509); self.x509 = NULL; - EVP_PKEY_free(self.pkey); self.pkey = NULL; -} - -+ (bool)parseX509:(NSData*)data consumed:(NSInteger *)consumed; -{ - X509 *crt = NULL; - const unsigned char *crtStart, *crtEnd; - crtStart = crtEnd = [data bytes]; - - d2i_X509(&crt, &crtEnd, [data length]); - - if (crt == NULL) { - return false; +- (id)init { + self = [super init]; + if (self) { + if ([self generateKeyPair] && [self generateX509]) { + printf("SelfSignedCertificate initialized\n"); } else { - X509_free(crt); - *consumed = crtEnd - crtStart; - return true; + printf("Error initializing SelfSignedCertificate\n"); } + } + return self; +} + +- (int)generateX509 { + self.x509 = X509_new(); + if (self.x509 == NULL) { + printf("failed to init x509\n"); + return 0; + } + + X509_set_version(self.x509, 2); + ASN1_INTEGER_set(X509_get_serialNumber(self.x509), 1); + X509_gmtime_adj(X509_get_notBefore(self.x509), 0); + X509_gmtime_adj(X509_get_notAfter(self.x509), (long)60 * 60 * 24 * 1); + + X509_NAME *name = X509_get_subject_name(self.x509); + X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (const unsigned char *)"mastahyeti", -1, -1, 0); + + X509_set_issuer_name(self.x509, name); + + if (!X509_set_pubkey(self.x509, self.pkey)) { + printf("failed to set public key.\n"); + return 0; + } + + if (!X509_sign(self.x509, self.pkey, EVP_sha256())) { + printf("failed to sign cert\n"); + return 0; + } + + return 1; +} + +- (int)generateKeyPair { + self.pkey = EVP_PKEY_new(); + if (self.pkey == NULL) { + printf("failed to init pkey\n"); + return 0; + } + + EC_KEY *ec = EC_KEY_new(); + if (ec == NULL) { + printf("EC_KEY_new failed\n"); + return 0; + } + + EC_GROUP *ecg = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); + if (ecg == NULL) { + printf("EC_GROUP_new_by_curve_name failed\n"); + return 0; + } + + EC_GROUP_set_asn1_flag(ecg, NID_X9_62_prime256v1); + EC_KEY_set_group(ec, ecg); + + if (EC_KEY_generate_key(ec) != 1) { + printf("couldn't generate ec key\n"); + return 0; + } + + if (EC_KEY_check_key(ec) != 1) { + printf("error checking key\n"); + return 0; + } + + if (EVP_PKEY_assign_EC_KEY(self.pkey, ec) != 1) { + printf("failed to assing ec to pkey\n"); + EC_KEY_free(ec); + return 0; + } + + return 1; +} + +- (NSData *)toDer { + unsigned char *buf = NULL; + unsigned int len = i2d_X509(self.x509, &buf); + return [[NSData alloc] initWithBytes:buf length:len]; +} + +- (NSData *)signData:(NSData *)msg { + EVP_MD_CTX ctx; + const unsigned char *cmsg = (const unsigned char *)[msg bytes]; + unsigned char *sig = (unsigned char *)malloc(EVP_PKEY_size(self.pkey)); + unsigned int len; + + if (EVP_SignInit(&ctx, EVP_sha256()) != 1) { + free(sig); + printf("failed to init signing context\n"); + return nil; + }; + + if (EVP_SignUpdate(&ctx, cmsg, (unsigned int)[msg length]) != 1) { + free(sig); + printf("failed to update digest\n"); + return nil; + } + + if (EVP_SignFinal(&ctx, sig, &len, self.pkey) != 1) { + free(sig); + printf("failed to finalize digest\n"); + return nil; + } + + return [[NSData alloc] initWithBytes:sig length:len]; +} + +- (void)dealloc { + X509_free(self.x509); + self.x509 = NULL; + EVP_PKEY_free(self.pkey); + self.pkey = NULL; +} + ++ (bool)parseX509:(NSData *)data consumed:(NSInteger *)consumed; +{ + X509 *crt = NULL; + const unsigned char *crtStart, *crtEnd; + crtStart = crtEnd = [data bytes]; + + d2i_X509(&crt, &crtEnd, [data length]); + + if (crt == NULL) { + return false; + } else { + X509_free(crt); + *consumed = crtEnd - crtStart; + return true; + } } @end diff --git a/SoftU2FTool/SoftU2FTool-Bridging-Header.h b/SoftU2FTool/SoftU2FTool-Bridging-Header.h index 815658d..8c2a89b 100644 --- a/SoftU2FTool/SoftU2FTool-Bridging-Header.h +++ b/SoftU2FTool/SoftU2FTool-Bridging-Header.h @@ -1,7 +1,7 @@ -#import -#import -#import #import "SelfSignedCertificate.h" #import "softu2f.h" #import "u2f.h" #import "u2f_hid.h" +#import +#import +#import