+
+ /* Free currently active x509 context (if any) unless it is still in use */
+ slot = array_get(&x509_creds, sizeof(x509_cred_slot), x509_cred_idx);
+ if ((slot != NULL) && (slot->refcnt <= 0) && (slot->x509_cred != NULL)) {
+ Log(LOG_INFO, "Discarding X509 certificate credentials from slot %zd.", x509_cred_idx);
+ /* TODO/FIXME: DH parameters will still leak memory. */
+ gnutls_certificate_free_keys(slot->x509_cred);
+ gnutls_certificate_free_credentials(slot->x509_cred);
+ slot->x509_cred = NULL;
+ slot->refcnt = 0;
+ }
+
+ /* Find free slot */
+ x509_cred_idx = (size_t) -1;
+ size_t i;
+ for (slot = array_start(&x509_creds), i = 0;
+ i < array_length(&x509_creds, sizeof(x509_cred_slot));
+ slot++, i++) {
+ if (slot->refcnt <= 0) {
+ x509_cred_idx = i;
+ break;
+ }
+ }
+ /* ... allocate new slot otherwise. */
+ if (x509_cred_idx == (size_t) -1) {
+ x509_cred_idx = array_length(&x509_creds, sizeof(x509_cred_slot));
+ slot = array_alloc(&x509_creds, sizeof(x509_cred_slot), x509_cred_idx);
+ if (slot == NULL) {
+ Log(LOG_ERR, "Failed to allocate new slot for certificate credentials");
+ return false;
+ }
+ }
+ Log(LOG_INFO, "Storing new X509 certificate credentials in slot %zd.", x509_cred_idx);
+ slot->x509_cred = x509_cred;
+ slot->refcnt = 0;
+