Changeset 484

Show
Ignore:
Timestamp:
08/10/07 13:07:33
Author:
rachel
Message:

Added explicit test for Encrypted PK Session Key
Some refactoring

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openpgpsdk/trunk/include/openpgpsdk/std_print.h

    r447 r484  
    55#define OPS_STD_PRINT_H 
    66 
     7#include "openpgpsdk/packet.h" 
     8#include "openpgpsdk/packet-parse.h" 
     9#include "openpgpsdk/keyring.h" 
     10 
     11void ops_print_pk_session_key(ops_content_tag_t tag, 
     12                          const ops_pk_session_key_t *key); 
    713void ops_print_public_key(const ops_key_data_t *key); 
    814 
  • openpgpsdk/trunk/src/advanced/adv_create.c

    r482 r484  
    815815        && ops_write_scalar(pksk->algorithm, 1, info) 
    816816        && ops_write_mpi(pksk->parameters.rsa.encrypted_m, info) 
    817         && ops_write_scalar(0, 2, info); 
     817        //??    && ops_write_scalar(0, 2, info); 
     818        ; 
    818819    } 
    819820 
  • openpgpsdk/trunk/src/standard/std_print.c

    r480 r484  
    488488    } 
    489489 
    490 static void print_pk_session_key(ops_content_tag_t tag, 
     490void ops_print_pk_session_key(ops_content_tag_t tag, 
    491491                                 const ops_pk_session_key_t *key) 
    492492    { 
     
    11161116    case OPS_PTAG_CT_PK_SESSION_KEY: 
    11171117    case OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY: 
    1118         print_pk_session_key(content_->tag,&content->pk_session_key); 
     1118        ops_print_pk_session_key(content_->tag,&content->pk_session_key); 
    11191119        break; 
    11201120 
    11211121    case OPS_PARSER_CMD_GET_SECRET_KEY: 
    1122         print_pk_session_key(OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY, 
     1122        ops_print_pk_session_key(OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY, 
    11231123                             content->get_secret_key.pk_session_key); 
    11241124 
  • openpgpsdk/trunk/tests/test_packet_types.c

    r480 r484  
    2020static unsigned char* mdc_data=NULL; 
    2121static size_t sz_mdc_data=0; 
     22static unsigned char* encrypted_pk_sk=NULL; 
     23static size_t sz_encrypted_pk_sk=0; 
    2224 
    2325#define MAXBUF 128 
    2426 
    2527static void cleanup(); 
    26 //static void print_hash(char* str, unsigned char* data); 
    2728 
    2829/*  
     
    3233int init_suite_packet_types(void) 
    3334    { 
     35    char keydetails[MAXBUF+1]; 
     36    char keyring_name[MAXBUF+1]; 
     37    int fd=0; 
     38    char cmd[MAXBUF+1]; 
     39 
    3440    // Initialise OPS  
    3541    ops_init(); 
     42 
     43    char *rsa_nopass="Key-Type: RSA\nKey-Usage: encrypt, sign\nName-Real: Alpha\nName-Comment: RSA, no passphrase\nName-Email: alpha@test.com\nKey-Length: 1024\n"; 
     44    // Create temp directory 
     45    if (!mktmpdir()) 
     46        return 1; 
     47 
     48    /* 
     49     * Create a RSA keypair with no passphrase 
     50     */ 
     51 
     52    snprintf(keydetails,MAXBUF,"%s/%s",dir,"keydetails.alpha"); 
     53 
     54    if ((fd=open(keydetails,O_WRONLY | O_CREAT | O_EXCL, 0600))<0) 
     55        { 
     56        fprintf(stderr,"Can't create key details\n"); 
     57        return 1; 
     58        } 
     59 
     60    write(fd,rsa_nopass,strlen(rsa_nopass)); 
     61    close(fd); 
     62 
     63    snprintf(cmd,MAXBUF,"gpg --quiet --gen-key --expert --homedir=%s --batch %s",dir,keydetails); 
     64    system(cmd); 
     65 
     66    // read keyrings 
     67    snprintf(keyring_name,MAXBUF,"%s/pubring.gpg", dir); 
     68    ops_keyring_read(&pub_keyring,keyring_name); 
     69 
     70    // read keyring 
     71    snprintf(keyring_name,MAXBUF,"%s/secring.gpg", dir); 
     72    ops_keyring_read(&sec_keyring,keyring_name); 
    3673 
    3774    // Return success 
     
    6299    case OPS_PTAG_CT_LITERAL_DATA_BODY: 
    63100        sz_literal_data=content->literal_data_body.length; 
    64         //      literal_data=ops_mallocz(content->literal_data_body.length+1); 
    65         //        memcpy(literal_data,content->literal_data_body.data,content->literal_data_body.length); 
    66101        literal_data=ops_mallocz(sz_literal_data+1); 
    67102        memcpy(literal_data,content->literal_data_body.data,sz_literal_data); 
    68103        break; 
    69104 
    70     case OPS_PARSER_PTAG: 
    71105    case OPS_PTAG_CT_LITERAL_DATA_HEADER: 
    72106        // ignore 
    73107        break; 
    74108 
    75     case OPS_PARSER_ERROR: 
    76         printf("parse error: %s\n",content->error.error); 
    77         break; 
    78  
    79     case OPS_PARSER_ERRCODE: 
    80         printf("parse error: %s\n", 
    81                ops_errcode(content->errcode.errcode)); 
    82         break; 
    83  
    84109    default: 
    85         fprintf(stderr,"Unexpected packet tag=%d (0x%x)\n",content_->tag, 
    86                 content_->tag); 
    87         assert(0); 
     110        return callback_general(content_,cbinfo); 
    88111        } 
    89112 
     
    100123        //      ops_print_packet(content_); 
    101124 
    102     // Read data from packet into static buffer 
    103125    switch(content_->tag) 
    104126        { 
     
    110132                break; 
    111133 
    112     case OPS_PARSER_PTAG: 
    113         // ignore 
     134    default: 
     135        return callback_general(content_,cbinfo); 
     136        } 
     137 
     138    return OPS_RELEASE_MEMORY; 
     139    } 
     140  
     141static ops_parse_cb_return_t 
     142callback_encrypted_pk_session_key(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) 
     143    { 
     144    ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content; 
     145 
     146    OPS_USED(cbinfo); 
     147 
     148        //      ops_print_packet(content_); 
     149 
     150    switch(content_->tag) 
     151        { 
     152    case OPS_PTAG_CT_PK_SESSION_KEY: 
    114153        break; 
    115154 
    116     case OPS_PARSER_ERROR: 
    117         printf("parse error: %s\n",content->error.error); 
    118         break; 
    119  
    120     case OPS_PARSER_ERRCODE: 
    121         printf("parse error: %s\n", 
    122                ops_errcode(content->errcode.errcode)); 
    123         break; 
     155        case OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY: 
     156        sz_encrypted_pk_sk=sizeof(*encrypted_pk_sk); 
     157                encrypted_pk_sk=ops_mallocz(sz_encrypted_pk_sk); 
     158                memcpy(encrypted_pk_sk,&content->pk_session_key,sz_encrypted_pk_sk); 
     159                break; 
     160 
     161    case OPS_PARSER_CMD_GET_SK_PASSPHRASE: 
     162        return callback_cmd_get_secret_key_passphrase(content_,cbinfo); 
     163 
     164    case OPS_PARSER_CMD_GET_SECRET_KEY: 
     165        return callback_cmd_get_secret_key(content_,cbinfo); 
    124166 
    125167    default: 
    126         fprintf(stderr,"Unexpected packet tag=%d (0x%x)\n",content_->tag, 
    127                 content_->tag); 
    128         assert(0); 
     168        return callback_general(content_,cbinfo); 
    129169        } 
    130170 
     
    143183    switch(content_->tag) 
    144184        { 
    145     case OPS_PARSER_PTAG: 
    146         // ignore 
    147         break; 
    148  
    149185    case OPS_PTAG_CT_LITERAL_DATA_HEADER: 
    150186    case OPS_PTAG_CT_LITERAL_DATA_BODY: 
     
    153189 
    154190    default: 
    155         fprintf(stderr,"Unexpected packet tag=%d (0x%x)\n",content_->tag, 
    156                 content_->tag); 
    157         assert(0); 
     191        return callback_general(content_,cbinfo); 
    158192        } 
    159193 
     
    438472    } 
    439473 
     474static void test_ops_encrypted_pk_sk() 
     475    { 
     476    char *user_id="Alpha (RSA, no passphrase) <alpha@test.com>"; 
     477    ops_pk_session_key_t *encrypted_pk_session_key; 
     478    ops_create_info_t *cinfo; 
     479    ops_parse_info_t *pinfo; 
     480    ops_memory_t *mem; 
     481    int rtn=0; 
     482 
     483    // setup for write 
     484    ops_setup_memory_write(&cinfo,&mem,MAXBUF); 
     485 
     486    // write 
     487    const ops_key_data_t *pub_key=ops_keyring_find_key_by_userid(&pub_keyring, user_id); 
     488    encrypted_pk_session_key=ops_create_pk_session_key(pub_key); 
     489    ops_write_pk_session_key(cinfo,encrypted_pk_session_key); 
     490 
     491    // setup for read 
     492    ops_setup_memory_read(&pinfo,mem,callback_encrypted_pk_session_key); 
     493 
     494    // read 
     495    rtn=ops_parse(pinfo); 
     496 
     497    // test 
     498    CU_ASSERT(memcmp(encrypted_pk_session_key, encrypted_pk_sk, sz_encrypted_pk_sk)==0); 
     499 
     500    // cleanup 
     501    cleanup(); 
     502    ops_teardown_memory_read(pinfo,mem); 
     503    } 
     504 
    440505CU_pSuite suite_packet_types() 
    441506{ 
     
    461526 
    462527    if (NULL == CU_add_test(suite, "Tag 20: Sym. Encrypted Integrity Protected Data packet", test_ops_se_ip)) 
     528            return NULL; 
     529 
     530    if (NULL == CU_add_test(suite, "Tag 1: PK Encrypted Session Key packet", test_ops_encrypted_pk_sk)) 
    463531            return NULL; 
    464532 
  • openpgpsdk/trunk/tests/tests.c

    r480 r484  
    88#include "CUnit/Basic.h" 
    99#include "openpgpsdk/readerwriter.h" 
     10// \todo remove the need for this 
     11#include "../src/advanced/parse_local.h" 
    1012 
    1113#include "tests.h" 
     
    1719 
    1820char dir[MAXBUF+1]; 
     21ops_keyring_t pub_keyring; 
     22ops_keyring_t sec_keyring; 
     23static char* no_passphrase=""; 
    1924 
    2025int main() 
     
    9499    } 
    95100 
    96  
    97  
     101ops_parse_cb_return_t 
     102callback_general(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) 
     103    { 
     104    ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content; 
     105     
     106    OPS_USED(cbinfo); 
     107     
     108    //    ops_print_packet(content_); 
     109     
     110    switch(content_->tag) 
     111        { 
     112    case OPS_PARSER_PTAG: 
     113        // ignore 
     114        break; 
     115         
     116    case OPS_PARSER_ERROR: 
     117        printf("parse error: %s\n",content->error.error); 
     118        break; 
     119         
     120    case OPS_PARSER_ERRCODE: 
     121        printf("parse error: %s\n", 
     122               ops_errcode(content->errcode.errcode)); 
     123        break; 
     124         
     125    default: 
     126        fprintf(stderr,"Unexpected packet tag=%d (0x%x)\n",content_->tag, 
     127                content_->tag); 
     128        assert(0); 
     129        } 
     130     
     131    return OPS_RELEASE_MEMORY; 
     132    } 
     133 
     134ops_parse_cb_return_t 
     135callback_cmd_get_secret_key(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) 
     136    { 
     137    ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content; 
     138    const ops_key_data_t *keydata=NULL; 
     139    const ops_secret_key_t *secret; 
     140    /* 
     141    static const ops_key_data_t *decrypt_key; 
     142    */ 
     143 
     144    OPS_USED(cbinfo); 
     145 
     146//    ops_print_packet(content_); 
     147 
     148    switch(content_->tag) 
     149        { 
     150    case OPS_PARSER_CMD_GET_SECRET_KEY: 
     151        keydata=ops_keyring_find_key_by_id(&sec_keyring,content->get_secret_key.pk_session_key->key_id); 
     152        if (!keydata || !ops_key_is_secret(keydata)) 
     153            return 0; 
     154 
     155        // Do we need the passphrase and not have it? If so, get it 
     156        ops_parser_content_t pc; 
     157        char *passphrase; 
     158        memset(&pc,'\0',sizeof pc); 
     159        passphrase=NULL; 
     160        pc.content.secret_key_passphrase.passphrase=&passphrase; 
     161        //        pc.content.secret_key_passphrase.secret_key=&(keydata->key.skey); 
     162        pc.content.secret_key_passphrase.secret_key=ops_get_secret_key_from_data(keydata); 
     163 
     164        /* Ugh. Need to duplicate this macro here to get the passphrase  
     165           Duplication to be removed when the callback gets moved to main code. 
     166           Can we make this inline code rather than a macro? 
     167        */ 
     168#define CB(cbinfo,t,pc) do { (pc)->tag=(t); if((cbinfo)->cb(pc,(cbinfo)) == OPS_RELEASE_MEMORY) ops_parser_content_free(pc); } while(0) 
     169        CB(cbinfo,OPS_PARSER_CMD_GET_SK_PASSPHRASE,&pc); 
     170         
     171        /* now get the key from the data */ 
     172        secret=ops_get_secret_key_from_data(keydata); 
     173        while(!secret) 
     174            { 
     175            /* then it must be encrypted */ 
     176            secret=ops_decrypt_secret_key_from_data(keydata,passphrase); 
     177            free(passphrase); 
     178            } 
     179         
     180        *content->get_secret_key.secret_key=secret; 
     181        break; 
     182 
     183    default: 
     184        return callback_general(content_,cbinfo); 
     185        } 
     186     
     187    return OPS_RELEASE_MEMORY; 
     188    } 
     189 
     190ops_parse_cb_return_t 
     191callback_cmd_get_secret_key_passphrase(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) 
     192    { 
     193    ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content; 
     194    /* 
     195    static const ops_key_data_t *decrypt_key; 
     196    const ops_key_data_t *keydata=NULL; 
     197    const ops_secret_key_t *secret; 
     198    */ 
     199 
     200    OPS_USED(cbinfo); 
     201 
     202//    ops_print_packet(content_); 
     203 
     204    switch(content_->tag) 
     205        { 
     206    case OPS_PARSER_CMD_GET_SK_PASSPHRASE: 
     207        /* 
     208          Doing this so the test can be automated. 
     209          Will move this into separate stacked callback later 
     210        */ 
     211        *(content->secret_key_passphrase.passphrase)=ops_malloc_passphrase(no_passphrase); 
     212        return OPS_KEEP_MEMORY; 
     213        break; 
     214         
     215    default: 
     216        return callback_general(content_,cbinfo); 
     217        } 
     218     
     219    return OPS_RELEASE_MEMORY; 
     220    } 
     221 
  • openpgpsdk/trunk/tests/tests.h

    r480 r484  
    2020#define MAXBUF 128 
    2121 
     22ops_parse_cb_return_t 
     23callback_general(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo); 
     24ops_parse_cb_return_t 
     25callback_cmd_get_secret_key(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo); 
     26ops_parse_cb_return_t 
     27callback_cmd_get_secret_key_passphrase(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo); 
     28 
     29ops_keyring_t pub_keyring; 
     30ops_keyring_t sec_keyring; 
    2231#endif 
    2332