Changeset 574

Show
Ignore:
Timestamp:
08/13/08 14:30:27
Author:
rachel
Message:

Implemented final part of command line application (list-packets).
Rationalised filenames.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openpgpsdk/trunk/Makefile

    r541 r574  
    1010default: 
    1111        @set -e; for d in $(SUBDIRS); do \ 
    12         (cd $$d; echo "+++ make in $$d"; make; echo "--- $$d"); \ 
     12        (cd $$d; echo "+++ make in $$d"; make -w; echo "** submake returns $$?\n"; echo "--- $$d"); \ 
    1313        done 
    1414 
  • openpgpsdk/trunk/include/openpgpsdk/armour.h

    r573 r574  
    2020 */ 
    2121 
     22#ifndef __OPS_ARMOUR_H__ 
     23#define __OPS_ARMOUR_H__ 
     24 
    2225#include "packet-parse.h" 
    2326#include "signature.h" 
     27 
     28unsigned ops_crc24(unsigned checksum,unsigned char c); 
    2429 
    2530void ops_reader_push_dearmour(ops_parse_info_t *parse_info, 
     
    4651void ops_writer_push_armoured(ops_create_info_t *info, ops_armor_type_t type); 
    4752 
     53#define CRC24_INIT 0xb704ceL 
     54 
     55#endif /* __OPS_ARMOUR_H__ */ 
     56 
    4857// EOF 
  • openpgpsdk/trunk/include/openpgpsdk/create.h

    r570 r574  
    2626#define OPS_CREATE_H 
    2727 
    28 #include "types.h" 
    29 #include "packet.h" 
    30 #include "crypto.h" 
    31 #include "memory.h" 
    32 #include "errors.h" 
    33 #include "keyring.h" 
     28#include <openpgpsdk/types.h> 
     29#include <openpgpsdk/packet.h> 
     30#include <openpgpsdk/crypto.h> 
     31#include <openpgpsdk/memory.h> 
     32#include <openpgpsdk/errors.h> 
     33#include <openpgpsdk/keyring.h> 
     34#include <openpgpsdk/writer.h> 
    3435 
    35 typedef struct ops_writer_info ops_writer_info_t; 
    3636/** 
    3737 * \ingroup Create 
    38  * the writer function prototype 
     38 * This struct contains the required information about how to write this stream 
    3939 */ 
    40 typedef ops_boolean_t ops_writer_t(const unsigned char *src, 
    41                                       unsigned length, 
    42                                       ops_error_t **errors, 
    43                                       ops_writer_info_t *winfo); 
    44 typedef ops_boolean_t ops_writer_finaliser_t(ops_error_t **errors, 
    45                                              ops_writer_info_t *winfo); 
    46 typedef void ops_writer_destroyer_t(ops_writer_info_t *winfo); 
     40struct ops_create_info 
     41    { 
     42    ops_writer_info_t winfo; 
     43    ops_error_t *errors;        /*!< an error stack */ 
     44    }; 
    4745 
    4846ops_create_info_t *ops_create_info_new(void); 
    4947void ops_create_info_delete(ops_create_info_t *info); 
    50 void *ops_writer_get_arg(ops_writer_info_t *winfo); 
    51 ops_boolean_t ops_stacked_write(const void *src,unsigned length, 
    52                                 ops_error_t **errors, 
    53                                 ops_writer_info_t *winfo); 
    5448 
    55 void ops_writer_set(ops_create_info_t *info, 
    56                     ops_writer_t *writer, 
    57                     ops_writer_finaliser_t *finaliser, 
    58                     ops_writer_destroyer_t *destroyer, 
    59                     void *arg); 
    60 void ops_writer_push(ops_create_info_t *info, 
    61                      ops_writer_t *writer, 
    62                      ops_writer_finaliser_t *finaliser, 
    63                      ops_writer_destroyer_t *destroyer, 
    64                      void *arg); 
    65 void ops_writer_pop(ops_create_info_t *info); 
    66 void ops_writer_generic_destroyer(ops_writer_info_t *winfo); 
    67 ops_boolean_t ops_writer_passthrough(const unsigned char *src, 
    68                                      unsigned length, 
    69                                      ops_error_t **errors, 
    70                                      ops_writer_info_t *winfo); 
     49ops_memory_t* ops_write_buf_from_file(const char *filename); 
     50int ops_write_file_from_buf(const char *filename, const char* buf, const size_t len, const ops_boolean_t overwrite); 
    7151 
    72 void ops_writer_set_fd(ops_create_info_t *info,int fd); 
    73 ops_boolean_t ops_writer_close(ops_create_info_t *info); 
    74  
    75 ops_boolean_t ops_write(const void *src,unsigned length, 
    76                         ops_create_info_t *opt); 
    77 ops_boolean_t ops_write_length(unsigned length,ops_create_info_t *opt); 
    78 ops_boolean_t ops_write_ptag(ops_content_tag_t tag,ops_create_info_t *opt); 
    79 ops_boolean_t ops_write_scalar(unsigned n,unsigned length, 
    80                                ops_create_info_t *opt); 
    81 ops_boolean_t ops_write_mpi(const BIGNUM *bn,ops_create_info_t *opt); 
    82 ops_boolean_t ops_write_encrypted_mpi(const BIGNUM *bn, ops_crypt_t* crypt, ops_create_info_t *info); 
    83 ops_boolean_t ops_write_ss_header(unsigned length,ops_content_tag_t type, 
    84                                   ops_create_info_t *opt); 
    85  
    86 void ops_fast_create_rsa_public_key(ops_public_key_t *key,time_t time, 
    87                                     BIGNUM *n,BIGNUM *e); 
    88 void ops_create_rsa_public_key(ops_public_key_t *key,time_t time, 
    89                                const BIGNUM *n,const BIGNUM *e); 
     52ops_boolean_t ops_calc_session_key_checksum(ops_pk_session_key_t *session_key, unsigned char *cs); 
    9053void ops_build_public_key(ops_memory_t *out,const ops_public_key_t *key, 
    9154                          ops_boolean_t make_packet); 
     55ops_boolean_t ops_write_struct_user_id(ops_user_id_t *id, 
     56                                       ops_create_info_t *info); 
    9257ops_boolean_t ops_write_struct_public_key(const ops_public_key_t *key, 
    93                                           ops_create_info_t *opt); 
    94 ops_boolean_t ops_write_rsa_public_key(time_t time,const BIGNUM *n, 
    95                                        const BIGNUM *e, 
    96                                        ops_create_info_t *opt); 
     58                                          ops_create_info_t *info); 
    9759 
    98 void ops_fast_create_user_id(ops_user_id_t *id,unsigned char *user_id); 
    99 ops_boolean_t ops_write_struct_user_id(ops_user_id_t *id, 
    100                                        ops_create_info_t *opt); 
    101 ops_boolean_t ops_write_user_id(const unsigned char *user_id,ops_create_info_t *opt); 
    102  
    103 void ops_create_rsa_secret_key(ops_secret_key_t *key,const BIGNUM *p, 
    104                                const BIGNUM *q,const BIGNUM *d, 
    105                                const BIGNUM *u,const BIGNUM *n, 
    106                                const BIGNUM *e); 
    107 void ops_fast_create_rsa_secret_key(ops_secret_key_t *key,time_t time, 
    108                                     BIGNUM *p,BIGNUM *q,BIGNUM *d,BIGNUM *u, 
    109                                     BIGNUM *n,BIGNUM *e); 
     60ops_boolean_t ops_write_ss_header(unsigned length,ops_content_tag_t type, 
     61                                  ops_create_info_t *info); 
    11062ops_boolean_t ops_write_struct_secret_key(const ops_secret_key_t *key, 
    11163                                          const unsigned char* passphrase, 
    11264                                          const size_t pplen, 
    11365                                          ops_create_info_t *info); 
    114  
     66ops_boolean_t ops_write_one_pass_sig(const ops_secret_key_t* skey, 
     67                                     const ops_hash_algorithm_t hash_alg, 
     68                                     const ops_sig_type_t sig_type, 
     69                                     ops_create_info_t* info); 
     70ops_boolean_t ops_write_literal_data_from_buf(const unsigned char *data,  
     71                                              const int maxlen,  
     72                                              const ops_literal_data_type_t type, 
     73                                              ops_create_info_t *info); 
    11574ops_pk_session_key_t *ops_create_pk_session_key(const ops_keydata_t *key); 
    116  
    117 void ops_create_m_buf(ops_pk_session_key_t *session_key, unsigned char *buf); 
    118  
    119 // RENAMED from ops_boolean_t ops_write_literal_data(const unsigned char *data,  
    120 ops_boolean_t ops_write_literal_data_from_buf(const unsigned char *data,  
    121                                      const int maxlen,  
    122                                      const ops_literal_data_type_t type, 
    123                                      ops_create_info_t *info); 
    124 ops_boolean_t ops_write_literal_data_from_file(const char *filename,  
    125                                                const ops_literal_data_type_t type, 
    126                                                ops_create_info_t *info); 
    127  
    128 ops_memory_t* ops_write_buf_from_file(const char *filename); 
    129 int ops_write_file_from_buf(const char* filename, const char* buf, const size_t len, const ops_boolean_t overwrite); 
    130  
    131 ops_boolean_t ops_write_symmetrically_encrypted_data(const unsigned char *data,  
    132                                                      const int len,  
    133                                                      ops_create_info_t *info); 
    134  
    135 ops_boolean_t ops_write_se_ip_data(const unsigned char *data, 
    136                                    const unsigned int len, 
    137                                    ops_crypt_t *crypt, 
    138                                    ops_create_info_t *info); 
    13975ops_boolean_t ops_write_pk_session_key(ops_create_info_t *info, 
    14076                                       ops_pk_session_key_t *pksk); 
    141 ops_boolean_t ops_calc_session_key_checksum(ops_pk_session_key_t *session_key, unsigned char *cs); 
    142 void ops_calc_mdc_hash(const unsigned char* preamble, const size_t sz_preamble, const unsigned char* data, const unsigned int len, unsigned char *hashed); 
     77ops_boolean_t ops_write_transferable_public_key(const ops_keydata_t *key, ops_boolean_t armoured, ops_create_info_t *info); 
     78ops_boolean_t ops_write_transferable_secret_key(const ops_keydata_t *key, const unsigned char* passphrase, const size_t pplen, ops_boolean_t armoured, ops_create_info_t *info); 
    14379 
    144 ops_boolean_t ops_write_one_pass_sig(const ops_secret_key_t* skey, const ops_hash_algorithm_t hash_alg, const ops_sig_type_t sig_type, ops_create_info_t* info); 
     80#endif /*OPS_CREATE_H*/ 
    14581 
    146 ops_boolean_t ops_write_transferable_public_key(const ops_keydata_t *key, ops_boolean_t armoured, ops_create_info_t *info); 
    147 ops_boolean_t ops_write_transferable_secret_key(const ops_keydata_t *key, const unsigned char* passphrase, size_t pplen, ops_boolean_t armoured, ops_create_info_t *info); 
    148  
    149 #endif 
     82// eof 
  • openpgpsdk/trunk/include/openpgpsdk/crypto.h

    r570 r574  
    160160                             const struct ops_key_data *key); 
    161161 
    162 void ops_encrypt_file(const char* input_filename, const char* output_filename, const ops_keydata_t *pub_key, const ops_boolean_t use_armour, const ops_boolean_t allow_overwrite); 
    163 void ops_decrypt_file(const char* input_filename, const char* output_filename, ops_keyring_t *keyring, const ops_boolean_t use_armour, const ops_boolean_t allow_overwrite,ops_parse_cb_t* cb_get_passphrase); 
     162ops_boolean_t ops_encrypt_file(const char* input_filename, const char* output_filename, const ops_keydata_t *pub_key, const ops_boolean_t use_armour, const ops_boolean_t allow_overwrite); 
     163ops_boolean_t ops_decrypt_file(const char* input_filename, const char* output_filename, ops_keyring_t *keyring, const ops_boolean_t use_armour, const ops_boolean_t allow_overwrite,ops_parse_cb_t* cb_get_passphrase); 
    164164 
    165165// Keys 
  • openpgpsdk/trunk/include/openpgpsdk/readerwriter.h

    r570 r574  
    2525#include <openpgpsdk/memory.h> 
    2626#include <openpgpsdk/create.h> 
    27  
    28 /** 
    29  * \ingroup Create 
    30  * This struct contains the required information about one writer 
    31  */ 
    32 struct ops_writer_info 
    33     { 
    34     ops_writer_t *writer; 
    35     ops_writer_finaliser_t *finaliser; 
    36     ops_writer_destroyer_t *destroyer; 
    37     void *arg; 
    38     ops_writer_info_t *next; 
    39     }; 
    40  
    41 /** 
    42  * \ingroup Create 
    43  * This struct contains the required information about how to write this stream 
    44  */ 
    45 struct ops_create_info 
    46     { 
    47     ops_writer_info_t winfo; 
    48     ops_error_t *errors;        /*!< an error stack */ 
    49     }; 
    5027 
    5128// 
  • openpgpsdk/trunk/include/openpgpsdk/std_print.h

    r570 r574  
    4343//void ops_print_secret_key(const ops_content_tag_t type, const ops_secret_key_t* skey); 
    4444int ops_print_packet(const ops_parser_content_t *content_); 
     45void ops_list_packets(char *filename, ops_boolean_t armour, ops_keyring_t* pubring, ops_parse_cb_t* cb_get_passphrase); 
     46 
    4547#endif 
  • openpgpsdk/trunk/src/app/openpgp.c

    r572 r574  
    3737#include "openpgpsdk/validate.h" 
    3838#include "openpgpsdk/readerwriter.h" 
     39#include "openpgpsdk/std_print.h" 
    3940 
    4041#define DEFAULT_NUMBITS 1024 
     
    5253static const char* usage_clearsign="%s --clearsign --userid=<userid> --filename=<filename> [--homedir=<homedir>]\n"; 
    5354static const char* usage_verify="%s --verify --filename=<filename> [--homedir=<homedir>] [--armour]\n"; 
     55static const char* usage_list_packets="%s --list-packets --filename=<filename> [--homedir=<homedir>] [--armour]\n"; 
    5456 
    5557static const char* pname; 
     
    6870CLEARSIGN, 
    6971VERIFY, 
     72LIST_PACKETS, 
     73 
    7074// options 
    7175KEYRING, 
     
    9397    { "clearsign", no_argument, NULL, CLEARSIGN }, 
    9498    { "verify", no_argument, NULL, VERIFY }, 
     99 
     100    { "list-packets", no_argument, NULL, LIST_PACKETS }, 
    95101 
    96102    // options 
     
    228234            break; 
    229235 
     236        case LIST_PACKETS: 
     237            cmd=LIST_PACKETS; 
     238            break; 
     239 
    230240            // option 
    231241 
     
    428438 
    429439        // write public key 
    430 #ifdef TMP 
    431         ops_setup_memory_write(&cinfo, &mem, 128); 
    432         ops_write_transferable_public_key(mykeydata, ops_true, cinfo); 
    433         ops_keyring_read_from_mem(pubring, mem); 
    434         ops_teardown_memory_write(cinfo,mem); 
    435  
    436         // write secret key 
    437         ops_setup_memory_write(&cinfo, &mem, 128); 
    438         ops_write_transferable_secret_key(mykeydata, NULL, 0, ops_true, cinfo); 
    439         ops_keyring_read_from_mem(secring, mem); 
    440         ops_teardown_memory_write(cinfo,mem); 
    441 #else 
    442440        // append to keyrings 
    443441        fd=ops_setup_file_append(&cinfo, pubring_name); 
     
    461459            exit(-1); 
    462460            } 
    463 #endif 
     461 
    464462        ops_keydata_free(mykeydata); 
    465463        break; 
     
    576574        break; 
    577575 
     576    case LIST_PACKETS: 
     577        if (!got_filename) 
     578            { 
     579            print_usage(usage_list_packets, pname); 
     580            exit(-1); 
     581            } 
     582        ops_list_packets(opt_filename, armour, pubring, callback_cmd_get_passphrase_from_cmdline); 
     583        break; 
     584 
    578585    default: 
    579586        print_usage(usage,pname); 
  • openpgpsdk/trunk/src/lib/Makefile.template

    r546 r574  
    88all: Makefile headers .depend lib 
    99 
    10 LIBOBJS = adv_packet-parse.o adv_util.o adv_openssl_crypto.o adv_accumulate.o \ 
    11         adv_memory.o adv_fingerprint.o adv_hash.o adv_keyring.o \ 
    12         adv_signature.o adv_compress.o adv_packet-show.o adv_create.o \ 
    13         adv_validate.o adv_lists.o adv_armour.o adv_errors.o \ 
    14     adv_writer_encrypt_se_ip.o adv_writer_encrypt.o \ 
    15     adv_writer_stream_encrypt_se_ip.o \ 
    16         adv_symmetric.o adv_crypto.o random.o adv_readerwriter.o \ 
    17     std_encrypt.o std_keyring.o std_print.o std_signature.o 
     10LIBOBJS = packet-parse.o packet-print.o packet-show.o \ 
     11        util.o openssl_crypto.o accumulate.o \ 
     12        memory.o fingerprint.o hash.o keyring.o \ 
     13        signature.o compress.o create.o \ 
     14        validate.o lists.o errors.o \ 
     15        symmetric.o crypto.o random.o readerwriter.o \ 
     16        reader.o reader_fd.o reader_mem.o \ 
     17        reader_armoured.o reader_hashed.o reader_encrypted.o \ 
     18        writer_fd.o writer_memory.o \ 
     19        writer.o writer_skey_checksum.o  writer_armour.o \ 
     20        writer_encrypt_se_ip.o writer_encrypt.o \ 
     21        writer_stream_encrypt_se_ip.o 
    1822 
    1923headers: 
  • openpgpsdk/trunk/src/lib/compress.c

    r571 r574  
    6666    } compress_arg_t; 
    6767 
    68 #define ERR(err)        do { content.content.error.error=err; content.tag=OPS_PARSER_ERROR; ops_parse_cb(&content,cbinfo); return -1; } while(0) 
    69  
    7068// \todo remove code duplication between this and bzip2_compressed_data_reader 
    7169static int zlib_compressed_data_reader(void *dest,size_t length, 
     
    7775    assert(arg->type==OPS_C_ZIP || arg->type==OPS_C_ZLIB); 
    7876 
    79     ops_parser_content_t content; 
     77    //ops_parser_content_t content; 
    8078    int saved=length; 
    8179 
     
    8785        { 
    8886        if(arg->inflate_ret != Z_STREAM_END) 
    89             ERR("Compressed data didn't end when region ended."); 
     87            OPS_ERROR(cbinfo->errors, OPS_E_P_DECOMPRESSION_ERROR,"Compressed data didn't end when region ended."); 
    9088        /* 
    9189          else 
     
    133131                if(!arg->region->indeterminate 
    134132                   && arg->region->length_read != arg->region->length) 
    135                     ERR("Compressed stream ended before packet end."); 
     133                    OPS_ERROR(cbinfo->errors,OPS_E_P_DECOMPRESSION_ERROR,"Compressed stream ended before packet end."); 
    136134                } 
    137135            else if(ret != Z_OK) 
    138136                { 
    139137                fprintf(stderr,"ret=%d\n",ret); 
    140                 ERR(arg->zstream.msg); 
     138                OPS_ERROR(cbinfo->errors,OPS_E_P_DECOMPRESSION_ERROR, arg->zstream.msg); 
    141139                } 
    142140            arg->inflate_ret=ret; 
     
    163161    assert(arg->type==OPS_C_BZIP2); 
    164162 
    165     ops_parser_content_t content; 
     163    //ops_parser_content_t content; 
    166164    int saved=length; 
    167165 
     
    173171        { 
    174172        if(arg->inflate_ret != BZ_STREAM_END) 
    175             ERR("Compressed data didn't end when region ended."); 
     173            OPS_ERROR(cbinfo->errors, OPS_E_P_DECOMPRESSION_ERROR,"Compressed data didn't end when region ended."); 
    176174        } 
    177175 
     
    214212                if(!arg->region->indeterminate 
    215213                   && arg->region->length_read != arg->region->length) 
    216                     ERR("Compressed stream ended before packet end."); 
     214                    OPS_ERROR(cbinfo->errors,OPS_E_P_DECOMPRESSION_ERROR,"Compressed stream ended before packet end."); 
    217215                } 
    218216            else if(ret != BZ_OK) 
    219217                { 
    220                 fprintf(stderr,"ret=%d\n",ret); 
    221         //              ERR(arg->bzstream.msg); //\todo add error handling 
     218                OPS_ERROR_1(cbinfo->errors,OPS_E_P_DECOMPRESSION_ERROR,"Invalid return %d from BZ2_bzDecompress", ret); 
    222219                } 
    223220            arg->inflate_ret=ret; 
  • openpgpsdk/trunk/src/lib/create.c

    r573 r574  
    4242#endif 
    4343 
     44#include <openpgpsdk/writer.h> 
    4445#include <openpgpsdk/final.h> 
    4546 
    4647static int debug=0; 
    47  
    48 static ops_boolean_t writer_info_finalise(ops_error_t **errors, 
    49                                           ops_writer_info_t *winfo); 
    50  
    51 /* 
    52  * return true if OK, otherwise false 
    53  */ 
    54 static ops_boolean_t base_write(const void *src,unsigned length, 
    55                                 ops_create_info_t *info) 
    56     { 
    57     return info->winfo.writer(src,length,&info->errors,&info->winfo); 
    58     } 
    59  
    60 /** 
    61  * \ingroup Create 
    62  * 
    63  * \param src 
    64  * \param length 
    65  * \param info 
    66  * \return 1 if OK, otherwise 0 
    67  */ 
    68  
    69 ops_boolean_t ops_write(const void *src,unsigned length, 
    70                         ops_create_info_t *info) 
    71     { 
    72     return base_write(src,length,info); 
    73     } 
    74  
    75 /** 
    76  * \ingroup Create 
    77  * \param n 
    78  * \param length 
    79  * \param info 
    80  * \return ops_true if OK, otherwise ops_false 
    81  */ 
    82  
    83 ops_boolean_t ops_write_scalar(unsigned n,unsigned length, 
    84                                ops_create_info_t *info) 
    85     { 
    86     while(length-- > 0) 
    87         { 
    88         unsigned char c[1]; 
    89  
    90         c[0]=n >> (length*8); 
    91         if(!base_write(c,1,info)) 
    92             return ops_false; 
    93         } 
    94     return ops_true; 
    95     } 
    96  
    97 /**  
    98  * \ingroup Create 
    99  * \param bn 
    100  * \param info 
    101  * \return 1 if OK, otherwise 0 
    102  */ 
    103  
    104 ops_boolean_t ops_write_mpi(const BIGNUM *bn,ops_create_info_t *info) 
    105     { 
    106     unsigned char buf[8192]; 
    107     int bits=BN_num_bits(bn); 
    108  
    109     assert(bits <= 65535); 
    110     BN_bn2bin(bn,buf); 
    111     return ops_write_scalar(bits,2,info) 
    112         && ops_write(buf,(bits+7)/8,info); 
    113     } 
    114  
    115 /**  
    116  * \ingroup Create 
    117  * \param tag 
    118  * \param info 
    119  * \return 1 if OK, otherwise 0 
    120  */ 
    121  
    122 ops_boolean_t ops_write_ptag(ops_content_tag_t tag,ops_create_info_t *info) 
    123     { 
    124     unsigned char c[1]; 
    125  
    126     c[0]=tag|OPS_PTAG_ALWAYS_SET|OPS_PTAG_NEW_FORMAT; 
    127  
    128     return base_write(c,1,info); 
    129     } 
    130  
    131 /**  
    132  * \ingroup Create 
    133  * \param length 
    134  * \param info 
    135  * \return 1 if OK, otherwise 0 
    136  */ 
    137  
    138 ops_boolean_t ops_write_length(unsigned length,ops_create_info_t *info) 
    139     { 
    140     unsigned char c[2]; 
    141  
    142     if(length < 192) 
    143         { 
    144         c[0]=length; 
    145         return base_write(c,1,info); 
    146         } 
    147     else if(length < 8384) 
    148         { 
    149         c[0]=((length-192) >> 8)+192; 
    150         c[1]=(length-192)%256; 
    151         return base_write(c,2,info); 
    152         } 
    153     return ops_write_scalar(0xff,1,info) && ops_write_scalar(length,4,info); 
    154     } 
    15548 
    15649/**  
     
    321214    } 
    322215 
    323 typedef struct 
    324     { 
    325     ops_hash_algorithm_t hash_algorithm; 
    326     ops_hash_t hash; 
    327     unsigned char *hashed; 
    328     } skey_checksum_arg_t; 
    329  
    330 static ops_boolean_t skey_checksum_writer(const unsigned char *src, const unsigned length, ops_error_t **errors, ops_writer_info_t *winfo) 
    331     { 
    332     skey_checksum_arg_t *arg=ops_writer_get_arg(winfo); 
    333     ops_boolean_t rtn=ops_true; 
    334  
    335     // add contents to hash 
    336     arg->hash.add(&arg->hash, src, length); 
    337  
    338     // write to next stacked writer 
    339     rtn=ops_stacked_write(src,length,errors,winfo); 
    340  
    341     // tidy up and return 
    342     return rtn; 
    343     } 
    344  
    345 static ops_boolean_t skey_checksum_finaliser(ops_error_t **errors __attribute__((unused)), ops_writer_info_t *winfo) 
    346     { 
    347     skey_checksum_arg_t *arg=ops_writer_get_arg(winfo); 
    348     arg->hash.finish(&arg->hash, arg->hashed); 
    349     return ops_true; 
    350     } 
    351  
    352 static void skey_checksum_destroyer(ops_writer_info_t* winfo) 
    353     { 
    354     skey_checksum_arg_t *arg=ops_writer_get_arg(winfo); 
    355     free(arg); 
    356     } 
    357  
    358 void ops_push_skey_checksum_writer(ops_create_info_t *cinfo, ops_secret_key_t *skey) 
    359     { 
    360     //    OPS_USED(info); 
    361     // XXX: push a SHA-1 checksum writer (and change s2k to 254). 
    362     skey_checksum_arg_t *arg=ops_mallocz(sizeof *arg); 
    363  
    364     // configure the arg 
    365     arg->hash_algorithm=skey->hash_algorithm; 
    366     arg->hashed=&skey->checkhash[0]; 
    367  
    368     // init the hash 
    369     ops_hash_any(&arg->hash, arg->hash_algorithm); 
    370     arg->hash.init(&arg->hash); 
    371  
    372     ops_writer_push(cinfo, skey_checksum_writer, skey_checksum_finaliser, skey_checksum_destroyer, arg); 
    373     } 
    374   
    375216/* Note that we support v3 keys here because they're needed for 
    376217 * for verification - the writer doesn't allow them, though */ 
     
    871712    { return ops_mallocz(sizeof(ops_create_info_t)); } 
    872713 
    873 /* Note that we finalise from the top down, so we don't use writers below 
    874  * that have already been finalised 
    875  */ 
    876 static ops_boolean_t writer_info_finalise(ops_error_t **errors, 
    877                                           ops_writer_info_t *winfo) 
    878     { 
    879     ops_boolean_t ret=ops_true; 
    880  
    881     if(winfo->finaliser) 
    882         { 
    883         ret=winfo->finaliser(errors,winfo); 
    884         winfo->finaliser=NULL; 
    885         } 
    886     if(winfo->next && !writer_info_finalise(errors,winfo->next)) 
    887         { 
    888         winfo->finaliser=NULL; 
    889         return ops_false; 
    890         } 
    891     return ret; 
    892     } 
    893  
    894 static void writer_info_delete(ops_writer_info_t *winfo) 
    895     { 
    896     // we should have finalised before deleting 
    897     assert(!winfo->finaliser); 
    898     if(winfo->next) 
    899         { 
    900         writer_info_delete(winfo->next); 
    901         free(winfo->next); 
    902         winfo->next=NULL; 
    903         } 
    904     if(winfo->destroyer) 
    905         { 
    906         winfo->destroyer(winfo); 
    907         winfo->destroyer=NULL; 
    908         } 
    909     winfo->writer=NULL; 
    910     } 
    911  
    912714/** 
    913715 * \ingroup Create 
     
    923725    free(info); 
    924726    } 
    925  
    926 typedef struct 
    927     { 
    928     int fd; 
    929     } writer_fd_arg_t; 
    930  
    931 static ops_boolean_t fd_writer(const unsigned char *src,unsigned length, 
    932                                ops_error_t **errors, 
    933                                ops_writer_info_t *winfo) 
    934     { 
    935     writer_fd_arg_t *arg=ops_writer_get_arg(winfo); 
    936     int n=write(arg->fd,src,length); 
    937  
    938     if(n == -1) 
    939         { 
    940         OPS_SYSTEM_ERROR_1(errors,OPS_E_W_WRITE_FAILED,"write", 
    941                            "file descriptor %d",arg->fd); 
    942         return ops_false; 
    943         } 
    944  
    945     if((unsigned)n != length) 
    946         { 
    947         OPS_ERROR_1(errors,OPS_E_W_WRITE_TOO_SHORT, 
    948                     "file descriptor %d",arg->fd); 
    949         return ops_false; 
    950         } 
    951  
    952     return ops_true; 
    953     } 
    954  
    955 static void fd_destroyer(ops_writer_info_t *winfo) 
    956     { 
    957     free(ops_writer_get_arg(winfo)); 
    958     } 
    959  
    960 /** 
    961  * \ingroup Create 
    962  * 
    963  * Set the writer in info to be a stock writer that writes to a file 
    964  * descriptor. If another writer has already been set, then that is 
    965  * first destroyed. 
    966  *  
    967  * \param info The info structure 
    968  * \param fd The file descriptor 
    969  * 
    970  */ 
    971  
    972 void ops_writer_set_fd(ops_create_info_t *info,int fd) 
    973     { 
    974     writer_fd_arg_t *arg=malloc(sizeof *arg); 
    975  
    976     arg->fd=fd; 
    977     ops_writer_set(info,fd_writer,NULL,fd_destroyer,arg); 
    978     } 
    979  
    980 /** 
    981  * \ingroup Create 
    982  * 
    983  * Set a writer in info. There should not be another writer set. 
    984  * 
    985  * \param info The info structure 
    986  * \param writer The writer 
    987  * \param destroyer The destroyer 
    988  * \param arg The argument for the writer and destroyer 
    989  */ 
    990 void ops_writer_set(ops_create_info_t *info, 
    991                     ops_writer_t *writer, 
    992                     ops_writer_finaliser_t *finaliser, 
    993                     ops_writer_destroyer_t *destroyer, 
    994                     void *arg) 
    995     { 
    996     assert(!info->winfo.writer); 
    997     info->winfo.writer=writer; 
    998     info->winfo.finaliser=finaliser; 
    999     info->winfo.destroyer=destroyer; 
    1000     info->winfo.arg=arg; 
    1001     } 
    1002  
    1003 /** 
    1004  * \ingroup Create 
    1005  * 
    1006  * Push a writer in info. There must already be another writer set. 
    1007  * 
    1008  * \param info The info structure 
    1009  * \param writer The writer 
    1010  * \param destroyer The destroyer 
    1011  * \param arg The argument for the writer and destroyer 
    1012  */ 
    1013 void ops_writer_push(ops_create_info_t *info, 
    1014                      ops_writer_t *writer, 
    1015                      ops_writer_finaliser_t *finaliser, 
    1016                      ops_writer_destroyer_t *destroyer, 
    1017                      void *arg) 
    1018     { 
    1019     ops_writer_info_t *copy=ops_mallocz(sizeof *copy); 
    1020  
    1021     assert(info->winfo.writer); 
    1022     *copy=info->winfo; 
    1023     info->winfo.next=copy; 
    1024  
    1025     info->winfo.writer=writer; 
    1026     info->winfo.finaliser=finaliser; 
    1027     info->winfo.destroyer=destroyer; 
    1028     info->winfo.arg=arg; 
    1029     } 
    1030  
    1031 void ops_writer_pop(ops_create_info_t *info) 
    1032     {  
    1033     ops_writer_info_t *next; 
    1034  
    1035     // Make sure the finaliser has been called. 
    1036     assert(!info->winfo.finaliser); 
    1037     // Make sure this is a stacked writer 
    1038     assert(info->winfo.next); 
    1039     if(info->winfo.destroyer) 
    1040         info->winfo.destroyer(&info->winfo); 
    1041  
    1042     next=info->winfo.next; 
    1043     info->winfo=*next; 
    1044  
    1045     free(next); 
    1046     } 
    1047  
    1048 /** 
    1049  * \ingroup Create 
    1050  * 
    1051  * Close the writer currently set in info. 
    1052  * 
    1053  * \param info The info structure 
    1054  */ 
    1055 ops_boolean_t ops_writer_close(ops_create_info_t *info) 
    1056     { 
    1057     ops_boolean_t ret=writer_info_finalise(&info->errors,&info->winfo); 
    1058  
    1059     writer_info_delete(&info->winfo); 
    1060  
    1061     return ret; 
    1062     } 
    1063  
    1064 /** 
    1065  * \ingroup Create 
    1066  * 
    1067  * Get the arg supplied to ops_create_info_set_writer(). 
    1068  * 
    1069  * \param winfo The writer_info structure 
    1070  * \return The arg 
    1071  */ 
    1072 void *ops_writer_get_arg(ops_writer_info_t *winfo) 
    1073     { return winfo->arg; } 
    1074  
    1075 /** 
    1076  * \ingroup Create 
    1077  * 
    1078  * Write to the next writer down in the stack. 
    1079  * 
    1080  * \param src The data to write. 
    1081  * \param length The length of src. 
    1082  * \param flags The writer flags. 
    1083  * \param errors A place to store errors. 
    1084  * \param info The writer_info structure. 
    1085  * \return Success - if ops_false, then errors should contain the error. 
    1086  */ 
    1087 ops_boolean_t ops_stacked_write(const void *src,unsigned length, 
    1088                                 ops_error_t **errors,ops_writer_info_t *winfo) 
    1089     { 
    1090     return winfo->next->writer(src,length,errors,winfo->next); 
    1091     } 
    1092  
    1093 /** 
    1094  * \ingroup Create 
    1095  * 
    1096  * Free the arg. Many writers just have a malloc()ed lump of storage, this 
    1097  * function releases it. 
    1098  * 
    1099  * \param winfo the info structure. 
    1100  */ 
    1101 void ops_writer_generic_destroyer(ops_writer_info_t *winfo) 
    1102     { free(ops_writer_get_arg(winfo)); } 
    1103  
    1104 /** 
    1105  * \ingroup Create 
    1106  * 
    1107  * A writer that just writes to the next one down. Useful for when you 
    1108  * want to insert just a finaliser into the stack. 
    1109  */ 
    1110 ops_boolean_t ops_writer_passthrough(const unsigned char *src, 
    1111                                      unsigned length, 
    1112                                      ops_error_t **errors, 
    1113                                      ops_writer_info_t *winfo) 
    1114     { return ops_stacked_write(src,length,errors,winfo); } 
    1115  
    1116727 
    1117728ops_boolean_t ops_calc_session_key_checksum(ops_pk_session_key_t *session_key, unsigned char *cs) 
     
    1310921    } 
    1311922 
    1312 // RENAMED from ops_boolean_t ops_write_literal_data(const unsigned char *data,  
    1313923ops_boolean_t ops_write_literal_data_from_buf(const unsigned char *data,  
    1314924                                     const int maxlen,  
     
    14791089    } 
    14801090 
    1481  
    1482  
    14831091// EOF 
  • openpgpsdk/trunk/src/lib/crypto.c

    r571 r574  
    2222#include <openpgpsdk/crypto.h> 
    2323#include <openpgpsdk/random.h> 
     24#include <openpgpsdk/readerwriter.h> 
     25#include <openpgpsdk/writer_armoured.h> 
     26#include "parse_local.h" 
    2427 
    2528#include <assert.h> 
    2629#include <string.h> 
     30#include <fcntl.h> 
    2731 
    2832#include <openpgpsdk/final.h> 
     
    130134    return ops_true; 
    131135    } 
     136 
     137#define MAXBUF 1024 
     138 
     139static ops_parse_cb_return_t 
     140callback_write_parsed(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo); 
     141 
     142ops_boolean_t ops_encrypt_file(const char* input_filename, const char* output_filename, const ops_keydata_t *pub_key, const ops_boolean_t use_armour, const ops_boolean_t allow_overwrite) 
     143    { 
     144    int fd_in=0; 
     145    int fd_out=0; 
     146 
     147    ops_create_info_t *cinfo; 
     148 
     149#ifdef WIN32 
     150    fd_in=open(input_filename,O_RDONLY | O_BINARY); 
     151#else 
     152    fd_in=open(input_filename,O_RDONLY); 
     153#endif 
     154    if(fd_in < 0) 
     155        { 
     156        perror(input_filename); 
     157        return ops_false; 
     158        } 
     159     
     160    fd_out=ops_setup_file_write(&cinfo, output_filename, allow_overwrite); 
     161    if (fd_out < 0) 
     162        return ops_false; 
     163 
     164    // set armoured/not armoured here 
     165    if (use_armour) 
     166        ops_writer_push_armoured_message(cinfo); 
     167 
     168    // Push the encrypted writer 
     169    ops_writer_push_encrypt_se_ip(cinfo,pub_key); 
     170 
     171    // Do the writing 
     172 
     173    unsigned char* buf=NULL; 
     174    size_t bufsz=16; 
     175    int done=0; 
     176    for (;;) 
     177        { 
     178        buf=realloc(buf,done+bufsz); 
     179         
     180            int n=0; 
     181 
     182            n=read(fd_in,buf+done,bufsz); 
     183            if (!n) 
     184                    break; 
     185            assert(n>=0); 
     186        done+=n; 
     187        } 
     188 
     189    // This does the writing 
     190    ops_write(buf,done,cinfo); 
     191 
     192    // tidy up 
     193    close(fd_in); 
     194    free(buf); 
     195    ops_teardown_file_write(cinfo,fd_out); 
     196 
     197    return ops_true; 
     198    } 
     199 
     200/*  
     201the output filename can either be given explicitly, or if NULL, 
     202it will be derived from the input filename following GPG conventions. 
     203That is, we assume a binary encrypted file will be called <origfile>.gpg 
     204and an armoured file will be called <origfile.asc> 
     205If neither is true, then we add a .decrypted suffix. 
     206*/ 
     207 
     208ops_boolean_t ops_decrypt_file(const char* input_filename, const char* output_filename, ops_keyring_t* keyring, const ops_boolean_t use_armour, const ops_boolean_t allow_overwrite, ops_parse_cb_t* cb_get_passphrase) 
     209    { 
     210    int fd_in=0; 
     211    int fd_out=0; 
     212    char* myfilename=NULL; 
     213 
     214    // 
     215    ops_parse_info_t *pinfo=NULL; 
     216 
     217    // setup for reading from given input file 
     218    fd_in=ops_setup_file_read(&pinfo, input_filename,  
     219                        NULL, 
     220                        callback_write_parsed, 
     221                        ops_false); 
     222    if (fd_in < 0) 
     223        { 
     224        perror(input_filename); 
     225        return ops_false; 
     226        } 
     227 
     228    // setup output filename 
     229 
     230    if (output_filename) 
     231        { 
     232        fd_out=ops_setup_file_write(&pinfo->cbinfo.cinfo, output_filename, allow_overwrite); 
     233 
     234        if (fd_out < 0) 
     235            {  
     236            perror(output_filename);  
     237            ops_teardown_file_read(pinfo,fd_in); 
     238            return ops_false; 
     239            } 
     240        } 
     241    else 
     242        { 
     243        int suffixlen=4; 
     244        char *defaultsuffix=".decrypted"; 
     245        const char *suffix=input_filename+strlen((char *)input_filename)-suffixlen; 
     246        if (!strcmp(suffix,".gpg") || !strcmp(suffix,".asc")) 
     247            { 
     248            myfilename=ops_mallocz(strlen(input_filename)-suffixlen+1); 
     249            strncpy(myfilename,input_filename,strlen(input_filename)-suffixlen); 
     250            } 
     251        else 
     252            { 
     253            myfilename=ops_mallocz(strlen(input_filename)+strlen(defaultsuffix)+1); 
     254            sprintf(myfilename,"%s%s",input_filename,defaultsuffix); 
     255            } 
     256 
     257        fd_out=ops_setup_file_write(&pinfo->cbinfo.cinfo, myfilename, allow_overwrite); 
     258         
     259        if (fd_out < 0) 
     260            {  
     261            perror(myfilename);  
     262            free(myfilename); 
     263            ops_teardown_file_read(pinfo,fd_in); 
     264            return ops_false; 
     265            } 
     266 
     267        free (myfilename); 
     268        } 
     269 
     270    // \todo check for suffix matching armour param 
     271 
     272    // setup for writing decrypted contents to given output file 
     273 
     274    // setup keyring and passphrase callback 
     275    pinfo->cbinfo.cryptinfo.keyring=keyring; 
     276    pinfo->cbinfo.cryptinfo.cb_get_passphrase=cb_get_passphrase; 
     277 
     278    // Set up armour/passphrase options 
     279 
     280    if (use_armour) 
     281        ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_false); 
     282     
     283    // Do it 
     284 
     285    ops_parse_and_print_errors(pinfo); 
     286 
     287    // Unsetup 
     288 
     289    if (use_armour) 
     290        ops_reader_pop_dearmour(pinfo); 
     291 
     292    ops_teardown_file_write(pinfo->cbinfo.cinfo, fd_out); 
     293    ops_teardown_file_read(pinfo, fd_in); 
     294    // \todo cleardown crypt 
     295 
     296    return ops_true; 
     297    } 
     298 
     299static ops_parse_cb_return_t 
     300callback_write_parsed(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) 
     301    { 
     302    ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content; 
     303    static ops_boolean_t skipping; 
     304    //    ops_boolean_t write=ops_true; 
     305 
     306    OPS_USED(cbinfo); 
     307 
     308//    ops_print_packet(content_); 
     309 
     310    if(content_->tag != OPS_PTAG_CT_UNARMOURED_TEXT && skipping) 
     311        { 
     312        puts("...end of skip"); 
     313        skipping=ops_false; 
     314        } 
     315 
     316    switch(content_->tag) 
     317        { 
     318    case OPS_PTAG_CT_UNARMOURED_TEXT: 
     319        printf("OPS_PTAG_CT_UNARMOURED_TEXT\n"); 
     320        if(!skipping) 
     321            { 
     322            puts("Skipping..."); 
     323            skipping=ops_true; 
     324            } 
     325        fwrite(content->unarmoured_text.data,1, 
     326               content->unarmoured_text.length,stdout); 
     327        break; 
     328 
     329    case OPS_PTAG_CT_PK_SESSION_KEY: 
     330        return callback_pk_session_key(content_,cbinfo); 
     331        break; 
     332 
     333    case OPS_PARSER_CMD_GET_SECRET_KEY: 
     334        return callback_cmd_get_secret_key(content_,cbinfo); 
     335        break; 
     336 
     337    case OPS_PARSER_CMD_GET_SK_PASSPHRASE: 
     338        //        return callback_cmd_get_secret_key_passphrase(content_,cbinfo); 
     339        return cbinfo->cryptinfo.cb_get_passphrase(content_,cbinfo); 
     340        break; 
     341 
     342    case OPS_PTAG_CT_LITERAL_DATA_BODY: 
     343        return callback_literal_data(content_,cbinfo); 
     344                break; 
     345 
     346    case OPS_PTAG_CT_ARMOUR_HEADER: 
     347    case OPS_PTAG_CT_ARMOUR_TRAILER: 
     348    case OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY: 
     349    case OPS_PTAG_CT_COMPRESSED: 
     350    case OPS_PTAG_CT_LITERAL_DATA_HEADER: 
     351    case OPS_PTAG_CT_SE_IP_DATA_BODY: 
     352    case OPS_PTAG_CT_SE_IP_DATA_HEADER: 
     353    case OPS_PTAG_CT_SE_DATA_BODY: 
     354    case OPS_PTAG_CT_SE_DATA_HEADER: 
     355 
     356        // Ignore these packets  
     357        // They're handled in ops_parse_one_packet() 
     358        // and nothing else needs to be done 
     359        break; 
     360 
     361    default: 
     362        //        return callback_general(content_,cbinfo); 
     363        break; 
     364        //      fprintf(stderr,"Unexpected packet tag=%d (0x%x)\n",content_->tag, 
     365        //              content_->tag); 
     366        //      assert(0); 
     367        } 
     368 
     369    return OPS_RELEASE_MEMORY; 
     370    } 
     371 
     372// EOF 
  • openpgpsdk/trunk/src/lib/errors.c

    r571 r574  
    3838#include <openpgpsdk/final.h> 
    3939 
    40 #define ERR(code)     { code, #code } 
     40#define ERRNAME(code) { code, #code } 
    4141 
    4242static ops_errcode_name_map_t errcode_name_map[] =  
    4343    { 
    44     { OPS_E_OK, "OPS_E_OK" }
    45     { OPS_E_FAIL, "OPS_E_FAIL" }
    46     { OPS_E_SYSTEM_ERROR, "OPS_E_SYSTEM_ERROR" }
    47     { OPS_E_UNIMPLEMENTED, "OPS_E_UNIMPLEMENTED" }
     44    ERRNAME(OPS_E_OK)
     45    ERRNAME(OPS_E_FAIL)
     46    ERRNAME(OPS_E_SYSTEM_ERROR)
     47    ERRNAME(OPS_E_UNIMPLEMENTED)
    4848 
    49     { OPS_E_R, "OPS_E_R" }
    50     { OPS_E_R_READ_FAILED, "OPS_E_R_READ_FAILED" }
    51     { OPS_E_R_EARLY_EOF, "OPS_E_R_EARLY_EOF" }
    52     { OPS_E_R_BAD_FORMAT, "OPS_E_R_BAD_FORMAT" }
    53     { OPS_E_R_UNCONSUMED_DATA, "OPS_E_R_UNCONSUMED_DATA" }
     49    ERRNAME(OPS_E_R)
     50    ERRNAME(OPS_E_R_READ_FAILED)
     51    ERRNAME(OPS_E_R_EARLY_EOF)
     52    ERRNAME(OPS_E_R_BAD_FORMAT)
     53    ERRNAME(OPS_E_R_UNCONSUMED_DATA)
    5454 
    55     { OPS_E_W, "OPS_E_W" }
    56     { OPS_E_W_WRITE_FAILED, "OPS_E_W_WRITE_FAILED" }
    57     { OPS_E_W_WRITE_TOO_SHORT, "OPS_E_W_WRITE_TOO_SHORT" }
     55    ERRNAME(OPS_E_W)
     56    ERRNAME(OPS_E_W_WRITE_FAILED)
     57    ERRNAME(OPS_E_W_WRITE_TOO_SHORT)
    5858 
    59     { OPS_E_P, "OPS_E_P" }
    60     { OPS_E_P_NOT_ENOUGH_DATA, "OPS_E_P_NOT_ENOUGH_DATA" }
    61     { OPS_E_P_UNKNOWN_TAG,"OPS_E_P_UNKNOWN_TAG" }
    62     { OPS_E_P_PACKET_CONSUMED,"OPS_E_P_PACKET_CONSUMED" }
    63     ERR(OPS_E_P_MPI_FORMAT_ERROR), 
     59    ERRNAME(OPS_E_P)
     60    ERRNAME(OPS_E_P_NOT_ENOUGH_DATA)
     61    ERRNAME(OPS_E_P_UNKNOWN_TAG)
     62    ERRNAME(OPS_E_P_PACKET_CONSUMED)
     63    ERRNAME(OPS_E_P_MPI_FORMAT_ERROR), 
    6464 
    65     { OPS_E_C, "OPS_E_C" }
     65    ERRNAME(OPS_E_C)
    6666 
    67     ERR(OPS_E_V), 
    68     ERR(OPS_E_V_BAD_SIGNATURE), 
    69     ERR(OPS_E_V_UNKNOWN_SIGNER), 
     67    ERRNAME(OPS_E_V), 
     68    ERRNAME(OPS_E_V_BAD_SIGNATURE), 
     69    ERRNAME(OPS_E_V_UNKNOWN_SIGNER), 
    7070 
    71     ERR(OPS_E_ALG), 
    72     ERR(OPS_E_ALG_UNSUPPORTED_SYMMETRIC_ALG), 
    73     ERR(OPS_E_ALG_UNSUPPORTED_PUBLIC_KEY_ALG), 
    74     ERR(OPS_E_ALG_UNSUPPORTED_SIGNATURE_ALG), 
    75     ERR(OPS_E_ALG_UNSUPPORTED_HASH_ALG), 
     71    ERRNAME(OPS_E_ALG), 
     72    ERRNAME(OPS_E_ALG_UNSUPPORTED_SYMMETRIC_ALG), 
     73    ERRNAME(OPS_E_ALG_UNSUPPORTED_PUBLIC_KEY_ALG), 
     74    ERRNAME(OPS_E_ALG_UNSUPPORTED_SIGNATURE_ALG), 
     75    ERRNAME(OPS_E_ALG_UNSUPPORTED_HASH_ALG), 
    7676 
    77     ERR(OPS_E_PROTO), 
    78     ERR(OPS_E_PROTO_BAD_SYMMETRIC_DECRYPT), 
    79     ERR(OPS_E_PROTO_UNKNOWN_SS), 
    80     ERR(OPS_E_PROTO_CRITICAL_SS_IGNORED), 
    81     ERR(OPS_E_PROTO_BAD_PUBLIC_KEY_VRSN), 
    82     ERR(OPS_E_PROTO_BAD_SIGNATURE_VRSN), 
    83     ERR(OPS_E_PROTO_BAD_ONE_PASS_SIG_VRSN), 
    84     ERR(OPS_E_PROTO_BAD_PKSK_VRSN), 
    85     ERR(OPS_E_PROTO_DECRYPTED_MSG_WRONG_LEN), 
    86     ERR(OPS_E_PROTO_BAD_SK_CHECKSUM), 
    87  
     77    ERRNAME(OPS_E_PROTO), 
     78    ERRNAME(OPS_E_PROTO_BAD_SYMMETRIC_DECRYPT), 
     79    ERRNAME(OPS_E_PROTO_UNKNOWN_SS), 
     80    ERRNAME(OPS_E_PROTO_CRITICAL_SS_IGNORED), 
     81    ERRNAME(OPS_E_PROTO_BAD_PUBLIC_KEY_VRSN), 
     82    ERRNAME(OPS_E_PROTO_BAD_SIGNATURE_VRSN), 
     83    ERRNAME(OPS_E_PROTO_BAD_ONE_PASS_SIG_VRSN), 
     84    ERRNAME(OPS_E_PROTO_BAD_PKSK_VRSN), 
     85    ERRNAME(OPS_E_PROTO_DECRYPTED_MSG_WRONG_LEN), 
     86    ERRNAME(OPS_E_PROTO_BAD_SK_CHECKSUM), 
    8887 
    8988    { (int) NULL,               (char *)NULL }, /* this is the end-of-array marker */ 
     
    175174    } 
    176175} 
     176 
     177// EOF 
  • openpgpsdk/trunk/src/lib/fingerprint.c

    r573 r574  
    141141        } 
    142142    } 
     143 
     144// EOF 
  • openpgpsdk/trunk/src/lib/hash.c

    r571 r574  
    9595    } 
    9696 
    97 static int hash_reader(void *dest,size_t length,ops_error_t **errors, 
    98                        ops_reader_info_t *rinfo,ops_parse_cb_info_t *cbinfo) 
     97void ops_calc_mdc_hash(const unsigned char* preamble, const size_t sz_preamble, const unsigned char* plaintext, const unsigned int sz_plaintext, unsigned char *hashed) 
    9998    { 
    100     ops_hash_t *hash=ops_reader_get_arg(rinfo); 
    101     int r=ops_stacked_read(dest,length,errors,rinfo,cbinfo); 
     99    int debug=0; 
     100    ops_hash_t hash; 
     101    unsigned char c[1]; 
    102102 
    103     if(r <= 0
    104         return r; 
    105          
    106     hash->add(hash,dest,r); 
     103    if (debug
     104        { 
     105        unsigned int i=0; 
     106        fprintf(stderr,"ops_calc_mdc_hash():\n"); 
    107107 
    108     return r; 
     108        fprintf(stderr,"\npreamble: "); 
     109        for (i=0; i<sz_preamble;i++) 
     110            fprintf(stderr," 0x%02x", preamble[i]); 
     111        fprintf(stderr,"\n"); 
     112 
     113        fprintf(stderr,"\nplaintext (len=%d): ",sz_plaintext); 
     114        for (i=0; i<sz_plaintext;i++) 
     115            fprintf(stderr," 0x%02x", plaintext[i]); 
     116        fprintf(stderr,"\n"); 
     117        } 
     118 
     119    // init 
     120    ops_hash_any(&hash, OPS_HASH_SHA1); 
     121    hash.init(&hash); 
     122 
     123    // preamble 
     124    hash.add(&hash,preamble,sz_preamble); 
     125    // plaintext 
     126    hash.add(&hash,plaintext,sz_plaintext);  
     127    // MDC packet tag 
     128    c[0]=0xD3; 
     129    hash.add(&hash,&c[0],1);    
     130    // MDC packet len 
     131    c[0]=0x14; 
     132    hash.add(&hash,&c[0],1);    
     133 
     134    //finish 
     135    hash.finish(&hash,hashed); 
     136 
     137    if (debug) 
     138        { 
     139        unsigned int i=0; 
     140        fprintf(stderr,"\nhashed (len=%d): ",SHA_DIGEST_LENGTH); 
     141        for (i=0; i<SHA_DIGEST_LENGTH;i++) 
     142            fprintf(stderr," 0x%02x", hashed[i]); 
     143        fprintf(stderr,"\n"); 
     144        } 
    109145    } 
    110146 
    111 void ops_reader_push_hash(ops_parse_info_t *pinfo,ops_hash_t *hash) 
    112     { 
    113     hash->init(hash); 
    114     ops_reader_push(pinfo,hash_reader,NULL,hash); 
    115     } 
    116  
    117 void ops_reader_pop_hash(ops_parse_info_t *pinfo) 
    118     { ops_reader_pop(pinfo); } 
     147// EOF 
  • openpgpsdk/trunk/src/lib/keyring.c

    r571 r574  
    2323 */ 
    2424 
     25#include <openpgpsdk/armour.h> 
     26#include <openpgpsdk/std_print.h> 
    2527#include <openpgpsdk/keyring.h> 
    2628#include <openpgpsdk/packet-parse.h> 
     
    9799    } 
    98100 
    99 static void echo_off() 
    100     { 
    101 #ifndef WIN32 
    102     struct termios term; 
    103     int r; 
    104  
    105     r=tcgetattr(0,&term); 
    106     if(r < 0 && errno == ENOTTY) 
    107         return; 
    108     assert(r >= 0); 
    109  
    110     term.c_lflag &= ~ECHO; 
    111  
    112     r=tcsetattr(0,TCSANOW,&term); 
    113     assert(r >= 0); 
    114 #endif 
    115     } 
    116          
    117 static void echo_on() 
    118     { 
    119 #ifndef WIN32 
    120     struct termios term; 
    121     int r; 
    122  
    123     r=tcgetattr(0,&term); 
    124     if(r < 0 && errno == ENOTTY) 
    125         return; 
    126     assert(r >= 0); 
    127  
    128     term.c_lflag |= ECHO; 
    129  
    130     r=tcsetattr(0,TCSANOW,&term); 
    131     assert(r >= 0); 
    132 #endif 
    133     } 
    134  
    135 char *ops_malloc_passphrase(char *pp) 
    136     { 
    137     char *passphrase; 
    138     size_t n; 
    139  
    140     n=strlen(pp); 
    141     passphrase=malloc(n+1); 
    142     strcpy(passphrase,pp); 
    143  
    144     return passphrase; 
    145     } 
    146  
    147 char *ops_get_passphrase(void) 
    148     { 
    149     char buffer[1024]; 
    150     size_t n; 
    151  
    152     printf("Passphrase: "); 
    153      
    154     echo_off(); 
    155     fgets(buffer,sizeof buffer,stdin); 
    156     echo_on(); 
    157  
    158     putchar('\n'); 
    159  
    160     n=strlen(buffer); 
    161     if(n && buffer[n-1] == '\n') 
    162         buffer[--n]='\0'; 
    163     return ops_malloc_passphrase(buffer); 
    164     } 
    165  
    166101typedef struct 
    167102    { 
     
    449384    } 
    450385 
     386/*! \file 
     387  \brief Standard API keyring functions 
     388 
     389*/ 
     390 
     391/** @defgroup StdKeyring Keyring 
     392    \ingroup StandardAPI 
     393 
     394    @defgroup StdKeyringFile Keyring File Operations 
     395    \ingroup StdKeyring 
     396    \brief Keyring Open/Read/Write/Close 
     397 
     398    Example Usage: 
     399    \code 
     400 
     401    // definition of variables 
     402    ops_keyring_t keyring; 
     403    char* filename="~/.gnupg/pubring.gpg"; 
     404 
     405    // Read keyring from file 
     406    ops_keyring_read_from_file(&keyring,filename); 
     407 
     408    // do actions using keyring    
     409    ...  
     410 
     411    // Free memory alloc-ed in ops_keyring_read_from_file() 
     412    ops_keyring_free(); 
     413    \endcode 
     414*/ 
     415 
     416/** 
     417    @defgroup StdKeyringFind Keyfind Find Operations 
     418    \ingroup StdKeyring 
     419    Find Key or its info within keyring 
     420 
     421    Example Usage: 
     422    \code 
     423 
     424    // definition of variables 
     425    ops_keyring_t keyring; 
     426    unsigned char* keyid; 
     427    ops_key_data_t *key; 
     428 
     429    // Read keyring from file 
     430    ops_keyring_read_from_file(&keyring,"~/.gnupg/pubring.gpg"); 
     431 
     432    // Search for keys 
     433 
     434    // - get Key ID from given userid 
     435    keyid=ops_keyring_find_keyid_by_userid (keyring, "user@domain.com") 
     436 
     437    // - now get key from Key ID 
     438    key=ops_keyring_find_key_by_id(keyring, keyid); 
     439 
     440    // do something with key 
     441    ... 
     442     
     443    // Free memory alloc-ed in ops_keyring_read_from_file() 
     444    ops_keyring_free(); 
     445    \endcode 
     446 */ 
     447 
     448static ops_parse_cb_return_t 
     449cb_keyring_read(const ops_parser_content_t *content_, 
     450                ops_parse_cb_info_t *cbinfo); 
     451 
     452/** 
     453   \ingroup StdKeyringFile 
     454    
     455   Reads a keyring from a file 
     456    
     457   \param keyring Ptr to existing keyring 
     458   \param file Filename of keyring 
     459    
     460   \note Keyring struct must already exist. 
     461 
     462   \note Can be used with either a public or secret keyring. 
     463 
     464   \note You must call ops_keyring_free() after usage to free alloc-ed memory. 
     465 
     466   \note If you call this twice on the same keyring struct, without calling 
     467   ops_keyring_free() between these calls, you will introduce a memory leak. 
     468*/ 
     469ops_boolean_t ops_keyring_read_from_file(ops_keyring_t *keyring, const ops_boolean_t armour, const char *filename) 
     470    { 
     471    ops_parse_info_t *pinfo; 
     472    int fd; 
     473    ops_boolean_t res = ops_true; 
     474 
     475    //memset(keyring,'\0',sizeof *keyring); 
     476 
     477    pinfo=ops_parse_info_new(); 
     478 
     479    // add this for the moment, 
     480    // \todo need to fix the problems with reading signature subpackets later 
     481 
     482    //    ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_RAW); 
     483    ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); 
     484 
     485#ifdef WIN32 
     486    fd=open(filename,O_RDONLY|O_BINARY); 
     487#else 
     488    fd=open(filename,O_RDONLY); 
     489#endif 
     490    if(fd < 0) 
     491        { 
     492        ops_parse_info_delete(pinfo); 
     493        perror(filename); 
     494        return ops_false; 
     495        } 
     496 
     497    ops_reader_set_fd(pinfo,fd); 
     498 
     499    ops_parse_cb_set(pinfo,cb_keyring_read,NULL); 
     500 
     501    if (armour) 
     502        { ops_reader_push_dearmour(pinfo, ops_false, ops_false, ops_false); } 
     503 
     504    if ( ops_parse_and_accumulate(keyring,pinfo) == 0 ) { 
     505        res = ops_false;  
     506    } 
     507    else 
     508        { 
     509        res = ops_true; 
     510        } 
     511    ops_print_errors(ops_parse_info_get_errors(pinfo)); 
     512 
     513    close(fd); 
     514 
     515    ops_parse_info_delete(pinfo); 
     516 
     517    return res; 
     518    } 
     519 
     520/** 
     521   \ingroup StdKeyring 
     522    
     523   Reads a keyring from memory 
     524    
     525   \param keyring Ptr to existing keyring 
     526   \param mem ptr to memory struct containing keyring info 
     527    
     528   \note Keyring struct must already exist. 
     529 
     530   \note Can be used with either a public or secret keyring. 
     531 
     532   \note You must call ops_keyring_free() after usage to free alloc-ed memory. 
     533 
     534   \note If you call this twice on the same keyring struct, without calling 
     535   ops_keyring_free() between these calls, you will introduce a memory leak. 
     536*/ 
     537ops_boolean_t ops_keyring_read_from_mem(ops_keyring_t *keyring, ops_memory_t* mem) 
     538    { 
     539    // \todo currently assuming this is an armoured key. 
     540 
     541    ops_parse_info_t *pinfo=NULL; 
     542    ops_boolean_t res = ops_true; 
     543 
     544    // \todo need to free memory first? 
     545    //memset(keyring,'\0',sizeof *keyring); 
     546 
     547    pinfo=ops_parse_info_new(); 
     548    ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); 
     549 
     550    ops_setup_memory_read(&pinfo, mem, NULL, cb_keyring_read); 
     551 
     552    ops_reader_push_dearmour(pinfo,ops_false,ops_false,ops_true); 
     553 
     554    if ( ops_parse_and_accumulate(keyring,pinfo) == 0 )  
     555        { 
     556        res = ops_false;  
     557        }  
     558    else  
     559        { 
     560        res = ops_true; 
     561        } 
     562    ops_print_errors(ops_parse_info_get_errors(pinfo)); 
     563 
     564    ops_reader_pop_dearmour(pinfo); 
     565 
     566    // don't call teardown_memory_read because memory was passed in 
     567    ops_parse_info_delete(pinfo); 
     568 
     569    return res; 
     570    } 
     571 
     572/** 
     573   \ingroup StdKeyringFile 
     574  
     575   Frees alloc-ed memory 
     576  
     577   \param keyring Keyring whose data is to be freed 
     578    
     579   \note This does not free keyring itself, just the memory alloc-ed in it. 
     580 */ 
     581void ops_keyring_free(ops_keyring_t *keyring) 
     582    { 
     583    free(keyring->keys); 
     584    keyring->keys=NULL; 
     585    keyring->nkeys=0; 
     586    keyring->nkeys_allocated=0; 
     587    } 
     588 
     589/** 
     590   \ingroup StdKeyringFind 
     591 
     592   Finds key in keyring from its Key ID 
     593 
     594   \param keyring Keyring to be searched 
     595   \param keyid ID of required key 
     596 
     597   \return Ptr to key, if found; NULL, if not found 
     598*/ 
     599const ops_keydata_t * 
     600ops_keyring_find_key_by_id(const ops_keyring_t *keyring, 
     601                           const unsigned char keyid[OPS_KEY_ID_SIZE]) 
     602    { 
     603    int n; 
     604 
     605    if (!keyring) 
     606        return NULL; 
     607 
     608    for(n=0 ; n < keyring->nkeys ; ++n) 
     609        { 
     610        if(!memcmp(keyring->keys[n].key_id,keyid,OPS_KEY_ID_SIZE)) 
     611            return &keyring->keys[n]; 
     612        } 
     613 
     614    return NULL; 
     615    } 
     616 
     617/** 
     618   \ingroup StdKeyringFind 
     619 
     620   Finds key from its User ID 
     621 
     622   \param keyring Keyring to be searched 
     623   \param userid User ID of required key 
     624 
     625   \return Ptr to Key, if found; NULL, if not found 
     626*/ 
     627const ops_keydata_t * 
     628ops_keyring_find_key_by_userid(const ops_keyring_t *keyring, 
     629                                 const char *userid) 
     630    { 
     631    int n=0; 
     632    unsigned int i=0; 
     633 
     634    if (!keyring) 
     635        return NULL; 
     636 
     637    for(n=0 ; n < keyring->nkeys ; ++n) 
     638        { 
     639        for(i=0; i<keyring->keys[n].nuids; i++) 
     640            { 
     641            //printf("[%d][%d] userid %s\n",n,i,keyring->keys[n].uids[i].user_id); 
     642            if(!strncmp((char *)keyring->keys[n].uids[i].user_id,userid,strlen(userid))) 
     643                return &keyring->keys[n]; 
     644            } 
     645        } 
     646 
     647    //printf("end: n=%d,i=%d\n",n,i); 
     648    return NULL; 
     649    } 
     650 
     651/** 
     652   \ingroup StdKeyringList 
     653 
     654   List keys in keyring 
     655 
     656   \param keyring Keyring to use 
     657   \param match optional string to match 
     658 
     659*/ 
     660 
     661void 
     662ops_keyring_list(const ops_keyring_t* keyring, 
     663                 const char* match) 
     664    { 
     665    int n; 
     666    unsigned int i; 
     667    ops_keydata_t* key; 
     668 
     669    printf ("%d keys\n", keyring->nkeys); 
     670    for(n=0,key=&keyring->keys[n] ; n < keyring->nkeys ; ++n,++key) 
     671        { 
     672        for(i=0; i<key->nuids; i++) 
     673            { 
     674            if (match) 
     675                printf ("*** match %s\n", match); 
     676            // if match, compare 
     677            //      if(!strcmp((char *)keyring->keys[n].uids[i].user_id,userid)) 
     678            //         return &keyring->keys[n].keyid[0]; 
     679            if (ops_key_is_secret(key)) 
     680                ops_print_secret_keydata(key); 
     681            else 
     682                ops_print_public_keydata(key); 
     683            } 
     684 
     685        } 
     686    } 
     687 
     688/* Static functions */ 
     689 
     690static ops_parse_cb_return_t 
     691cb_keyring_read(const ops_parser_content_t *content_, 
     692                ops_parse_cb_info_t *cbinfo) 
     693    { 
     694    OPS_USED(cbinfo); 
     695 
     696    switch(content_->tag) 
     697        { 
     698    case OPS_PARSER_PTAG: 
     699    case OPS_PTAG_CT_ENCRYPTED_SECRET_KEY: // we get these because we didn't prompt 
     700    case OPS_PTAG_CT_SIGNATURE_HEADER: 
     701    case OPS_PTAG_CT_SIGNATURE_FOOTER: 
     702    case OPS_PTAG_CT_SIGNATURE: 
     703    case OPS_PTAG_CT_TRUST: 
     704    case OPS_PARSER_ERRCODE: 
     705        break; 
     706 
     707    default: 
     708        ; 
     709        } 
     710 
     711    return OPS_RELEASE_MEMORY; 
     712    } 
     713 
     714/*\@}*/ 
     715 
    451716// eof 
  • openpgpsdk/trunk/src/lib/memory.c

    r571 r574  
    9696    } 
    9797 
    98 static ops_boolean_t memory_writer(const unsigned char *src,unsigned length, 
    99                                       ops_error_t **errors, 
    100                                       ops_writer_info_t *winfo) 
    101     { 
    102     ops_memory_t *mem=ops_writer_get_arg(winfo); 
    103  
    104     OPS_USED(errors); 
    105     ops_memory_add(mem,src,length); 
    106     return ops_true; 
    107     } 
    108  
    109 /** 
    110  * \ingroup Create 
    111  * 
    112  * Set a memory writer. Note that it is the caller's resposibility to 
    113  * release mem. 
    114  * 
    115  * \param info The info structure 
    116  * \param mem The memory structure */ 
    117  
    118 void ops_writer_set_memory(ops_create_info_t *info,ops_memory_t *mem) 
    119     { 
    120     ops_writer_set(info,memory_writer,NULL,NULL,mem); 
    121     } 
    122  
    12398void ops_memory_make_packet(ops_memory_t *out,ops_content_tag_t tag) 
    12499    { 
     
    170145void *ops_memory_get_data(ops_memory_t *mem) 
    171146    { return mem->buf; } 
     147 
     148// EOF 
  • openpgpsdk/trunk/src/lib/packet-parse.c

    r571 r574  
    2626#include <openssl/cast.h> 
    2727 
     28#include <openpgpsdk/callback.h> 
    2829#include <openpgpsdk/packet.h> 
    2930#include <openpgpsdk/packet-parse.h> 
     
    3536#include <openpgpsdk/packet-show.h> 
    3637#include <openpgpsdk/std_print.h> 
     38#include <openpgpsdk/create.h> 
     39#include <openpgpsdk/hash.h> 
    3740 
    3841#include "parse_local.h" 
     
    156159/*! \todo descr for CB macro */ 
    157160/*! \todo check other callback functions to check they match this usage */ 
    158 #define CB(cbinfo,t,pc) do { (pc)->tag=(t); if((cbinfo)->cb(pc,(cbinfo)) == OPS_RELEASE_MEMORY) ops_parser_content_free(pc); } while(0) 
    159 #define CBP(info,t,pc) CB(&(info)->cbinfo,t,pc) 
    160161/*! macro to save typing */ 
    161162#define C               content.content 
    162163 
     164// \todo replace ERRCODE with OPS_ERROR? 
    163165/*! set error code in content and run CallBack to handle error */ 
    164166#define ERRCODE(cbinfo,err)     do { C.errcode.errcode=err; CB(cbinfo,OPS_PARSER_ERRCODE,&content); } while(0) 
    165167#define ERRCODEP(pinfo,err)     do { C.errcode.errcode=err; CBP(pinfo,OPS_PARSER_ERRCODE,&content); } while(0) 
    166168/*! set error text in content and run CallBack to handle error, then return */ 
    167 #define ERR(cbinfo,err)       do { C.error.error=err; CB(cbinfo,OPS_PARSER_ERROR,&content); return ops_false; } while(0) 
    168 #define ERRP(info,err)        do { C.error.error=err; CBP(info,OPS_PARSER_ERROR,&content); return ops_false; } while(0) 
     169//#define ERR(cbinfo,err)     do { C.error.error=err; CB(cbinfo,OPS_PARSER_ERROR,&content); return ops_false; } while(0) 
     170//#define ERRP(info,err)      do { C.error.error=err; CBP(info,OPS_PARSER_ERROR,&content); return ops_false; } while(0) 
    169171/*! set error text in content and run CallBack to handle warning, do not return */ 
    170172#define WARN(warn)      do { C.error.error=warn; CB(OPS_PARSER_ERROR,&content);; } while(0) 
     
    31903192    { return ops_parse_cb(content,cbinfo->next); } 
    31913193 
    3192 /** 
    3193  * \brief 
    3194  * \param pinfo 
    3195  * \param reader 
    3196  * \param arg 
    3197  */ 
    3198 void ops_reader_set(ops_parse_info_t *pinfo,ops_reader_t *reader,ops_reader_destroyer_t *destroyer,void *arg) 
    3199     { 
    3200     pinfo->rinfo.reader=reader; 
    3201     pinfo->rinfo.destroyer=destroyer; 
    3202     pinfo->rinfo.arg=arg; 
    3203     } 
    3204  
    3205 /** 
    3206  * \brief  
    3207  * \param pinfo 
    3208  * \param reader 
    3209  * \param arg 
    3210  */ 
    3211 void ops_reader_push(ops_parse_info_t *pinfo,ops_reader_t *reader,ops_reader_destroyer_t *destroyer,void *arg) 
    3212     { 
    3213     ops_reader_info_t *rinfo=malloc(sizeof *rinfo); 
    3214  
    3215     *rinfo=pinfo->rinfo; 
    3216     memset(&pinfo->rinfo,'\0',sizeof pinfo->rinfo); 
    3217     pinfo->rinfo.next=rinfo; 
    3218     pinfo->rinfo.pinfo=pinfo; 
    3219  
    3220     // should copy accumulate flags from other reader? RW 
    3221     pinfo->rinfo.accumulate=rinfo->accumulate; 
    3222      
    3223     ops_reader_set(pinfo,reader,destroyer,arg); 
    3224     } 
    3225  
    3226 /** 
    3227  * \param pinfo 
    3228  */ 
    3229 void ops_reader_pop(ops_parse_info_t *pinfo) 
    3230     {  
    3231     ops_reader_info_t *next=pinfo->rinfo.next; 
    3232  
    3233     pinfo->rinfo=*next; 
    3234     free(next); 
    3235     } 
    3236  
    3237 void *ops_reader_get_arg(ops_reader_info_t *rinfo) 
    3238     { return rinfo->arg; } 
    3239  
    3240 void *ops_reader_get_arg_from_pinfo(ops_parse_info_t *pinfo) 
    3241     { return pinfo->rinfo.arg; } 
    3242  
    32433194ops_error_t *ops_parse_info_get_errors(ops_parse_info_t *pinfo) 
    32443195    { return pinfo->errors; } 
  • openpgpsdk/trunk/src/lib/packet-print.c

    r573 r574  
    3333#include "openpgpsdk/keyring.h" 
    3434#include "keyring_local.h" 
     35#include "parse_local.h" 
    3536#include "openpgpsdk/packet-show.h" 
    3637#include "openpgpsdk/util.h" 
    3738#include "openpgpsdk/std_print.h" 
     39#include "openpgpsdk/readerwriter.h" 
     40#include "openpgpsdk/armour.h" 
    3841 
    3942static int indent=0; 
     
    11891192 return 1; 
    11901193    } 
     1194 
     1195static ops_parse_cb_return_t cb_list_packets(const ops_parser_content_t * content_, ops_parse_cb_info_t *cbinfo) 
     1196    { 
     1197    const ops_parser_content_union_t *content=&content_->content; 
     1198    ops_text_t *text; 
     1199    const char *str; 
     1200    //    const ops_keydata_t *decrypter; 
     1201    //    const ops_secret_key_t *secret; 
     1202    static ops_boolean_t unarmoured; 
     1203 
     1204    OPS_USED(cbinfo); 
     1205 
     1206    if(unarmoured && content_->tag != OPS_PTAG_CT_UNARMOURED_TEXT) 
     1207        { 
     1208        unarmoured=ops_false; 
     1209        puts("UNARMOURED TEXT ends"); 
     1210        } 
     1211 
     1212    switch(content_->tag) 
     1213        { 
     1214    case OPS_PARSER_ERROR: 
     1215        printf("parse error: %s\n",content->error.error); 
     1216        break; 
     1217 
     1218    case OPS_PARSER_ERRCODE: 
     1219        printf("parse error: %s\n", 
     1220               ops_errcode(content->errcode.errcode)); 
     1221        break; 
     1222 
     1223    case OPS_PARSER_PACKET_END: 
     1224        print_packet_hex(&content->packet); 
     1225        break; 
     1226 
     1227    case OPS_PARSER_PTAG: 
     1228        if(content->ptag.content_tag == OPS_PTAG_CT_PUBLIC_KEY) 
     1229            { 
     1230            indent=0; 
     1231            printf("\n*** NEXT KEY ***\n"); 
     1232            } 
     1233 
     1234        printf("\n"); 
     1235        print_indent(); 
     1236        printf("==== ptag new_format=%d content_tag=%d length_type=%d" 
     1237               " length=0x%x (%d) position=0x%x (%d)\n",content->ptag.new_format, 
     1238               content->ptag.content_tag,content->ptag.length_type, 
     1239               content->ptag.length,content->ptag.length, 
     1240               content->ptag.position,content->ptag.position); 
     1241        print_tagname(ops_show_packet_tag(content->ptag.content_tag)); 
     1242        break; 
     1243 
     1244    case OPS_PTAG_CT_SE_DATA_HEADER: 
     1245        print_tagname("SYMMETRIC ENCRYPTED DATA"); 
     1246        break; 
     1247 
     1248    case OPS_PTAG_CT_SE_IP_DATA_HEADER: 
     1249        print_tagname("SYMMETRIC ENCRYPTED INTEGRITY PROTECTED DATA HEADER"); 
     1250        printf("Version: %d\n",content->se_ip_data_header.version); 
     1251        break; 
     1252 
     1253    case OPS_PTAG_CT_SE_IP_DATA_BODY: 
     1254        print_tagname("SYMMETRIC ENCRYPTED INTEGRITY PROTECTED DATA BODY"); 
     1255        printf("  data body length=%d\n", 
     1256               content->se_data_body.length); 
     1257        printf("    data="); 
     1258        hexdump(content->se_data_body.data, 
     1259                content->se_data_body.length); 
     1260        printf("\n"); 
     1261        break; 
     1262 
     1263    case OPS_PTAG_CT_PUBLIC_KEY: 
     1264    case OPS_PTAG_CT_PUBLIC_SUBKEY: 
     1265        if (content_->tag == OPS_PTAG_CT_PUBLIC_KEY) 
     1266            print_tagname("PUBLIC KEY"); 
     1267        else 
     1268            print_tagname("PUBLIC SUBKEY"); 
     1269 
     1270        ops_print_public_key(&content->public_key); 
     1271        break; 
     1272 
     1273    case OPS_PTAG_CT_TRUST: 
     1274        print_tagname("TRUST"); 
     1275        print_data("Trust",&content->trust.data); 
     1276        break; 
     1277         
     1278    case OPS_PTAG_CT_USER_ID: 
     1279        /* XXX: how do we print UTF-8? */ 
     1280        print_tagname("USER ID"); 
     1281        print_utf8_string("user_id",content->user_id.user_id); 
     1282        break; 
     1283 
     1284    case OPS_PTAG_CT_SIGNATURE: 
     1285        print_tagname("SIGNATURE"); 
     1286        print_indent(indent); 
     1287        print_unsigned_int("Signature Version", 
     1288               content->signature.version); 
     1289        if (content->signature.creation_time_set)  
     1290            print_time("Signature Creation Time", 
     1291                       content->signature.creation_time); 
     1292 
     1293        print_string_and_value("Signature Type", 
     1294                               ops_show_sig_type(content->signature.type), 
     1295                               content->signature.type); 
     1296 
     1297        if(content->signature.signer_id_set) 
     1298            print_hexdump_data("Signer ID", 
     1299                               content->signature.signer_id, 
     1300                               sizeof content->signature.signer_id); 
     1301 
     1302        print_string_and_value("Public Key Algorithm", 
     1303                               ops_show_pka(content->signature.key_algorithm), 
     1304                               content->signature.key_algorithm); 
     1305        print_string_and_value("Hash Algorithm", 
     1306                               ops_show_hash_algorithm(content->signature.hash_algorithm), 
     1307                               content->signature.hash_algorithm); 
     1308 
     1309        print_indent(); 
     1310        print_hexdump_data("hash2",&content->signature.hash2[0],2); 
     1311 
     1312        switch(content->signature.key_algorithm) 
     1313            { 
     1314        case OPS_PKA_RSA: 
     1315        case OPS_PKA_RSA_SIGN_ONLY: 
     1316            print_bn("sig",content->signature.signature.rsa.sig); 
     1317            break; 
     1318 
     1319        case OPS_PKA_DSA: 
     1320            print_bn("r",content->signature.signature.dsa.r); 
     1321            print_bn("s",content->signature.signature.dsa.s); 
     1322            break; 
     1323 
     1324        case OPS_PKA_ELGAMAL_ENCRYPT_OR_SIGN: 
     1325            print_bn("r",content->signature.signature.elgamal.r); 
     1326            print_bn("s",content->signature.signature.elgamal.s); 
     1327            break; 
     1328 
     1329        default: 
     1330            assert(0); 
     1331            } 
     1332 
     1333        if(content->signature.hash) 
     1334            printf("data hash is set\n"); 
     1335 
     1336        break; 
     1337 
     1338    case OPS_PTAG_CT_COMPRESSED: 
     1339        print_tagname("COMPRESSED"); 
     1340        print_unsigned_int("Compressed Data Type", content->compressed.type); 
     1341        break; 
     1342 
     1343    case OPS_PTAG_CT_ONE_PASS_SIGNATURE: 
     1344        print_tagname("ONE PASS SIGNATURE"); 
     1345 
     1346        print_unsigned_int("Version",content->one_pass_signature.version); 
     1347        print_string_and_value("Signature Type", 
     1348                               ops_show_sig_type(content->one_pass_signature.sig_type), 
     1349                               content->one_pass_signature.sig_type); 
     1350        print_string_and_value("Hash Algorithm", 
     1351                               ops_show_hash_algorithm(content->one_pass_signature.hash_algorithm), 
     1352                               content->one_pass_signature.hash_algorithm); 
     1353        print_string_and_value("Public Key Algorithm", 
     1354                               ops_show_pka(content->one_pass_signature.key_algorithm), 
     1355                               content->one_pass_signature.key_algorithm); 
     1356        print_hexdump_data("Signer ID", 
     1357                           content->one_pass_signature.keyid, 
     1358                           sizeof content->one_pass_signature.keyid); 
     1359 
     1360        print_unsigned_int("Nested", 
     1361                           content->one_pass_signature.nested); 
     1362        break; 
     1363 
     1364    case OPS_PTAG_CT_USER_ATTRIBUTE: 
     1365        print_tagname("USER ATTRIBUTE"); 
     1366        print_hexdump("User Attribute", 
     1367                      content->user_attribute.data.contents, 
     1368                      content->user_attribute.data.len); 
     1369        break; 
     1370 
     1371    case OPS_PTAG_RAW_SS: 
     1372        assert(!content_->critical); 
     1373        start_subpacket(content_->tag); 
     1374        print_unsigned_int("Raw Signature Subpacket: tag", 
     1375                           content->ss_raw.tag-OPS_PTAG_SIGNATURE_SUBPACKET_BASE); 
     1376        print_hexdump("Raw Data", 
     1377                      content->ss_raw.raw, 
     1378                      content->ss_raw.length); 
     1379        break; 
     1380 
     1381    case OPS_PTAG_SS_CREATION_TIME: 
     1382        start_subpacket(content_->tag); 
     1383        print_time("Signature Creation Time",content->ss_time.time); 
     1384        end_subpacket(); 
     1385        break; 
     1386 
     1387    case OPS_PTAG_SS_EXPIRATION_TIME: 
     1388        start_subpacket(content_->tag); 
     1389        print_duration("Signature Expiration Time",content->ss_time.time); 
     1390        end_subpacket(); 
     1391        break; 
     1392 
     1393    case OPS_PTAG_SS_KEY_EXPIRATION_TIME: 
     1394        start_subpacket(content_->tag); 
     1395        print_duration("Key Expiration Time", content->ss_time.time); 
     1396        end_subpacket(); 
     1397        break; 
     1398 
     1399    case OPS_PTAG_SS_TRUST: 
     1400        start_subpacket(content_->tag); 
     1401        print_string("Trust Signature",""); 
     1402        print_unsigned_int("Level", 
     1403                           content->ss_trust.level); 
     1404        print_unsigned_int("Amount", 
     1405                           content->ss_trust.amount); 
     1406        end_subpacket(); 
     1407        break; 
     1408                 
     1409    case OPS_PTAG_SS_REVOCABLE: 
     1410        start_subpacket(content_->tag); 
     1411        print_boolean("Revocable",content->ss_revocable.revocable); 
     1412        end_subpacket(); 
     1413        break;       
     1414 
     1415    case OPS_PTAG_SS_REVOCATION_KEY: 
     1416        start_subpacket(content_->tag); 
     1417        /* not yet tested */ 
     1418        printf ("  revocation key: class=0x%x", 
     1419                content->ss_revocation_key.class); 
     1420        if (content->ss_revocation_key.class&0x40) 
     1421            printf (" (sensitive)"); 
     1422        printf (", algid=0x%x", 
     1423                content->ss_revocation_key.algid); 
     1424        printf(", fingerprint="); 
     1425        hexdump(content->ss_revocation_key.fingerprint,20); 
     1426        printf("\n"); 
     1427        end_subpacket(); 
     1428        break; 
     1429     
     1430    case OPS_PTAG_SS_ISSUER_KEY_ID: 
     1431        start_subpacket(content_->tag); 
     1432        print_hexdump("Issuer Key Id", 
     1433                      &content->ss_issuer_key_id.key_id[0], 
     1434                      sizeof content->ss_issuer_key_id.key_id); 
     1435        end_subpacket(); 
     1436        break; 
     1437 
     1438    case OPS_PTAG_SS_PREFERRED_SKA: 
     1439        start_subpacket(content_->tag); 
     1440        print_data( "Preferred Symmetric Algorithms", 
     1441                   &content->ss_preferred_ska.data); 
     1442 
     1443        text = ops_showall_ss_preferred_ska(content->ss_preferred_ska); 
     1444        print_text_breakdown(text); 
     1445        ops_text_free(text); 
     1446 
     1447        end_subpacket(); 
     1448        break; 
     1449 
     1450    case OPS_PTAG_SS_PRIMARY_USER_ID: 
     1451        start_subpacket(content_->tag); 
     1452        print_boolean("Primary User ID", 
     1453                      content->ss_primary_user_id.primary_user_id); 
     1454        end_subpacket(); 
     1455        break;       
     1456 
     1457    case OPS_PTAG_SS_PREFERRED_HASH: 
     1458        start_subpacket(content_->tag); 
     1459        print_data("Preferred Hash Algorithms", 
     1460                   &content->ss_preferred_hash.data); 
     1461 
     1462        text = ops_showall_ss_preferred_hash(content->ss_preferred_hash); 
     1463        print_text_breakdown(text); 
     1464        ops_text_free(text); 
     1465        end_subpacket(); 
     1466        break; 
     1467 
     1468    case OPS_PTAG_SS_PREFERRED_COMPRESSION: 
     1469        start_subpacket(content_->tag); 
     1470        print_data( "Preferred Compression Algorithms", 
     1471                   &content->ss_preferred_compression.data); 
     1472 
     1473        text = ops_showall_ss_preferred_compression(content->ss_preferred_compression); 
     1474        print_text_breakdown(text); 
     1475        ops_text_free(text); 
     1476        end_subpacket(); 
     1477        break; 
     1478         
     1479    case OPS_PTAG_SS_KEY_FLAGS: 
     1480        start_subpacket(content_->tag); 
     1481        print_data( "Key Flags", &content->ss_key_flags.data); 
     1482 
     1483        text = ops_showall_ss_key_flags(content->ss_key_flags); 
     1484        print_text_breakdown( text); 
     1485        ops_text_free(text); 
     1486 
     1487        end_subpacket(); 
     1488        break; 
     1489         
     1490    case OPS_PTAG_SS_KEY_SERVER_PREFS: 
     1491        start_subpacket(content_->tag); 
     1492        print_data( "Key Server Preferences", 
     1493                   &content->ss_key_server_prefs.data); 
     1494 
     1495        text = ops_showall_ss_key_server_prefs(content->ss_key_server_prefs); 
     1496        print_text_breakdown( text); 
     1497        ops_text_free(text); 
     1498 
     1499        end_subpacket(); 
     1500        break; 
     1501         
     1502    case OPS_PTAG_SS_FEATURES: 
     1503        start_subpacket(content_->tag); 
     1504        print_data( "Features",  
     1505                   &content->ss_features.data); 
     1506 
     1507        text = ops_showall_ss_features(content->ss_features); 
     1508        print_text_breakdown( text); 
     1509        ops_text_free(text); 
     1510 
     1511        end_subpacket(); 
     1512        break; 
     1513 
     1514    case OPS_PTAG_SS_NOTATION_DATA: 
     1515        start_subpacket(content_->tag); 
     1516        print_indent(); 
     1517        printf("Notation Data:\n"); 
     1518 
     1519        indent++; 
     1520        print_data( "Flags", 
     1521                   &content->ss_notation_data.flags); 
     1522        text = ops_showall_ss_notation_data_flags(content->ss_notation_data); 
     1523        print_text_breakdown( text); 
     1524        ops_text_free(text); 
     1525 
     1526        /* xxx - TODO: print out UTF - rachel */ 
     1527 
     1528        print_data( "Name", 
     1529                   &content->ss_notation_data.name); 
     1530 
     1531        print_data( "Value", 
     1532                   &content->ss_notation_data.value); 
     1533 
     1534        indent--; 
     1535        end_subpacket(); 
     1536        break; 
     1537 
     1538    case OPS_PTAG_SS_REGEXP: 
     1539        start_subpacket(content_->tag); 
     1540        print_hexdump("Regular Expression", 
     1541                      (unsigned char *)content->ss_regexp.text, 
     1542                      strlen(content->ss_regexp.text)); 
     1543        print_string(NULL, 
     1544                     content->ss_regexp.text); 
     1545        end_subpacket(); 
     1546        break; 
     1547 
     1548    case OPS_PTAG_SS_POLICY_URL: 
     1549        start_subpacket(content_->tag); 
     1550        print_string("Policy URL", 
     1551                     content->ss_policy_url.text); 
     1552        end_subpacket(); 
     1553        break; 
     1554 
     1555    case OPS_PTAG_SS_SIGNERS_USER_ID: 
     1556        start_subpacket(content_->tag); 
     1557        print_utf8_string("Signer's User ID",content->ss_signers_user_id.user_id); 
     1558        end_subpacket(); 
     1559        break; 
     1560 
     1561    case OPS_PTAG_SS_PREFERRED_KEY_SERVER: 
     1562        start_subpacket(content_->tag); 
     1563        print_string("Preferred Key Server", 
     1564                     content->ss_preferred_key_server.text); 
     1565        end_subpacket(); 
     1566        break; 
     1567 
     1568    case OPS_PTAG_SS_USERDEFINED00: 
     1569    case OPS_PTAG_SS_USERDEFINED01: 
     1570    case OPS_PTAG_SS_USERDEFINED02: 
     1571    case OPS_PTAG_SS_USERDEFINED03: 
     1572    case OPS_PTAG_SS_USERDEFINED04: 
     1573    case OPS_PTAG_SS_USERDEFINED05: 
     1574    case OPS_PTAG_SS_USERDEFINED06: 
     1575    case OPS_PTAG_SS_USERDEFINED07: 
     1576    case OPS_PTAG_SS_USERDEFINED08: 
     1577    case OPS_PTAG_SS_USERDEFINED09: 
     1578    case OPS_PTAG_SS_USERDEFINED10: 
     1579        start_subpacket(content_->tag); 
     1580        print_hexdump("Internal or user-defined", 
     1581                      content->ss_userdefined.data.contents, 
     1582                      content->ss_userdefined.data.len); 
     1583        end_subpacket(); 
     1584        break; 
     1585 
     1586    case OPS_PTAG_SS_RESERVED: 
     1587        start_subpacket(content_->tag); 
     1588        print_hexdump("Reserved", 
     1589                      content->ss_userdefined.data.contents, 
     1590                      content->ss_userdefined.data.len); 
     1591        end_subpacket(); 
     1592        break; 
     1593 
     1594    case OPS_PTAG_SS_REVOCATION_REASON: 
     1595        start_subpacket(content_->tag); 
     1596        print_hexdump("Revocation Reason", 
     1597                      &content->ss_revocation_reason.code, 
     1598                      1); 
     1599        str=ops_show_ss_rr_code(content->ss_revocation_reason.code); 
     1600        print_string(NULL,str); 
     1601        /* xxx - todo : output text as UTF-8 string */ 
     1602        end_subpacket(); 
     1603        break; 
     1604 
     1605    case OPS_PTAG_CT_LITERAL_DATA_HEADER: 
     1606        print_tagname("LITERAL DATA HEADER"); 
     1607        printf("  literal data header format=%c filename='%s'\n", 
     1608               content->literal_data_header.format, 
     1609               content->literal_data_header.filename); 
     1610        showtime("    modification time", 
     1611                 content->literal_data_header.modification_time); 
     1612        printf("\n"); 
     1613        break; 
     1614 
     1615    case OPS_PTAG_CT_LITERAL_DATA_BODY: 
     1616        print_tagname("LITERAL DATA BODY"); 
     1617        printf("  literal data body length=%d\n", 
     1618               content->literal_data_body.length); 
     1619        printf("    data="); 
     1620        print_escaped(content->literal_data_body.data, 
     1621                      content->literal_data_body.length); 
     1622        printf("\n"); 
     1623        break; 
     1624 
     1625    case OPS_PTAG_CT_SIGNATURE_HEADER: 
     1626        print_tagname("SIGNATURE"); 
     1627        print_indent(indent); 
     1628        print_unsigned_int("Signature Version", 
     1629               content->signature.version); 
     1630        if(content->signature.creation_time_set)  
     1631            print_time("Signature Creation Time", content->signature.creation_time); 
     1632 
     1633        print_string_and_value("Signature Type", 
     1634                               ops_show_sig_type(content->signature.type), 
     1635                               content->signature.type); 
     1636 
     1637        if(content->signature.signer_id_set) 
     1638            print_hexdump_data("Signer ID", 
     1639                               content->signature.signer_id, 
     1640                               sizeof content->signature.signer_id); 
     1641 
     1642        print_string_and_value("Public Key Algorithm", 
     1643                               ops_show_pka(content->signature.key_algorithm), 
     1644                               content->signature.key_algorithm); 
     1645        print_string_and_value("Hash Algorithm", 
     1646                               ops_show_hash_algorithm(content->signature.hash_algorithm), 
     1647                               content->signature.hash_algorithm); 
     1648 
     1649        break; 
     1650 
     1651    case OPS_PTAG_CT_SIGNATURE_FOOTER: 
     1652        print_indent(); 
     1653        print_hexdump_data("hash2",&content->signature.hash2[0],2); 
     1654 
     1655        switch(content->signature.key_algorithm) 
     1656            { 
     1657        case OPS_PKA_RSA: 
     1658            print_bn("sig",content->signature.signature.rsa.sig); 
     1659            break; 
     1660 
     1661        case OPS_PKA_DSA: 
     1662            print_bn("r",content->signature.signature.dsa.r); 
     1663            print_bn("s",content->signature.signature.dsa.s); 
     1664            break; 
     1665 
     1666        case OPS_PKA_ELGAMAL_ENCRYPT_OR_SIGN: 
     1667            print_bn("r",content->signature.signature.elgamal.r); 
     1668            print_bn("s",content->signature.signature.elgamal.s); 
     1669            break; 
     1670 
     1671        case OPS_PKA_PRIVATE00: 
     1672        case OPS_PKA_PRIVATE01: 
     1673        case OPS_PKA_PRIVATE02: 
     1674        case OPS_PKA_PRIVATE03: 
     1675        case OPS_PKA_PRIVATE04: 
     1676        case OPS_PKA_PRIVATE05: 
     1677        case OPS_PKA_PRIVATE06: 
     1678        case OPS_PKA_PRIVATE07: 
     1679        case OPS_PKA_PRIVATE08: 
     1680        case OPS_PKA_PRIVATE09: 
     1681        case OPS_PKA_PRIVATE10: 
     1682            print_data("Private/Experimental", 
     1683                       &content->signature.signature.unknown.data); 
     1684            break; 
     1685 
     1686        default: 
     1687            assert(0); 
     1688            } 
     1689        break; 
     1690 
     1691    case OPS_PARSER_CMD_GET_SK_PASSPHRASE: 
     1692#ifdef XXX 
     1693        if(passphrase_prompt) 
     1694            { 
     1695            ops_print_secret_key(OPS_PTAG_CT_ENCRYPTED_SECRET_KEY, 
     1696                             content->secret_key_passphrase.secret_key); 
     1697            *content->secret_key_passphrase.passphrase=ops_get_passphrase(); 
     1698            if(!**content->secret_key_passphrase.passphrase) 
     1699                break; 
     1700            return OPS_KEEP_MEMORY; 
     1701            } 
     1702        else 
     1703            printf(">>> ASKED FOR PASSPHRASE <<<\n"); 
     1704#else 
     1705        if (cbinfo->cryptinfo.cb_get_passphrase) 
     1706            return cbinfo->cryptinfo.cb_get_passphrase(content_,cbinfo); 
     1707#endif /*XXX*/ 
     1708        break; 
     1709 
     1710    case OPS_PTAG_CT_SECRET_KEY: 
     1711    case OPS_PTAG_CT_ENCRYPTED_SECRET_KEY: 
     1712        ops_print_secret_key_verbose(content_->tag,&content->secret_key); 
     1713        break; 
     1714 
     1715    case OPS_PTAG_CT_ARMOUR_HEADER: 
     1716        print_tagname("ARMOUR HEADER"); 
     1717        print_string("type",content->armour_header.type); 
     1718        break; 
     1719 
     1720    case OPS_PTAG_CT_SIGNED_CLEARTEXT_HEADER: 
     1721        print_tagname("SIGNED CLEARTEXT HEADER"); 
     1722        print_headers(&content->signed_cleartext_header.headers); 
     1723        break; 
     1724 
     1725    case OPS_PTAG_CT_SIGNED_CLEARTEXT_BODY: 
     1726        print_tagname("SIGNED CLEARTEXT BODY"); 
     1727        print_block("signed cleartext",content->signed_cleartext_body.data, 
     1728                    content->signed_cleartext_body.length); 
     1729        break; 
     1730 
     1731    case OPS_PTAG_CT_SIGNED_CLEARTEXT_TRAILER: 
     1732        print_tagname("SIGNED CLEARTEXT TRAILER"); 
     1733        printf("hash algorithm: %d\n", 
     1734               content->signed_cleartext_trailer.hash->algorithm); 
     1735        printf("\n"); 
     1736        break; 
     1737 
     1738    case OPS_PTAG_CT_UNARMOURED_TEXT: 
     1739        if(!unarmoured) 
     1740            { 
     1741            print_tagname("UNARMOURED TEXT"); 
     1742            unarmoured=ops_true; 
     1743            } 
     1744        putchar('['); 
     1745        print_escaped(content->unarmoured_text.data, 
     1746                      content->unarmoured_text.length); 
     1747        putchar(']'); 
     1748        break; 
     1749 
     1750    case OPS_PTAG_CT_ARMOUR_TRAILER: 
     1751        print_tagname("ARMOUR TRAILER"); 
     1752        print_string("type",content->armour_header.type); 
     1753        break; 
     1754 
     1755    case OPS_PTAG_CT_PK_SESSION_KEY: 
     1756    case OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY: 
     1757        ops_print_pk_session_key(content_->tag,&content->pk_session_key); 
     1758        break; 
     1759 
     1760    case OPS_PARSER_CMD_GET_SECRET_KEY: 
     1761        ops_print_pk_session_key(OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY, 
     1762                             content->get_secret_key.pk_session_key); 
     1763 
     1764#ifdef XXX 
     1765        decrypter=ops_keyring_find_key_by_id(&keyring, 
     1766                                             content->get_secret_key.pk_session_key->key_id); 
     1767        if(!decrypter || !ops_key_is_secret(decrypter)) 
     1768            break; 
     1769 
     1770        puts("[Decryption key found in keyring]"); 
     1771 
     1772        secret=ops_get_secret_key_from_data(decrypter); 
     1773        while(!secret) 
     1774            { 
     1775            /* then it must be encrypted */ 
     1776            char *phrase=ops_get_passphrase(); 
     1777            secret=ops_decrypt_secret_key_from_data(decrypter,phrase); 
     1778            free(phrase); 
     1779            } 
     1780 
     1781        *content->get_secret_key.secret_key=secret; 
     1782#else 
     1783        return callback_cmd_get_secret_key(content_,cbinfo); 
     1784#endif /*XXX*/   
     1785        break; 
     1786 
     1787    default: 
     1788        print_tagname("UNKNOWN PACKET TYPE"); 
     1789        fprintf(stderr,"packet-dump: unknown tag=%d (0x%x)\n",content_->tag, 
     1790                content_->tag); 
     1791        exit(1); 
     1792        } 
     1793    return OPS_RELEASE_MEMORY; 
     1794    } 
     1795 
     1796void ops_list_packets(char* filename, ops_boolean_t armour, ops_keyring_t* keyring, ops_parse_cb_t* cb_get_passphrase) 
     1797    { 
     1798    int fd=0; 
     1799    ops_parse_info_t *pinfo=NULL; 
     1800    const ops_boolean_t accumulate=ops_true; 
     1801 
     1802    fd=ops_setup_file_read(&pinfo, filename, NULL, cb_list_packets, accumulate); 
     1803    ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); 
     1804    pinfo->cryptinfo.keyring=keyring; 
     1805    pinfo->cryptinfo.cb_get_passphrase=cb_get_passphrase; 
     1806 
     1807    if(armour) 
     1808        ops_reader_push_dearmour(pinfo,ops_true,ops_true,ops_true); 
     1809 
     1810    ops_parse_and_print_errors(pinfo); 
     1811 
     1812    ops_teardown_file_read(pinfo,fd); 
     1813 
     1814    ops_finish(); 
     1815    } 
  • openpgpsdk/trunk/src/lib/parse_local.h

    r570 r574  
    6666 
    6767    ops_create_info_t *cinfo; /*!< used if writing out parsed info */ 
    68     ops_crypt_info_t crypt; 
     68    ops_crypt_info_t cryptinfo; /*!< used when decrypting */ 
    6969    }; 
    7070 
     
    110110    ops_error_t *errors; 
    111111    ops_crypt_t decrypt; 
     112    ops_crypt_info_t cryptinfo; 
    112113    size_t nhashes; 
    113114    ops_parse_hash_info_t *hashes; 
  • openpgpsdk/trunk/src/lib/readerwriter.c

    r571 r574  
    2626#include <direct.h> 
    2727#endif 
     28#include <termios.h> 
    2829#include <assert.h> 
    2930#include <string.h> 
     
    3132 
    3233#include <openpgpsdk/readerwriter.h> 
     34#include <openpgpsdk/callback.h> 
     35 
    3336#include "parse_local.h" 
    34  
    35 /*! \todo descr for CB macro */ 
    36 /*! \todo check other callback functions to check they match this usage */ 
    37 #define CB(cbinfo,t,pc) do { (pc)->tag=(t); if((cbinfo)->cb(pc,(cbinfo)) == OPS_RELEASE_MEMORY) ops_parser_content_free(pc); } while(0) 
    38 #define CBP(info,t,pc) CB(&(info)->cbinfo,t,pc) 
    3937 
    4038void ops_setup_memory_write(ops_create_info_t **cinfo, ops_memory_t **mem, size_t bufsz) 
     
    116114void ops_teardown_file_write(ops_create_info_t *cinfo, int fd) 
    117115    { 
    118     ops_writer_close(cinfo); // new 
     116    ops_writer_close(cinfo); 
    119117    close(fd); 
    120118    ops_create_info_delete(cinfo); 
     
    239237    case OPS_PTAG_CT_PK_SESSION_KEY: 
    240238                //      printf ("OPS_PTAG_CT_PK_SESSION_KEY\n"); 
    241         assert(cbinfo->crypt.keyring); 
    242         cbinfo->crypt.keydata=ops_keyring_find_key_by_id(cbinfo->crypt.keyring, 
     239        assert(cbinfo->cryptinfo.keyring); 
     240        cbinfo->cryptinfo.keydata=ops_keyring_find_key_by_id(cbinfo->cryptinfo.keyring, 
    243241                                             content->pk_session_key.key_id); 
    244         if(!cbinfo->crypt.keydata) 
     242        if(!cbinfo->cryptinfo.keydata) 
    245243            break; 
    246244        break; 
     
    268266        { 
    269267    case OPS_PARSER_CMD_GET_SECRET_KEY: 
    270         cbinfo->crypt.keydata=ops_keyring_find_key_by_id(cbinfo->crypt.keyring,content->get_secret_key.pk_session_key->key_id); 
    271         if (!cbinfo->crypt.keydata || !ops_key_is_secret(cbinfo->crypt.keydata)) 
     268        cbinfo->cryptinfo.keydata=ops_keyring_find_key_by_id(cbinfo->cryptinfo.keyring,content->get_secret_key.pk_session_key->key_id); 
     269        if (!cbinfo->cryptinfo.keydata || !ops_key_is_secret(cbinfo->cryptinfo.keydata)) 
    272270            return 0; 
    273271 
    274272        /* do we need the passphrase and not have it? if so, get it */ 
    275         if (!cbinfo->crypt.passphrase) 
     273        if (!cbinfo->cryptinfo.passphrase) 
    276274            { 
    277275            memset(&pc,'\0',sizeof pc); 
    278             pc.content.secret_key_passphrase.passphrase=&cbinfo->crypt.passphrase; 
     276            pc.content.secret_key_passphrase.passphrase=&cbinfo->cryptinfo.passphrase; 
    279277            CB(cbinfo,OPS_PARSER_CMD_GET_SK_PASSPHRASE,&pc); 
    280             if (!cbinfo->crypt.passphrase) 
     278            if (!cbinfo->cryptinfo.passphrase) 
    281279                { 
    282280                fprintf(stderr,"can't get passphrase\n"); 
     
    286284 
    287285        /* now get the key from the data */ 
    288         secret=ops_get_secret_key_from_data(cbinfo->crypt.keydata); 
     286        secret=ops_get_secret_key_from_data(cbinfo->cryptinfo.keydata); 
    289287        while(!secret) 
    290288            { 
    291             if (!cbinfo->crypt.passphrase) 
     289            if (!cbinfo->cryptinfo.passphrase) 
    292290                { 
    293                 /* get the passphrase */ 
     291                /* get the passphrase again?*/ 
    294292                } 
    295293            /* then it must be encrypted */ 
    296             secret=ops_decrypt_secret_key_from_data(cbinfo->crypt.keydata,cbinfo->crypt.passphrase); 
     294            secret=ops_decrypt_secret_key_from_data(cbinfo->cryptinfo.keydata,cbinfo->cryptinfo.passphrase); 
    297295            } 
    298296         
     
    308306    } 
    309307 
     308static void echo_off() 
     309    { 
     310#ifndef WIN32 
     311    struct termios term; 
     312    int r; 
     313 
     314    r=tcgetattr(0,&term); 
     315    if(r < 0 && errno == ENOTTY) 
     316        return; 
     317    assert(r >= 0); 
     318 
     319    term.c_lflag &= ~ECHO; 
     320 
     321    r=tcsetattr(0,TCSANOW,&term); 
     322    assert(r >= 0); 
     323#endif 
     324    } 
     325         
     326static void echo_on() 
     327    { 
     328#ifndef WIN32 
     329    struct termios term; 
     330    int r; 
     331 
     332    r=tcgetattr(0,&term); 
     333    if(r < 0 && errno == ENOTTY) 
     334        return; 
     335    assert(r >= 0); 
     336 
     337    term.c_lflag |= ECHO; 
     338 
     339    r=tcsetattr(0,TCSANOW,&term); 
     340    assert(r >= 0); 
     341#endif 
     342    } 
     343 
     344char *ops_get_passphrase(void) 
     345    { 
     346    char buffer[1024]; 
     347    size_t n; 
     348 
     349    printf("Passphrase: "); 
     350     
     351    echo_off(); 
     352    fgets(buffer,sizeof buffer,stdin); 
     353    echo_on(); 
     354 
     355    putchar('\n'); 
     356 
     357    n=strlen(buffer); 
     358    if(n && buffer[n-1] == '\n') 
     359        buffer[--n]='\0'; 
     360    return ops_malloc_passphrase(buffer); 
     361    } 
     362 
     363char *ops_malloc_passphrase(char *pp) 
     364    { 
     365    char *passphrase; 
     366    size_t n; 
     367 
     368    n=strlen(pp); 
     369    passphrase=malloc(n+1); 
     370    strcpy(passphrase,pp); 
     371 
     372    return passphrase; 
     373    } 
     374 
    310375ops_parse_cb_return_t 
    311376callback_cmd_get_passphrase_from_cmdline(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) 
    312377    { 
    313     const int max_pp=256; 
    314     char pp[max_pp+1]; 
    315     //char *pp=NULL; 
    316  
    317378    ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content; 
    318379 
     
    324385        { 
    325386    case OPS_PARSER_CMD_GET_SK_PASSPHRASE: 
    326  
    327         printf("\nEnter passphrase: "); 
    328         fgets(&pp[0],max_pp, stdin); 
    329  
    330         *(content->secret_key_passphrase.passphrase)=ops_malloc_passphrase(pp); 
     387        *(content->secret_key_passphrase.passphrase)=ops_get_passphrase(); 
    331388        return OPS_KEEP_MEMORY; 
    332389        break; 
  • openpgpsdk/trunk/src/lib/signature.c

    r573 r574  
    2727#include <openpgpsdk/crypto.h> 
    2828#include <openpgpsdk/create.h> 
     29#include <openpgpsdk/writer_armoured.h> 
     30 
    2931#include <assert.h> 
    3032#include <string.h> 
     33#include <fcntl.h> 
     34#include <unistd.h> 
    3135 
    3236#include <openpgpsdk/final.h> 
    3337 
    3438static int debug=0; 
     39#define MAXBUF 1024 
    3540 
    3641/** \ingroup Create 
     
    768773    { return &sig->hash; } 
    769774 
     775ops_boolean_t ops_sign_file_as_cleartext(const char* filename, const ops_secret_key_t *skey, const ops_boolean_t overwrite) 
     776    { 
     777    // \todo allow choice of hash algorithams 
     778    // enforce use of SHA1 for now 
     779 
     780    unsigned char keyid[OPS_KEY_ID_SIZE]; 
     781    ops_create_signature_t *sig=NULL; 
     782 
     783    char signed_file[MAXBUF+1]; 
     784    char *suffix= "asc"; 
     785    int fd_in=0; 
     786    int fd_out=0; 
     787    ops_create_info_t *cinfo=NULL; 
     788    unsigned char buf[MAXBUF]; 
     789    //int flags=0; 
     790    ops_boolean_t rtn=ops_false; 
     791 
     792    // open file to sign 
     793#ifdef WIN32 
     794    fd_in=open(filename,O_RDONLY | O_BINARY); 
     795#else 
     796    fd_in=open(filename,O_RDONLY); 
     797#endif 
     798    if(fd_in < 0) 
     799        { 
     800        return ops_false; 
     801        } 
     802     
     803    // set up output file 
     804    snprintf(signed_file,sizeof signed_file,"%s.%s",filename,suffix); 
     805    fd_out=ops_setup_file_write(&cinfo, signed_file, overwrite); 
     806    if (fd_out < 0) 
     807        {  
     808        close (fd_in); 
     809        return ops_false;  
     810        } 
     811 
     812    // set up signature 
     813    sig=ops_create_signature_new(); 
     814    if (!sig) 
     815        { 
     816        close (fd_in); 
     817        ops_teardown_file_write(cinfo,fd_out); 
     818        return ops_false; 
     819        } 
     820 
     821    // \todo could add more error detection here 
     822    ops_signature_start_cleartext_signature(sig,skey,OPS_HASH_SHA1,OPS_SIG_BINARY); 
     823    if (ops_writer_push_clearsigned(cinfo,sig)!=ops_true) 
     824        { return ops_false; } 
     825 
     826    // Do the signing 
     827 
     828    for (;;) 
     829        { 
     830        int n=0; 
     831     
     832        n=read(fd_in,buf,sizeof(buf)); 
     833        if (!n) 
     834            break; 
     835        assert(n>=0); 
     836        ops_write(buf,n,cinfo); 
     837        } 
     838    close(fd_in); 
     839 
     840    // add signature with subpackets: 
     841    // - creation time 
     842    // - key id 
     843    rtn = ops_writer_switch_to_armoured_signature(cinfo) 
     844        && ops_signature_add_creation_time(sig,time(NULL)); 
     845    if (rtn==ops_false) 
     846        { 
     847        ops_teardown_file_write(cinfo,fd_out); 
     848        return ops_false; 
     849        } 
     850 
     851    ops_keyid(keyid,&skey->public_key); 
     852 
     853    rtn = ops_signature_add_issuer_key_id(sig,keyid) 
     854        && ops_signature_hashed_subpackets_end(sig) 
     855        && ops_write_signature(sig,&skey->public_key,skey,cinfo); 
     856 
     857    ops_teardown_file_write(cinfo,fd_out); 
     858 
     859    if (rtn==ops_false) 
     860        { 
     861        OPS_ERROR(&cinfo->errors,OPS_E_W,"Cannot sign file as cleartext"); 
     862        } 
     863    return rtn; 
     864    } 
     865 
     866 
     867/* It is the calling function's responsibility to free signed_cleartext */ 
     868/* signed_cleartext should be a NULL pointer when passed in */ 
     869ops_boolean_t ops_sign_buf_as_cleartext(const char* cleartext, const size_t len, ops_memory_t** signed_cleartext, const ops_secret_key_t *skey) 
     870    { 
     871    ops_boolean_t rtn=ops_false; 
     872 
     873    // \todo allow choice of hash algorithams 
     874    // enforce use of SHA1 for now 
     875 
     876    unsigned char keyid[OPS_KEY_ID_SIZE]; 
     877    ops_create_signature_t *sig=NULL; 
     878 
     879    ops_create_info_t *cinfo=NULL; 
     880     
     881    assert(*signed_cleartext==NULL); 
     882 
     883    // set up signature 
     884    sig=ops_create_signature_new(); 
     885    if (!sig) 
     886        {  
     887        return ops_false; 
     888        } 
     889 
     890    // \todo could add more error detection here 
     891    ops_signature_start_cleartext_signature(sig,skey,OPS_HASH_SHA1,OPS_SIG_BINARY); 
     892 
     893    // set up output file 
     894    ops_setup_memory_write(&cinfo, signed_cleartext, len); 
     895 
     896    // Do the signing 
     897    // add signature with subpackets: 
     898    // - creation time 
     899    // - key id 
     900    rtn = ops_writer_push_clearsigned(cinfo,sig) 
     901        && ops_write(cleartext,len,cinfo) 
     902        && ops_writer_switch_to_armoured_signature(cinfo) 
     903        && ops_signature_add_creation_time(sig,time(NULL)); 
     904 
     905    if (rtn==ops_false) 
     906        { 
     907        return ops_false; 
     908        } 
     909 
     910    ops_keyid(keyid,&skey->public_key); 
     911 
     912    rtn = ops_signature_add_issuer_key_id(sig,keyid) 
     913        && ops_signature_hashed_subpackets_end(sig) 
     914        && ops_write_signature(sig,&skey->public_key,skey,cinfo) 
     915        && ops_writer_close(cinfo); 
     916 
     917    // Note: the calling function must free signed_cleartext 
     918    ops_create_info_delete(cinfo); 
     919 
     920    return rtn; 
     921    } 
     922 
     923void ops_sign_file(const char* input_filename, const char* output_filename, const ops_secret_key_t *skey, const ops_boolean_t use_armour, const ops_boolean_t overwrite) 
     924    { 
     925    // \todo allow choice of hash algorithams 
     926    // enforce use of SHA1 for now 
     927 
     928    char *myfilename=NULL; 
     929    unsigned char keyid[OPS_KEY_ID_SIZE]; 
     930    ops_create_signature_t *sig=NULL; 
     931 
     932    int fd_out=0; 
     933    ops_create_info_t *cinfo=NULL; 
     934 
     935    ops_hash_algorithm_t hash_alg=OPS_HASH_SHA1; 
     936    ops_sig_type_t sig_type=OPS_SIG_BINARY; 
     937 
     938    ops_memory_t* mem_buf=NULL; 
     939    ops_hash_t* hash=NULL; 
     940 
     941    // read input file into buf 
     942 
     943    mem_buf=ops_write_buf_from_file(input_filename); 
     944 
     945    // setup output filename 
     946    if (!output_filename) 
     947        { 
     948        myfilename=ops_mallocz(strlen(input_filename)+4+1); 
     949        if (use_armour) 
     950            sprintf(myfilename,"%s.asc",input_filename); 
     951        else 
     952            sprintf(myfilename,"%s.gpg",input_filename); 
     953        fd_out=ops_setup_file_write(&cinfo, myfilename, overwrite); 
     954        free(myfilename); 
     955        }  
     956    else 
     957        { 
     958        fd_out=ops_setup_file_write(&cinfo, output_filename, overwrite); 
     959        } 
     960 
     961    // set up signature 
     962    sig=ops_create_signature_new(); 
     963    ops_signature_start_message_signature(sig, skey, hash_alg, sig_type); 
     964 
     965    //  set armoured/not armoured here 
     966    if (use_armour) 
     967        ops_writer_push_armoured_message(cinfo); 
     968 
     969    if (debug) 
     970        { fprintf(stderr, "** Writing out one pass sig\n"); }  
     971 
     972    // write one_pass_sig 
     973    ops_write_one_pass_sig(skey, hash_alg, sig_type, cinfo); 
     974 
     975    // hash file contents 
     976    hash=ops_signature_get_hash(sig); 
     977    hash->add(hash, ops_memory_get_data(mem_buf), ops_memory_get_length(mem_buf)); 
     978     
     979    // output file contents as Literal Data packet 
     980 
     981    if (debug) 
     982        { fprintf(stderr,"** Writing out data now\n"); } 
     983 
     984    ops_write_literal_data_from_buf(ops_memory_get_data(mem_buf), ops_memory_get_length(mem_buf), OPS_LDT_BINARY, cinfo); 
     985 
     986    if (debug) 
     987        { fprintf(stderr,"** After Writing out data now\n");} 
     988 
     989    // add subpackets to signature 
     990    // - creation time 
     991    // - key id 
     992 
     993    ops_signature_add_creation_time(sig,time(NULL)); 
     994 
     995    ops_keyid(keyid,&skey->public_key); 
     996    ops_signature_add_issuer_key_id(sig,keyid); 
     997 
     998    ops_signature_hashed_subpackets_end(sig); 
     999 
     1000    // write out sig 
     1001    ops_write_signature(sig,&skey->public_key,skey,cinfo); 
     1002 
     1003    ops_teardown_file_write(cinfo, fd_out); 
     1004 
     1005    // tidy up 
     1006    ops_create_signature_delete(sig); 
     1007    ops_memory_free(mem_buf); 
     1008    } 
     1009 
     1010ops_memory_t* ops_sign_mem(const void* input, const int input_len, const ops_sig_type_t sig_type, const ops_secret_key_t *skey, const ops_boolean_t use_armour) 
     1011    { 
     1012    // \todo allow choice of hash algorithams 
     1013    // enforce use of SHA1 for now 
     1014 
     1015    unsigned char keyid[OPS_KEY_ID_SIZE]; 
     1016    ops_create_signature_t *sig=NULL; 
     1017 
     1018    ops_create_info_t *cinfo=NULL; 
     1019    ops_memory_t *mem=ops_memory_new(); 
     1020 
     1021    ops_hash_algorithm_t hash_alg=OPS_HASH_SHA1; 
     1022    //    ops_sig_type_t sig_type=OPS_SIG_BINARY; 
     1023    ops_literal_data_type_t ld_type; 
     1024    ops_hash_t* hash=NULL; 
     1025 
     1026    // setup literal data packet type 
     1027    if (sig_type==OPS_SIG_BINARY) 
     1028        ld_type=OPS_LDT_BINARY; 
     1029    else 
     1030        ld_type=OPS_LDT_TEXT; 
     1031 
     1032    // set up signature 
     1033    sig=ops_create_signature_new(); 
     1034    ops_signature_start_message_signature(sig, skey, hash_alg, sig_type); 
     1035 
     1036    // setup writer 
     1037    ops_setup_memory_write(&cinfo, &mem, input_len); 
     1038 
     1039    //  set armoured/not armoured here 
     1040    if (use_armour) 
     1041        ops_writer_push_armoured_message(cinfo); 
     1042 
     1043    if (debug) 
     1044        { fprintf(stderr, "** Writing out one pass sig\n"); }  
     1045 
     1046    // write one_pass_sig 
     1047    ops_write_one_pass_sig(skey, hash_alg, sig_type, cinfo); 
     1048 
     1049    // hash file contents 
     1050    hash=ops_signature_get_hash(sig); 
     1051    hash->add(hash, input, input_len); 
     1052     
     1053    // output file contents as Literal Data packet 
     1054 
     1055    if (debug) 
     1056        { fprintf(stderr,"** Writing out data now\n"); } 
     1057 
     1058    ops_write_literal_data_from_buf(input, input_len, ld_type, cinfo); 
     1059 
     1060    if (debug) 
     1061        { fprintf(stderr,"** After Writing out data now\n");} 
     1062 
     1063    // add subpackets to signature 
     1064    // - creation time 
     1065    // - key id 
     1066 
     1067    ops_signature_add_creation_time(sig,time(NULL)); 
     1068 
     1069    ops_keyid(keyid,&skey->public_key); 
     1070    ops_signature_add_issuer_key_id(sig,keyid); 
     1071 
     1072    ops_signature_hashed_subpackets_end(sig); 
     1073 
     1074    // write out sig 
     1075    ops_write_signature(sig,&skey->public_key,skey,cinfo); 
     1076 
     1077    // tidy up 
     1078    ops_writer_close(cinfo); 
     1079    ops_create_signature_delete(sig); 
     1080 
     1081    return mem; 
     1082    } 
     1083 
     1084// EOF 
  • openpgpsdk/trunk/src/lib/symmetric.c

    r571 r574  
    3434#include <openpgpsdk/final.h> 
    3535 
    36 static int debug=0; 
     36//static int debug=0; 
    3737 
    3838#ifndef ATTRIBUTE_UNUSED 
     
    4545 
    4646#endif /* ATTRIBUTE_UNUSED */ 
    47  
    48  
    49 // \todo there's also a encrypted_arg_t in adv_create.c  
    50 // which is used for *encrypting* whereas this is used 
    51 // for *decrypting* 
    52  
    53 typedef struct 
    54     { 
    55     unsigned char decrypted[1024]; 
    56     size_t decrypted_count; 
    57     size_t decrypted_offset; 
    58     ops_crypt_t *decrypt; 
    59     ops_region_t *region; 
    60     ops_boolean_t prev_read_was_plain:1; 
    61     } encrypted_arg_t; 
    62  
    63 static int encrypted_data_reader(void *dest,size_t length,ops_error_t **errors, 
    64                                  ops_reader_info_t *rinfo, 
    65                                  ops_parse_cb_info_t *cbinfo) 
    66     { 
    67     encrypted_arg_t *arg=ops_reader_get_arg(rinfo); 
    68     int saved=length; 
    69  
    70     // V3 MPIs have the count plain and the cipher is reset after each count 
    71     if(arg->prev_read_was_plain && !rinfo->pinfo->reading_mpi_length) 
    72         { 
    73         assert(rinfo->pinfo->reading_v3_secret); 
    74         arg->decrypt->decrypt_resync(arg->decrypt); 
    75         arg->prev_read_was_plain=ops_false; 
    76         } 
    77     else if(rinfo->pinfo->reading_v3_secret 
    78             && rinfo->pinfo->reading_mpi_length) 
    79         { 
    80         arg->prev_read_was_plain=ops_true; 
    81         } 
    82  
    83     while(length > 0) 
    84         { 
    85         if(arg->decrypted_count) 
    86             { 
    87  
    88             unsigned n; 
    89  
    90             // if we are reading v3 we should never read more than 
    91             // we're asked for 
    92             assert(length >= arg->decrypted_count 
    93                    || (!rinfo->pinfo->reading_v3_secret 
    94                        && !rinfo->pinfo->exact_read)); 
    95  
    96             if(length > arg->decrypted_count) 
    97                 n=arg->decrypted_count; 
    98             else 
    99                 n=length; 
    100  
    101             memcpy(dest,arg->decrypted+arg->decrypted_offset,n); 
    102             arg->decrypted_count-=n; 
    103             arg->decrypted_offset+=n; 
    104             length-=n; 
    105 #ifdef WIN32 
    106             (char*)dest+=n; 
    107 #else 
    108         dest+=n; 
    109 #endif 
    110             } 
    111         else 
    112             { 
    113             unsigned n=arg->region->length; 
    114             unsigned char buffer[1024]; 
    115  
    116             if(!n) 
    117             { 
    118                 return -1; 
    119             } 
    120  
    121             if(!arg->region->indeterminate) 
    122                 { 
    123                 n-=arg->region->length_read; 
    124                 if(n == 0) 
    125                     return saved-length; 
    126                 if(n > sizeof buffer) 
    127                     n=sizeof buffer; 
    128                 } 
    129             else 
    130             { 
    131                 n=sizeof buffer; 
    132             } 
    133  
    134             // we can only read as much as we're asked for in v3 keys 
    135             // because they're partially unencrypted! 
    136             if((rinfo->pinfo->reading_v3_secret || rinfo->pinfo->exact_read) 
    137                && n > length) 
    138                 n=length; 
    139  
    140             if(!ops_stacked_limited_read(buffer,n,arg->region,errors,rinfo, 
    141                                          cbinfo)) 
    142             { 
    143                 return -1; 
    144             } 
    145  
    146             if(!rinfo->pinfo->reading_v3_secret 
    147                || !rinfo->pinfo->reading_mpi_length) 
    148                 { 
    149                 arg->decrypted_count=ops_decrypt_se_ip(arg->decrypt, 
    150                                   arg->decrypted, 
    151                                   buffer,n); 
    152  
    153                 if (debug) 
    154                     { 
    155                     fprintf(stderr,"READING:\nencrypted: "); 
    156                     int i=0; 
    157                     for (i=0; i<16; i++) 
    158                         fprintf(stderr,"%2x ", buffer[i]); 
    159                     fprintf(stderr,"\n"); 
    160                     fprintf(stderr,"decrypted:   "); 
    161                     for (i=0; i<16; i++) 
    162                         fprintf(stderr,"%2x ", arg->decrypted[i]); 
    163                     fprintf(stderr,"\n"); 
    164                     } 
    165                 } 
    166             else 
    167                 { 
    168                 memcpy(arg->decrypted,buffer,n); 
    169                 arg->decrypted_count=n; 
    170                 } 
    171  
    172             assert(arg->decrypted_count > 0); 
    173  
    174             arg->decrypted_offset=0; 
    175             } 
    176         } 
    177  
    178     return saved; 
    179     } 
    180  
    181 static void encrypted_data_destroyer(ops_reader_info_t *rinfo) 
    182     { free(ops_reader_get_arg(rinfo)); } 
    183  
    184 void ops_reader_push_decrypt(ops_parse_info_t *pinfo,ops_crypt_t *decrypt, 
    185                              ops_region_t *region) 
    186     { 
    187     encrypted_arg_t *arg=ops_mallocz(sizeof *arg); 
    188  
    189     arg->decrypt=decrypt; 
    190     arg->region=region; 
    191  
    192     ops_decrypt_init(arg->decrypt); 
    193  
    194     ops_reader_push(pinfo,encrypted_data_reader,encrypted_data_destroyer,arg); 
    195     } 
    196  
    197 void ops_reader_pop_decrypt(ops_parse_info_t *pinfo) 
    198     { 
    199     encrypted_arg_t *arg=ops_reader_get_arg(ops_parse_get_rinfo(pinfo)); 
    200  
    201     arg->decrypt->decrypt_finish(arg->decrypt); 
    202     free(arg); 
    203      
    204     ops_reader_pop(pinfo); 
    205     } 
    20647 
    20748static void std_set_iv(ops_crypt_t *crypt,const unsigned char *iv) 
     
    675516    return count; 
    676517    } 
     518 
     519// EOF 
  • openpgpsdk/trunk/src/lib/util.c

    r571 r574  
    106106    } 
    107107 
    108 /** Arguments for reader_fd 
    109  */ 
    110 typedef struct 
    111     { 
    112     int fd; /*!< file descriptor */ 
    113     } reader_fd_arg_t; 
    114  
    115 /** 
    116  * \ingroup Parse 
    117  * 
    118  * ops_reader_fd() attempts to read up to "plength" bytes from the file  
    119  * descriptor in "parse_info" into the buffer starting at "dest" using the 
    120  * rules contained in "flags" 
    121  * 
    122  * \param       dest    Pointer to previously allocated buffer 
    123  * \param       plength Number of bytes to try to read 
    124  * \param       flags   Rules about reading to use 
    125  * \param       parse_info      Gets cast to ops_reader_fd_arg_t 
    126  * 
    127  * \return      OPS_R_EOF       if no bytes were read 
    128  * \return      OPS_R_PARTIAL_READ      if not enough bytes were read, and OPS_RETURN_LENGTH is set in "flags" 
    129  * \return      OPS_R_EARLY_EOF if not enough bytes were read, and OPS_RETURN_LENGTH was not set in "flags" 
    130  * \return      OPS_R_OK        if expected length was read 
    131  * \return      OPS_R_ERROR     if cannot read 
    132  * 
    133  * OPS_R_EARLY_EOF and OPS_R_ERROR push errors on the stack 
    134  * 
    135  * \sa enum opt_reader_ret_t 
    136  * 
    137  * \todo change arg_ to typesafe?  
    138  */ 
    139 static int fd_reader(void *dest,size_t length,ops_error_t **errors, 
    140                      ops_reader_info_t *rinfo,ops_parse_cb_info_t *cbinfo) 
    141     { 
    142     reader_fd_arg_t *arg=ops_reader_get_arg(rinfo); 
    143     int n=read(arg->fd,dest,length); 
    144  
    145     OPS_USED(cbinfo); 
    146  
    147     if(n == 0) 
    148         return 0; 
    149  
    150     if(n < 0) 
    151         { 
    152         OPS_SYSTEM_ERROR_1(errors,OPS_E_R_READ_FAILED,"read", 
    153                            "file descriptor %d",arg->fd); 
    154         return -1; 
    155         } 
    156  
    157     return n; 
    158     } 
    159  
    160 static void fd_destroyer(ops_reader_info_t *rinfo) 
    161     { free(ops_reader_get_arg(rinfo)); } 
    162  
    163 void ops_reader_set_fd(ops_parse_info_t *pinfo,int fd) 
    164     { 
    165     reader_fd_arg_t *arg=malloc(sizeof *arg); 
    166  
    167     arg->fd=fd; 
    168     ops_reader_set(pinfo,fd_reader,fd_destroyer,arg); 
    169     } 
    170  
    171108typedef struct 
    172109    { 
  • openpgpsdk/trunk/src/lib/writer_encrypt_se_ip.c

    r571 r574  
    3535#include <openpgpsdk/compress.h> 
    3636#include <openpgpsdk/create.h> 
     37#include <openpgpsdk/hash.h> 
    3738#include <openpgpsdk/keyring.h> 
    3839#include <openpgpsdk/random.h> 
     
    141142    free(arg->crypt); 
    142143    free(arg); 
    143     } 
    144  
    145 void ops_calc_mdc_hash(const unsigned char* preamble, const size_t sz_preamble, const unsigned char* plaintext, const unsigned int sz_plaintext, unsigned char *hashed) 
    146     { 
    147     int debug=0; 
    148     ops_hash_t hash; 
    149     unsigned char c[1]; 
    150  
    151     if (debug) 
    152         { 
    153         unsigned int i=0; 
    154         fprintf(stderr,"ops_calc_mdc_hash():\n"); 
    155  
    156         fprintf(stderr,"\npreamble: "); 
    157         for (i=0; i<sz_preamble;i++) 
    158             fprintf(stderr," 0x%02x", preamble[i]); 
    159         fprintf(stderr,"\n"); 
    160  
    161         fprintf(stderr,"\nplaintext (len=%d): ",sz_plaintext); 
    162         for (i=0; i<sz_plaintext;i++) 
    163             fprintf(stderr," 0x%02x", plaintext[i]); 
    164         fprintf(stderr,"\n"); 
    165         } 
    166  
    167     // init 
    168     ops_hash_any(&hash, OPS_HASH_SHA1); 
    169     hash.init(&hash); 
    170  
    171     // preamble 
    172     hash.add(&hash,preamble,sz_preamble); 
    173     // plaintext 
    174     hash.add(&hash,plaintext,sz_plaintext);  
    175     // MDC packet tag 
    176     c[0]=0xD3; 
    177     hash.add(&hash,&c[0],1);    
    178     // MDC packet len 
    179     c[0]=0x14; 
    180     hash.add(&hash,&c[0],1);    
    181  
    182     //finish 
    183     hash.finish(&hash,hashed); 
    184  
    185     if (debug) 
    186         { 
    187         unsigned int i=0; 
    188         fprintf(stderr,"\nhashed (len=%d): ",SHA_DIGEST_LENGTH); 
    189         for (i=0; i<SHA_DIGEST_LENGTH;i++) 
    190             fprintf(stderr," 0x%02x", hashed[i]); 
    191         fprintf(stderr,"\n"); 
    192         } 
    193144    } 
    194145 
  • openpgpsdk/trunk/tests/test_common.c

    r572 r574  
    363363        */ 
    364364         
    365         if (cbinfo->crypt.keydata==alpha_sec_keydata) 
     365        if (cbinfo->cryptinfo.keydata==alpha_sec_keydata) 
    366366            passphrase=alpha_passphrase; 
    367         else if (cbinfo->crypt.keydata==bravo_sec_keydata) 
     367        else if (cbinfo->cryptinfo.keydata==bravo_sec_keydata) 
    368368            passphrase=bravo_passphrase; 
    369369        else 
    370370            assert(0); 
    371371        //        *(content->secret_key_passphrase.passphrase)=ops_malloc_passphrase(no_passphrase); 
    372         cbinfo->crypt.passphrase=ops_malloc_passphrase(passphrase); 
     372        cbinfo->cryptinfo.passphrase=ops_malloc_passphrase(passphrase); 
    373373        return OPS_KEEP_MEMORY; 
    374374        break; 
  • openpgpsdk/trunk/tests/test_packet_types.c

    r572 r574  
    2323  
    2424#include <openpgpsdk/types.h> 
     25#include <openpgpsdk/hash.h> 
    2526#include "openpgpsdk/packet.h" 
    2627#include "openpgpsdk/packet-parse.h" 
  • openpgpsdk/trunk/tests/test_rsa_decrypt.c

    r573 r574  
    149149 
    150150    // setup keyring and passphrase callback 
    151     pinfo->cbinfo.crypt.keyring=&sec_keyring; 
    152     pinfo->cbinfo.crypt.cb_get_passphrase=test_cb_get_passphrase; 
     151    pinfo->cbinfo.cryptinfo.keyring=&sec_keyring; 
     152    pinfo->cbinfo.cryptinfo.cb_get_passphrase=test_cb_get_passphrase; 
    153153 
    154154    // Set up armour/passphrase options 
  • openpgpsdk/trunk/tests/test_rsa_signature.c

    r573 r574  
    2525#include "openpgpsdk/keyring.h" 
    2626#include <openpgpsdk/armour.h> 
     27#include <openpgpsdk/create.h> 
    2728#include "openpgpsdk/packet.h" 
    2829#include "openpgpsdk/packet-parse.h"