Skip to content

Commit

Permalink
Implemented the new verifier and fixed CFobError.
Browse files Browse the repository at this point in the history
  • Loading branch information
Danny Greg committed Aug 24, 2010
1 parent 9f22388 commit 7ae85d8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
1 change: 1 addition & 0 deletions objc/CFobError.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// Licensed under CC Attribution Licence 3.0 <http://creativecommons.org/licenses/by/3.0/>
//

#import <Foundation/Foundation.h>

enum _CFobErrorCode {
CFobErrorCodeInvalidKey = -1,
Expand Down
56 changes: 40 additions & 16 deletions objc/CFobLicVerifier.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
// Licensed under CC Attribution License 3.0 <http://creativecommons.org/licenses/by/3.0/>
//

#import "NSString-Base64Extensions.h"
#import "NSString+PECrypt.h"
#import "CFobLicVerifier.h"

#import "CFobError.h"

#import "decoder.h"

#import "NSString-Base64Extensions.h"
#import "NSString+PECrypt.h"

#import <openssl/evp.h>
#import <openssl/err.h>
#import <openssl/pem.h>
Expand Down Expand Up @@ -84,7 +89,8 @@ - (void)finalize
[super finalize];
}

- (void)dealloc {
- (void)dealloc
{
if (self.dsa)
DSA_free(self.dsa);

Expand All @@ -96,34 +102,47 @@ - (void)dealloc {
#pragma mark -
#pragma mark API

- (BOOL)setPublicKey:(NSString *)pubKey {
- (BOOL)setPublicKey:(NSString *)pubKey error:(NSError **)err
{
// Validate the argument.
if (!pubKey || ![pubKey length]) {
self.lastError = @"Invalid key";
if (pubKey == nil || [pubKey length] < 1) {
CFobAssignErrorWithDescriptionAndCode(err, @"Invalid key.", CFobErrorCodeInvalidKey);
return NO;
}

if (self.dsa)
DSA_free(self.dsa);
self.dsa = DSA_new();

// Prepare BIO to read PEM-encoded public key from memory.
// Prepare buffer given NSString
const char *pubkeyCString = [pubKey UTF8String];
BIO *bio = BIO_new_mem_buf((void *)pubkeyCString, -1);
PEM_read_bio_DSA_PUBKEY(bio, &self.dsa, NULL, NULL);
PEM_read_bio_DSA_PUBKEY(bio, &_dsa, NULL, NULL);

BOOL result = YES;
if (!self.dsa->pub_key) {
self.lastError = @"Unable to decode key";
CFobAssignErrorWithDescriptionAndCode(err, @"Unable to decode key.", CFobErrorCodeCouldNotDecode);
result = NO;
}

// Cleanup BIO
BIO_vfree(bio);
return result;
}

- (BOOL)verify {
if (![regName length] || ![regCode length] || !self.dsa || !self.dsa->pub_key)
- (BOOL)verifyRegCode:(NSString *)regCode forName:(NSString *)name error:(NSError **)err
{
if (name == nil || [name length] < 1) {
CFobAssignErrorWithDescriptionAndCode(err, @"No name for the registration code.", CFobErrorCodeNoName);
return NO;
}

if (!self.dsa || !self.dsa->pub_key) {
CFobAssignErrorWithDescriptionAndCode(err, @"Invalid key.", CFobErrorCodeInvalidKey);
return NO;
BOOL result = NO;
}

// Replace 9s with Is and 8s with Os
NSString *regKeyTemp = [regCode stringByReplacingOccurrencesOfString:@"9" withString:@"I"];
NSString *regKeyBase32 = [regKeyTemp stringByReplacingOccurrencesOfString:@"8" withString:@"O"];
Expand All @@ -144,13 +163,18 @@ - (BOOL)verify {
return NO;
// Decode signature from Base32 to a byte buffer.
size_t sigSize = base32_decode(sig, decodeBufSize, (unsigned char *)keyBase32Utf8, base32Length);
if (!sigSize)
self.lastError = @"Unable to decode registration key";
if (!sigSize) {
CFobAssignErrorWithDescriptionAndCode(err, @"Unable to decode registration key.", CFobErrorCodeCouldNotDecode);
free(sig);
return NO;
}

// Produce a SHA-1 hash of the registration name string. This is what was signed during registration key generation.
NSData *digest = [regName sha1];
NSData *digest = [name sha1];
// Verify DSA signature.
int check = DSA_verify(0, [digest bytes], [digest length], sig, sigSize, dsa);
result = check > 0;
int check = DSA_verify(0, [digest bytes], [digest length], sig, sigSize, self.dsa);

BOOL result = check > 0;
// Cleanup
free(sig);
return result;
Expand Down
2 changes: 0 additions & 2 deletions objc/cocoafob.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
objects = {

/* Begin PBXBuildFile section */
3035240F1223BC2200AACD22 /* cocoafob.m in Sources */ = {isa = PBXBuildFile; fileRef = 08FB7796FE84155DC02AAC07 /* cocoafob.m */; };
303524101223BC2200AACD22 /* decoder.c in Sources */ = {isa = PBXBuildFile; fileRef = C7E378410F59D737002061CD /* decoder.c */; };
303524111223BC2200AACD22 /* encoder.c in Sources */ = {isa = PBXBuildFile; fileRef = C7E378430F59D737002061CD /* encoder.c */; };
303524121223BC2200AACD22 /* CFobLicGenerator.m in Sources */ = {isa = PBXBuildFile; fileRef = C7E378480F59DB15002061CD /* CFobLicGenerator.m */; };
Expand Down Expand Up @@ -232,7 +231,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3035240F1223BC2200AACD22 /* cocoafob.m in Sources */,
303524101223BC2200AACD22 /* decoder.c in Sources */,
303524111223BC2200AACD22 /* encoder.c in Sources */,
303524121223BC2200AACD22 /* CFobLicGenerator.m in Sources */,
Expand Down

0 comments on commit 7ae85d8

Please sign in to comment.