document fix
[gnutls.git] / src / certtool-extras.c
blob8ea890e9be8ce3d7d576579a6f882bb69776eb41
1 /*
2 * Copyright (C) 2012 Lucas Fisher *lucas.fisher [at] gmail.com*
3 * Copyright (C) 2012 Free Software Foundation, Inc.
5 * This file is part of GnuTLS.
7 * GnuTLS is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuTLS is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see
19 * <http://www.gnu.org/licenses/>.
22 #include <config.h>
24 #include <gnutls/gnutls.h>
25 #include <gnutls/x509.h>
26 #include <gnutls/openpgp.h>
27 #include <gnutls/pkcs12.h>
28 #include <gnutls/pkcs11.h>
29 #include <gnutls/abstract.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <time.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 #include <error.h>
42 #include "certtool-common.h"
43 #include "certtool-cfg.h"
46 #define MAX_KEYS 256
48 /* Loads a x509 private key list
50 gnutls_x509_privkey_t *
51 load_privkey_list (int mand, size_t * privkey_size, common_info_st * info)
53 static gnutls_x509_privkey_t key[MAX_KEYS];
54 char *ptr;
55 int ret, i;
56 gnutls_datum_t dat, file_data;
57 int ptr_size;
59 *privkey_size = 0;
60 fprintf (stderr, "Loading private key list...\n");
62 if (info->privkey == NULL)
64 if (mand)
65 error (EXIT_FAILURE, 0, "missing --load-privkey");
66 else
67 return NULL;
70 ret = gnutls_load_file(info->privkey, &file_data);
71 if (ret < 0)
72 error (EXIT_FAILURE, errno, "%s", info->privkey);
74 ptr = (void*)file_data.data;
75 ptr_size = file_data.size;
77 for (i = 0; i < MAX_KEYS; i++)
79 ret = gnutls_x509_privkey_init (&key[i]);
80 if (ret < 0)
81 error (EXIT_FAILURE, 0, "privkey_init: %s", gnutls_strerror (ret));
83 dat.data = (void*)ptr;
84 dat.size = ptr_size;
86 ret = gnutls_x509_privkey_import (key[i], &dat, info->incert_format);
87 if (ret < 0 && *privkey_size > 0)
88 break;
89 if (ret < 0)
90 error (EXIT_FAILURE, 0, "privkey_import: %s", gnutls_strerror (ret));
92 ptr = strstr (ptr, "---END");
93 if (ptr == NULL)
94 break;
95 ptr++;
97 ptr_size = file_data.size;
98 ptr_size -=
99 (unsigned int) ((unsigned char *) ptr - (unsigned char *) buffer);
101 if (ptr_size < 0)
102 break;
104 (*privkey_size)++;
107 gnutls_free(file_data.data);
108 fprintf (stderr, "Loaded %d private keys.\n", (int) *privkey_size);
110 return key;