Changeset 136

Show
Ignore:
Timestamp:
05/20/05 13:44:25
Author:
ben
Message:

Partial signature code, prior to refactor for verification.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openpgpsdk/trunk/examples/Makefile.template

    r133 r136  
    88LIBS=$(LIBDEPS) -lcrypto -lz $(DM_LIB) 
    99 
    10 all: Makefile .depend packet-dump verify create-key 
     10all: Makefile .depend packet-dump verify create-key create-signed-key 
    1111 
    1212packet-dump: packet-dump.o $(LIBDEPS) 
     
    1818create-key: create-key.o $(LIBDEPS) 
    1919        $(CC) $(LDFLAGS) -o create-key create-key.o $(LIBS) 
     20 
     21create-signed-key: create-signed-key.o $(LIBDEPS) 
     22        $(CC) $(LDFLAGS) -o create-signed-key create-signed-key.o $(LIBS) 
    2023 
    2124tags: 
  • openpgpsdk/trunk/include/create.h

    r135 r136  
    22 */ 
    33 
     4#ifndef OPS_CREATE_H 
     5#define OPS_CREATE_H 
     6 
    47#include "types.h" 
    58#include "packet.h" 
     9#include "crypto.h" 
     10#include "memory.h" 
    611 
    712enum ops_writer_ret_t 
     
    2227    } ops_create_options_t; 
    2328 
    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); 
     29typedef 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 
    2642ops_boolean_t ops_write(const void *src,unsigned length, 
    2743                        ops_create_options_t *opt); 
     44ops_boolean_t ops_write_length(unsigned length,ops_create_options_t *opt); 
     45ops_boolean_t ops_write_ptag(ops_content_tag_t tag,ops_create_options_t *opt); 
     46ops_boolean_t ops_write_scalar(unsigned n,unsigned length, 
     47                               ops_create_options_t *opt); 
    2848 
     49void ops_fast_create_rsa_public_key(ops_public_key_t *key,time_t time, 
     50                                    BIGNUM *n,BIGNUM *e); 
     51void ops_create_rsa_public_key(ops_public_key_t *key,time_t time, 
     52                               const BIGNUM *n,const BIGNUM *e); 
    2953ops_boolean_t ops_write_struct_public_key(const ops_public_key_t *key, 
    3054                                          ops_create_options_t *opt); 
     
    3357                                       ops_create_options_t *opt); 
    3458 
     59void ops_fast_create_user_id(ops_user_id_t *id,char *user_id); 
    3560ops_boolean_t ops_write_struct_user_id(ops_user_id_t *id, 
    3661                                       ops_create_options_t *opt); 
    3762ops_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  
    44#include "util.h" 
    55#include "packet.h" 
     6 
     7#ifndef OPS_CRYPTO_H 
     8#define OPS_CRYPTO_H 
    69 
    710#define OPS_MAX_HASH    20 
     
    3437int ops_rsa_public_decrypt(unsigned char *out,const unsigned char *in, 
    3538                           size_t length,const ops_rsa_public_key_t *rsa); 
     39int 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  
    11/** \file 
    22 */ 
    3  
    4 #ifndef OPS_MEMORY_H 
    5 #define OPS_MEMORY_H 
    63 
    74#include <sys/types.h> 
    85#include <openssl/bn.h> 
    96#include "packet.h" 
     7 
     8#ifndef OPS_MEMORY_H 
     9#define OPS_MEMORY_H 
    1010 
    1111typedef struct  
     
    2020void ops_memory_add(ops_memory_t *mem,const unsigned char *src,size_t length); 
    2121void ops_memory_add_int(ops_memory_t *mem,unsigned n,size_t length); 
     22void ops_memory_place_int(ops_memory_t *mem,unsigned offset,unsigned n, 
     23                          size_t length); 
    2224void ops_memory_add_mpi(ops_memory_t *out,const BIGNUM *bn); 
    2325void ops_memory_make_packet(ops_memory_t *out,ops_content_tag_t tag); 
    2426void ops_memory_release(ops_memory_t *mem); 
    2527 
     28ops_writer_ret_t ops_writer_memory(const unsigned char *src,unsigned length, 
     29                                   ops_writer_flags_t flags,void *arg_); 
     30 
    2631#endif 
  • openpgpsdk/trunk/include/packet.h

    r135 r136  
    276276    ops_public_key_union_t      key;            /*!< Public Key Parameters */ 
    277277    } ops_public_key_t; 
     278 
     279typedef struct 
     280    { 
     281    BIGNUM *d; 
     282    BIGNUM *p; 
     283    BIGNUM *q; 
     284    BIGNUM *u; 
     285    } ops_rsa_secret_key_t; 
     286 
     287typedef struct 
     288    { 
     289    ops_rsa_secret_key_t rsa; 
     290    } ops_secret_key_union_t; 
     291 
     292typedef struct 
     293    { 
     294    ops_secret_key_union_t key; 
     295    } ops_secret_key_t; 
    278296 
    279297/** Symmetric Key Algorithm Numbers. 
  • openpgpsdk/trunk/include/signature.h

    r135 r136  
    44#include "packet.h" 
    55#include "util.h" 
     6#include "create.h" 
    67 
    78ops_boolean_t 
     
    1718                           const ops_public_key_t *signer, 
    1819                           const unsigned char *raw_packet); 
     20void ops_signature_start(ops_create_signature_t *sig, 
     21                         const ops_public_key_t *key, 
     22                         const ops_user_id_t *id); 
     23void ops_signature_hashed_subpackets_end(ops_create_signature_t *sig); 
     24void ops_signature_end(ops_create_signature_t *sig,ops_public_key_t *key, 
     25                       ops_secret_key_t *skey); 
     26void ops_signature_add_creation_time(ops_create_signature_t *sig,time_t when); 
     27void ops_signature_add_issuer_key_id(ops_create_signature_t *sig, 
     28                                     const unsigned char keyid[OPS_KEY_ID_SIZE]); 
     29void 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  
    6161wording. 
    6262 
     63------ 
     64 
     655.2.3.5 Issuer 
     66 
     67should be: 
     68 
     695.2.3.5 Issuer key ID 
     70 
     71A tiny point, I know, but it made it hard to find. 
     72 
     73Key algorithms ... these are used in various contexts, and there's a 
     74list in 9.1 - some of these are clearly unsuitable in some contexts - 
     75for example, one would not expect to see RSA Ecnrpyt-Only (3) in a 
     76signature. But I can't find any language saying anything about 
     77this. Are there any rules? 
     78 
  • openpgpsdk/trunk/src/build.c

    r135 r136  
    4545    } 
    4646 
     47// XXX: this should be refactored into a write 
    4748void ops_build_public_key(ops_memory_t *out,const ops_public_key_t *key, 
    4849                          ops_boolean_t make_packet) 
  • openpgpsdk/trunk/src/create.c

    r135 r136  
    7272    } 
    7373 
     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. 
     77void ops_fast_create_user_id(ops_user_id_t *id,char *user_id) 
     78    { 
     79    id->user_id=user_id;; 
     80    } 
     81 
    7482ops_boolean_t ops_write_struct_user_id(ops_user_id_t *id, 
    7583                                       ops_create_options_t *opt) 
     
    107115    } 
    108116 
     117void 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 
    109127ops_boolean_t ops_write_struct_public_key(const ops_public_key_t *key, 
    110128                                          ops_create_options_t *opt) 
     
    139157    ops_public_key_t key; 
    140158 
    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)); 
    146161    return ops_write_struct_public_key(&key,opt); 
    147162    } 
  • openpgpsdk/trunk/src/memory.c

    r135 r136  
    33 
    44#include "memory.h" 
     5#include "create.h" 
    56#include <stdlib.h> 
    67#include <string.h> 
     
    5657    } 
    5758 
     59void 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 
    5868void ops_memory_add_mpi(ops_memory_t *out,const BIGNUM *bn) 
    5969    { 
     
    7282    mem->buf=NULL; 
    7383    } 
     84 
     85ops_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  
    118118    } 
    119119 
     120int 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 
    120139void ops_crypto_init() 
    121140    { 
  • openpgpsdk/trunk/src/signature.c

    r135 r136  
    66#include "memory.h" 
    77#include "build.h" 
     8#include "create.h" 
    89#include <assert.h> 
    910#include <string.h> 
     
    1516static unsigned char prefix_sha1[]={ 0x30,0x21,0x30,0x09,0x06,0x05,0x2b,0x0E, 
    1617                                     0x03,0x02,0x1A,0x05,0x00,0x04,0x14 }; 
     18 
     19static 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    } 
    1750 
    1851static ops_boolean_t rsa_verify(ops_hash_algorithm_t type, 
     
    4275    hexdump(hashbuf,n); 
    4376 
     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? 
    4479    if(hashbuf[0] != 0 || hashbuf[1] != 1) 
    4580        return ops_false; 
     
    201236    } 
    202237 
    203  
     238void 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 
     274void 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 
     284void 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    }