Skip to content

Commit

Permalink
Renamed old classes, added libcrypto to the project; builds OK
Browse files Browse the repository at this point in the history
  • Loading branch information
Gleb Dolgich committed Mar 1, 2009
1 parent 88ad6bc commit fd6d23b
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 40 deletions.
48 changes: 42 additions & 6 deletions objc/CFobLicGenerator.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,66 @@
//
// PxLicGenerator.h
// pxlic
// CFobLicGenerator.h
// CocoaFob
//
// Created by Gleb Dolgich on 09/02/2009.
// Follow me on Twitter @gbd.
// Copyright 2009 PixelEspresso. All rights reserved.
// Copyright (C) 2009 PixelEspresso. All rights reserved.
// Licensed under CC Attribution License <http://creativecommons.org/licenses/by/3.0/>
//

#import <Foundation/Foundation.h>
#import <openssl/dsa.h>

@interface PxLicGenerator : NSObject {
/*!
@class CFobLicGenerator
@superclass NSObject
@abstract Generates CocoaFob-style registration codes.
@discussion Given user name and DSA private key, generates a human-readable registration code.
*/
@interface CFobLicGenerator : NSObject {
DSA *dsa;
NSString *regName;
NSString *regKey;
NSString *regCode;
NSString *lastError;
}

@property (nonatomic, copy) NSString *regName;
@property (nonatomic, copy) NSString *regKey;
@property (nonatomic, copy) NSString *regCode;
@property (nonatomic, copy) NSString *lastError;

/*!
@method generatorWithPrivateKey:
@abstract Creates a new registration code generator given DSA private key.
@discussion Use this class method to create an autoreleased registration code generator.
@param privKey PEM-encoded non-encrypted DSA private key.
@result A new autoreleased registration code generator object.
*/
+ (id)generatorWithPrivateKey:(NSString *)privKey;

/*!
@method initWithPrivateKey:
@abstract Designated initializer that takes a DSA private key.
@discussion Initializes registration code generator using a DSA private key.
@param privKey PEM-encoded non-encrypted DSA private key.
@result An initialized registration code generator object.
*/
- (id)initWithPrivateKey:(NSString *)privKey;

/*!
@method setPrivateKey:
@abstract Sets a new DSA private key.
@discussion Sets a new DSA private key to be used for subsequent generated registration codes.
@param privKey PEM-encoded non-encrypted DSA private key.
@result YES on success, NO on error.
*/
- (BOOL)setPrivateKey:(NSString *)privKey;

/*!
@method generate
@abstract Generates a registration code from regName property.
@discussion Takes regName property and DSA private key and generates a new registration code that is placed in regCode property.
@result YES on success, NO on error.
*/
- (BOOL)generate;

@end
23 changes: 12 additions & 11 deletions objc/CFobLicGenerator.m
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
//
// PxLicGenerator.m
// pxlic
// CFobLicGenerator.m
// CocoaFob
//
// Created by Gleb Dolgich on 09/02/2009.
// Follow me on Twitter @gbd.
// Copyright 2009 PixelEspresso. All rights reserved.
// Copyright (C) 2009 PixelEspresso. All rights reserved.
// Licensed under CC Attribution Licence <http://creativecommons.org/licenses/by/3.0/>
//

#import "NSData+PECrypt.h"
#import "NSString+PECrypt.h"
#import "PxLicGenerator.h"
#import "CFobLicGenerator.h"
#import <openssl/evp.h>
#import <openssl/err.h>
#import <openssl/pem.h>


@interface PxLicGenerator ()
@interface CFobLicGenerator ()
- (void)initOpenSSL;
- (void)shutdownOpenSSL;
@end


@implementation PxLicGenerator
@implementation CFobLicGenerator

@synthesize regName;
@synthesize regKey;
@synthesize regCode;
@synthesize lastError;

#pragma mark -
#pragma mark Class methods

+ (id)generatorWithPrivateKey:(NSString *)privKey {
return [[[PxLicGenerator alloc] initWithPrivateKey:privKey] autorelease];
return [[[CFobLicGenerator alloc] initWithPrivateKey:privKey] autorelease];
}

#pragma mark -
Expand All @@ -52,7 +53,7 @@ - (id)initWithPrivateKey:(NSString *)privKey {
- (void)dealloc {
if (dsa)
DSA_free(dsa);
self.regKey = nil;
self.regCode = nil;
self.regName = nil;
self.lastError = nil;
[self shutdownOpenSSL];
Expand Down Expand Up @@ -87,7 +88,7 @@ - (BOOL)setPrivateKey:(NSString *)privKey {
}

- (BOOL)generate {
if (!regName || ![regName length] || !dsa || !dsa->priv_key)
if (![regName length] || !dsa || !dsa->priv_key)
return NO;
NSData *digest = [regName sha1];
unsigned int siglen;
Expand Down Expand Up @@ -116,7 +117,7 @@ - (BOOL)generate {
[serial insertString:@"-" atIndex:index];
index += 6;
}
self.regKey = serial;
self.regCode = serial;
return YES;
}

Expand Down
34 changes: 21 additions & 13 deletions objc/CFobLicVerifier.h
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
//
// PxLicVerifier.h
// pxlic
// CFobLicVerifier.h
// CocoaFob
//
// Created by Gleb Dolgich on 06/02/2009.
// Follow me on Twitter @gbd.
// Copyright 2009 PixelEspresso. All rights reserved.
// Licensed under CC Attribution License <http://creativecommons.org/licenses/by/3.0/>
//

#import <Foundation/Foundation.h>
#import <openssl/dsa.h>

/*!
@class PxLicVerifier
@class CFobLicVerifier
@superclass NSObject
@abstract Verifies PxLic-style registration key.
@discussion Verifies PxLic-style registration key given licensing information (application name, user name, and number of copies as suggested in Potion Store) and signature in human-readable format. A signature is a base32-encoded bignum with padding removed and dashes inserted.
@abstract Verifies CocoaFob-style registration key.
@discussion Verifies CocoaFob-style registration key given licensing information (for example, application name, user name, and number of copies as suggested in Potion Store) and signature in human-readable format. A signature is a base32-encoded bignum with padding removed and dashes inserted.
*/
@interface PxLicVerifier : NSObject {
@interface CFobLicVerifier : NSObject {
DSA *dsa;
NSString *regName;
NSString *regKey;
NSString *regCode;
NSArray *blacklist;
NSString *lastError;
}

@property (nonatomic, copy) NSString *regName;
@property (nonatomic, copy) NSString *regKey;
@property (nonatomic, copy) NSString *regCode;
@property (nonatomic, retain) NSArray *blacklist;
@property (nonatomic, copy) NSString *lastError;

/*!
@method verifierWithPublicKey:
@abstract Creates a new PxLic verifier object given a DSA public key.
@discussion Creates a new PxLic verifier object. Use setRegName: and setRegKey: on it, then call verify to verify registration key.
@abstract Creates a new registration code verifier object given a DSA public key.
@discussion Creates a new registration code verifier object. Use setRegName: and setRegKey: on it, then call verify to verify registration key.
@param pubKey A DSA public key in PEM encoding. See completePublicKeyPEM: for help on how to construct a PEM-encoded DSA public key.
@result A new autoreleased PxLic verifier object.
@result A new autoreleased registration code verifier object.
*/
+ (id)verifierWithPublicKey:(NSString *)pubKey;

Expand All @@ -50,9 +51,9 @@
/*!
@method initWithPublicKey:
@abstract Designated initialiser.
@discussion Initialises a newly allocated PxLicVerifier object with a PEM-encoded DSA public key.
@discussion Initialises a newly allocated registration code verifier object with a PEM-encoded DSA public key.
@param pubKey A PEM-encoded DSA public key. See completePublicKeyPEM: for help on how to constuct a PEM-encoded DSA key string from base64-encoded lines.
@result An initialised PxLicVerifier object.
@result An initialised registration code verifier object.
*/
- (id)initWithPublicKey:(NSString *)pubKey;

Expand All @@ -64,6 +65,13 @@
@result YES on success, NO on error (check lastError property).
*/
- (BOOL)setPublicKey:(NSString *)pubKey;

/*!
@method verify
@abstract Verifies registration code in the regName property using public DSA key.
@discussion Takes regName and regCode properties and verifies regCode against regName using public DSA certificate.
@result YES if regCode is valid, NO if not.
*/
- (BOOL)verify;

@end
21 changes: 11 additions & 10 deletions objc/CFobLicVerifier.m
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
//
// PxLicVerifier.m
// pxlic
// CFobLicVerifier.m
// CocoaFob
//
// Created by Gleb Dolgich on 06/02/2009.
// Follow me on Twitter @gbd.
// Copyright 2009 PixelEspresso. All rights reserved.
// Licensed under CC Attribution License <http://creativecommons.org/licenses/by/3.0/>
//

#import "NSString+PECrypt.h"
#import "PxLicVerifier.h"
#import "CFobLicVerifier.h"
#import "decoder.h"
#import <openssl/evp.h>
#import <openssl/err.h>
#import <openssl/pem.h>


@interface PxLicVerifier ()
@interface CFobLicVerifier ()
- (void)initOpenSSL;
- (void)shutdownOpenSSL;
@end


@implementation PxLicVerifier
@implementation CFobLicVerifier

@synthesize regName;
@synthesize regKey;
@synthesize regCode;
@synthesize blacklist;
@synthesize lastError;

#pragma mark -
#pragma mark Class methods

+ (id)verifierWithPublicKey:(NSString *)pubKey {
return [[[PxLicVerifier alloc] initWithPublicKey:pubKey] autorelease];
return [[[CFobLicVerifier alloc] initWithPublicKey:pubKey] autorelease];
}

+ (NSString *)completePublicKeyPEM:(NSString *)partialPEM {
Expand Down Expand Up @@ -81,7 +82,7 @@ - (void)dealloc {
if (dsa)
DSA_free(dsa);
self.regName = nil;
self.regKey = nil;
self.regCode = nil;
self.blacklist = nil;
self.lastError = nil;
[self shutdownOpenSSL];
Expand Down Expand Up @@ -116,11 +117,11 @@ - (BOOL)setPublicKey:(NSString *)pubKey {
}

- (BOOL)verify {
if (!regName || ![regName length] || !dsa || !dsa->pub_key)
if (![regName length] || !dsa || !dsa->pub_key)
return NO;
BOOL result = NO;
// Replace 9s with Is and 8s with Os
NSString *regKeyTemp = [regKey stringByReplacingOccurrencesOfString:@"9" withString:@"I"];
NSString *regKeyTemp = [regCode stringByReplacingOccurrencesOfString:@"9" withString:@"I"];
NSString *regKeyBase32 = [regKeyTemp stringByReplacingOccurrencesOfString:@"8" withString:@"O"];
// Remove dashes from the registration key if they are there (dashes are optional).
NSString *keyNoDashes = [regKeyBase32 stringByReplacingOccurrencesOfString:@"-" withString:@""];
Expand Down
Loading

0 comments on commit fd6d23b

Please sign in to comment.