Rename compat.h -> git-compat-util.h
[git-daemon2.git] / client / keypairs.c
blob74fe1b29ff3705da3605df05e15e55c9b5c369cf
1 /*
2 * Copyright (C) Ilari Liusvaara 2009-2010
4 * This code is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8 #include "keypairs.h"
9 #include "home.h"
10 #include "certificate.h"
11 #include <stdio.h>
12 #include <limits.h>
13 #include <gnutls/openpgp.h>
14 #include <errno.h>
15 #include "git-compat-util.h"
17 int select_keypair_int(gnutls_certificate_credentials_t creds,
18 const char *username)
20 char keypath[PATH_MAX + 1];
21 char *keypath2;
22 struct certificate cert;
23 int r;
25 r = snprintf(keypath, PATH_MAX + 1, "$XDG_CONFIG_HOME/gits/keys/%s",
26 username);
27 if (r < 0 || r > PATH_MAX) {
28 die("Username too long");
31 keypath2 = expand_path(keypath);
32 cert = parse_certificate(keypath2, &r);
33 if (r) {
34 if (r == CERTERR_NOCERT)
35 return -1;
36 if (r == CERTERR_CANTREAD)
37 die_errno("Can't read keypair");
38 else
39 die("Can't read keypair: %s",
40 cert_parse_strerr(r));
43 r = gnutls_certificate_set_openpgp_keyring_mem(creds,
44 cert.public_key.data, cert.public_key.size,
45 GNUTLS_OPENPGP_FMT_RAW);
46 if (r < 0)
47 die("Can't load public key: %s", gnutls_strerror(r));
49 r = gnutls_certificate_set_openpgp_key_mem(creds, &cert.public_key,
50 &cert.private_key, GNUTLS_OPENPGP_FMT_RAW);
51 if (r < 0)
52 die("Can't load keypair: %s", gnutls_strerror(r));
54 free(keypath2);
55 return 0;