5 // Created by Martin Baulig on 3/3/16.
6 // Copyright © 2016 Xamarin. All rights reserved.
9 #include <btls-x509-chain.h>
11 struct MonoBtlsX509Chain
{
12 STACK_OF(X509
) *certs
;
13 CRYPTO_refcount_t references
;
16 MONO_API MonoBtlsX509Chain
*
17 mono_btls_x509_chain_new (void)
19 MonoBtlsX509Chain
*chain
= (MonoBtlsX509Chain
*)OPENSSL_malloc (sizeof (MonoBtlsX509Chain
));
23 memset(chain
, 0, sizeof(MonoBtlsX509Chain
));
24 chain
->certs
= sk_X509_new_null ();
25 chain
->references
= 1;
29 MONO_API MonoBtlsX509Chain
*
30 mono_btls_x509_chain_from_certs (STACK_OF(X509
) *certs
)
32 MonoBtlsX509Chain
*chain
= (MonoBtlsX509Chain
*)OPENSSL_malloc (sizeof (MonoBtlsX509Chain
));
36 memset(chain
, 0, sizeof(MonoBtlsX509Chain
));
37 chain
->certs
= X509_chain_up_ref(certs
);
38 chain
->references
= 1;
42 MONO_API
STACK_OF(X509
) *
43 mono_btls_x509_chain_peek_certs (MonoBtlsX509Chain
*chain
)
49 mono_btls_x509_chain_get_count (MonoBtlsX509Chain
*chain
)
51 return (int)sk_X509_num(chain
->certs
);
55 mono_btls_x509_chain_get_cert (MonoBtlsX509Chain
*chain
, int index
)
59 if ((size_t)index
>= sk_X509_num(chain
->certs
))
61 cert
= sk_X509_value(chain
->certs
, index
);
67 MONO_API
STACK_OF(X509
) *
68 mono_btls_x509_chain_get_certs (MonoBtlsX509Chain
*chain
)
74 mono_btls_x509_chain_free (MonoBtlsX509Chain
*chain
)
76 if (!CRYPTO_refcount_dec_and_test_zero(&chain
->references
))
79 sk_X509_pop_free(chain
->certs
, X509_free
);
84 MONO_API MonoBtlsX509Chain
*
85 mono_btls_x509_chain_up_ref (MonoBtlsX509Chain
*chain
)
87 CRYPTO_refcount_inc(&chain
->references
);
92 mono_btls_x509_chain_add_cert (MonoBtlsX509Chain
*chain
, X509
*x509
)
95 sk_X509_push(chain
->certs
, x509
);