6 #include "utilities/SecCFRelease.h"
8 #define OS_OBJECT_HAVE_OBJC_SUPPORT 1
10 #define SEC_NULL_BAD_INPUT ((void *_Nonnull)NULL)
11 #define SEC_NULL_OUT_OF_MEMORY SEC_NULL_BAD_INPUT
13 #define SEC_NIL_BAD_INPUT ((void *_Nonnull)nil)
14 #define SEC_NIL_OUT_OF_MEMORY SEC_NIL_BAD_INPUT
16 #define SEC_CONCRETE_CLASS_NAME(external_type) SecConcrete_##external_type
17 #define SEC_CONCRETE_PREFIX_STR "SecConcrete_"
19 #define SEC_OBJECT_DECL_INTERNAL_OBJC(external_type) \
20 @class SEC_CONCRETE_CLASS_NAME(external_type); \
21 typedef SEC_CONCRETE_CLASS_NAME(external_type) *external_type##_t
23 #define SEC_OBJECT_IMPL_INTERNAL_OBJC_WITH_PROTOCOL_AND_VISBILITY(external_type, _protocol, visibility, ...) \
24 @protocol OS_OBJECT_CLASS(external_type) <_protocol> \
27 @interface SEC_CONCRETE_CLASS_NAME(external_type) : NSObject<OS_OBJECT_CLASS(external_type)> \
28 _Pragma("clang diagnostic push") \
29 _Pragma("clang diagnostic ignored \"-Wobjc-interface-ivars\"") \
31 _Pragma("clang diagnostic pop") \
33 typedef int _useless_typedef_oio_##external_type
35 #define SEC_OBJECT_IMPL_INTERNAL_OBJC_WITH_PROTOCOL(external_type, _protocol, ...) \
36 SEC_OBJECT_IMPL_INTERNAL_OBJC_WITH_PROTOCOL_AND_VISBILITY(external_type, _protocol, ,__VA_ARGS__)
38 #define SEC_OBJECT_IMPL_INTERNAL_OBJC(external_type, ...) \
39 SEC_OBJECT_IMPL_INTERNAL_OBJC_WITH_PROTOCOL(external_type, NSObject, ##__VA_ARGS__)
41 #define SEC_OBJECT_IMPL_INTERNAL_OBJC_WITH_VISIBILITY(external_type, visibility, ...) \
42 SEC_OBJECT_IMPL_INTERNAL_OBJC_WITH_PROTOCOL_AND_VISBILITY(external_type, NSObject, visibility, ##__VA_ARGS__)
44 #define SEC_OBJECT_IMPL 1
46 SEC_OBJECT_DECL_INTERNAL_OBJC(sec_array);
47 SEC_OBJECT_DECL_INTERNAL_OBJC(sec_identity);
48 SEC_OBJECT_DECL_INTERNAL_OBJC(sec_trust);
49 SEC_OBJECT_DECL_INTERNAL_OBJC(sec_certificate);
50 SEC_OBJECT_DECL_INTERNAL_OBJC(sec_tls_extension);
51 SEC_OBJECT_DECL_INTERNAL_OBJC(sec_object);
52 SEC_OBJECT_DECL_INTERNAL_OBJC(sec_protocol_options);
53 SEC_OBJECT_DECL_INTERNAL_OBJC(sec_protocol_metadata);
55 #import <Security/SecProtocolPriv.h>
57 #import <Foundation/Foundation.h>
58 #import <CoreFoundation/CoreFoundation.h>
60 #import <xpc/private.h>
64 #ifndef SEC_ANALYZER_HIDE_DEADSTORE
65 # ifdef __clang_analyzer__
66 # define SEC_ANALYZER_HIDE_DEADSTORE(var) do { if (var) {} } while (0)
67 # else // __clang_analyzer__
68 # define SEC_ANALYZER_HIDE_DEADSTORE(var) do {} while (0)
69 # endif // __clang_analyzer__
70 #endif // SEC_ANALYZER_HIDE_DEADSTORE
72 SEC_OBJECT_IMPL_INTERNAL_OBJC(sec_array,
74 xpc_object_t xpc_array;
77 @implementation SEC_CONCRETE_CLASS_NAME(sec_array)
83 return SEC_NIL_OUT_OF_MEMORY;
85 self->xpc_array = xpc_array_create(NULL, 0);
91 if (self->xpc_array != nil) {
92 xpc_array_apply(self->xpc_array, ^bool(size_t index, __unused xpc_object_t value) {
93 void *pointer = xpc_array_get_pointer(self->xpc_array, index);
94 sec_object_t object = (sec_object_t)CFBridgingRelease(pointer);
95 SEC_ANALYZER_HIDE_DEADSTORE(object);
99 self->xpc_array = nil;
104 sec_array_create(void)
106 return [[SEC_CONCRETE_CLASS_NAME(sec_array) alloc] init];
110 sec_array_append(sec_array_t array, sec_object_t object)
113 array->xpc_array != NULL && xpc_get_type(array->xpc_array) == XPC_TYPE_ARRAY &&
115 void *retained_pointer = __DECONST(void *, CFBridgingRetain(object));
116 xpc_array_set_pointer(array->xpc_array, XPC_ARRAY_APPEND, retained_pointer);
117 // 'Leak' the retain, and save the pointer into the array
122 sec_array_get_count(sec_array_t array)
125 array->xpc_array != NULL && xpc_get_type(array->xpc_array) == XPC_TYPE_ARRAY) {
126 return xpc_array_get_count(array->xpc_array);
132 sec_array_apply(sec_array_t array, sec_array_applier_t applier)
135 array->xpc_array != NULL && xpc_get_type(array->xpc_array) == XPC_TYPE_ARRAY) {
136 return xpc_array_apply(array->xpc_array, ^bool(size_t index, __unused xpc_object_t value) {
137 void *pointer = xpc_array_get_pointer(array->xpc_array, index);
138 return applier(index, (__bridge sec_object_t)(pointer));
146 SEC_OBJECT_IMPL_INTERNAL_OBJC(sec_identity,
148 SecIdentityRef identity;
152 @implementation SEC_CONCRETE_CLASS_NAME(sec_identity)
154 - (instancetype)initWithIdentity:(SecIdentityRef)_identity
156 if (_identity == NULL) {
157 return SEC_NIL_BAD_INPUT;
162 return SEC_NIL_OUT_OF_MEMORY;
164 self->identity = __DECONST(SecIdentityRef, CFRetainSafe(_identity));
168 - (instancetype)initWithIdentityAndCertificates:(SecIdentityRef)_identity certificates:(CFArrayRef)certificates
170 if (_identity == NULL) {
171 return SEC_NIL_BAD_INPUT;
176 return SEC_NIL_OUT_OF_MEMORY;
178 self->identity = __DECONST(SecIdentityRef, CFRetainSafe(_identity));
179 self->certs = __DECONST(CFArrayRef, CFRetainSafe(certificates));
186 if (self->identity != NULL) {
187 CFRelease(self->identity);
188 self->identity = NULL;
191 CFRelease(self->certs);
198 sec_identity_create(SecIdentityRef identity)
200 return [[SEC_CONCRETE_CLASS_NAME(sec_identity) alloc] initWithIdentity:identity];
204 sec_identity_create_with_certificates(SecIdentityRef identity, CFArrayRef certificates)
206 return [[SEC_CONCRETE_CLASS_NAME(sec_identity) alloc] initWithIdentityAndCertificates:identity certificates:certificates];
210 sec_identity_copy_ref(sec_identity_t object)
212 if (object == NULL) {
213 return SEC_NULL_BAD_INPUT;
215 if (object->identity != NULL) {
216 return __DECONST(SecIdentityRef, CFRetain(object->identity));
218 return SEC_NULL_BAD_INPUT;
222 sec_identity_copy_certificates_ref(sec_identity_t object)
224 if (object == NULL) {
225 return SEC_NULL_BAD_INPUT;
227 if (object->certs != NULL) {
228 return __DECONST(CFArrayRef, CFRetain(object->certs));
230 return SEC_NULL_BAD_INPUT;
235 SEC_OBJECT_IMPL_INTERNAL_OBJC(sec_certificate,
237 SecCertificateRef certificate;
240 @implementation SEC_CONCRETE_CLASS_NAME(sec_certificate)
242 - (instancetype)initWithCertificate:(SecCertificateRef)_certificate
244 if (_certificate == NULL) {
245 return SEC_NIL_BAD_INPUT;
250 return SEC_NIL_OUT_OF_MEMORY;
252 self->certificate = __DECONST(SecCertificateRef, CFRetainSafe(_certificate));
258 if (self->certificate != NULL) {
259 CFRelease(self->certificate);
260 self->certificate = NULL;
265 sec_certificate_create(SecCertificateRef certificate)
267 return [[SEC_CONCRETE_CLASS_NAME(sec_certificate) alloc] initWithCertificate:certificate];
271 sec_certificate_copy_ref(sec_certificate_t object)
273 if (object == NULL) {
274 return SEC_NULL_BAD_INPUT;
276 if (object->certificate != NULL) {
277 return __DECONST(SecCertificateRef, CFRetain(object->certificate));
279 return SEC_NULL_BAD_INPUT;
284 SEC_OBJECT_IMPL_INTERNAL_OBJC(sec_trust,
289 @implementation SEC_CONCRETE_CLASS_NAME(sec_trust)
291 - (instancetype)initWithTrust:(SecTrustRef)_trust
293 if (_trust == NULL) {
294 return SEC_NIL_BAD_INPUT;
299 return SEC_NIL_OUT_OF_MEMORY;
301 self->trust = __DECONST(SecTrustRef, CFRetainSafe(_trust));
307 if (self->trust != NULL) {
308 CFRelease(self->trust);
314 sec_trust_create(SecTrustRef trust)
316 return [[SEC_CONCRETE_CLASS_NAME(sec_trust) alloc] initWithTrust:trust];
320 sec_trust_copy_ref(sec_trust_t object)
322 if (object == NULL) {
323 return SEC_NULL_BAD_INPUT;
325 if (object->trust != NULL) {
326 return __DECONST(SecTrustRef, CFRetain(object->trust));
328 return SEC_NULL_BAD_INPUT;
333 SEC_OBJECT_IMPL_INTERNAL_OBJC(sec_tls_extension,
336 sec_protocol_tls_ext_add_callback adder;
337 sec_protocol_tls_ext_parse_callback parser;
338 sec_protocol_tls_ext_free_callback freer;
341 @implementation SEC_CONCRETE_CLASS_NAME(sec_tls_extension)
343 - (instancetype)initWithCallbacks:(uint16_t)ext_type
344 adder:(sec_protocol_tls_ext_add_callback)add_block
345 parser:(sec_protocol_tls_ext_parse_callback)parse_block
346 freer:(sec_protocol_tls_ext_free_callback)free_block
348 if (add_block == nil) {
349 return SEC_NIL_BAD_INPUT;
351 if (parse_block == nil) {
352 return SEC_NIL_BAD_INPUT;
354 if (free_block == nil) {
355 return SEC_NIL_BAD_INPUT;
360 return SEC_NIL_OUT_OF_MEMORY;
363 self->type = ext_type;
364 self->adder = add_block;
365 self->parser = parse_block;
366 self->freer = free_block;
371 sec_tls_extension_get_type(sec_tls_extension_t extension)
373 if (extension == NULL) {
377 return extension->type;
380 sec_protocol_tls_ext_add_callback
381 sec_tls_extension_copy_add_block(sec_tls_extension_t extension)
383 if (extension == NULL) {
384 return SEC_NULL_BAD_INPUT;
387 return extension->adder;
390 sec_protocol_tls_ext_parse_callback
391 sec_tls_extension_copy_parse_block(sec_tls_extension_t extension)
393 if (extension == NULL) {
394 return SEC_NULL_BAD_INPUT;
397 return extension->parser;
400 sec_protocol_tls_ext_free_callback
401 sec_tls_extension_copy_free_block(sec_tls_extension_t extension)
403 if (extension == NULL) {
404 return SEC_NULL_BAD_INPUT;
407 return extension->freer;
411 sec_tls_extension_create(uint16_t type, sec_protocol_tls_ext_add_callback adder, sec_protocol_tls_ext_parse_callback parser, sec_protocol_tls_ext_free_callback freer)
413 return [[SEC_CONCRETE_CLASS_NAME(sec_tls_extension) alloc] initWithCallbacks:type adder:adder parser:parser freer:freer];