Skip to content

Instantly share code, notes, and snippets.

@frankshaka
Created October 10, 2016 09:47
Show Gist options
  • Save frankshaka/ff69b6057bd7cd0e2aa7bfcc54aacc01 to your computer and use it in GitHub Desktop.
Save frankshaka/ff69b6057bd7cd0e2aa7bfcc54aacc01 to your computer and use it in GitHub Desktop.
Patch of PKCS12ParametersGenerator.m correcting array lengths
@@ -7,19 +7,7 @@
#define NUL_TERMINATOR '\0'
@implementation PKCS12ParametersGenerator
-@synthesize keySize;
-@synthesize ivSize;
-@synthesize derivedKey;
-@synthesize derivedIV;
-@synthesize password;
-@synthesize salt;
-@synthesize ptrPasswordBytes;
-@synthesize ptrPasswdPCKS12Bytes;
-@synthesize ptrSaltBytes;
-@synthesize iterations;
-@synthesize szPasswordLen;
-@synthesize szSaltLen;
-@synthesize szPasswdPKCS12Len;
+
-(id) init :(NSString *)argPassword
saltedHash:(NSData *)argSalt
@@ -38,24 +26,14 @@ -(id) init :(NSString *)argPassword
self.ptrPasswdPCKS12Bytes = [self pkcs12Password:self.ptrPasswordBytes];
self.szPasswordLen = [self.password length];
self.szSaltLen = [self.salt length];
- if (self.szPasswordLen > 0) self.szPasswdPKCS12Len = (self.szPasswordLen * 2);
+ if (self.szPasswordLen > 0) self.szPasswdPKCS12Len = ((self.szPasswordLen + 1) * 2);
else self.szPasswdPKCS12Len = 0;
//self.szPasswdPKCS12Len = strlen(self.ptrPasswdPCKS12Bytes);
}
return self;
}
--(void) dealloc{
- [super dealloc];
- [self.derivedIV release];
- [self.derivedKey release];
- self.derivedKey = nil;
- self.derivedIV = nil;
- self.ptrPasswordBytes = nil;
- self.ptrSaltBytes = nil;
- self.szPasswordLen = 0;
- self.szSaltLen = 0;
-}
+
-(void) generateDerivedParameters{
uint8_t *lDerivedKey = [self generateDerivedKey: PKCS12_KEY_MATERIAL byteLen:self.keySize];
self.derivedKey = [NSData dataWithBytesNoCopy:lDerivedKey length:self.keySize];
@@ -88,7 +66,7 @@ -(unsigned char *)generateDerivedKey :(NSInteger) iMaterialType
for (int i = 0; i != szSLen; i++){
S[i] = self.ptrSaltBytes[i % self.szSaltLen];
}
- S[self.szSaltLen] = NUL_TERMINATOR;
+ S[szSLen] = NUL_TERMINATOR;
}else{
szSLen = sizeof(unsigned char) * 1;
S = calloc(szSLen + 1, sizeof(unsigned char));
@@ -100,10 +78,10 @@ -(unsigned char *)generateDerivedKey :(NSInteger) iMaterialType
if (self.ptrPasswdPCKS12Bytes != NULL && self.szPasswdPKCS12Len != 0){
szPLen = sizeof(unsigned char) * (v * (((self.szPasswdPKCS12Len + v) - 1) / v)); // NUL terminator?
P = calloc(szPLen + 1, sizeof(unsigned char));
- for (int i = 0; i != self.szPasswdPKCS12Len; i++){
+ for (int i = 0; i != szPLen; i++){
P[i] = self.ptrPasswdPCKS12Bytes[i % self.szPasswdPKCS12Len];
}
- P[self.szPasswdPKCS12Len] = NUL_TERMINATOR;
+ P[szPLen] = NUL_TERMINATOR;
}else{
szPLen = sizeof(unsigned char) * 1;
P = calloc(szPLen + 1, sizeof(unsigned char));
@@ -113,8 +91,8 @@ -(unsigned char *)generateDerivedKey :(NSInteger) iMaterialType
size_t szILen = sizeof(unsigned char) * (szSLen + szPLen);
unsigned char *I = calloc(szILen + 1, sizeof(unsigned char));
//
- memcpy(&I[0], S, szSaltLen);
- memcpy(&I[szSaltLen], P, szPLen);
+ memcpy(&I[0], S, szSLen);
+ memcpy(&I[szSLen], P, szPLen);
I[szILen] = NUL_TERMINATOR;
//
size_t szBLen = sizeof(unsigned char) * v;
@@ -181,12 +159,14 @@ -(uint8_t *) pkcs12Password : (uint8_t *)argPtrPassword{
size_t lenPasswdChk = strlen(argPtrPassword);
uint8_t *passwordPKCS12 = NULL;
if (lenPasswdChk > 0){
- size_t lenPassword = (lenPasswdChk * 2) + 1;
+ size_t lenPassword = (lenPasswdChk + 1) * 2;
passwordPKCS12 = calloc(lenPassword, sizeof(uint8_t));
for (int i = 0; i != lenPassword; i++){
passwordPKCS12[i * 2] = (uint8_t)argPtrPassword[i] >> 8;
passwordPKCS12[i * 2 + 1] = (uint8_t)argPtrPassword[i];
}
+ passwordPKCS12[lenPassword - 2] = NUL_TERMINATOR;
+ passwordPKCS12[lenPassword - 1] = NUL_TERMINATOR;
passwordPKCS12[lenPassword] = NUL_TERMINATOR;
}else{
passwordPKCS12 = calloc(1, sizeof(uint8_t));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment