root/openpgpsdk/trunk/examples/create-signed-key.c

Revision 146 (checked in by rachel, 8 years ago)

Must include string.h for memset, memcpy definitions

Line 
1 #include "create.h"
2 #include "util.h"
3 #include "signature.h"
4 #include "packet-parse.h"
5 #include <stdio.h>
6 #include <fcntl.h>
7 #include <string.h>
8 #include <assert.h>
9
10 /*
11  * Slightly strange beast that might get replaced later - it needs
12  * some other OpenPGP package to generate a key for it to use - this
13  * is because we don't have a way to generate our own (yet).
14  */
15
16 static ops_secret_key_t skey;
17 static ops_boolean_t skey_found;
18
19 static ops_parse_callback_return_t
20 callback(const ops_parser_content_t *content,void *arg_)
21     {
22     if(content->tag == OPS_PTAG_CT_SECRET_KEY)
23         {
24         memcpy(&skey,&content->content.secret_key,sizeof skey);
25         skey_found=ops_true;
26         return OPS_KEEP_MEMORY;
27         }
28
29     return OPS_RELEASE_MEMORY;
30     }
31    
32 static void get_key(const char *keyfile)
33     {
34     ops_reader_fd_arg_t arg;
35     ops_parse_options_t opt;
36
37     ops_parse_options_init(&opt);
38     opt.cb=callback;
39
40     arg.fd=open(keyfile,O_RDONLY);
41     assert(arg.fd >= 0);
42     opt.reader_arg=&arg;
43     opt.reader=ops_reader_fd;
44
45     ops_parse(&opt);
46
47     assert(skey_found);
48     }
49
50 int main(int argc,char **argv)
51     {
52     ops_writer_fd_arg_t arg;
53     ops_create_options_t opt;
54     ops_create_signature_t sig;
55     ops_user_id_t id;
56     unsigned char keyid[OPS_KEY_ID_SIZE];
57     char *user_id; /* not const coz we use _fast_ */
58     const char *keyfile;
59
60     if(argc != 3)
61         {
62         fprintf(stderr,"%s <secret key file> <user_id>\n",argv[0]);
63         exit(1);
64         }
65
66     keyfile=argv[1];
67     user_id=argv[2];
68
69     ops_init();
70
71     get_key(keyfile);
72
73     arg.fd=1;
74     opt.writer=ops_writer_fd;
75     opt.arg=&arg;
76
77     ops_write_struct_public_key(&skey.public_key,&opt);
78
79     ops_fast_create_user_id(&id,user_id);
80     ops_write_struct_user_id(&id,&opt);
81
82     ops_signature_start(&sig,&skey.public_key,&id,OPS_CERT_POSITIVE);
83     ops_signature_add_creation_time(&sig,time(NULL));
84
85     ops_keyid(keyid,&skey.public_key);
86     ops_signature_add_issuer_key_id(&sig,keyid);
87
88     ops_signature_add_primary_user_id(&sig,ops_true);
89
90     ops_signature_hashed_subpackets_end(&sig);
91
92     ops_write_signature(&sig,&skey.public_key,&skey,&opt);
93
94     ops_secret_key_free(&skey);
95
96     ops_finish();
97
98     return 0;
99     }
Note: See TracBrowser for help on using the browser.