Changeset 335

Show
Ignore:
Timestamp:
01/22/06 12:23:40
Author:
ben
Message:

Rearrange error codes, add codes for specific events, read a GPG
secret keyring. Add DSA secret key support.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openpgpsdk/trunk/examples/decrypt.c

    r334 r335  
    11#include <openpgpsdk/packet-parse.h> 
    22#include <openpgpsdk/util.h> 
     3#include <openpgpsdk/keyring.h> 
     4#include <openpgpsdk/accumulate.h> 
     5#include <openpgpsdk/armour.h> 
     6 
    37#include <unistd.h> 
    48#include <string.h> 
    5 #include <openpgpsdk/keyring.h> 
    69#include <fcntl.h> 
    7 #include <openpgpsdk/accumulate.h> 
    8 #include <openpgpsdk/armour.h> 
     10#include <assert.h> 
    911 
    1012static char *pname; 
     
    1416cb_secret_key(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) 
    1517    { 
    16     const ops_parser_content_union_t *content=&content_->content; 
    17     char buffer[1024]; 
    18     size_t n; 
     18    //    const ops_parser_content_union_t *content=&content_->content; 
     19    //    char buffer[1024]; 
     20    //    size_t n; 
    1921 
    2022    OPS_USED(cbinfo); 
     
    2325        { 
    2426    case OPS_PARSER_PTAG: 
     27    case OPS_PTAG_CT_ENCRYPTED_SECRET_KEY: // we get these because we didn't prompt 
     28    case OPS_PARSER_ERROR_UNKNOWN_TAG: 
     29    case OPS_PARSER_ERROR_PACKET_CONSUMED: // only happens after another error we've deemed to be OK 
     30    case OPS_PTAG_CT_SIGNATURE_HEADER: 
     31    case OPS_PTAG_CT_SIGNATURE_FOOTER: 
     32    case OPS_PTAG_CT_SIGNATURE: 
     33    case OPS_PTAG_CT_TRUST: 
    2534        break; 
    2635 
    2736    case OPS_PTAG_CMD_GET_PASSPHRASE: 
     37        /* 
    2838        printf("Passphrase: "); 
    2939        fgets(buffer,sizeof buffer,stdin); 
     
    3444        strcpy(*content->passphrase,buffer); 
    3545        return OPS_KEEP_MEMORY; 
     46        */ 
     47        // we don't want to prompt when reading the keyring 
     48        break; 
    3649 
    3750    default: 
    3851        fprintf(stderr,"Unexpected packet tag=%d (0x%x)\n",content_->tag, 
    3952                content_->tag); 
     53        assert(0); 
    4054        exit(1); 
    4155        } 
     
    5569        fprintf(stderr,"Unexpected packet tag=%d (0x%x)\n",content_->tag, 
    5670                content_->tag); 
    57         exit(1); 
     71        assert(0); 
    5872        } 
    5973 
  • openpgpsdk/trunk/examples/packet-dump.c

    r332 r335  
    3535    print_indent(); 
    3636    printf("%s=",name); 
    37     BN_print_fp(stdout,bn); 
     37    if(bn) 
     38        BN_print_fp(stdout,bn); 
     39    else 
     40        puts("(unset)"); 
    3841    printf("\n"); 
    3942    } 
     
    780783        break; 
    781784 
     785    case OPS_PTAG_CMD_GET_PASSPHRASE: 
     786        printf(">>> ASKED FOR PASSPHRASE <<<\n"); 
     787        break; 
     788 
    782789    case OPS_PTAG_CT_SECRET_KEY: 
     790    case OPS_PTAG_CT_ENCRYPTED_SECRET_KEY: 
    783791        // XXX: fix me 
    784         print_tagname("SECRET_KEY"); 
     792        if(content_->tag == OPS_PTAG_CT_SECRET_KEY) 
     793            print_tagname("SECRET_KEY"); 
     794        else 
     795            print_tagname("ENCRYPTED_SECRET_KEY"); 
    785796        print_public_key(&content->secret_key.public_key); 
    786797 
     
    792803            print_bn("q",content->secret_key.key.rsa.q); 
    793804            print_bn("u",content->secret_key.key.rsa.u); 
     805            break; 
     806 
     807        case OPS_PKA_DSA: 
     808            print_bn("x",content->secret_key.key.dsa.x); 
    794809            break; 
    795810 
  • openpgpsdk/trunk/include/openpgpsdk/packet-parse.h

    r334 r335  
    9292    OPS_PARSE_RAW,      /*!< Callback Raw */ 
    9393    OPS_PARSE_PARSED,   /*!< Callback Parsed */ 
    94     OPS_PARSE_IGNORE  /*!< Don't callback */ 
     94    OPS_PARSE_IGNORE,         /*!< Don't callback */ 
    9595    }; 
    9696 
  • openpgpsdk/trunk/include/openpgpsdk/packet.h

    r334 r335  
    140140    OPS_PTAG_CT_MDC                     =19,    /*!< Modification Detection Code Packet */ 
    141141 
    142     OPS_PARSER_ERROR                    =0x100, /*!< Internal Use: Parser Error */ 
    143     OPS_PARSER_PTAG                     =0x101, /*!< Internal Use: The packet is the "Packet Tag" itself - used when 
     142    OPS_PARSER_PTAG                     =0x100, /*!< Internal Use: The packet is the "Packet Tag" itself - used when 
    144143                                                     callback sends back the PTag. */ 
    145     OPS_PTAG_RAW_SS                     =0x102, /*!< Internal Use: content is raw sig subtag */ 
    146     OPS_PTAG_SS_ALL                     =0x103, /*!< Internal Use: select all subtags */ 
    147     OPS_PARSER_PACKET_END               =0x104, 
    148     OPS_PARSER_ERRCODE                  =0x105, /*! < Internal Use: Parser Error with errcode returned */ 
     144    OPS_PTAG_RAW_SS                     =0x101, /*!< Internal Use: content is raw sig subtag */ 
     145    OPS_PTAG_SS_ALL                     =0x102, /*!< Internal Use: select all subtags */ 
     146    OPS_PARSER_PACKET_END               =0x103, 
    149147 
    150148    /* signature subpackets (0x200-2ff) (type+0x200) */ 
     
    199197    OPS_PTAG_CT_SIGNED_CLEARTEXT_TRAILER=0x300+8, 
    200198    OPS_PTAG_CT_UNARMOURED_TEXT         =0x300+9, 
     199    OPS_PTAG_CT_ENCRYPTED_SECRET_KEY    =0x300+10, // In this case the algorithm specific fields will not be initialised 
    201200 
    202201    /* commands to the callback */ 
    203202    OPS_PTAG_CMD_GET_PASSPHRASE         =0x400, 
     203 
     204 
     205    /* Errors */ 
     206    OPS_PARSER_ERROR                    =0x500, /*!< Internal Use: Parser Error */ 
     207    OPS_PARSER_ERRCODE                  =0x500+1, /*! < Internal Use: Parser Error with errcode returned */ 
     208    OPS_PARSER_ERROR_UNKNOWN_TAG        =0x500+2, 
     209    OPS_PARSER_ERROR_PACKET_CONSUMED    =0x500+3, 
    204210    }; 
    205211 
     
    336342    } ops_rsa_secret_key_t; 
    337343 
     344typedef struct 
     345    { 
     346    BIGNUM *x; 
     347    } ops_dsa_secret_key_t; 
     348 
    338349/** ops_secret_key_union_t 
    339350 */  
     
    341352    { 
    342353    ops_rsa_secret_key_t rsa; 
     354    ops_dsa_secret_key_t dsa; 
    343355    } ops_secret_key_union_t; 
    344356 
     
    349361    OPS_S2KU_NONE=0, 
    350362    OPS_S2KU_ENCRYPTED_AND_HASHED=254, 
    351     OPS_S2KU_ENCRYPTED=255 
     363    OPS_S2KU_ENCRYPTED=255, 
     364    OPS_S2KU_NOT_SET=256 
    352365    } ops_s2k_usage_t; 
    353366 
  • openpgpsdk/trunk/src/accumulate.c

    r320 r335  
    3232        { 
    3333    case OPS_PTAG_CT_PUBLIC_KEY: 
     34    case OPS_PTAG_CT_SECRET_KEY: 
     35    case OPS_PTAG_CT_ENCRYPTED_SECRET_KEY: 
    3436        //      printf("New key\n"); 
    3537        ++keyring->nkeys; 
  • openpgpsdk/trunk/src/packet-parse.c

    r334 r335  
    119119#define WARN(warn)      do { C.error.error=warn; CB(OPS_PARSER_ERROR,&content);; } while(0) 
    120120#define WARNP(info,warn)        do { C.error.error=warn; CBP(info,OPS_PARSER_ERROR,&content); } while(0) 
     121#define SWARNP(errtype,info,warn)       do { C.error.error=warn; CBP(info,errtype,&content); } while(0) 
    121122/*! \todo descr ERR1 macro */ 
    122123#define ERR1P(info,fmt,x)       do { format_error(&content,(fmt),(x)); CBP(info,OPS_PARSER_ERROR,&content); return ops_false; } while(0) 
     
    593594    free(trailer->hash); 
    594595    trailer->hash=NULL; 
     596    } 
     597 
     598void ops_cmd_get_passphrase_free(char **passphrase) 
     599    { 
     600    free(*passphrase); 
     601    *passphrase=NULL; 
    595602    } 
    596603 
     
    725732    case OPS_PARSER_ERROR: 
    726733    case OPS_PARSER_ERRCODE: 
     734    case OPS_PARSER_ERROR_UNKNOWN_TAG: 
     735    case OPS_PARSER_ERROR_PACKET_CONSUMED: 
    727736        break; 
    728737 
    729738    case OPS_PTAG_CT_SECRET_KEY: 
     739    case OPS_PTAG_CT_ENCRYPTED_SECRET_KEY: 
    730740        ops_secret_key_free(&c->content.secret_key); 
    731741        break; 
     
    733743    case OPS_PTAG_CT_PK_SESSION_KEY: 
    734744        ops_pk_session_key_free(&c->content.pk_session_key); 
     745        break; 
     746 
     747    case OPS_PTAG_CMD_GET_PASSPHRASE: 
     748        ops_cmd_get_passphrase_free(c->content.passphrase); 
    735749        break; 
    736750 
     
    17361750        break; 
    17371751 
     1752    case OPS_PKA_DSA: 
     1753        free_BN(&key->key.dsa.x); 
     1754        break; 
     1755 
    17381756    default: 
     1757        fprintf(stderr,"Unknown algorithm: %d\n",key->public_key.algorithm); 
    17391758        assert(0); 
    17401759        } 
    17411760 
    17421761    ops_public_key_free(&key->public_key); 
     1762    } 
     1763 
     1764static int consume_packet(ops_region_t *region,ops_parse_info_t *parse_info, 
     1765                          ops_boolean_t warn) 
     1766    { 
     1767    ops_data_t remainder; 
     1768    ops_parser_content_t content; 
     1769 
     1770    if(read_data(&remainder,region,parse_info)) 
     1771        { 
     1772        /* now throw it away */ 
     1773        data_free(&remainder); 
     1774        if(warn) 
     1775            SWARNP(OPS_PARSER_ERROR_PACKET_CONSUMED,parse_info, 
     1776                   "Remainder of packet consumed and discarded."); 
     1777        } 
     1778    else if(warn) 
     1779        WARNP(parse_info,"Problem consuming remainder of error packet."); 
     1780    else 
     1781        return 0; 
     1782 
     1783    return 1; 
    17431784    } 
    17441785 
     
    17481789    unsigned char c[1]; 
    17491790 
     1791    memset(&content,'\0',sizeof content); 
    17501792    if(!parse_public_key_data(&C.secret_key.public_key,region,parse_info)) 
    17511793        return 0; 
     
    17531795        return 0; 
    17541796    C.secret_key.s2k_usage=c[0]; 
     1797 
     1798    if(C.secret_key.s2k_usage == OPS_S2KU_ENCRYPTED 
     1799       || C.secret_key.s2k_usage == OPS_S2KU_ENCRYPTED_AND_HASHED) 
     1800        { 
     1801        if(!limited_read(c,1,region,parse_info)) 
     1802            return 0; 
     1803        C.secret_key.algorithm=c[0]; 
     1804 
     1805        if(!limited_read(c,1,region,parse_info)) 
     1806            return 0; 
     1807        C.secret_key.s2k_specifier=c[0]; 
     1808        } 
     1809    else if(C.secret_key.s2k_usage != OPS_S2KU_NONE) 
     1810        { 
     1811        C.secret_key.algorithm=C.secret_key.s2k_usage; 
     1812        C.secret_key.s2k_usage=OPS_S2KU_NOT_SET; 
     1813        } 
     1814 
    17551815    if(C.secret_key.s2k_usage != OPS_S2KU_NONE) 
    17561816        { 
     
    17591819        char *passphrase; 
    17601820 
    1761         assert(C.secret_key.s2k_usage == OPS_S2KU_ENCRYPTED 
    1762                || C.secret_key.s2k_usage == OPS_S2KU_ENCRYPTED_AND_HASHED); 
    1763  
    1764         if(!limited_read(c,1,region,parse_info)) 
    1765             return 0; 
    1766         C.secret_key.algorithm=c[0]; 
    1767  
    1768         if(!limited_read(c,1,region,parse_info)) 
    1769             return 0; 
    1770         C.secret_key.s2k_specifier=c[0]; 
    1771  
    17721821        n=ops_block_size(C.secret_key.algorithm); 
    17731822        assert(n > 0 && n <= OPS_MAX_BLOCK_SIZE); 
     
    17761825            return 0; 
    17771826 
     1827        passphrase=NULL; 
    17781828        pc.content.passphrase=&passphrase; 
    17791829        CBP(parse_info,OPS_PTAG_CMD_GET_PASSPHRASE,&pc); 
     1830        if(!passphrase) 
     1831            { 
     1832            if(!consume_packet(region,parse_info,ops_false)) 
     1833               return 0; 
     1834            CBP(parse_info,OPS_PTAG_CT_ENCRYPTED_SECRET_KEY,&content); 
     1835            return 1; 
     1836            } 
    17801837        } 
    17811838 
     
    18551912    return 1; 
    18561913    } 
    1857      
    18581914 
    18591915/** Parse one packet. 
     
    19812037        format_error(&content,"Format error (unknown content tag %d)", 
    19822038                     C.ptag.content_tag); 
    1983         CBP(parse_info,OPS_PARSER_ERROR,&content); 
     2039        CBP(parse_info,OPS_PARSER_ERROR_UNKNOWN_TAG,&content); 
    19842040        r=0; 
    19852041        } 
     
    19882044 
    19892045    if(region.length != region.length_read) 
    1990         { 
    1991         ops_data_t remainder; 
    1992  
    1993         if (read_data(&remainder,&region,parse_info)) 
    1994             { 
    1995             /* now throw it away */ 
    1996             data_free(&remainder); 
    1997             WARNP(parse_info,"Remainder of packet consumed and discarded."); 
    1998             } 
    1999         else 
    2000             WARNP(parse_info,"Problem consuming remainder of error packet."); 
    2001         } 
     2046        consume_packet(&region,parse_info,ops_true); 
    20022047 
    20032048    /* set pktlen */