blob: 5b0edfe38df372b3f248540abbc0315082292f04 (
plain)
1 // GPGEdit edits GPG encrypted files
2 // Copyright (C) 2018 Aaron Ball <nullspoon@oper.io>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 #include "gpg_keyids.h"
17
18
19 // gpg_keyids_load_from_recip:
20 // Converts a gpgme_recipient_t linked list to a string array of key ids. This
21 // is useful when storing a list of recipients for an already-encrypted file to
22 // be used when later re-encrypting that file.
23 //
24 // @keyids Pointer to existing keyids string array. Note that the pointer
25 // to this will be updated by the array growth process.
26 // @firstrecip First recipient in the linked list. The list will be traversed
27 // starting here.
28 void gpg_keyids_load_from_recip(char*** keyids, gpgme_recipient_t firstrecip) {
29 gpgme_recipient_t recip; // Current recipient object
30 // Write each recipient key id to the char array
31 recip = firstrecip;
32 while(recip != NULL) {
33 strarray_add(keyids, recip->keyid);
34 recip = recip->next;
35 }
36 }
37
38
39 // gpg_keyids_to_key_t:
40 // Converts a string array containing keyids (multiple identifier types are
41 // supported) into an array of gpgme_key_t objects.
42 //
43 // @ctx GPGME context (already initialized)
44 // @keyids Pointer to string array of key id strings
45 //
46 // @return gpgme_key_t* Array of gpgme_key_t objects, one for each keyid string
47 gpgme_key_t* gpg_keyids_to_key_t(gpgme_ctx_t ctx, char** keyids) {
48 gpgme_key_t* out;
49 gpgme_error_t err;
50 int count = 0; // Number of recipients
51 int i = 0;
52
53 // Count the keys
54 while(keyids[count] != NULL)
55 count++;
56
57 // Allocate the gpgme_key_t pointer array
58 out = (gpgme_key_t*) malloc(sizeof(gpgme_key_t) * (count + 1));
59
60 // Loop over the keys back to zero
61 while(i < count) {
62 err = gpgme_get_key(ctx, keyids[i], &out[i], 0);
63 gpg_failiferr(err);
64 i++;
65 }
66 out[i] = NULL;
67 return out;
68 }
|