5 // Created by Martin Baulig on 3/6/16.
6 // Copyright © 2016 Xamarin. All rights reserved.
9 #include "btls-x509-lookup.h"
10 #include "btls-x509-lookup-mono.h"
12 struct MonoBtlsX509Lookup
{
13 MonoBtlsX509LookupType type
;
16 MonoBtlsX509Store
*store
;
17 CRYPTO_refcount_t references
;
20 static X509_LOOKUP_METHOD
*
21 get_lookup_method (MonoBtlsX509LookupType type
)
24 case MONO_BTLS_X509_LOOKUP_TYPE_FILE
:
25 return X509_LOOKUP_file ();
26 case MONO_BTLS_X509_LOOKUP_TYPE_HASH_DIR
:
27 return X509_LOOKUP_hash_dir ();
28 case MONO_BTLS_X509_LOOKUP_TYPE_MONO
:
29 return mono_btls_x509_lookup_mono_method ();
36 mono_btls_x509_lookup_new (MonoBtlsX509Store
*store
, MonoBtlsX509LookupType type
)
38 MonoBtlsX509Lookup
*lookup
;
39 X509_LOOKUP
*store_lookup
;
40 X509_LOOKUP_METHOD
*method
;
42 method
= get_lookup_method (type
);
46 lookup
= OPENSSL_malloc (sizeof(MonoBtlsX509Lookup
));
50 store_lookup
= X509_STORE_add_lookup (mono_btls_x509_store_peek_store (store
), method
);
52 OPENSSL_free (lookup
);
56 memset (lookup
, 0, sizeof(MonoBtlsX509Lookup
));
57 // The X509_STORE owns the X509_LOOKUP.
58 lookup
->store
= mono_btls_x509_store_up_ref (store
);
59 lookup
->lookup
= store_lookup
;
60 lookup
->owns_lookup
= 0;
61 lookup
->references
= 1;
67 mono_btls_x509_lookup_load_file (MonoBtlsX509Lookup
*lookup
, const char *file
, MonoBtlsX509FileType type
)
69 return X509_LOOKUP_load_file (lookup
->lookup
, file
, type
);
73 mono_btls_x509_lookup_add_dir (MonoBtlsX509Lookup
*lookup
, const char *dir
, MonoBtlsX509FileType type
)
75 return X509_LOOKUP_add_dir (lookup
->lookup
, dir
, type
);
79 mono_btls_x509_lookup_up_ref (MonoBtlsX509Lookup
*lookup
)
81 CRYPTO_refcount_inc (&lookup
->references
);
86 mono_btls_x509_lookup_free (MonoBtlsX509Lookup
*lookup
)
88 if (!CRYPTO_refcount_dec_and_test_zero (&lookup
->references
))
92 mono_btls_x509_store_free (lookup
->store
);
97 if (lookup
->owns_lookup
)
98 X509_LOOKUP_free (lookup
->lookup
);
99 lookup
->lookup
= NULL
;
102 OPENSSL_free (lookup
);
107 mono_btls_x509_lookup_init (MonoBtlsX509Lookup
*lookup
)
109 return X509_LOOKUP_init (lookup
->lookup
);
113 mono_btls_x509_lookup_shutdown (MonoBtlsX509Lookup
*lookup
)
115 return X509_LOOKUP_shutdown (lookup
->lookup
);
118 MonoBtlsX509LookupType
119 mono_btls_x509_lookup_get_type (MonoBtlsX509Lookup
*lookup
)
125 mono_btls_x509_lookup_peek_lookup (MonoBtlsX509Lookup
*lookup
)
127 return lookup
->lookup
;
131 mono_btls_x509_lookup_by_subject (MonoBtlsX509Lookup
*lookup
, MonoBtlsX509Name
*name
)
137 ret
= X509_LOOKUP_by_subject (lookup
->lookup
, X509_LU_X509
, mono_btls_x509_name_peek_name (name
), &obj
);
138 if (ret
!= X509_LU_X509
) {
139 X509_OBJECT_free_contents (&obj
);
143 x509
= X509_up_ref (obj
.data
.x509
);
148 mono_btls_x509_lookup_by_fingerprint (MonoBtlsX509Lookup
*lookup
, unsigned char *bytes
, int len
)
154 ret
= X509_LOOKUP_by_fingerprint (lookup
->lookup
, X509_LU_X509
, bytes
, len
, &obj
);
155 if (ret
!= X509_LU_X509
) {
156 X509_OBJECT_free_contents (&obj
);
160 x509
= X509_up_ref (obj
.data
.x509
);