2 * ssl_client_cert_pw_providers.c: providers for
3 * SVN_AUTH_CRED_SSL_CLIENT_CERT_PW
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 * ====================================================================
26 #include <apr_pools.h>
30 #include "svn_error.h"
31 #include "svn_config.h"
32 #include "svn_string.h"
34 #include "private/svn_auth_private.h"
36 #include "svn_private_config.h"
38 /*-----------------------------------------------------------------------*/
40 /*-----------------------------------------------------------------------*/
42 /* Baton type for the ssl client cert passphrase provider. */
43 typedef struct ssl_client_cert_pw_file_provider_baton_t
45 svn_auth_plaintext_passphrase_prompt_func_t plaintext_passphrase_prompt_func;
47 /* We cache the user's answer to the plaintext prompt, keyed
48 by realm, in case we'll be called multiple times for the
49 same realm. So: keys are 'const char *' realm strings, and
50 values are 'svn_boolean_t *'. */
51 apr_hash_t *plaintext_answers;
52 } ssl_client_cert_pw_file_provider_baton_t;
54 /* This implements the svn_auth__password_get_t interface.
55 Set **PASSPHRASE to the plaintext passphrase retrieved from CREDS;
56 ignore other parameters. */
58 svn_auth__ssl_client_cert_pw_get(svn_boolean_t *done,
59 const char **passphrase,
61 const char *realmstring,
63 apr_hash_t *parameters,
64 svn_boolean_t non_interactive,
68 str = svn_hash_gets(creds, SVN_CONFIG_AUTHN_PASSPHRASE_KEY);
71 *passphrase = str->data;
79 /* This implements the svn_auth__password_set_t interface.
80 Store PASSPHRASE in CREDS; ignore other parameters. */
82 svn_auth__ssl_client_cert_pw_set(svn_boolean_t *done,
84 const char *realmstring,
86 const char *passphrase,
87 apr_hash_t *parameters,
88 svn_boolean_t non_interactive,
91 svn_hash_sets(creds, SVN_CONFIG_AUTHN_PASSPHRASE_KEY,
92 svn_string_create(passphrase, pool));
98 svn_auth__ssl_client_cert_pw_cache_get(void **credentials_p,
100 void *provider_baton,
101 apr_hash_t *parameters,
102 const char *realmstring,
103 svn_auth__password_get_t passphrase_get,
104 const char *passtype,
107 svn_config_t *cfg = svn_hash_gets(parameters,
108 SVN_AUTH_PARAM_CONFIG_CATEGORY_SERVERS);
109 const char *server_group = svn_hash_gets(parameters,
110 SVN_AUTH_PARAM_SERVER_GROUP);
111 svn_boolean_t non_interactive = svn_hash_gets(parameters,
112 SVN_AUTH_PARAM_NON_INTERACTIVE)
114 const char *password =
115 svn_config_get_server_setting(cfg, server_group,
116 SVN_CONFIG_OPTION_SSL_CLIENT_CERT_PASSWORD,
121 apr_hash_t *creds_hash = NULL;
122 const char *config_dir = svn_hash_gets(parameters,
123 SVN_AUTH_PARAM_CONFIG_DIR);
125 /* Try to load passphrase from the auth/ cache. */
126 err = svn_config_read_auth_data(&creds_hash,
127 SVN_AUTH_CRED_SSL_CLIENT_CERT_PW,
128 realmstring, config_dir, pool);
129 svn_error_clear(err);
130 if (! err && creds_hash)
134 SVN_ERR(passphrase_get(&done, &password, creds_hash, realmstring,
135 NULL, parameters, non_interactive, pool));
143 svn_auth_cred_ssl_client_cert_pw_t *cred
144 = apr_palloc(pool, sizeof(*cred));
145 cred->password = password;
146 cred->may_save = FALSE;
147 *credentials_p = cred;
149 else *credentials_p = NULL;
156 svn_auth__ssl_client_cert_pw_cache_set(svn_boolean_t *saved,
158 void *provider_baton,
159 apr_hash_t *parameters,
160 const char *realmstring,
161 svn_auth__password_set_t passphrase_set,
162 const char *passtype,
165 svn_auth_cred_ssl_client_cert_pw_t *creds = credentials;
166 apr_hash_t *creds_hash = NULL;
167 const char *config_dir;
169 svn_boolean_t dont_store_passphrase =
170 svn_hash_gets(parameters, SVN_AUTH_PARAM_DONT_STORE_SSL_CLIENT_CERT_PP)
172 svn_boolean_t non_interactive =
173 svn_hash_gets(parameters, SVN_AUTH_PARAM_NON_INTERACTIVE) != NULL;
174 svn_boolean_t no_auth_cache =
176 || (svn_hash_gets(parameters, SVN_AUTH_PARAM_NO_AUTH_CACHE) != NULL);
183 config_dir = svn_hash_gets(parameters, SVN_AUTH_PARAM_CONFIG_DIR);
184 creds_hash = apr_hash_make(pool);
186 /* Don't store passphrase in any form if the user has told
188 if (! dont_store_passphrase)
190 svn_boolean_t may_save_passphrase = FALSE;
192 /* If the passphrase is going to be stored encrypted, go right
193 ahead and store it to disk. Else determine whether saving
194 in plaintext is OK. */
195 if (strcmp(passtype, SVN_AUTH__WINCRYPT_PASSWORD_TYPE) == 0
196 || strcmp(passtype, SVN_AUTH__KWALLET_PASSWORD_TYPE) == 0
197 || strcmp(passtype, SVN_AUTH__GNOME_KEYRING_PASSWORD_TYPE) == 0
198 || strcmp(passtype, SVN_AUTH__KEYCHAIN_PASSWORD_TYPE) == 0)
200 may_save_passphrase = TRUE;
204 #ifdef SVN_DISABLE_PLAINTEXT_PASSWORD_STORAGE
205 may_save_passphrase = FALSE;
207 const char *store_ssl_client_cert_pp_plaintext =
208 svn_hash_gets(parameters,
209 SVN_AUTH_PARAM_STORE_SSL_CLIENT_CERT_PP_PLAINTEXT);
210 ssl_client_cert_pw_file_provider_baton_t *b =
211 (ssl_client_cert_pw_file_provider_baton_t *)provider_baton;
213 if (svn_cstring_casecmp(store_ssl_client_cert_pp_plaintext,
214 SVN_CONFIG_ASK) == 0)
218 /* In non-interactive mode, the default behaviour is
219 to not store the passphrase */
220 may_save_passphrase = FALSE;
222 else if (b->plaintext_passphrase_prompt_func)
224 /* We're interactive, and the client provided a
225 prompt callback. So we can ask the user.
226 Check for a cached answer before prompting.
228 This is a pointer-to-boolean, rather than just a
229 boolean, because we must distinguish between
230 "cached answer is no" and "no answer has been
232 svn_boolean_t *cached_answer =
233 svn_hash_gets(b->plaintext_answers, realmstring);
235 if (cached_answer != NULL)
237 may_save_passphrase = *cached_answer;
241 apr_pool_t *cached_answer_pool;
243 /* Nothing cached for this realm, prompt the user. */
244 SVN_ERR((*b->plaintext_passphrase_prompt_func)(
245 &may_save_passphrase,
250 /* Cache the user's answer in case we're called again
251 * for the same realm.
253 * We allocate the answer cache in the hash table's pool
254 * to make sure that is has the same life time as the
255 * hash table itself. This means that the answer will
256 * survive across RA sessions -- which is important,
257 * because otherwise we'd prompt users once per RA session.
259 cached_answer_pool = apr_hash_pool_get(b->plaintext_answers);
260 cached_answer = apr_palloc(cached_answer_pool,
261 sizeof(*cached_answer));
262 *cached_answer = may_save_passphrase;
263 svn_hash_sets(b->plaintext_answers, realmstring,
269 may_save_passphrase = FALSE;
272 else if (svn_cstring_casecmp(store_ssl_client_cert_pp_plaintext,
273 SVN_CONFIG_FALSE) == 0)
275 may_save_passphrase = FALSE;
277 else if (svn_cstring_casecmp(store_ssl_client_cert_pp_plaintext,
278 SVN_CONFIG_TRUE) == 0)
280 may_save_passphrase = TRUE;
284 return svn_error_createf
285 (SVN_ERR_RA_DAV_INVALID_CONFIG_VALUE, NULL,
286 _("Config error: invalid value '%s' for option '%s'"),
287 store_ssl_client_cert_pp_plaintext,
288 SVN_AUTH_PARAM_STORE_SSL_CLIENT_CERT_PP_PLAINTEXT);
293 if (may_save_passphrase)
295 SVN_ERR(passphrase_set(saved, creds_hash, realmstring,
296 NULL, creds->password, parameters,
297 non_interactive, pool));
299 if (*saved && passtype)
301 svn_hash_sets(creds_hash, SVN_CONFIG_AUTHN_PASSTYPE_KEY,
302 svn_string_create(passtype, pool));
305 /* Save credentials to disk. */
306 err = svn_config_write_auth_data(creds_hash,
307 SVN_AUTH_CRED_SSL_CLIENT_CERT_PW,
308 realmstring, config_dir, pool);
309 svn_error_clear(err);
318 /* This implements the svn_auth_provider_t.first_credentials API.
319 It gets cached (unencrypted) credentials from the ssl client cert
320 password provider's cache. */
322 ssl_client_cert_pw_file_first_credentials(void **credentials_p,
324 void *provider_baton,
325 apr_hash_t *parameters,
326 const char *realmstring,
329 return svn_auth__ssl_client_cert_pw_cache_get(credentials_p, iter_baton,
330 provider_baton, parameters,
332 svn_auth__ssl_client_cert_pw_get,
333 SVN_AUTH__SIMPLE_PASSWORD_TYPE,
338 /* This implements the svn_auth_provider_t.save_credentials API.
339 It saves the credentials unencrypted. */
341 ssl_client_cert_pw_file_save_credentials(svn_boolean_t *saved,
343 void *provider_baton,
344 apr_hash_t *parameters,
345 const char *realmstring,
348 return svn_auth__ssl_client_cert_pw_cache_set(saved, credentials,
352 svn_auth__ssl_client_cert_pw_set,
353 SVN_AUTH__SIMPLE_PASSWORD_TYPE,
358 static const svn_auth_provider_t ssl_client_cert_pw_file_provider = {
359 SVN_AUTH_CRED_SSL_CLIENT_CERT_PW,
360 ssl_client_cert_pw_file_first_credentials,
362 ssl_client_cert_pw_file_save_credentials
366 /*** Public API to SSL file providers. ***/
368 svn_auth_get_ssl_client_cert_pw_file_provider2
369 (svn_auth_provider_object_t **provider,
370 svn_auth_plaintext_passphrase_prompt_func_t plaintext_passphrase_prompt_func,
374 svn_auth_provider_object_t *po = apr_pcalloc(pool, sizeof(*po));
375 ssl_client_cert_pw_file_provider_baton_t *pb = apr_pcalloc(pool,
378 pb->plaintext_passphrase_prompt_func = plaintext_passphrase_prompt_func;
379 pb->prompt_baton = prompt_baton;
380 pb->plaintext_answers = apr_hash_make(pool);
382 po->vtable = &ssl_client_cert_pw_file_provider;
383 po->provider_baton = pb;
388 /*-----------------------------------------------------------------------*/
389 /* Prompt provider */
390 /*-----------------------------------------------------------------------*/
392 /* Baton type for client passphrase prompting.
393 There is no iteration baton type. */
394 typedef struct ssl_client_cert_pw_prompt_provider_baton_t
396 svn_auth_ssl_client_cert_pw_prompt_func_t prompt_func;
399 /* how many times to re-prompt after the first one fails */
401 } ssl_client_cert_pw_prompt_provider_baton_t;
403 /* Iteration baton. */
404 typedef struct ssl_client_cert_pw_prompt_iter_baton_t
406 /* The original provider baton */
407 ssl_client_cert_pw_prompt_provider_baton_t *pb;
409 /* The original realmstring */
410 const char *realmstring;
412 /* how many times we've reprompted */
414 } ssl_client_cert_pw_prompt_iter_baton_t;
418 ssl_client_cert_pw_prompt_first_cred(void **credentials_p,
420 void *provider_baton,
421 apr_hash_t *parameters,
422 const char *realmstring,
425 ssl_client_cert_pw_prompt_provider_baton_t *pb = provider_baton;
426 ssl_client_cert_pw_prompt_iter_baton_t *ib =
427 apr_pcalloc(pool, sizeof(*ib));
428 const char *no_auth_cache = svn_hash_gets(parameters,
429 SVN_AUTH_PARAM_NO_AUTH_CACHE);
431 SVN_ERR(pb->prompt_func((svn_auth_cred_ssl_client_cert_pw_t **)
432 credentials_p, pb->prompt_baton, realmstring,
433 ! no_auth_cache, pool));
436 ib->realmstring = apr_pstrdup(pool, realmstring);
445 ssl_client_cert_pw_prompt_next_cred(void **credentials_p,
447 void *provider_baton,
448 apr_hash_t *parameters,
449 const char *realmstring,
452 ssl_client_cert_pw_prompt_iter_baton_t *ib = iter_baton;
453 const char *no_auth_cache = svn_hash_gets(parameters,
454 SVN_AUTH_PARAM_NO_AUTH_CACHE);
456 if ((ib->pb->retry_limit >= 0) && (ib->retries >= ib->pb->retry_limit))
458 /* give up, go on to next provider. */
459 *credentials_p = NULL;
464 return ib->pb->prompt_func((svn_auth_cred_ssl_client_cert_pw_t **)
465 credentials_p, ib->pb->prompt_baton,
466 ib->realmstring, ! no_auth_cache, pool);
470 static const svn_auth_provider_t client_cert_pw_prompt_provider = {
471 SVN_AUTH_CRED_SSL_CLIENT_CERT_PW,
472 ssl_client_cert_pw_prompt_first_cred,
473 ssl_client_cert_pw_prompt_next_cred,
478 void svn_auth_get_ssl_client_cert_pw_prompt_provider
479 (svn_auth_provider_object_t **provider,
480 svn_auth_ssl_client_cert_pw_prompt_func_t prompt_func,
485 svn_auth_provider_object_t *po = apr_pcalloc(pool, sizeof(*po));
486 ssl_client_cert_pw_prompt_provider_baton_t *pb =
487 apr_palloc(pool, sizeof(*pb));
489 pb->prompt_func = prompt_func;
490 pb->prompt_baton = prompt_baton;
491 pb->retry_limit = retry_limit;
493 po->vtable = &client_cert_pw_prompt_provider;
494 po->provider_baton = pb;