]> git.saurik.com Git - apple/security.git/blob - KeychainCircle/NSError+KCCreationHelpers.m
Security-59306.61.1.tar.gz
[apple/security.git] / KeychainCircle / NSError+KCCreationHelpers.m
1 //
2 // NSError+KCCreationHelpers.m
3 // KechainCircle
4 //
5 //
6
7 #import <Foundation/Foundation.h>
8
9 #import <NSError+KCCreationHelpers.h>
10
11 static NSString* coreCryptoDomain = @"kSecCoreCryptoDomain";
12 static NSString* srpDomain = @"com.apple.security.srp";
13
14 static NSDictionary* UserInfoFromVA(NSString*description, va_list va)
15 NS_FORMAT_FUNCTION(1, 0);
16
17 static NSDictionary* UserInfoFromVA(NSString*description, va_list va) {
18 return @{NSLocalizedDescriptionKey:[[NSString alloc] initWithFormat:description
19 arguments:va]};
20 }
21
22 // We should get this from SecCFError.h and Security.framework..
23 bool CoreCryptoError(int cc_result, NSError** error, NSString* description, ...)
24 {
25 bool failed = cc_result != 0;
26
27 if (failed && error && !*error) {
28 va_list va;
29 va_start(va, description);
30 *error = [NSError errorWithCoreCryptoStatus:cc_result
31 description:description
32 args:va];
33 va_end(va);
34 }
35
36 return !failed;
37 }
38
39 bool OSStatusError(OSStatus status, NSError** error, NSString*description, ...) {
40 bool failed = status != 0;
41
42 if (failed && error && !*error) {
43 va_list va;
44 va_start(va, description);
45 *error = [NSError errorWithOSStatus:status
46 description:description
47 args:va];
48 va_end(va);
49 }
50
51 return !failed;
52 }
53
54 bool RequirementError(bool requirement, NSError** error, NSString*description, ...) {
55 bool failed = !requirement;
56
57 if (failed && error && !*error) {
58 va_list va;
59 va_start(va, description);
60 *error = [NSError errorWithOSStatus:-50
61 description:description
62 args:va];
63 va_end(va);
64 }
65
66 return !failed;
67 }
68
69
70 @implementation NSError(KCCreationHelpers)
71
72 + (instancetype) errorWithOSStatus:(OSStatus) status
73 userInfo:(NSDictionary *)dict {
74 return [[NSError alloc] initWithOSStatus:status userInfo:dict];
75 }
76
77 - (instancetype) initWithOSStatus:(OSStatus) status
78 userInfo:(NSDictionary *)dict {
79 return [self initWithDomain:NSOSStatusErrorDomain code:status userInfo:dict];
80 }
81
82 + (instancetype) errorWithOSStatus:(OSStatus) status
83 description:(NSString*)description
84 args:(va_list)va {
85 return [[NSError alloc] initWithOSStatus:status description:description args:va];
86 }
87
88 - (instancetype) initWithOSStatus:(OSStatus) status
89 description:(NSString*)description
90 args:(va_list)va {
91 return [self initWithOSStatus:status
92 userInfo:UserInfoFromVA(description, va)];
93 }
94
95 + (instancetype) errorWithCoreCryptoStatus:(int) status
96 userInfo:(NSDictionary *)dict {
97 return [[NSError alloc] initWithCoreCryptoStatus:status userInfo:dict];
98 }
99
100
101 - (instancetype) initWithCoreCryptoStatus:(int) status
102 userInfo:(NSDictionary *)dict {
103 return [self initWithDomain:coreCryptoDomain code:status userInfo:dict];
104 }
105
106 + (instancetype) errorWithCoreCryptoStatus:(int) status
107 description:(NSString*)description
108 args:(va_list)va {
109 return [[NSError alloc] initWithCoreCryptoStatus:status description:description args:va];
110 }
111
112 - (instancetype) initWithCoreCryptoStatus:(int) status
113 description:(NSString*)description
114 args:(va_list)va {
115 return [self initWithCoreCryptoStatus:status
116 userInfo:UserInfoFromVA(description, va)];
117 }
118
119 @end