Changeset 136
- Timestamp:
- 05/20/05 13:44:25
- Files:
-
- openpgpsdk/trunk/examples/Makefile.template (modified) (2 diffs)
- openpgpsdk/trunk/examples/create-signed-key.c (added)
- openpgpsdk/trunk/include/create.h (modified) (3 diffs)
- openpgpsdk/trunk/include/crypto.h (modified) (2 diffs)
- openpgpsdk/trunk/include/memory.h (modified) (2 diffs)
- openpgpsdk/trunk/include/packet.h (modified) (1 diff)
- openpgpsdk/trunk/include/signature.h (modified) (2 diffs)
- openpgpsdk/trunk/ref/draft-ietf-openpgp-rfc2440bis-12-comments.txt (modified) (1 diff)
- openpgpsdk/trunk/src/build.c (modified) (1 diff)
- openpgpsdk/trunk/src/create.c (modified) (3 diffs)
- openpgpsdk/trunk/src/memory.c (modified) (3 diffs)
- openpgpsdk/trunk/src/openssl_crypto.c (modified) (1 diff)
- openpgpsdk/trunk/src/signature.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openpgpsdk/trunk/examples/Makefile.template
r133 r136 8 8 LIBS=$(LIBDEPS) -lcrypto -lz $(DM_LIB) 9 9 10 all: Makefile .depend packet-dump verify create-key 10 all: Makefile .depend packet-dump verify create-key create-signed-key 11 11 12 12 packet-dump: packet-dump.o $(LIBDEPS) … … 18 18 create-key: create-key.o $(LIBDEPS) 19 19 $(CC) $(LDFLAGS) -o create-key create-key.o $(LIBS) 20 21 create-signed-key: create-signed-key.o $(LIBDEPS) 22 $(CC) $(LDFLAGS) -o create-signed-key create-signed-key.o $(LIBS) 20 23 21 24 tags: openpgpsdk/trunk/include/create.h
r135 r136 2 2 */ 3 3 4 #ifndef OPS_CREATE_H 5 #define OPS_CREATE_H 6 4 7 #include "types.h" 5 8 #include "packet.h" 9 #include "crypto.h" 10 #include "memory.h" 6 11 7 12 enum ops_writer_ret_t … … 22 27 } ops_create_options_t; 23 28 24 ops_boolean_t ops_write_ptag(ops_content_tag_t tag,ops_create_options_t *opt); 25 ops_boolean_t ops_write_length(unsigned length,ops_create_options_t *opt); 29 typedef struct 30 { 31 ops_packet_writer_t *writer; 32 void *arg; 33 ops_hash_t hash; 34 ops_signature_t sig; 35 ops_memory_t mem; 36 ops_create_options_t opt; 37 unsigned hashed_count_offset; 38 unsigned hashed_data_length; 39 unsigned unhashed_count_offset; 40 } ops_create_signature_t; 41 26 42 ops_boolean_t ops_write(const void *src,unsigned length, 27 43 ops_create_options_t *opt); 44 ops_boolean_t ops_write_length(unsigned length,ops_create_options_t *opt); 45 ops_boolean_t ops_write_ptag(ops_content_tag_t tag,ops_create_options_t *opt); 46 ops_boolean_t ops_write_scalar(unsigned n,unsigned length, 47 ops_create_options_t *opt); 28 48 49 void ops_fast_create_rsa_public_key(ops_public_key_t *key,time_t time, 50 BIGNUM *n,BIGNUM *e); 51 void ops_create_rsa_public_key(ops_public_key_t *key,time_t time, 52 const BIGNUM *n,const BIGNUM *e); 29 53 ops_boolean_t ops_write_struct_public_key(const ops_public_key_t *key, 30 54 ops_create_options_t *opt); … … 33 57 ops_create_options_t *opt); 34 58 59 void ops_fast_create_user_id(ops_user_id_t *id,char *user_id); 35 60 ops_boolean_t ops_write_struct_user_id(ops_user_id_t *id, 36 61 ops_create_options_t *opt); 37 62 ops_boolean_t ops_write_user_id(const char *user_id,ops_create_options_t *opt); 63 64 #endif openpgpsdk/trunk/include/crypto.h
r135 r136 4 4 #include "util.h" 5 5 #include "packet.h" 6 7 #ifndef OPS_CRYPTO_H 8 #define OPS_CRYPTO_H 6 9 7 10 #define OPS_MAX_HASH 20 … … 34 37 int ops_rsa_public_decrypt(unsigned char *out,const unsigned char *in, 35 38 size_t length,const ops_rsa_public_key_t *rsa); 39 int ops_rsa_private_encrypt(unsigned char *out,const unsigned char *in, 40 size_t length,const ops_rsa_secret_key_t *rsa); 41 42 #endif openpgpsdk/trunk/include/memory.h
r135 r136 1 1 /** \file 2 2 */ 3 4 #ifndef OPS_MEMORY_H5 #define OPS_MEMORY_H6 3 7 4 #include <sys/types.h> 8 5 #include <openssl/bn.h> 9 6 #include "packet.h" 7 8 #ifndef OPS_MEMORY_H 9 #define OPS_MEMORY_H 10 10 11 11 typedef struct … … 20 20 void ops_memory_add(ops_memory_t *mem,const unsigned char *src,size_t length); 21 21 void ops_memory_add_int(ops_memory_t *mem,unsigned n,size_t length); 22 void ops_memory_place_int(ops_memory_t *mem,unsigned offset,unsigned n, 23 size_t length); 22 24 void ops_memory_add_mpi(ops_memory_t *out,const BIGNUM *bn); 23 25 void ops_memory_make_packet(ops_memory_t *out,ops_content_tag_t tag); 24 26 void ops_memory_release(ops_memory_t *mem); 25 27 28 ops_writer_ret_t ops_writer_memory(const unsigned char *src,unsigned length, 29 ops_writer_flags_t flags,void *arg_); 30 26 31 #endif openpgpsdk/trunk/include/packet.h
r135 r136 276 276 ops_public_key_union_t key; /*!< Public Key Parameters */ 277 277 } ops_public_key_t; 278 279 typedef struct 280 { 281 BIGNUM *d; 282 BIGNUM *p; 283 BIGNUM *q; 284 BIGNUM *u; 285 } ops_rsa_secret_key_t; 286 287 typedef struct 288 { 289 ops_rsa_secret_key_t rsa; 290 } ops_secret_key_union_t; 291 292 typedef struct 293 { 294 ops_secret_key_union_t key; 295 } ops_secret_key_t; 278 296 279 297 /** Symmetric Key Algorithm Numbers. openpgpsdk/trunk/include/signature.h
r135 r136 4 4 #include "packet.h" 5 5 #include "util.h" 6 #include "create.h" 6 7 7 8 ops_boolean_t … … 17 18 const ops_public_key_t *signer, 18 19 const unsigned char *raw_packet); 20 void ops_signature_start(ops_create_signature_t *sig, 21 const ops_public_key_t *key, 22 const ops_user_id_t *id); 23 void ops_signature_hashed_subpackets_end(ops_create_signature_t *sig); 24 void ops_signature_end(ops_create_signature_t *sig,ops_public_key_t *key, 25 ops_secret_key_t *skey); 26 void ops_signature_add_creation_time(ops_create_signature_t *sig,time_t when); 27 void ops_signature_add_issuer_key_id(ops_create_signature_t *sig, 28 const unsigned char keyid[OPS_KEY_ID_SIZE]); 29 void ops_signature_add_primary_user_id(ops_create_signature_t *sig, 30 ops_boolean_t primary); 31 openpgpsdk/trunk/ref/draft-ietf-openpgp-rfc2440bis-12-comments.txt
r127 r136 61 61 wording. 62 62 63 ------ 64 65 5.2.3.5 Issuer 66 67 should be: 68 69 5.2.3.5 Issuer key ID 70 71 A tiny point, I know, but it made it hard to find. 72 73 Key algorithms ... these are used in various contexts, and there's a 74 list in 9.1 - some of these are clearly unsuitable in some contexts - 75 for example, one would not expect to see RSA Ecnrpyt-Only (3) in a 76 signature. But I can't find any language saying anything about 77 this. Are there any rules? 78 openpgpsdk/trunk/src/build.c
r135 r136 45 45 } 46 46 47 // XXX: this should be refactored into a write 47 48 void ops_build_public_key(ops_memory_t *out,const ops_public_key_t *key, 48 49 ops_boolean_t make_packet) openpgpsdk/trunk/src/create.c
r135 r136 72 72 } 73 73 74 // XXX: the general idea of _fast_ is that it doesn't copy stuff 75 // the safe (i.e. non _fast_) version will, and so will also need to 76 // be freed. 77 void ops_fast_create_user_id(ops_user_id_t *id,char *user_id) 78 { 79 id->user_id=user_id;; 80 } 81 74 82 ops_boolean_t ops_write_struct_user_id(ops_user_id_t *id, 75 83 ops_create_options_t *opt) … … 107 115 } 108 116 117 void ops_fast_create_rsa_public_key(ops_public_key_t *key,time_t time, 118 BIGNUM *n,BIGNUM *e) 119 { 120 key->version=4; 121 key->creation_time=time; 122 key->algorithm=OPS_PKA_RSA; 123 key->key.rsa.n=n; 124 key->key.rsa.e=e; 125 } 126 109 127 ops_boolean_t ops_write_struct_public_key(const ops_public_key_t *key, 110 128 ops_create_options_t *opt) … … 139 157 ops_public_key_t key; 140 158 141 key.version=4; 142 key.creation_time=time; 143 key.algorithm=OPS_PKA_RSA; 144 key.key.rsa.n=DECONST(BIGNUM,n); 145 key.key.rsa.e=DECONST(BIGNUM,e); 159 ops_fast_create_rsa_public_key(&key,time,DECONST(BIGNUM,n), 160 DECONST(BIGNUM,e)); 146 161 return ops_write_struct_public_key(&key,opt); 147 162 } openpgpsdk/trunk/src/memory.c
r135 r136 3 3 4 4 #include "memory.h" 5 #include "create.h" 5 6 #include <stdlib.h> 6 7 #include <string.h> … … 56 57 } 57 58 59 void ops_memory_place_int(ops_memory_t *mem,unsigned offset,unsigned n, 60 size_t length) 61 { 62 assert(mem->allocated >= offset+length); 63 64 while(length--) 65 mem->buf[offset++]=n >> (length*8); 66 } 67 58 68 void ops_memory_add_mpi(ops_memory_t *out,const BIGNUM *bn) 59 69 { … … 72 82 mem->buf=NULL; 73 83 } 84 85 ops_writer_ret_t ops_writer_memory(const unsigned char *src,unsigned length, 86 ops_writer_flags_t flags,void *arg_) 87 { 88 ops_memory_t *mem=arg_; 89 90 ops_memory_add(mem,src,length); 91 return OPS_W_OK; 92 } 93 openpgpsdk/trunk/src/openssl_crypto.c
r135 r136 118 118 } 119 119 120 int ops_rsa_private_encrypt(unsigned char *out,const unsigned char *in, 121 size_t length,const ops_rsa_secret_key_t *rsa) 122 { 123 RSA *orsa; 124 int n; 125 126 orsa=RSA_new(); 127 orsa->d=rsa->d; 128 orsa->p=rsa->p; 129 orsa->q=rsa->q; 130 131 n=RSA_private_encrypt(length,in,out,orsa,RSA_NO_PADDING); 132 133 orsa->d=orsa->p=orsa->q=NULL; 134 RSA_free(orsa); 135 136 return n; 137 } 138 120 139 void ops_crypto_init() 121 140 { openpgpsdk/trunk/src/signature.c
r135 r136 6 6 #include "memory.h" 7 7 #include "build.h" 8 #include "create.h" 8 9 #include <assert.h> 9 10 #include <string.h> … … 15 16 static unsigned char prefix_sha1[]={ 0x30,0x21,0x30,0x09,0x06,0x05,0x2b,0x0E, 16 17 0x03,0x02,0x1A,0x05,0x00,0x04,0x14 }; 18 19 static void rsa_sign(ops_hash_t *hash,const ops_rsa_public_key_t *rsa, 20 const ops_rsa_secret_key_t *srsa) 21 { 22 unsigned char hashbuf[8192]; 23 unsigned char sigbuf[8192]; 24 unsigned keysize; 25 unsigned hashsize; 26 unsigned n; 27 unsigned t; 28 29 // XXX: we assume hash is sha-1 for now 30 hashsize=20+sizeof prefix_sha1; 31 32 keysize=(BN_num_bits(rsa->n)+7)/8; 33 assert(keysize <= sizeof hashbuf); 34 assert(10+hashsize <= keysize); 35 36 hashbuf[0]=1; 37 for(n=1 ; n < keysize-hashsize-2 ; ++n) 38 hashbuf[n]=0xff; 39 hashbuf[n++]=0; 40 41 memcpy(&hashbuf[n],prefix_sha1,sizeof prefix_sha1); 42 n+=sizeof prefix_sha1; 43 44 t=hash->finish(hash,&hashbuf[n]); 45 assert(t == 20); 46 n+=t; 47 48 ops_rsa_private_encrypt(sigbuf,hashbuf,keysize,srsa); 49 } 17 50 18 51 static ops_boolean_t rsa_verify(ops_hash_algorithm_t type, … … 42 75 hexdump(hashbuf,n); 43 76 77 // XXX: why is there a leading 0? The first byte should be 1... 78 // XXX: because the decrypt should use keysize and not sigsize? 44 79 if(hashbuf[0] != 0 || hashbuf[1] != 1) 45 80 return ops_false; … … 201 236 } 202 237 203 238 void ops_signature_start(ops_create_signature_t *sig, 239 const ops_public_key_t *key, 240 const ops_user_id_t *id) 241 { 242 // XXX: refactor with check (in several ways - check should probably 243 // use the buffered writer to construct packets, and also should 244 // share code for hash calculation) 245 sig->sig.version=OPS_SIG_V4; 246 sig->sig.hash_algorithm=OPS_HASH_SHA1; 247 sig->sig.key_algorithm=key->algorithm; 248 249 sig->hashed_data_length=-1; 250 251 init_signature(&sig->hash,&sig->sig,key); 252 253 hash_add_int(&sig->hash,0xb4,1); 254 hash_add_int(&sig->hash,strlen(id->user_id),4); 255 sig->hash.add(&sig->hash,id->user_id,strlen(id->user_id)); 256 257 // since this has subpackets and stuff, we have to buffer the whole 258 // thing to get counts before writing. 259 ops_memory_init(&sig->mem,100); 260 sig->opt.writer=ops_writer_memory; 261 sig->opt.arg=&sig->mem; 262 263 // write nearly up to the first subpacket 264 ops_write_scalar(sig->sig.version,1,&sig->opt); 265 ops_write_scalar(sig->sig.type,1,&sig->opt); 266 ops_write_scalar(sig->sig.key_algorithm,1,&sig->opt); 267 ops_write_scalar(sig->sig.hash_algorithm,1,&sig->opt); 268 269 // dummy hashed subpacket count 270 sig->hashed_count_offset=sig->mem.length; 271 ops_write_scalar(0,2,&sig->opt); 272 } 273 274 void ops_signature_hashed_subpackets_end(ops_create_signature_t *sig) 275 { 276 sig->hashed_data_length=sig->mem.length-sig->hashed_count_offset-2; 277 ops_memory_place_int(&sig->mem,sig->hashed_count_offset, 278 sig->hashed_data_length,2); 279 // dummy unhashed subpacket count 280 sig->unhashed_count_offset=sig->mem.length; 281 ops_write_scalar(0,2,&sig->opt); 282 } 283 284 void ops_signature_end(ops_create_signature_t *sig,ops_public_key_t *key, 285 ops_secret_key_t *skey) 286 { 287 assert(sig->hashed_data_length != -1); 288 289 ops_memory_place_int(&sig->mem,sig->unhashed_count_offset, 290 sig->mem.length-sig->unhashed_count_offset-2,2); 291 292 // add the packet from version number to end of hashed subpackets 293 sig->hash.add(&sig->hash,sig->mem.buf,sig->unhashed_count_offset); 294 hash_add_int(&sig->hash,sig->sig.version,1); 295 hash_add_int(&sig->hash,0xff,1); 296 hash_add_int(&sig->hash,sig->hashed_data_length,4); 297 298 assert(key->algorithm == OPS_PKA_RSA); 299 rsa_sign(&sig->hash,&key->key.rsa,&skey->key.rsa); 300 }
