X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/ce3c8656732c924baf7e88df75eab50891bdc471..fa7225c82381bac4432a6edf16f53b5370238d85:/OSX/sec/Security/Regressions/secitem/si-73-secpasswordgenerate.c diff --git a/OSX/sec/Security/Regressions/secitem/si-73-secpasswordgenerate.c b/OSX/sec/Security/Regressions/secitem/si-73-secpasswordgenerate.c index 4206fb84..e87294c8 100644 --- a/OSX/sec/Security/Regressions/secitem/si-73-secpasswordgenerate.c +++ b/OSX/sec/Security/Regressions/secitem/si-73-secpasswordgenerate.c @@ -7,13 +7,88 @@ #include #include #include "Security_regressions.h" +#include + +static void test_password_generate(bool ok, SecPasswordType type, int n,...) +{ + va_list argp; + CFTypeRef key, value; + va_start(argp, n); + int i; + + CFMutableDictionaryRef passwordRequirements = NULL; + CFStringRef password = NULL; + CFErrorRef error = NULL; + + passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL); + + for(i=0; i max - min = 32; - max = 20; - - minRef = CFNumberCreate(NULL, kCFNumberIntType, &min); - maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max); - - passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef); - CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef); - allowedCharacters = CFSTR("abcdsefw2345"); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters); - CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets); - password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements); - ok(password == NULL); - ok(error != NULL); - - error = NULL; - CFRelease(minRef); - CFRelease(maxRef); - CFRelease(allowedCharacters); - CFRelease(passwordRequirements); + test_password_generate(false, kSecPasswordTypeWifi, 4, + kSecPasswordMinLengthKey, cf32, + kSecPasswordMaxLengthKey, cf24, + kSecPasswordAllowedCharactersKey, CFSTR("abcdsefw2345"), + kSecPasswordRequiredCharactersKey, requiredCharacterSets); //test by ommitting dictionary parameters //omit max length - min = 20; - max = 32; - - minRef = CFNumberCreate(NULL, kCFNumberIntType, &min); - maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max); - - passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef); - allowedCharacters = CFSTR("abcdsefw2345"); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters); - CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets); - password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements); - ok(password == NULL); - ok(error != NULL); - - error = NULL; - CFRelease(minRef); - CFRelease(maxRef); - CFRelease(allowedCharacters); - CFRelease(passwordRequirements); + test_password_generate(false, kSecPasswordTypeWifi, 3, + kSecPasswordMinLengthKey, cf20, + kSecPasswordAllowedCharactersKey, CFSTR("abcdsefw2345"), + kSecPasswordRequiredCharactersKey, requiredCharacterSets); //omit min length - min = 20; - max = 32; - - minRef = CFNumberCreate(NULL, kCFNumberIntType, &min); - maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max); - - passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef); - allowedCharacters = CFSTR("abcdsefw2345"); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters); - CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets); - password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements); - ok(password == NULL); - ok(error != NULL); - - error = NULL; - CFRelease(minRef); - CFRelease(maxRef); - CFRelease(allowedCharacters); - CFRelease(passwordRequirements); + test_password_generate(false, kSecPasswordTypeWifi, 3, + kSecPasswordMaxLengthKey, cf32, + kSecPasswordAllowedCharactersKey, CFSTR("abcdsefw2345"), + kSecPasswordRequiredCharactersKey, requiredCharacterSets); //omit allowed characters - min = 20; - max = 32; - - minRef = CFNumberCreate(NULL, kCFNumberIntType, &min); - maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max); - - passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef); - CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef); - allowedCharacters = CFSTR("abcdsefw2345"); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets); - password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements); - ok(password == NULL); - ok(error != NULL); - - error = NULL; - CFRelease(minRef); - CFRelease(maxRef); - CFRelease(allowedCharacters); - CFRelease(passwordRequirements); + test_password_generate(false, kSecPasswordTypeWifi, 3, + kSecPasswordMinLengthKey, cf20, + kSecPasswordMaxLengthKey, cf32, + kSecPasswordRequiredCharactersKey, requiredCharacterSets); //omit required characters - min = 20; - max = 32; - - minRef = CFNumberCreate(NULL, kCFNumberIntType, &min); - maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max); - - passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef); - CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef); - allowedCharacters = CFSTR("abcdsefw2345"); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters); - password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements); - ok(password == NULL); - ok(error != NULL); - - error = NULL; - CFRelease(minRef); - CFRelease(maxRef); - CFRelease(allowedCharacters); - CFRelease(passwordRequirements); + test_password_generate(false, kSecPasswordTypeWifi, 3, + kSecPasswordMinLengthKey, cf20, + kSecPasswordMaxLengthKey, cf32, + kSecPasswordAllowedCharactersKey, CFSTR("abcdsefw2345")); + //pass in wrong type for min - min = 20; - max = 32; - - minRef = CFNumberCreate(NULL, kCFNumberIntType, &min); - maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max); - - passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, allowedCharacters); - CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef); - allowedCharacters = CFSTR("abcdsefw2345"); - requiredCharacterSets = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); - CFArrayAppendValue(requiredCharacterSets, uppercaseLetterCharacterSet); - CFArrayAppendValue(requiredCharacterSets, lowercaseLetterCharacterSet); - CFArrayAppendValue(requiredCharacterSets, decimalDigitCharacterSet); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters); - CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets); - - password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements); - ok(password == NULL); - ok(error != NULL); - - error = NULL; - CFRelease(passwordRequirements); - CFRelease(minRef); - CFRelease(maxRef); - CFRelease(allowedCharacters); - + test_password_generate(false, kSecPasswordTypeWifi, 4, + kSecPasswordMinLengthKey, CFSTR("20"), + kSecPasswordMaxLengthKey, cf32, + kSecPasswordAllowedCharactersKey, CFSTR("abcdsefw2345"), + kSecPasswordRequiredCharactersKey, requiredCharacterSets); + //pass in wrong type for max - min = 20; - max = 32; - - minRef = CFNumberCreate(NULL, kCFNumberIntType, &min); - maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max); - - passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef); - CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, allowedCharacters); - allowedCharacters = CFSTR("abcdsefw2345"); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters); - CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets); - - password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements); - ok(password == NULL); - ok(error != NULL); - - error = NULL; - CFRelease(passwordRequirements); - CFRelease(minRef); - CFRelease(maxRef); - CFRelease(allowedCharacters); - + test_password_generate(false, kSecPasswordTypeWifi, 4, + kSecPasswordMinLengthKey, cf20, + kSecPasswordMaxLengthKey, CFSTR("32"), + kSecPasswordAllowedCharactersKey, CFSTR("abcdsefw2345"), + kSecPasswordRequiredCharactersKey, requiredCharacterSets); + //pass in wrong type for allowed - min = 20; - max = 32; - - minRef = CFNumberCreate(NULL, kCFNumberIntType, &min); - maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max); - - passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef); - CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef); - allowedCharacters = CFSTR("abcdsefw2345"); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, minRef); - CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets); - - password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements); - ok(password == NULL); - ok(error != NULL); - - error = NULL; - CFRelease(passwordRequirements); - CFRelease(minRef); - CFRelease(maxRef); - CFRelease(allowedCharacters); - + test_password_generate(false, kSecPasswordTypeWifi, 4, + kSecPasswordMinLengthKey, cf20, + kSecPasswordMaxLengthKey, cf32, + kSecPasswordAllowedCharactersKey, requiredCharacterSets, + kSecPasswordRequiredCharactersKey, requiredCharacterSets); + //pass in wrong type for required - min = 20; - max = 32; - - minRef = CFNumberCreate(NULL, kCFNumberIntType, &min); - maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max); - - passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef); - CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef); - allowedCharacters = CFSTR("abcdsefw2345"); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters); - CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, minRef); - password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements); - ok(password == NULL); - ok(error != NULL); - - error = NULL; - CFRelease(passwordRequirements); - CFRelease(minRef); - CFRelease(maxRef); - CFRelease(allowedCharacters); + test_password_generate(false, kSecPasswordTypeWifi, 4, + kSecPasswordMinLengthKey, cf20, + kSecPasswordMaxLengthKey, cf32, + kSecPasswordAllowedCharactersKey, CFSTR("abcdsefw2345"), + kSecPasswordRequiredCharactersKey, CFSTR("abcdsefw2345")); //pass in wrong type for no less than - min = 20; - max = 32; - - minRef = CFNumberCreate(NULL, kCFNumberIntType, &min); - maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max); - - passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL); - requiredCharacterSets = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); - CFArrayAppendValue(requiredCharacterSets, uppercaseLetterCharacterSet); - CFArrayAppendValue(requiredCharacterSets, lowercaseLetterCharacterSet); - CFArrayAppendValue(requiredCharacterSets, decimalDigitCharacterSet); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters); - CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets); - CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef); - CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef); - allowedCharacters = CFSTR("abcdsefw2345"); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters); - CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, minRef); - CFDictionaryAddValue(passwordRequirements, kSecPasswordContainsAtLeastNSpecificCharacters, CFSTR("hehe")); - password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements); - ok(password == NULL); - ok(error != NULL); - - error = NULL; - CFRelease(passwordRequirements); - CFRelease(minRef); - CFRelease(maxRef); - CFRelease(allowedCharacters); - - //pass in wrong type for no more than - min = 20; - max = 32; - - minRef = CFNumberCreate(NULL, kCFNumberIntType, &min); - maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max); - - passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL); - requiredCharacterSets = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); - CFArrayAppendValue(requiredCharacterSets, uppercaseLetterCharacterSet); - CFArrayAppendValue(requiredCharacterSets, lowercaseLetterCharacterSet); - CFArrayAppendValue(requiredCharacterSets, decimalDigitCharacterSet); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters); - CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets); - CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef); - CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef); - allowedCharacters = CFSTR("abcdsefw2345"); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters); - CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, minRef); - CFDictionaryAddValue(passwordRequirements, kSecPasswordContainsNoMoreThanNSpecificCharacters, CFSTR("hehe")); + test_password_generate(false, kSecPasswordTypeWifi, 5, + kSecPasswordMinLengthKey, cf20, + kSecPasswordMaxLengthKey, cf32, + kSecPasswordAllowedCharactersKey, CFSTR("abcdsefw2345"), + kSecPasswordRequiredCharactersKey, requiredCharacterSets, + kSecPasswordContainsAtLeastNSpecificCharacters, CFSTR("hehe")); - password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements); - ok(password == NULL); - ok(error != NULL); - error = NULL; - CFRelease(passwordRequirements); - CFRelease(minRef); - CFRelease(maxRef); - CFRelease(allowedCharacters); + //pass in wrong type for no more than + test_password_generate(false, kSecPasswordTypeWifi, 5, + kSecPasswordMinLengthKey, cf20, + kSecPasswordMaxLengthKey, cf32, + kSecPasswordAllowedCharactersKey, CFSTR("abcdsefw2345"), + kSecPasswordRequiredCharactersKey, requiredCharacterSets, + kSecPasswordContainsNoMoreThanNSpecificCharacters, CFSTR("hehe")); //pass in wrong disallowed characters - min = 20; - max = 32; - - minRef = CFNumberCreate(NULL, kCFNumberIntType, &min); - maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max); - - passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL); - requiredCharacterSets = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); - CFArrayAppendValue(requiredCharacterSets, uppercaseLetterCharacterSet); - CFArrayAppendValue(requiredCharacterSets, lowercaseLetterCharacterSet); - CFArrayAppendValue(requiredCharacterSets, decimalDigitCharacterSet); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters); - CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets); - CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef); - CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef); - allowedCharacters = CFSTR("abcdsefw2345"); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters); - CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, minRef); - CFDictionaryAddValue(passwordRequirements, kSecPasswordDisallowedCharacters, requiredCharacterSets); - - password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements); - ok(password == NULL); - ok(error != NULL); - - error = NULL; - CFRelease(passwordRequirements); - CFRelease(minRef); - CFRelease(maxRef); - CFRelease(allowedCharacters); + test_password_generate(false, kSecPasswordTypeWifi, 5, + kSecPasswordMinLengthKey, cf20, + kSecPasswordMaxLengthKey, cf32, + kSecPasswordAllowedCharactersKey, CFSTR("abcdsefw2345"), + kSecPasswordRequiredCharactersKey, requiredCharacterSets, + kSecPasswordDisallowedCharacters, requiredCharacterSets); //pass in wrong type for no more than's dictionary - min = 20; - max = 32; - - minRef = CFNumberCreate(NULL, kCFNumberIntType, &min); - maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max); - CFMutableDictionaryRef wrongCount = CFDictionaryCreateMutable(NULL, 0, NULL, NULL); CFDictionaryAddValue(wrongCount, kSecPasswordCharacters, CFSTR("lkj")); CFDictionaryAddValue(wrongCount, kSecPasswordCharacterCount, CFSTR("sdf")); - - passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL); - requiredCharacterSets = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); - CFArrayAppendValue(requiredCharacterSets, uppercaseLetterCharacterSet); - CFArrayAppendValue(requiredCharacterSets, lowercaseLetterCharacterSet); - CFArrayAppendValue(requiredCharacterSets, decimalDigitCharacterSet); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters); - CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets); - CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef); - CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef); - allowedCharacters = CFSTR("abcdsefw2345"); - - CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters); - CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, minRef); - CFDictionaryAddValue(passwordRequirements, kSecPasswordContainsNoMoreThanNSpecificCharacters, wrongCount); - - password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements); - ok(password == NULL); - ok(error != NULL); - - error = NULL; - CFRelease(wrongCount); - CFRelease(passwordRequirements); - CFRelease(minRef); - CFRelease(maxRef); - CFRelease(allowedCharacters); - + + test_password_generate(false, kSecPasswordTypeWifi, 5, + kSecPasswordMinLengthKey, cf20, + kSecPasswordMaxLengthKey, cf32, + kSecPasswordAllowedCharactersKey, CFSTR("abcdsefw2345"), + kSecPasswordRequiredCharactersKey, requiredCharacterSets, + kSecPasswordContainsNoMoreThanNSpecificCharacters, wrongCount); + + CFReleaseSafe(wrongCount); + + + //release CF objects: + CFReleaseSafe(cf2); + CFReleaseSafe(cf4); + CFReleaseSafe(cf5); + CFReleaseSafe(cf6); + CFReleaseSafe(cf12); + CFReleaseSafe(cf19); + CFReleaseSafe(cf20); + CFReleaseSafe(cf23); + CFReleaseSafe(cf24); + CFReleaseSafe(cf32); + CFReleaseSafe(cf56); + + CFReleaseSafe(requiredCharacterSets); + + + // Weak Passwords tests password = CFSTR("Apple1?"); isnt(true, SecPasswordIsPasswordWeak(password)); CFRelease(password); @@ -1021,12 +524,15 @@ static void tests(void) is(true, SecPasswordIsPasswordWeak2(true, CFSTR("525252"))); is(true, SecPasswordIsPasswordWeak2(false, CFSTR("525252"))); is(true, SecPasswordIsPasswordWeak2(false, CFSTR("52525"))); - + + is(true, SecPasswordIsPasswordWeak2(true, CFSTR("098765"))); + is(true, SecPasswordIsPasswordWeak(CFSTR("0987"))); + } int si_73_secpasswordgenerate(int argc, char *const *argv) { - plan_tests(308); + plan_tests(310); tests(); return 0;