| 1 |
#include <openpgpsdk/packet-parse.h> |
|---|
| 2 |
#include <openpgpsdk/util.h> |
|---|
| 3 |
#include <openpgpsdk/keyring.h> |
|---|
| 4 |
#include <openpgpsdk/accumulate.h> |
|---|
| 5 |
#include <openpgpsdk/armour.h> |
|---|
| 6 |
|
|---|
| 7 |
#include <unistd.h> |
|---|
| 8 |
#include <string.h> |
|---|
| 9 |
#include <fcntl.h> |
|---|
| 10 |
#include <assert.h> |
|---|
| 11 |
|
|---|
| 12 |
static char *pname; |
|---|
| 13 |
static ops_keyring_t keyring; |
|---|
| 14 |
|
|---|
| 15 |
static ops_parse_cb_return_t |
|---|
| 16 |
callback(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) |
|---|
| 17 |
{ |
|---|
| 18 |
const ops_parser_content_union_t *content=&content_->content; |
|---|
| 19 |
static ops_boolean_t skipping; |
|---|
| 20 |
static const ops_key_data_t *decrypter; |
|---|
| 21 |
|
|---|
| 22 |
OPS_USED(cbinfo); |
|---|
| 23 |
|
|---|
| 24 |
if(content_->tag != OPS_PTAG_CT_UNARMOURED_TEXT && skipping) |
|---|
| 25 |
{ |
|---|
| 26 |
puts("...end of skip"); |
|---|
| 27 |
skipping=ops_false; |
|---|
| 28 |
} |
|---|
| 29 |
|
|---|
| 30 |
switch(content_->tag) |
|---|
| 31 |
{ |
|---|
| 32 |
case OPS_PTAG_CT_UNARMOURED_TEXT: |
|---|
| 33 |
if(!skipping) |
|---|
| 34 |
{ |
|---|
| 35 |
puts("Skipping..."); |
|---|
| 36 |
skipping=ops_true; |
|---|
| 37 |
} |
|---|
| 38 |
fwrite(content->unarmoured_text.data,1, |
|---|
| 39 |
content->unarmoured_text.length,stdout); |
|---|
| 40 |
break; |
|---|
| 41 |
|
|---|
| 42 |
case OPS_PTAG_CT_ARMOUR_HEADER: |
|---|
| 43 |
case OPS_PARSER_PTAG: |
|---|
| 44 |
break; |
|---|
| 45 |
|
|---|
| 46 |
case OPS_PTAG_CT_PK_SESSION_KEY: |
|---|
| 47 |
if(decrypter) |
|---|
| 48 |
break; |
|---|
| 49 |
|
|---|
| 50 |
decrypter=ops_keyring_find_key_by_id(&keyring, |
|---|
| 51 |
content->pk_session_key.key_id); |
|---|
| 52 |
if(!decrypter) |
|---|
| 53 |
break; |
|---|
| 54 |
break; |
|---|
| 55 |
|
|---|
| 56 |
default: |
|---|
| 57 |
fprintf(stderr,"Unexpected packet tag=%d (0x%x)\n",content_->tag, |
|---|
| 58 |
content_->tag); |
|---|
| 59 |
assert(0); |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
return OPS_RELEASE_MEMORY; |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
static void usage() |
|---|
| 66 |
{ |
|---|
| 67 |
fprintf(stderr,"%s [-a] <keyring> <file to decrypt>\n",pname); |
|---|
| 68 |
exit(1); |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
int main(int argc,char **argv) |
|---|
| 72 |
{ |
|---|
| 73 |
ops_parse_info_t *pinfo; |
|---|
| 74 |
int fd; |
|---|
| 75 |
const char *keyfile; |
|---|
| 76 |
const char *encfile; |
|---|
| 77 |
ops_boolean_t armour=ops_false; |
|---|
| 78 |
int ch; |
|---|
| 79 |
|
|---|
| 80 |
pname=argv[0]; |
|---|
| 81 |
|
|---|
| 82 |
while((ch=getopt(argc,argv,"a")) != -1) |
|---|
| 83 |
switch(ch) |
|---|
| 84 |
{ |
|---|
| 85 |
case 'a': |
|---|
| 86 |
armour=ops_true; |
|---|
| 87 |
break; |
|---|
| 88 |
|
|---|
| 89 |
default: |
|---|
| 90 |
usage(); |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
keyfile=argv[optind++]; |
|---|
| 94 |
encfile=argv[optind++]; |
|---|
| 95 |
|
|---|
| 96 |
ops_init(); |
|---|
| 97 |
|
|---|
| 98 |
ops_keyring_read(&keyring,keyfile); |
|---|
| 99 |
|
|---|
| 100 |
pinfo=ops_parse_info_new(); |
|---|
| 101 |
|
|---|
| 102 |
fd=open(encfile,O_RDONLY); |
|---|
| 103 |
if(fd < 0) |
|---|
| 104 |
{ |
|---|
| 105 |
perror(encfile); |
|---|
| 106 |
exit(2); |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
ops_reader_set_fd(pinfo,fd); |
|---|
| 110 |
|
|---|
| 111 |
ops_parse_cb_set(pinfo,callback,NULL); |
|---|
| 112 |
|
|---|
| 113 |
if(armour) |
|---|
| 114 |
ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false); |
|---|
| 115 |
|
|---|
| 116 |
ops_parse(pinfo); |
|---|
| 117 |
|
|---|
| 118 |
if(armour) |
|---|
| 119 |
ops_reader_pop_dearmour(pinfo); |
|---|
| 120 |
|
|---|
| 121 |
ops_keyring_free(&keyring); |
|---|
| 122 |
ops_finish(); |
|---|
| 123 |
|
|---|
| 124 |
return 0; |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
|
|---|