2 * Copyright (C) 2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include "android_creds.h"
17 #include "../charonservice.h"
21 #include <credentials/sets/mem_cred.h>
22 #include <threading/rwlock.h>
24 typedef struct private_android_creds_t private_android_creds_t
;
27 * Private data of an android_creds_t object
29 struct private_android_creds_t
{
34 android_creds_t
public;
37 * Credential set storing trusted certificates
42 * read/write lock to make sure certificates are only loaded once
47 * TRUE if certificates have been loaded via JNI
53 * Load trusted certificates via charonservice (JNI).
55 static void load_trusted_certificates(private_android_creds_t
*this)
61 certs
= charonservice
->get_trusted_certificates(charonservice
);
64 while (certs
->remove_first(certs
, (void**)¤t
) == SUCCESS
)
66 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
67 BUILD_BLOB_ASN1_DER
, *current
, BUILD_END
);
70 DBG2(DBG_CFG
, "loaded CA certificate '%Y'",
71 cert
->get_subject(cert
));
72 this->creds
->add_cert(this->creds
, TRUE
, cert
);
77 certs
->destroy(certs
);
81 METHOD(credential_set_t
, create_cert_enumerator
, enumerator_t
*,
82 private_android_creds_t
*this, certificate_type_t cert
, key_type_t key
,
83 identification_t
*id
, bool trusted
)
85 enumerator_t
*enumerator
;
87 if (!trusted
|| (cert
!= CERT_ANY
&& cert
!= CERT_X509
))
91 this->lock
->read_lock(this->lock
);
94 this->lock
->unlock(this->lock
);
95 this->lock
->write_lock(this->lock
);
96 /* check again after acquiring the write lock */
99 load_trusted_certificates(this);
102 this->lock
->unlock(this->lock
);
103 this->lock
->read_lock(this->lock
);
105 enumerator
= this->creds
->set
.create_cert_enumerator(&this->creds
->set
,
106 cert
, key
, id
, trusted
);
107 return enumerator_create_cleaner(enumerator
, (void*)this->lock
->unlock
,
111 METHOD(android_creds_t
, clear
, void,
112 private_android_creds_t
*this)
114 this->lock
->write_lock(this->lock
);
115 this->creds
->clear(this->creds
);
116 this->loaded
= FALSE
;
117 this->lock
->unlock(this->lock
);
120 METHOD(android_creds_t
, destroy
, void,
121 private_android_creds_t
*this)
124 this->creds
->destroy(this->creds
);
125 this->lock
->destroy(this->lock
);
130 * Described in header.
132 android_creds_t
*android_creds_create()
134 private_android_creds_t
*this;
139 .create_cert_enumerator
= _create_cert_enumerator
,
140 .create_shared_enumerator
= (void*)return_null
,
141 .create_private_enumerator
= (void*)return_null
,
142 .create_cdp_enumerator
= (void*)return_null
,
143 .cache_cert
= (void*)nop
,
148 .creds
= mem_cred_create(),
149 .lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
),
152 return &this->public;