]> the.earth.li Git - onak.git/blob - stripkey.c
8aed81005bee3b8e37f16dc72830380752b8177d
[onak.git] / stripkey.c
1 /*
2  * stripkey.c - Strip a key of all signatures except self-sigs.
3  *
4  * Copyright 2004 Daniel Silverstone <dsilvers@digital-scurf.org>
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include "charfuncs.h"
25 #include "cleankey.h"
26 #include "keyid.h"
27 #include "keystructs.h"
28 #include "mem.h"
29 #include "parsekey.h"
30 #include "decodekey.h"
31
32 int main(int argc, char** argv) {
33   struct openpgp_packet_list *packets = NULL;
34   struct openpgp_packet_list *list_end = NULL;
35   struct openpgp_publickey   *keys = NULL;
36   struct openpgp_publickey   *key = NULL;
37   struct openpgp_signedpacket_list *uid = NULL;
38   struct openpgp_packet_list *sig = NULL;
39   struct openpgp_packet_list *prevsig = NULL;
40   uint64_t my_key = 0;
41
42   if( argc > 1 )
43      my_key = strtoull( argv[1], NULL, 16 );
44    
45   /* expect a stream of openpgp packets on stdin comprising some keys */
46   /* strip each key of everything but its pubkey component, uids and
47    * selfsigs and revsigs on those selfsigs */
48
49   read_openpgp_stream( stdin_getchar, NULL, &packets, 0 );
50   parse_keys( packets, &keys );
51   free_packet_list(packets);
52   packets = NULL;
53   cleankeys( keys );
54   /* Iterate over the keys... */
55   for( key = keys; key; key = key->next ) {
56     uint64_t keyid;
57     get_keyid( key, &keyid );
58     for( uid = key->uids; uid; uid = uid->next ) {
59       REPEATTHISUID: 
60       for( sig = uid->sigs, prevsig = NULL; 
61            sig; 
62            prevsig = sig, sig = sig->next ) {
63         uint64_t thissig = sig_keyid( sig->packet );
64         if( thissig != keyid && thissig != my_key ) {
65           /* Don't care about this packet... */
66           if( prevsig ) {
67             prevsig->next = sig->next;
68           } else {
69             uid->sigs = sig->next;
70           }
71           sig->next = NULL;
72           free_packet_list( sig );
73           goto REPEATTHISUID;
74         }
75       }
76     }
77   }
78   flatten_publickey( keys, &packets, &list_end );
79   write_openpgp_stream( stdout_putchar, NULL, packets );
80   return 0;
81 }