2 * ssl_server_trust_providers.c: providers for
3 * SVN_AUTH_CRED_SSL_SERVER_TRUST
5 * ====================================================================
6 * Licensed to the Apache Software Foundation (ASF) under one
7 * or more contributor license agreements. See the NOTICE file
8 * distributed with this work for additional information
9 * regarding copyright ownership. The ASF licenses this file
10 * to you under the Apache License, Version 2.0 (the
11 * "License"); you may not use this file except in compliance
12 * with the License. You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing,
17 * software distributed under the License is distributed on an
18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 * KIND, either express or implied. See the License for the
20 * specific language governing permissions and limitations
22 * ====================================================================
25 #include <apr_pools.h>
29 #include "svn_error.h"
30 #include "svn_config.h"
31 #include "svn_string.h"
34 /*-----------------------------------------------------------------------*/
36 /*-----------------------------------------------------------------------*/
38 /* retrieve ssl server CA failure overrides (if any) from servers
41 ssl_server_trust_file_first_credentials(void **credentials,
44 apr_hash_t *parameters,
45 const char *realmstring,
48 apr_uint32_t *failures = svn_hash_gets(parameters,
49 SVN_AUTH_PARAM_SSL_SERVER_FAILURES);
50 const svn_auth_ssl_server_cert_info_t *cert_info =
51 svn_hash_gets(parameters, SVN_AUTH_PARAM_SSL_SERVER_CERT_INFO);
52 apr_hash_t *creds_hash = NULL;
53 const char *config_dir;
54 svn_error_t *error = SVN_NO_ERROR;
59 /* Check if this is a permanently accepted certificate */
60 config_dir = svn_hash_gets(parameters, SVN_AUTH_PARAM_CONFIG_DIR);
62 svn_config_read_auth_data(&creds_hash, SVN_AUTH_CRED_SSL_SERVER_TRUST,
63 realmstring, config_dir, pool);
64 svn_error_clear(error);
65 if (! error && creds_hash)
67 svn_string_t *trusted_cert, *this_cert, *failstr;
68 apr_uint32_t last_failures = 0;
70 trusted_cert = svn_hash_gets(creds_hash, SVN_CONFIG_AUTHN_ASCII_CERT_KEY);
71 this_cert = svn_string_create(cert_info->ascii_cert, pool);
72 failstr = svn_hash_gets(creds_hash, SVN_CONFIG_AUTHN_FAILURES_KEY);
75 SVN_ERR(svn_cstring_atoui(&last_failures, failstr->data));
77 /* If the cert is trusted and there are no new failures, we
78 * accept it by clearing all failures. */
80 svn_string_compare(this_cert, trusted_cert) &&
81 (*failures & ~last_failures) == 0)
87 /* If all failures are cleared now, we return the creds */
90 svn_auth_cred_ssl_server_trust_t *creds =
91 apr_pcalloc(pool, sizeof(*creds));
92 creds->may_save = FALSE; /* No need to save it again... */
101 ssl_server_trust_file_save_credentials(svn_boolean_t *saved,
103 void *provider_baton,
104 apr_hash_t *parameters,
105 const char *realmstring,
108 svn_auth_cred_ssl_server_trust_t *creds = credentials;
109 const svn_auth_ssl_server_cert_info_t *cert_info;
110 apr_hash_t *creds_hash = NULL;
111 const char *config_dir;
113 if (! creds->may_save)
116 config_dir = svn_hash_gets(parameters, SVN_AUTH_PARAM_CONFIG_DIR);
118 cert_info = svn_hash_gets(parameters, SVN_AUTH_PARAM_SSL_SERVER_CERT_INFO);
120 creds_hash = apr_hash_make(pool);
121 svn_hash_sets(creds_hash, SVN_CONFIG_AUTHN_ASCII_CERT_KEY,
122 svn_string_create(cert_info->ascii_cert, pool));
123 svn_hash_sets(creds_hash, SVN_CONFIG_AUTHN_FAILURES_KEY,
124 svn_string_createf(pool, "%lu",
125 (unsigned long)creds->accepted_failures));
127 SVN_ERR(svn_config_write_auth_data(creds_hash,
128 SVN_AUTH_CRED_SSL_SERVER_TRUST,
137 static const svn_auth_provider_t ssl_server_trust_file_provider = {
138 SVN_AUTH_CRED_SSL_SERVER_TRUST,
139 ssl_server_trust_file_first_credentials,
141 ssl_server_trust_file_save_credentials,
145 /*** Public API to SSL file providers. ***/
147 svn_auth_get_ssl_server_trust_file_provider
148 (svn_auth_provider_object_t **provider, apr_pool_t *pool)
150 svn_auth_provider_object_t *po = apr_pcalloc(pool, sizeof(*po));
152 po->vtable = &ssl_server_trust_file_provider;
157 /*-----------------------------------------------------------------------*/
158 /* Prompt provider */
159 /*-----------------------------------------------------------------------*/
161 /* Baton type for prompting to verify server ssl creds.
162 There is no iteration baton type. */
163 typedef struct ssl_server_trust_prompt_provider_baton_t
165 svn_auth_ssl_server_trust_prompt_func_t prompt_func;
167 } ssl_server_trust_prompt_provider_baton_t;
171 ssl_server_trust_prompt_first_cred(void **credentials_p,
173 void *provider_baton,
174 apr_hash_t *parameters,
175 const char *realmstring,
178 ssl_server_trust_prompt_provider_baton_t *pb = provider_baton;
179 apr_uint32_t *failures = svn_hash_gets(parameters,
180 SVN_AUTH_PARAM_SSL_SERVER_FAILURES);
181 const char *no_auth_cache = svn_hash_gets(parameters,
182 SVN_AUTH_PARAM_NO_AUTH_CACHE);
183 const svn_auth_ssl_server_cert_info_t *cert_info =
184 svn_hash_gets(parameters, SVN_AUTH_PARAM_SSL_SERVER_CERT_INFO);
185 svn_boolean_t may_save = (!no_auth_cache
186 && !(*failures & SVN_AUTH_SSL_OTHER));
188 SVN_ERR(pb->prompt_func((svn_auth_cred_ssl_server_trust_t **)credentials_p,
189 pb->prompt_baton, realmstring, *failures, cert_info,
197 static const svn_auth_provider_t ssl_server_trust_prompt_provider = {
198 SVN_AUTH_CRED_SSL_SERVER_TRUST,
199 ssl_server_trust_prompt_first_cred,
205 /*** Public API to SSL prompting providers. ***/
207 svn_auth_get_ssl_server_trust_prompt_provider
208 (svn_auth_provider_object_t **provider,
209 svn_auth_ssl_server_trust_prompt_func_t prompt_func,
213 svn_auth_provider_object_t *po = apr_pcalloc(pool, sizeof(*po));
214 ssl_server_trust_prompt_provider_baton_t *pb =
215 apr_palloc(pool, sizeof(*pb));
216 pb->prompt_func = prompt_func;
217 pb->prompt_baton = prompt_baton;
218 po->vtable = &ssl_server_trust_prompt_provider;
219 po->provider_baton = pb;