]> the.earth.li Git - onak.git/blob - stripkey.c
Add ability to drop overly large packets
[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, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include "charfuncs.h"
24 #include "cleankey.h"
25 #include "keyid.h"
26 #include "keystructs.h"
27 #include "mem.h"
28 #include "parsekey.h"
29 #include "decodekey.h"
30
31 int main(int argc, char** argv) {
32   struct openpgp_packet_list *packets = NULL;
33   struct openpgp_packet_list *list_end = NULL;
34   struct openpgp_publickey   *keys = NULL;
35   struct openpgp_publickey   *key = NULL;
36   struct openpgp_signedpacket_list *uid = NULL;
37   struct openpgp_packet_list *sig = NULL;
38   struct openpgp_packet_list *prevsig = NULL;
39   uint64_t my_key = 0;
40
41   if( argc > 1 )
42      my_key = strtoull( argv[1], NULL, 16 );
43    
44   /* expect a stream of openpgp packets on stdin comprising some keys */
45   /* strip each key of everything but its pubkey component, uids and
46    * selfsigs and revsigs on those selfsigs */
47
48   read_openpgp_stream( stdin_getchar, NULL, &packets, 0 );
49   parse_keys( packets, &keys );
50   free_packet_list(packets);
51   packets = NULL;
52   cleankeys(&keys, ONAK_CLEAN_ALL);
53   /* Iterate over the keys... */
54   for( key = keys; key; key = key->next ) {
55     uint64_t keyid;
56     get_keyid( key, &keyid );
57     for( uid = key->uids; uid; uid = uid->next ) {
58       REPEATTHISUID: 
59       for( sig = uid->sigs, prevsig = NULL; 
60            sig; 
61            prevsig = sig, sig = sig->next ) {
62         uint64_t thissig = sig_keyid( sig->packet );
63         if( thissig != keyid && thissig != my_key ) {
64           /* Don't care about this packet... */
65           if( prevsig ) {
66             prevsig->next = sig->next;
67           } else {
68             uid->sigs = sig->next;
69           }
70           sig->next = NULL;
71           free_packet_list( sig );
72           goto REPEATTHISUID;
73         }
74       }
75     }
76   }
77   flatten_publickey( keys, &packets, &list_end );
78   write_openpgp_stream( stdout_putchar, NULL, packets );
79   return 0;
80 }