deps/libgit2/src/libgit2/transports/credential.c

filter: Statementstmt 0.0% 0 / 210 Branchbran 0.0% 0 / 92 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 302 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« deps/libgit2/src/libgit2/transports/auth.c ↑ Summary deps/libgit2/src/libgit2/transports/credential_helpers.c »
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7  
8 #include "common.h"
9  
10 #include "git2/credential.h"
11 #include "git2/sys/credential.h"
12 #include "git2/credential_helpers.h"
13  
14 static int git_credential_ssh_key_type_new(
15 git_credential **cred,
16 const char *username,
17 const char *publickey,
18 const char *privatekey,
19 const char *passphrase,
20 git_credential_t credtype);
21  
22 0 int git_credential_has_username(git_credential *cred)
23 {
24 0 ▶ if (cred->credtype == GIT_CREDENTIAL_DEFAULT)
Branch: 1 branch
BranchTrueFalseTotal
if (cred->credtype == GIT_CREDENTIAL_DEFAULT) 0 0 0
25 0 return 0;
26  
27 0 return 1;
28 }
29  
30 0 const char *git_credential_get_username(git_credential *cred)
31 {
32 0 switch (cred->credtype) {
33 0 case GIT_CREDENTIAL_USERNAME:
34 {
35 0 git_credential_username *c = (git_credential_username *) cred;
36 0 return c->username;
37 }
38 0 case GIT_CREDENTIAL_USERPASS_PLAINTEXT:
39 {
40 0 git_credential_userpass_plaintext *c = (git_credential_userpass_plaintext *) cred;
41 0 return c->username;
42 }
43 0 case GIT_CREDENTIAL_SSH_KEY:
44 case GIT_CREDENTIAL_SSH_MEMORY:
45 {
46 0 git_credential_ssh_key *c = (git_credential_ssh_key *) cred;
47 0 return c->username;
48 }
49 0 case GIT_CREDENTIAL_SSH_CUSTOM:
50 {
51 0 git_credential_ssh_custom *c = (git_credential_ssh_custom *) cred;
52 0 return c->username;
53 }
54 0 case GIT_CREDENTIAL_SSH_INTERACTIVE:
55 {
56 0 git_credential_ssh_interactive *c = (git_credential_ssh_interactive *) cred;
57 0 return c->username;
58 }
59  
60 0 default:
61 0 return NULL;
62 }
63 }
64  
65 0 static void plaintext_free(struct git_credential *cred)
66 {
67 0 git_credential_userpass_plaintext *c = (git_credential_userpass_plaintext *)cred;
68  
69 0 git__free(c->username);
70  
71 /* Zero the memory which previously held the password */
72 0 ▶ if (c->password) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->password) { 0 0 0
73 0 size_t pass_len = strlen(c->password);
74 0 git__memzero(c->password, pass_len);
75 0 git__free(c->password);
76 }
77  
78 0 git__free(c);
79 0 }
80  
81 0 int git_credential_userpass_plaintext_new(
82 git_credential **cred,
83 const char *username,
84 const char *password)
85 {
86 git_credential_userpass_plaintext *c;
87  
88 0 ▶ GIT_ASSERT_ARG(cred);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(cred); 0 0 0
89 0 ▶ GIT_ASSERT_ARG(username);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(username); 0 0 0
90 0 ▶ GIT_ASSERT_ARG(password);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(password); 0 0 0
91  
92 0 c = git__malloc(sizeof(git_credential_userpass_plaintext));
93 0 ▶ GIT_ERROR_CHECK_ALLOC(c);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(c); 0 0 0
94  
95 0 c->parent.credtype = GIT_CREDENTIAL_USERPASS_PLAINTEXT;
96 0 c->parent.free = plaintext_free;
97 0 c->username = git__strdup(username);
98  
99 0 ▶ if (!c->username) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->username) { 0 0 0
100 0 git__free(c);
101 0 return -1;
102 }
103  
104 0 c->password = git__strdup(password);
105  
106 0 ▶ if (!c->password) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->password) { 0 0 0
107 0 git__free(c->username);
108 0 git__free(c);
109 0 return -1;
110 }
111  
112 0 *cred = &c->parent;
113 0 return 0;
114 }
115  
116 0 static void ssh_key_free(struct git_credential *cred)
117 {
118 0 git_credential_ssh_key *c =
119 (git_credential_ssh_key *)cred;
120  
121 0 git__free(c->username);
122  
123 0 ▶ if (c->privatekey) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->privatekey) { 0 0 0
124 /* Zero the memory which previously held the private key */
125 0 size_t key_len = strlen(c->privatekey);
126 0 git__memzero(c->privatekey, key_len);
127 0 git__free(c->privatekey);
128 }
129  
130 0 ▶ if (c->passphrase) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->passphrase) { 0 0 0
131 /* Zero the memory which previously held the passphrase */
132 0 size_t pass_len = strlen(c->passphrase);
133 0 git__memzero(c->passphrase, pass_len);
134 0 git__free(c->passphrase);
135 }
136  
137 0 ▶ if (c->publickey) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->publickey) { 0 0 0
138 /* Zero the memory which previously held the public key */
139 0 size_t key_len = strlen(c->publickey);
140 0 git__memzero(c->publickey, key_len);
141 0 git__free(c->publickey);
142 }
143  
144 0 git__free(c);
145 0 }
146  
147 0 static void ssh_interactive_free(struct git_credential *cred)
148 {
149 0 git_credential_ssh_interactive *c = (git_credential_ssh_interactive *)cred;
150  
151 0 git__free(c->username);
152  
153 0 git__free(c);
154 0 }
155  
156 0 static void ssh_custom_free(struct git_credential *cred)
157 {
158 0 git_credential_ssh_custom *c = (git_credential_ssh_custom *)cred;
159  
160 0 git__free(c->username);
161  
162 0 ▶ if (c->publickey) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->publickey) { 0 0 0
163 /* Zero the memory which previously held the publickey */
164 0 size_t key_len = strlen(c->publickey);
165 0 git__memzero(c->publickey, key_len);
166 0 git__free(c->publickey);
167 }
168  
169 0 git__free(c);
170 0 }
171  
172 0 static void default_free(struct git_credential *cred)
173 {
174 0 git_credential_default *c = (git_credential_default *)cred;
175  
176 0 git__free(c);
177 0 }
178  
179 0 static void username_free(struct git_credential *cred)
180 {
181 0 git__free(cred);
182 0 }
183  
184 0 int git_credential_ssh_key_new(
185 git_credential **cred,
186 const char *username,
187 const char *publickey,
188 const char *privatekey,
189 const char *passphrase)
190 {
191 0 return git_credential_ssh_key_type_new(
192 cred,
193 username,
194 publickey,
195 privatekey,
196 passphrase,
197 GIT_CREDENTIAL_SSH_KEY);
198 }
199  
200 0 int git_credential_ssh_key_memory_new(
201 git_credential **cred,
202 const char *username,
203 const char *publickey,
204 const char *privatekey,
205 const char *passphrase)
206 {
207 #ifdef GIT_SSH_MEMORY_CREDENTIALS
208 return git_credential_ssh_key_type_new(
209 cred,
210 username,
211 publickey,
212 privatekey,
213 passphrase,
214 GIT_CREDENTIAL_SSH_MEMORY);
215 #else
216 0 GIT_UNUSED(cred);
217 0 GIT_UNUSED(username);
218 0 GIT_UNUSED(publickey);
219 0 GIT_UNUSED(privatekey);
220 0 GIT_UNUSED(passphrase);
221  
222 0 git_error_set(GIT_ERROR_INVALID,
223 "this version of libgit2 was not built with ssh memory credentials.");
224 0 return -1;
225 #endif
226 }
227  
228 0 static int git_credential_ssh_key_type_new(
229 git_credential **cred,
230 const char *username,
231 const char *publickey,
232 const char *privatekey,
233 const char *passphrase,
234 git_credential_t credtype)
235 {
236 git_credential_ssh_key *c;
237  
238 0 ▶ GIT_ASSERT_ARG(username);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(username); 0 0 0
239 0 ▶ GIT_ASSERT_ARG(cred);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(cred); 0 0 0
240 0 ▶ GIT_ASSERT_ARG(privatekey);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(privatekey); 0 0 0
241  
242 0 c = git__calloc(1, sizeof(git_credential_ssh_key));
243 0 ▶ GIT_ERROR_CHECK_ALLOC(c);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(c); 0 0 0
244  
245 0 c->parent.credtype = credtype;
246 0 c->parent.free = ssh_key_free;
247  
248 0 c->username = git__strdup(username);
249 0 ▶ GIT_ERROR_CHECK_ALLOC(c->username);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(c->username); 0 0 0
250  
251 0 c->privatekey = git__strdup(privatekey);
252 0 ▶ GIT_ERROR_CHECK_ALLOC(c->privatekey);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(c->privatekey); 0 0 0
253  
254 0 ▶ if (publickey) {
Branch: 1 branch
BranchTrueFalseTotal
if (publickey) { 0 0 0
255 0 c->publickey = git__strdup(publickey);
256 0 ▶ GIT_ERROR_CHECK_ALLOC(c->publickey);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(c->publickey); 0 0 0
257 }
258  
259 0 ▶ if (passphrase) {
Branch: 1 branch
BranchTrueFalseTotal
if (passphrase) { 0 0 0
260 0 c->passphrase = git__strdup(passphrase);
261 0 ▶ GIT_ERROR_CHECK_ALLOC(c->passphrase);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(c->passphrase); 0 0 0
262 }
263  
264 0 *cred = &c->parent;
265 0 return 0;
266 }
267  
268 0 int git_credential_ssh_interactive_new(
269 git_credential **out,
270 const char *username,
271 git_credential_ssh_interactive_cb prompt_callback,
272 void *payload)
273 {
274 git_credential_ssh_interactive *c;
275  
276 0 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 0 0
277 0 ▶ GIT_ASSERT_ARG(username);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(username); 0 0 0
278 0 ▶ GIT_ASSERT_ARG(prompt_callback);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(prompt_callback); 0 0 0
279  
280 0 c = git__calloc(1, sizeof(git_credential_ssh_interactive));
281 0 ▶ GIT_ERROR_CHECK_ALLOC(c);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(c); 0 0 0
282  
283 0 c->parent.credtype = GIT_CREDENTIAL_SSH_INTERACTIVE;
284 0 c->parent.free = ssh_interactive_free;
285  
286 0 c->username = git__strdup(username);
287 0 ▶ GIT_ERROR_CHECK_ALLOC(c->username);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(c->username); 0 0 0
288  
289 0 c->prompt_callback = prompt_callback;
290 0 c->payload = payload;
291  
292 0 *out = &c->parent;
293 0 return 0;
294 }
295  
296 0 int git_credential_ssh_key_from_agent(git_credential **cred, const char *username) {
297 git_credential_ssh_key *c;
298  
299 0 ▶ GIT_ASSERT_ARG(username);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(username); 0 0 0
300 0 ▶ GIT_ASSERT_ARG(cred);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(cred); 0 0 0
301  
302 0 c = git__calloc(1, sizeof(git_credential_ssh_key));
303 0 ▶ GIT_ERROR_CHECK_ALLOC(c);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(c); 0 0 0
304  
305 0 c->parent.credtype = GIT_CREDENTIAL_SSH_KEY;
306 0 c->parent.free = ssh_key_free;
307  
308 0 c->username = git__strdup(username);
309 0 ▶ GIT_ERROR_CHECK_ALLOC(c->username);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(c->username); 0 0 0
310  
311 0 c->privatekey = NULL;
312  
313 0 *cred = &c->parent;
314 0 return 0;
315 }
316  
317 0 int git_credential_ssh_custom_new(
318 git_credential **cred,
319 const char *username,
320 const char *publickey,
321 size_t publickey_len,
322 git_credential_sign_cb sign_callback,
323 void *payload)
324 {
325 git_credential_ssh_custom *c;
326  
327 0 ▶ GIT_ASSERT_ARG(username);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(username); 0 0 0
328 0 ▶ GIT_ASSERT_ARG(cred);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(cred); 0 0 0
329  
330 0 c = git__calloc(1, sizeof(git_credential_ssh_custom));
331 0 ▶ GIT_ERROR_CHECK_ALLOC(c);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(c); 0 0 0
332  
333 0 c->parent.credtype = GIT_CREDENTIAL_SSH_CUSTOM;
334 0 c->parent.free = ssh_custom_free;
335  
336 0 c->username = git__strdup(username);
337 0 ▶ GIT_ERROR_CHECK_ALLOC(c->username);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(c->username); 0 0 0
338  
339 0 ▶ if (publickey_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (publickey_len > 0) { 0 0 0
340 0 c->publickey = git__malloc(publickey_len);
341 0 ▶ GIT_ERROR_CHECK_ALLOC(c->publickey);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(c->publickey); 0 0 0
342  
343 0 memcpy(c->publickey, publickey, publickey_len);
344 }
345  
346 0 c->publickey_len = publickey_len;
347 0 c->sign_callback = sign_callback;
348 0 c->payload = payload;
349  
350 0 *cred = &c->parent;
351 0 return 0;
352 }
353  
354 0 int git_credential_default_new(git_credential **cred)
355 {
356 git_credential_default *c;
357  
358 0 ▶ GIT_ASSERT_ARG(cred);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(cred); 0 0 0
359  
360 0 c = git__calloc(1, sizeof(git_credential_default));
361 0 ▶ GIT_ERROR_CHECK_ALLOC(c);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(c); 0 0 0
362  
363 0 c->credtype = GIT_CREDENTIAL_DEFAULT;
364 0 c->free = default_free;
365  
366 0 *cred = c;
367 0 return 0;
368 }
369  
370 0 int git_credential_username_new(git_credential **cred, const char *username)
371 {
372 git_credential_username *c;
373 size_t len, allocsize;
374  
375 0 ▶ GIT_ASSERT_ARG(cred);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(cred); 0 0 0
376  
377 0 len = strlen(username);
378  
379 0 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&allocsize, sizeof(git_credential_username), len);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&allocsize, sizeof(git_credential_username), len); 0 0 0
GIT_ERROR_CHECK_ALLOC_ADD(&allocsize, sizeof(git_credential_username), len); 0 0 0
380 0 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&allocsize, allocsize, 1);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&allocsize, allocsize, 1); 0 0 0
GIT_ERROR_CHECK_ALLOC_ADD(&allocsize, allocsize, 1); 0 0 0
381 0 c = git__malloc(allocsize);
382 0 ▶ GIT_ERROR_CHECK_ALLOC(c);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(c); 0 0 0
383  
384 0 c->parent.credtype = GIT_CREDENTIAL_USERNAME;
385 0 c->parent.free = username_free;
386 0 memcpy(c->username, username, len + 1);
387  
388 0 *cred = (git_credential *) c;
389 0 return 0;
390 }
391  
392 0 void git_credential_free(git_credential *cred)
393 {
394 0 ▶ if (!cred)
Branch: 1 branch
BranchTrueFalseTotal
if (!cred) 0 0 0
395 0 return;
396  
397 0 cred->free(cred);
398 }
399  
400 /* Deprecated credential functions */
401  
402 #ifndef GIT_DEPRECATE_HARD
403 0 int git_cred_has_username(git_credential *cred)
404 {
405 0 return git_credential_has_username(cred);
406 }
407  
408 0 const char *git_cred_get_username(git_credential *cred)
409 {
410 0 return git_credential_get_username(cred);
411 }
412  
413 0 int git_cred_userpass_plaintext_new(
414 git_credential **out,
415 const char *username,
416 const char *password)
417 {
418 0 return git_credential_userpass_plaintext_new(out,username, password);
419 }
420  
421 0 int git_cred_default_new(git_credential **out)
422 {
423 0 return git_credential_default_new(out);
424 }
425  
426 0 int git_cred_username_new(git_credential **out, const char *username)
427 {
428 0 return git_credential_username_new(out, username);
429 }
430  
431 0 int git_cred_ssh_key_new(
432 git_credential **out,
433 const char *username,
434 const char *publickey,
435 const char *privatekey,
436 const char *passphrase)
437 {
438 0 return git_credential_ssh_key_new(out, username,
439 publickey, privatekey, passphrase);
440 }
441  
442 0 int git_cred_ssh_key_memory_new(
443 git_credential **out,
444 const char *username,
445 const char *publickey,
446 const char *privatekey,
447 const char *passphrase)
448 {
449 0 return git_credential_ssh_key_memory_new(out, username,
450 publickey, privatekey, passphrase);
451 }
452  
453 0 int git_cred_ssh_interactive_new(
454 git_credential **out,
455 const char *username,
456 git_credential_ssh_interactive_cb prompt_callback,
457 void *payload)
458 {
459 0 return git_credential_ssh_interactive_new(out, username,
460 prompt_callback, payload);
461 }
462  
463 0 int git_cred_ssh_key_from_agent(
464 git_credential **out,
465 const char *username)
466 {
467 0 return git_credential_ssh_key_from_agent(out, username);
468 }
469  
470 0 int git_cred_ssh_custom_new(
471 git_credential **out,
472 const char *username,
473 const char *publickey,
474 size_t publickey_len,
475 git_credential_sign_cb sign_callback,
476 void *payload)
477 {
478 0 return git_credential_ssh_custom_new(out, username,
479 publickey, publickey_len, sign_callback, payload);
480 }
481  
482 0 void git_cred_free(git_credential *cred)
483 {
484 0 git_credential_free(cred);
485 0 }
486 #endif
« deps/libgit2/src/libgit2/transports/auth.c ↑ Summary deps/libgit2/src/libgit2/transports/credential_helpers.c »