This commit is contained in:
Ben Toews 2017-02-02 18:27:08 -07:00
Родитель 007eee7e98
Коммит 3aec00cfea
5 изменённых файлов: 152 добавлений и 152 удалений

3
.clang-format Normal file
Просмотреть файл

@ -0,0 +1,3 @@
BasedOnStyle: LLVM
ColumnLimit: 0
BreakBeforeBraces: Attach

@ -1 +1 @@
Subproject commit ba2682d64b5b9a6fbb163f743d9ddcb3c1f18925
Subproject commit ff64767d1a559183695e64e1fa2c3e340a44e27c

Просмотреть файл

@ -8,21 +8,22 @@
#import <Foundation/Foundation.h>
#undef I // complex.h defines some crazy `I` macro...
#import <openssl/x509.h>
#import <openssl/asn1.h>
#import <openssl/ec.h>
#import <openssl/ecdsa.h>
#import <openssl/evp.h>
#import <openssl/objects.h>
#import <openssl/asn1.h>
#import <openssl/pem.h>
#import <openssl/x509.h>
@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

Просмотреть файл

@ -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

Просмотреть файл

@ -1,7 +1,7 @@
#import <CommonCrypto/CommonCrypto.h>
#import <Security/Security.h>
#import <Security/SecKey.h>
#import "SelfSignedCertificate.h"
#import "softu2f.h"
#import "u2f.h"
#import "u2f_hid.h"
#import <CommonCrypto/CommonCrypto.h>
#import <Security/SecKey.h>
#import <Security/Security.h>