VLC 4.0.0-dev
Loading...
Searching...
No Matches
vlc_keystore.h
Go to the documentation of this file.
1/*****************************************************************************
2 * vlc_keystore.h:
3 *****************************************************************************
4 * Copyright (C) 2015-2016 VLC authors and VideoLAN
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
20
21#ifndef VLC_KEYSTORE_H
22# define VLC_KEYSTORE_H
23
24#include <vlc_common.h>
25
26typedef struct vlc_keystore vlc_keystore;
30/* Called from src/libvlc.c */
31int
33
34/* Called from src/libvlc.c */
35void
37
38/**
39 * @defgroup keystore Keystore and credential API
40 * @ingroup os
41 * @{
42 * @file
43 * This file declares vlc keystore API
44 * @defgroup keystore_public Keystore public API
45 * @{
46 */
47
48/**
49 * List of keys that can be stored via the keystore API
50 */
61};
62#define VLC_KEYSTORE_VALUES_INIT(ppsz_values) memset(ppsz_values, 0, sizeof(const char *) * KEY_MAX)
64/**
65 * Keystore entry returned by vlc_keystore_find()
66 */
69 /** Set of key/values. Values can be NULL */
70 char * ppsz_values[KEY_MAX];
71 /** Secret password */
72 uint8_t * p_secret;
73 /** Length of the secret */
74 size_t i_secret_len;
75};
76
77/**
78 * Create a keystore object
79 *
80 * A keystore object is persistent across runtime. It is saved on local
81 * filesystem via a vlc keystore module (KWallet, SecretService, Apple Keychain
82 * Service ...).
83 *
84 * @note to be released with vlc_keystore_release()
85 *
86 * @param p_parent the parent object used to create the keystore object
87 *
88 * @return a pointer to the keystore object, or NULL in case of error
89 */
92#define vlc_keystore_create(x) vlc_keystore_create(VLC_OBJECT(x))
94/**
95 * Release a keystore object
96 */
97VLC_API void
99
100
101/**
102 * Store a secret associated with a set of key/values
103 *
104 * @param p_keystore the keystore to store the secret into
105 * @param ppsz_values set of key/values, see vlc_keystore_key.
106 * ppsz_values[KEY_PROTOCOL] and ppsz_values[KEY_SERVER] must be valid
107 * strings
108 * @param p_secret binary secret or string password
109 * @param i_secret_len length of p_secret. If it's less than 0, then p_secret
110 * is assumed to be a '\0' terminated string
111 * @param psz_label user friendly label
112 *
113 * @return VLC_SUCCESS on success, or VLC_EGENERIC on error
114 */
115VLC_API int
117 const char *const ppsz_values[KEY_MAX],
118 const uint8_t* p_secret, ssize_t i_secret_len,
119 const char *psz_label);
120
121/**
122 * Find all entries that match a set of key/values
123 *
124 * @param p_keystore the keystore instance to look into
125 * @param ppsz_values set of key/values, see vlc_keystore_key, any values can
126 * be NULL
127 * @param pp_entries list of found entries. To be released with
128 * vlc_keystore_release_entries()
129 *
130 * @return the number of entries
131 */
132VLC_API unsigned int
134 const char *const ppsz_values[KEY_MAX],
135 vlc_keystore_entry **pp_entries) VLC_USED;
136
137/**
138 * Remove all entries that match a set of key/values
139 *
140 * @note only entries added by VLC can be removed
141 *
142 * @param p_keystore the keystore instance to remove the secrets from
143 * @param ppsz_values set of key/values, see vlc_keystore_key, any values can
144 * be NULL
145 *
146 * @return the number of entries
147 */
148VLC_API unsigned int
150 const char *const ppsz_values[KEY_MAX]);
151
152/**
153 * Release the list of entries returned by vlc_keystore_find()
154 */
155VLC_API void
156vlc_keystore_release_entries(vlc_keystore_entry *p_entries, unsigned int i_count);
157
158/**
159 * @}
160 * @defgroup credential Credential API
161 * @{
162 */
163
164/**
165 * @note init with vlc_credential_init()
166 */
167struct vlc_credential
169 /** url to store or to search */
170 const vlc_url_t *p_url;
171 /** http realm or smb domain to search, can be overridden after a call to
172 * vlc_credential_get() */
173 const char *psz_realm;
174 /** http authtype to search, can be overridden after a call to
175 * vlc_credential_get() */
176 const char *psz_authtype;
177 /** valid only if vlc_credential_get() returned true */
178 const char *psz_username;
179 /** valid only if vlc_credential_get() returned true */
180 const char *psz_password;
181 /** valid only if vlc_credential_get_bearer() returned 0 */
182 const char *psz_token;
183 /** scopes the bearer token must be stored with */
184 const char *const *ppsz_scopes;
185 size_t i_scopes;
187 /* internal */
188 enum {
198 unsigned int i_entries_count;
202 char *psz_split_domain;
211
212/**
213 * Init a credential struct
214 *
215 * @note to be cleaned with vlc_credential_clean()
216 *
217 * @param p_credential a credential instance to initialize
218 * @param p_url url to store or to search
219 */
220VLC_API void
221vlc_credential_init(vlc_credential *p_credential, const vlc_url_t *p_url);
222
223/**
224 * Clean a credential struct
225 */
226VLC_API void
228
229/**
230 * Get a username/password couple
231 *
232 * This will search for a credential using url, VLC options, the vlc_keystore
233 * or by asking the user via dialog_Login(). This function can be called
234 * indefinitely, it will first return the user/password from the url (if any),
235 * then from VLC options (if any), then from the keystore (if any), and finally
236 * from the dialog (if any). This function will return true as long as the user
237 * fill the dialog texts and will return false when the user cancel it.
238 *
239 * @param p_credential a credential instance initialized with TODO
240 * @param p_parent the parent object (for var, keystore and dialog)
241 * @param psz_option_username VLC option name for the username
242 * @param psz_option_password VLC option name for the password
243 * @param psz_dialog_title dialog title, if NULL, this function won't use the
244 * keystore or the dialog
245 * @param psz_dialog_fmt dialog text using format
246 *
247 * @return 0 if vlc_credential.psz_username and vlc_credential.psz_password
248 * are valid, or a negative errno code.
249 */
250
251VLC_API int
252vlc_credential_get(vlc_credential *p_credential, vlc_object_t *p_parent,
253 const char *psz_option_username,
254 const char *psz_option_password,
255 const char *psz_dialog_title,
256 const char *psz_dialog_fmt, ...) VLC_FORMAT(6, 7);
257#define vlc_credential_get(a, b, c, d, e, f, ...) \
258 vlc_credential_get(a, VLC_OBJECT(b), c, d, e, f, ##__VA_ARGS__)
259
260/**
261 * Store the last dialog credential returned by vlc_credential_get()
262 *
263 * This function will store the credential in the memory keystore if it's
264 * valid, or will store in the permanent one if it comes from the dialog and if
265 * the user asked for it.
266 *
267 * @return true if the credential was stored or comes from the keystore, false
268 * otherwise
269 */
270VLC_API bool
271vlc_credential_store(vlc_credential *p_credential, vlc_object_t *p_parent);
272#define vlc_credential_store(a, b) \
273 vlc_credential_store(a, VLC_OBJECT(b))
274
275/**
276 * Look up a bearer token for the URL, realm and scopes set on p_credential.
277 *
278 * @return 0 if p_credential->psz_token is valid, or a negative errno code.
279 */
280VLC_API int
282#define vlc_credential_get_bearer(a, b) \
283 vlc_credential_get_bearer(a, VLC_OBJECT(b))
284
285/**
286 * Store a bearer token for an HTTPS origin URL in the per-libvlc memory
287 * keystore.
288 *
289 * The token replaces, scope by scope, any token previously stored for the
290 * same origin and realm. A NULL realm matches any realm and an empty scope
291 * list matches any scope, so a token stored with neither replaces all tokens
292 * of the origin.
293 *
294 * If psz_token is NULL, the matching tokens are removed.
295 */
296VLC_API int
297libvlc_InternalHttpBearerStore(libvlc_int_t *p_libvlc, const char *psz_origin,
298 const char *psz_realm,
299 const char *const *ppsz_scopes, size_t i_scopes,
300 const char *psz_token);
301
302/**
303 * @}
304 * @defgroup keystore_implementation Implemented by keystore modules
305 * @{
306 */
307
308#define VLC_KEYSTORE_NAME "libVLC"
310static inline int
312 const uint8_t *p_secret, size_t i_secret_len)
313{
314 p_entry->p_secret = (uint8_t*) malloc(i_secret_len);
315 if (!p_entry->p_secret)
316 return VLC_EGENERIC;
317 memcpy(p_entry->p_secret, p_secret, i_secret_len);
318 p_entry->i_secret_len = i_secret_len;
319 return VLC_SUCCESS;
320}
321
322static inline void
325 for (unsigned int j = 0; j < KEY_MAX; ++j)
326 {
327 free(p_entry->ppsz_values[j]);
328 p_entry->ppsz_values[j] = NULL;
329 }
330 free(p_entry->p_secret);
331 p_entry->p_secret = NULL;
332}
333
337 struct vlc_object_t obj;
341 /** See vlc_keystore_store() */
342 int (*pf_store)(vlc_keystore *p_keystore,
343 const char *const ppsz_values[KEY_MAX],
344 const uint8_t *p_secret,
345 size_t i_secret_len, const char *psz_label);
346 /** See vlc_keystore_find() */
347 unsigned int (*pf_find)(vlc_keystore *p_keystore,
348 const char *const ppsz_values[KEY_MAX],
349 vlc_keystore_entry **pp_entries);
350
351 /** See vlc_keystore_remove() */
352 unsigned int (*pf_remove)(vlc_keystore *p_keystore,
353 const char *const ppsz_values[KEY_MAX]);
354};
355
356/** @} */
357/** @} */
358
359#endif
#define VLC_USED
Definition fourcc_gen.c:32
#define VLC_API
Definition fourcc_gen.c:31
#define VLC_FORMAT(x, y)
String format function annotation.
Definition vlc_common.h:193
#define vlc_credential_get(a, b, c, d, e, f,...)
Definition vlc_keystore.h:258
void vlc_credential_clean(vlc_credential *p_credential)
Clean a credential struct.
Definition keystore.c:380
int libvlc_InternalHttpBearerStore(libvlc_int_t *p_libvlc, const char *psz_origin, const char *psz_realm, const char *const *ppsz_scopes, size_t i_scopes, const char *psz_token)
Store a bearer token for an HTTPS origin URL in the per-libvlc memory keystore.
Definition keystore.c:679
void vlc_credential_init(vlc_credential *p_credential, const vlc_url_t *p_url)
Init a credential struct.
Definition keystore.c:370
#define vlc_credential_store(a, b)
Definition vlc_keystore.h:273
#define vlc_credential_get_bearer(a, b)
Definition vlc_keystore.h:283
#define VLC_EGENERIC
Unspecified error.
Definition vlc_common.h:482
#define VLC_SUCCESS
No error.
Definition vlc_common.h:480
static void vlc_keystore_release_entry(vlc_keystore_entry *p_entry)
Definition vlc_keystore.h:324
static int vlc_keystore_entry_set_secret(vlc_keystore_entry *p_entry, const uint8_t *p_secret, size_t i_secret_len)
Definition vlc_keystore.h:312
struct vlc_keystore_sys vlc_keystore_sys
Definition vlc_keystore.h:335
#define vlc_keystore_create(x)
Definition vlc_keystore.h:93
void vlc_keystore_release(vlc_keystore *p_keystore)
Release a keystore object.
Definition keystore.c:72
unsigned int vlc_keystore_find(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX], vlc_keystore_entry **pp_entries)
Find all entries that match a set of key/values.
Definition keystore.c:111
unsigned int vlc_keystore_remove(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX])
Remove all entries that match a set of key/values.
Definition keystore.c:120
vlc_keystore_key
List of keys that can be stored via the keystore API.
Definition vlc_keystore.h:52
void vlc_keystore_release_entries(vlc_keystore_entry *p_entries, unsigned int i_count)
Release the list of entries returned by vlc_keystore_find().
Definition keystore.c:128
int vlc_keystore_store(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX], const uint8_t *p_secret, ssize_t i_secret_len, const char *psz_label)
Store a secret associated with a set of key/values.
Definition keystore.c:81
@ KEY_PROTOCOL
Definition vlc_keystore.h:53
@ KEY_PATH
Definition vlc_keystore.h:56
@ KEY_USER
Definition vlc_keystore.h:54
@ KEY_SCOPE
Definition vlc_keystore.h:60
@ KEY_SERVER
Definition vlc_keystore.h:55
@ KEY_REALM
Definition vlc_keystore.h:58
@ KEY_MAX
Definition vlc_keystore.h:61
@ KEY_AUTHTYPE
Definition vlc_keystore.h:59
@ KEY_PORT
Definition vlc_keystore.h:57
Definition vlc_objects.h:103
Internal module descriptor.
Definition modules.h:76
Definition vlc_keystore.h:169
const char * psz_authtype
http authtype to search, can be overridden after a call to vlc_credential_get()
Definition vlc_keystore.h:177
unsigned int i_entries_count
Definition vlc_keystore.h:199
size_t i_scopes
Definition vlc_keystore.h:186
char * psz_var_password
Definition vlc_keystore.h:205
char * psz_var_username
Definition vlc_keystore.h:204
unsigned int i_bearer_entries_count
Definition vlc_keystore.h:201
bool b_store
Definition vlc_keystore.h:210
vlc_keystore_entry * p_entries
Definition vlc_keystore.h:198
char * psz_dialog_username
Definition vlc_keystore.h:207
char * psz_split_domain
Definition vlc_keystore.h:203
const char *const * ppsz_scopes
scopes the bearer token must be stored with
Definition vlc_keystore.h:185
vlc_keystore_entry * p_bearer_entries
Definition vlc_keystore.h:200
const char * psz_realm
http realm or smb domain to search, can be overridden after a call to vlc_credential_get()
Definition vlc_keystore.h:174
bool b_from_keystore
Definition vlc_keystore.h:209
char * psz_dialog_password
Definition vlc_keystore.h:208
@ GET_FROM_KEYSTORE
Definition vlc_keystore.h:193
@ GET_FROM_MEMORY_KEYSTORE
Definition vlc_keystore.h:192
@ GET_FROM_URL
Definition vlc_keystore.h:190
@ GET_FROM_DIALOG
Definition vlc_keystore.h:194
@ GET_FROM_OPTION
Definition vlc_keystore.h:191
const char * psz_username
valid only if vlc_credential_get() returned true
Definition vlc_keystore.h:179
const char * psz_token
valid only if vlc_credential_get_bearer() returned 0
Definition vlc_keystore.h:183
enum vlc_credential::@261107221260255042017273316203157133362304255246 i_get_order
vlc_keystore * p_keystore
Definition vlc_keystore.h:197
const vlc_url_t * p_url
url to store or to search
Definition vlc_keystore.h:171
const char * psz_password
valid only if vlc_credential_get() returned true
Definition vlc_keystore.h:181
Keystore entry returned by vlc_keystore_find().
Definition vlc_keystore.h:69
uint8_t * p_secret
Secret password.
Definition vlc_keystore.h:73
char * ppsz_values[KEY_MAX]
Set of key/values.
Definition vlc_keystore.h:71
size_t i_secret_len
Length of the secret.
Definition vlc_keystore.h:75
Definition vlc_keystore.h:337
unsigned int(* pf_remove)(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX])
See vlc_keystore_remove().
Definition vlc_keystore.h:353
int(* pf_store)(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX], const uint8_t *p_secret, size_t i_secret_len, const char *psz_label)
See vlc_keystore_store().
Definition vlc_keystore.h:343
struct vlc_object_t obj
Definition vlc_keystore.h:338
unsigned int(* pf_find)(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX], vlc_keystore_entry **pp_entries)
See vlc_keystore_find().
Definition vlc_keystore.h:348
module_t * p_module
Definition vlc_keystore.h:339
vlc_keystore_sys * p_sys
Definition vlc_keystore.h:340
VLC object common members.
Definition vlc_objects.h:53
Definition vlc_url.h:146
This file is a collection of common definitions and types.
void libvlc_InternalKeystoreClean(libvlc_int_t *p_libvlc)
Definition keystore.c:146
int libvlc_InternalKeystoreInit(libvlc_int_t *p_libvlc)
Definition keystore.c:136
char psz_label[13]
Definition vout_intf.c:100