Changeset 485

Show
Ignore:
Timestamp:
08/13/07 18:48:48
Author:
rachel
Message:

RSA encryption produces packets which can be decrypted by GPG.
MDC error still to fix.
Some refactoring to re-use common code in tests.

Files:

Legend:

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

    r482 r485  
    114114void ops_reader_pop_hash(ops_parse_info_t *pinfo); 
    115115 
    116 int ops_decrypt_mpi(unsigned char *buf,unsigned buflen,const BIGNUM *encmpi, 
     116int ops_decrypt_and_unencode_mpi(unsigned char *buf,unsigned buflen,const BIGNUM *encmpi, 
    117117                    const ops_secret_key_t *skey); 
    118118ops_boolean_t ops_encrypt_mpi(const unsigned char *buf, size_t buflen, 
  • openpgpsdk/trunk/include/openpgpsdk/std_print.h

    r484 r485  
    99#include "openpgpsdk/keyring.h" 
    1010 
     11void print_bn( const char *name,  
     12                      const BIGNUM *bn); 
    1113void ops_print_pk_session_key(ops_content_tag_t tag, 
    1214                          const ops_pk_session_key_t *key); 
  • openpgpsdk/trunk/src/advanced/adv_create.c

    r484 r485  
    11/** \file 
    22 */ 
     3 
     4#include <openssl/cast.h> 
    35 
    46#include <openpgpsdk/create.h> 
     
    751753    { return ops_stacked_write(src,length,errors,winfo); } 
    752754 
    753 void ops_create_m_buf(ops_pk_session_key_t *session_key, unsigned char *buf) 
     755static void create_unencoded_m_buf(ops_pk_session_key_t *session_key, unsigned char *m_buf) 
    754756    { 
    755757    int i=0; 
    756     unsigned int checksum=0; 
    757  
     758    unsigned long checksum=0; 
     759 
     760    // m_buf is the buffer which will be encoded in PKCS#1 block 
     761    // encoding to form the "m" value used in the  
     762    // Public Key Encrypted Session Key Packet 
    758763    // as defined in RFC Section 5.1 "Public-Key Encrypted Session Key Packet" 
    759764 
    760     buf[0]=session_key->symmetric_algorithm; 
    761  
    762     // \todo parameterise key length 
    763     for (i=0; i<256/8; i++) 
     765    m_buf[0]=session_key->symmetric_algorithm; 
     766    assert(session_key->symmetric_algorithm==OPS_SA_CAST5); 
     767 
     768    for (i=0; i<CAST_KEY_LENGTH; i++) 
    764769        { 
    765770        checksum+=session_key->key[i]; 
    766         buf[1+i]=session_key->key[i]; 
     771        m_buf[1+i]=session_key->key[i]; 
    767772        } 
    768773    checksum = checksum % 65536; 
    769774 
    770     buf[i++]=checksum >> 8; 
    771     buf[i++]=checksum & 0xFF; 
     775    m_buf[1+i++]=checksum >> 8; 
     776    m_buf[1+i++]=checksum & 0xFF; 
     777    } 
     778 
     779ops_boolean_t encode_m_buf(const unsigned char *M, size_t mLen, 
     780                           const ops_public_key_t *pkey, 
     781                           unsigned char* EM 
     782
     783    { 
     784    //unsigned char encmpibuf[8192]; 
     785    //    unsigned char EM[8192]; 
     786    unsigned int k; 
     787    unsigned i; 
     788 
     789    // implementation of EME-PKCS1-v1_5-ENCODE, as defined in OpenPGP RFC 
     790     
     791    assert(pkey->algorithm == OPS_PKA_RSA); 
     792 
     793    k=BN_num_bytes(pkey->key.rsa.n); 
     794    assert(mLen <= k-11); 
     795    if (mLen > k-11) 
     796        { 
     797        fprintf(stderr,"message too long\n"); 
     798        return ops_false; 
     799        } 
     800 
     801    // these two bytes defined by RFC 
     802    EM[0]=0x00; 
     803    EM[1]=0x02; 
     804 
     805    // add non-zero random bytes of length k - mLen -3 
     806    for(i=2 ; i < k-mLen-1 ; ++i) 
     807        do 
     808            ops_random(EM+i, 1); 
     809        while(EM[i] == 0); 
     810 
     811    assert (i >= 8+2); 
     812 
     813    EM[i++]=0; 
     814 
     815    memcpy(EM+i, M, mLen); 
     816     
     817 
     818    /* 
     819    //    int i=0; 
     820    fprintf(stderr,"Encoded Message: \n"); 
     821    for (i=0; i<mLen; i++) 
     822        fprintf(stderr,"%2x ", EM[i]); 
     823    fprintf(stderr,"\n"); 
     824    */ 
     825 
     826    return ops_true; 
    772827    } 
    773828 
    774829ops_pk_session_key_t *ops_create_pk_session_key(const ops_key_data_t *key) 
    775830    { 
    776     unsigned char buf[256/8+1+2]; 
     831    /* 
     832     * Creates a random session key and encrypts it for the given key 
     833     * 
     834     * Session Key is for use with a SK algo,  
     835     * can be any, we're hardcoding CAST5 for now 
     836     * 
     837     * Encryption used is PK,  
     838     * can be any, we're hardcoding RSA for now 
     839     */ 
     840 
     841    const ops_public_key_t* pub_key=ops_get_public_key_from_data(key); 
     842    const size_t sz_unencoded_m_buf=CAST_KEY_LENGTH+1+2; 
     843    unsigned char unencoded_m_buf[sz_unencoded_m_buf]; 
     844 
     845    const size_t sz_encoded_m_buf=BN_num_bytes(pub_key->key.rsa.n); 
     846    unsigned char encoded_m_buf[sz_encoded_m_buf]; 
     847 
    777848    ops_pk_session_key_t *session_key=ops_mallocz(sizeof *session_key); 
    778849 
     
    780851    session_key->version=OPS_PKSK_V3; 
    781852    memcpy(session_key->key_id, key->key_id, sizeof session_key->key_id); 
     853    /* 
     854    fprintf(stderr,"Encrypting for RSA key id : "); 
     855    unsigned int i=0; 
     856    for (i=0; i<sizeof session_key->key_id; i++) 
     857        fprintf(stderr,"%2x ", key->key_id[i]); 
     858    fprintf(stderr,"\n"); 
     859    */ 
    782860 
    783861    assert(key->key.pkey.algorithm == OPS_PKA_RSA); 
    784862    session_key->algorithm=key->key.pkey.algorithm; 
     863    /* 
    785864    session_key->symmetric_algorithm=OPS_SA_AES_256; 
    786865    ops_random(session_key->key, 256/8); 
    787  
    788     ops_create_m_buf(session_key, buf); 
    789  
    790     // and encode it 
    791     if(!ops_encrypt_mpi(buf, (256/8+1+2), &key->key.pkey, &session_key->parameters)) 
    792         return NULL; 
     866    */ 
     867    session_key->symmetric_algorithm=OPS_SA_CAST5; 
     868 
     869    ops_random(session_key->key, CAST_KEY_LENGTH); 
     870    /* 
     871    fprintf(stderr,"CAST5 session key created (len=%d):\n ", CAST_KEY_LENGTH); 
     872    for (i=0; i<CAST_KEY_LENGTH; i++) 
     873        fprintf(stderr,"%2x ", session_key->key[i]); 
     874    fprintf(stderr,"\n"); 
     875    */ 
     876 
     877    create_unencoded_m_buf(session_key, &unencoded_m_buf[0]); 
     878    /* 
     879    printf("unencoded m buf:\n"); 
     880    for (i=0; i<sz_unencoded_m_buf; i++) 
     881        printf("%2x ", unencoded_m_buf[i]); 
     882    printf("\n"); 
     883    */ 
     884    encode_m_buf(&unencoded_m_buf[0], sz_unencoded_m_buf, pub_key, &encoded_m_buf[0]); 
     885 
     886    // and encrypt it 
     887    if(!ops_encrypt_mpi(encoded_m_buf, sz_encoded_m_buf, pub_key, &session_key->parameters)) 
     888        return NULL; 
    793889 
    794890    return session_key; 
     
    842938 
    843939        assert(done==len); 
     940 
     941        /* 
     942        fprintf(stderr,"WRITING:\nunencrypted: "); 
     943        int i=0; 
     944        for (i=0; i<16; i++) 
     945            fprintf(stderr,"%2x ", buf[i]); 
     946        fprintf(stderr,"\n"); 
     947        fprintf(stderr,"encrypted:   "); 
     948        for (i=0; i<16; i++) 
     949            fprintf(stderr,"%2x ", encbuf[i]); 
     950        fprintf(stderr,"\n"); 
     951        */ 
     952 
    844953        if (!ops_stacked_write(encbuf,len,errors,winfo)) 
    845954            return ops_false; 
     
    9301039    ops_writer_push(info,encrypted_writer,encrypted_finaliser, 
    9311040                    encrypted_destroyer,arg); 
     1041 
     1042    /* 
     1043    fprintf(stderr,"writing %ld + %d + %ld\n", sz_preamble, len, ops_memory_get_length(mem_mdc)); 
     1044    */ 
    9321045 
    9331046    if (!ops_write(preamble, sz_preamble,info) 
     
    9971110 
    9981111    int done=ops_encrypt_se(&crypt_info, encrypted, data, len); 
    999     printf("len=%d, done: %d\n", len, done); 
     1112    assert(done==len); 
     1113    //    printf("len=%d, done: %d\n", len, done); 
    10001114 
    10011115    return ops_write_ptag(OPS_PTAG_CT_SE_DATA, info) 
  • openpgpsdk/trunk/src/advanced/adv_crypto.c

    r473 r485  
    77#include <openpgpsdk/final.h> 
    88 
    9 int ops_decrypt_mpi(unsigned char *buf,unsigned buflen,const BIGNUM *encmpi, 
     9int ops_decrypt_and_unencode_mpi(unsigned char *buf,unsigned buflen,const BIGNUM *encmpi, 
    1010                    const ops_secret_key_t *skey) 
    1111    { 
     
    2323    assert(skey->public_key.algorithm == OPS_PKA_RSA); 
    2424 
     25    /* 
     26    fprintf(stderr,"\nDECRYPTING\n"); 
     27    fprintf(stderr,"encrypted data     : "); 
     28    for (i=0; i<16; i++) 
     29        fprintf(stderr,"%2x ", encmpibuf[i]); 
     30    fprintf(stderr,"\n"); 
     31    */ 
     32 
    2533    n=ops_rsa_private_decrypt(mpibuf,encmpibuf,(BN_num_bits(encmpi)+7)/8, 
    2634                              &skey->key.rsa,&skey->public_key.key.rsa); 
     35    assert(n!=-1); 
     36 
     37    /* 
     38    fprintf(stderr,"decrypted encoded m buf     : "); 
     39    for (i=0; i<16; i++) 
     40        fprintf(stderr,"%2x ", mpibuf[i]); 
     41    fprintf(stderr,"\n"); 
     42    */ 
    2743 
    2844    if(n <= 0) 
     
    3854 
    3955    if(mpibuf[0] != 0 || mpibuf[1] != 2) 
    40        return ops_false; 
     56        return ops_false; 
    4157 
    4258    // Skip the random bytes. 
    4359    for(i=2 ; i < n && mpibuf[i] ; ++i) 
    44        
     60       
    4561 
    4662    if(i == n || i < 10) 
    47        return ops_false; 
     63        return ops_false; 
    4864 
    4965    // Skip the zero 
    5066    ++i; 
    5167 
     68    // this is the unencoded m buf 
    5269    if((unsigned)(n-i) <= buflen) 
    53         memcpy(buf,mpibuf+i,n-i); 
     70        memcpy(buf,mpibuf+i,n-i); 
     71 
     72    /* 
     73    printf("unencoded m buf:\n"); 
     74    int j; 
     75    for (j=0; j<n-i; j++) 
     76        printf("%2x ",buf[j]); 
     77    printf("\n"); 
     78    */ 
    5479 
    5580    return n-i; 
    5681    } 
    5782 
    58 ops_boolean_t ops_encrypt_mpi(const unsigned char *buf, size_t buflen, 
     83ops_boolean_t ops_encrypt_mpi(const unsigned char *encoded_m_buf, 
     84                              const size_t sz_encoded_m_buf, 
    5985                              const ops_public_key_t *pkey, 
    6086                              ops_pk_session_key_parameters_t *skp) 
    6187    { 
     88    assert(sz_encoded_m_buf==(size_t) BN_num_bytes(pkey->key.rsa.n)); 
     89 
    6290    unsigned char encmpibuf[8192]; 
    63     unsigned char padded[8192]; 
    64     int n; 
     91    int n=0; 
     92#ifdef XXX 
     93    unsigned char EM[8192]; 
     94    int k; 
    6595    unsigned i; 
    6696 
     
    6999    assert(pkey->algorithm == OPS_PKA_RSA); 
    70100 
    71     n=BN_num_bytes(pkey->key.rsa.n); 
     101    k=BN_num_bytes(pkey->key.rsa.n); 
     102    /* 
     103    printf("k=%d (length in octets of key modulus)\n",k); 
     104    printf("mLen=%d\n",mLen); 
     105    */ 
     106    assert(mLen <= k-11); 
     107    if (mLen > k-11) 
     108        { 
     109        fprintf(stderr,"message too long\n"); 
     110        return false; 
     111        } 
     112 
     113    // output will be written to ?? 
    72114 
    73115    // these two bytes defined by RFC 
    74     padded[0]=0; 
    75     padded[1]=2; 
     116    EM[0]=0x00; 
     117    EM[1]=0x02; 
     118 
    76119    // add non-zero random bytes of length k - mLen -3 
    77     for(i=2 ; i < n-buflen-1 ; ++i) 
    78        do 
    79            ops_random(padded+i, 1); 
    80        while(padded[i] == 0); 
     120    for(i=2 ; i < k-mLen-1 ; ++i) 
     121        do 
     122            ops_random(EM+i, 1); 
     123        while(EM[i] == 0); 
    81124 
    82125    assert (i >= 8+2); 
    83126 
    84     padded[i++]=0; 
     127    EM[i++]=0; 
    85128 
    86     memcpy(padded+i, buf, buflen); 
     129    memcpy(EM+i, M, mLen); 
    87130     
    88     n=ops_rsa_public_encrypt(encmpibuf, padded, n, &pkey->key.rsa); 
     131    /* 
     132    int i=0; 
     133    fprintf(stderr,"Encoded Message: \n"); 
     134    for (i=0; i<mLen; i++) 
     135        fprintf(stderr,"%2x ", EM[i]); 
     136    fprintf(stderr,"\n"); 
     137    */ 
     138 
     139#endif 
     140    n=ops_rsa_public_encrypt(encmpibuf, encoded_m_buf, sz_encoded_m_buf, &pkey->key.rsa); 
     141    assert(n!=-1); 
    89142 
    90143    if(n <= 0) 
     
    93146    skp->rsa.encrypted_m=BN_bin2bn(encmpibuf, n, NULL); 
    94147 
     148    /* 
     149    fprintf(stderr,"encrypted mpi buf     : "); 
     150    int i; 
     151    for (i=0; i<16; i++) 
     152        fprintf(stderr,"%2x ", encmpibuf[i]); 
     153    fprintf(stderr,"\n"); 
     154    */ 
     155 
    95156    return ops_true; 
    96157    } 
  • openpgpsdk/trunk/src/advanced/adv_openssl_crypto.c

    r470 r485  
    44#include <openpgpsdk/configure.h> 
    55#include <openpgpsdk/crypto.h> 
     6#include <openpgpsdk/std_print.h> 
    67#include <openssl/md5.h> 
    78#include <openssl/sha.h> 
     
    167168    n=RSA_private_decrypt(length,in,out,orsa,RSA_NO_PADDING); 
    168169 
     170    char errbuf[1024]; 
     171    errbuf[0]='\0'; 
     172    if (n==-1) 
     173        { 
     174        unsigned long err=ERR_get_error(); 
     175        ERR_error_string(err,&errbuf[0]); 
     176        fprintf(stderr,"openssl error : %s\n",errbuf); 
     177        } 
    169178    orsa->n=orsa->d=orsa->p=orsa->q=NULL; 
    170179    RSA_free(orsa); 
     
    179188    int n; 
    180189 
     190    //    printf("ops_rsa_public_encrypt: length=%ld\n", length); 
     191 
    181192    orsa=RSA_new(); 
    182193    orsa->n=rsa->n; 
    183194    orsa->e=rsa->e; 
    184195 
     196    //    printf("len: %ld\n", length); 
     197    //    ops_print_bn("n: ", orsa->n); 
     198    //    ops_print_bn("e: ", orsa->e); 
    185199    n=RSA_public_encrypt(length,in,out,orsa,RSA_NO_PADDING); 
     200 
     201    if (n==-1) 
     202        { 
     203        BIO *out; 
     204        out=BIO_new_fd(fileno(stderr), BIO_NOCLOSE); 
     205        ERR_print_errors(out); 
     206        } 
    186207 
    187208    orsa->n=orsa->e=NULL; 
  • openpgpsdk/trunk/src/advanced/adv_packet-parse.c

    r480 r485  
    22 * \brief Parser for OpenPGP packets 
    33 */ 
     4 
     5#include <openssl/cast.h> 
    46 
    57#include <openpgpsdk/packet.h> 
     
    21662168 
    21672169static int parse_pk_session_key(ops_region_t *region, 
    2168                                ops_parse_info_t *pinfo) 
     2170                                ops_parse_info_t *pinfo) 
    21692171    { 
    21702172    unsigned char c[1]; 
    21712173    ops_parser_content_t content; 
    21722174    ops_parser_content_t pc; 
    2173     unsigned char buf[8192]; 
     2175    //    unsigned char buf[8192]; 
    21742176    int n; 
    21752177    BIGNUM *enc_m; 
    21762178    unsigned k; 
    21772179    const ops_secret_key_t *secret; 
     2180     
     2181    const size_t sz_unencoded_m_buf=CAST_KEY_LENGTH+1+2; 
     2182    unsigned char unencoded_m_buf[sz_unencoded_m_buf]; 
     2183     
     2184    //    const size_t sz_encoded_m_buf=BN_num_bytes(pub_key->key.rsa.n); 
     2185    //    const size_t sz_encoded_m_buf=128; //\todo FIXME RW 
     2186    //unsigned char encoded_m_buf[sz_encoded_m_buf]; 
    21782187 
    21792188    if(!limited_read(c,1,region,pinfo)) 
     
    21892198        return 0; 
    21902199 
     2200    /* 
     2201    int i; 
     2202    int x=sizeof C.pk_session_key.key_id; 
     2203    printf("session key id: x=%d\n",x); 
     2204    for (i=0; i<x; i++) 
     2205        printf("%2x ", C.pk_session_key.key_id[i]); 
     2206    printf("\n"); 
     2207    */ 
     2208 
    21912209    if(!limited_read(c,1,region,pinfo)) 
    21922210        return 0; 
     
    22312249        } 
    22322250 
    2233     n=ops_decrypt_mpi(buf,sizeof buf,enc_m,secret); 
     2251    //    n=ops_decrypt_mpi(buf,sizeof buf,enc_m,secret); 
     2252    n=ops_decrypt_and_unencode_mpi(unencoded_m_buf,sizeof unencoded_m_buf,enc_m,secret); 
    22342253 
    22352254    if(n < 1) 
    22362255        ERRP(pinfo,"decrypted message too short"); 
    22372256 
    2238     C.pk_session_key.symmetric_algorithm=buf[0]; 
     2257    // PKA 
     2258    C.pk_session_key.symmetric_algorithm=unencoded_m_buf[0]; 
     2259    assert(unencoded_m_buf[0]==OPS_SA_CAST5); 
    22392260    k=ops_key_size(C.pk_session_key.symmetric_algorithm); 
    22402261 
    22412262    if((unsigned)n != k+3) 
    2242        ERR2P(pinfo,"decrypted message wrong length (got %d expected %d)", 
    2243              n,k+3); 
     2263        ERR2P(pinfo,"decrypted message wrong length (got %d expected %d)", 
     2264              n,k+3); 
    22442265     
    22452266    assert(k <= sizeof C.pk_session_key.key); 
    22462267 
    2247     memcpy(C.pk_session_key.key,buf+1,k); 
    2248  
    2249     C.pk_session_key.checksum=buf[k+1]+(buf[k+2] << 8); 
     2268    memcpy(C.pk_session_key.key,unencoded_m_buf+1,k); 
     2269 
     2270    /* 
     2271    printf("session key recovered (len=%d):\n",k); 
     2272    unsigned int j; 
     2273    for(j=0; j<k; j++) 
     2274        printf("%2x ", C.pk_session_key.key[j]); 
     2275    printf("\n"); 
     2276    */ 
     2277 
     2278    C.pk_session_key.checksum=unencoded_m_buf[k+1]+(unencoded_m_buf[k+2] << 8); 
     2279    /* 
     2280    printf("checksum: %2x %2x\n", unencoded_m_buf[k+1], unencoded_m_buf[k+2]); 
     2281    */ 
    22502282 
    22512283    // XXX: Check checksum! 
     
    22542286 
    22552287    ops_crypt_any(&pinfo->decrypt,C.pk_session_key.symmetric_algorithm); 
     2288    unsigned char *iv=ops_mallocz(pinfo->decrypt.blocksize); 
     2289    pinfo->decrypt.set_iv(&pinfo->decrypt, iv); 
    22562290    pinfo->decrypt.set_key(&pinfo->decrypt,C.pk_session_key.key); 
    2257  
     2291    ops_encrypt_init(&pinfo->decrypt); 
    22582292    return 1; 
    22592293    } 
     
    22982332        if(buf[b-2] != buf[b] || buf[b-1] != buf[b+1]) 
    22992333            { 
     2334            fprintf(stderr,"Bad symmetric decrypt (%02x%02x vs %02x%02x)\n", 
     2335                    buf[b-2],buf[b-1],buf[b],buf[b+1]); 
    23002336            //            ERR4P(pinfo,"Bad symmetric decrypt (%02x%02x vs %02x%02x)", 
    2301             //              buf[b-2],buf[b-1],buf[b],buf[b+1]); 
     2337            //                  buf[b-2],buf[b-1],buf[b],buf[b+1]); 
    23022338            return 0; 
    23032339            } 
     
    23272363        if (memcmp(mdc_hash,hashed,OPS_SHA1_HASH_SIZE)) 
    23282364            { 
    2329             fprintf(stderr,"Hash is bad"); 
     2365            fprintf(stderr,"Hash is bad\n"); 
    23302366            //            ERRP(pinfo,"Bad hash in MDC"); 
    23312367            return 0; 
  • openpgpsdk/trunk/src/advanced/adv_symmetric.c

    r480 r485  
    102102                                  arg->decrypted, 
    103103                                  buffer,n); 
     104 
     105                /* 
     106        fprintf(stderr,"READING:\nencrypted: "); 
     107        int i=0; 
     108        for (i=0; i<16; i++) 
     109            fprintf(stderr,"%2x ", buffer[i]); 
     110        fprintf(stderr,"\n"); 
     111        fprintf(stderr,"decrypted:   "); 
     112        for (i=0; i<16; i++) 
     113            fprintf(stderr,"%2x ", arg->decrypted[i]); 
     114        fprintf(stderr,"\n"); 
     115                */ 
    104116                } 
    105117            else 
  • openpgpsdk/trunk/src/standard/std_print.c

    r484 r485  
    1818static int indent=0; 
    1919 
    20 static void print_bn( const char *name,  
     20void print_bn( const char *name,  
    2121                      const BIGNUM *bn); 
    2222#ifdef NOTYETUSED 
     
    225225    } 
    226226 
    227 static void print_bn( const char *name, const BIGNUM *bn) 
     227void print_bn( const char *name, const BIGNUM *bn) 
    228228    { 
    229229    print_indent(); 
  • openpgpsdk/trunk/tests/Makefile.template

    r480 r485  
    1414TESTSRC=tests.c \ 
    1515                test_packet_types.c \ 
    16                 test_crypt_mpi.c test_rsa_decrypt.c test_rsa_encrypt.c 
     16                test_crypt_mpi.c test_rsa_decrypt.c test_rsa_encrypt.c \ 
     17                test_crypto.c 
     18 
    1719TESTOBJ=$(TESTSRC:.c=.o) 
    1820 
  • openpgpsdk/trunk/tests/test_crypt_mpi.c

    r474 r485  
    11#include "CUnit/Basic.h" 
     2 
     3#include <openssl/cast.h> 
    24 
    35#include "tests.h" 
     
    810#include "openpgpsdk/create.h" 
    911 
    10 static char secring[MAXBUF+1]; 
    11 static char pubring[MAXBUF+1]; 
    12 static ops_keyring_t pub_keyring; 
    13 static ops_keyring_t sec_keyring; 
     12//static char secring[MAXBUF+1]; 
     13//static char pubring[MAXBUF+1]; 
    1414static const ops_key_data_t *pubkey; 
    1515static const ops_key_data_t *seckey; 
     
    1717int init_suite_crypt_mpi(void) 
    1818    { 
     19#ifdef XXX 
    1920    static char keydetails[MAXBUF+1]; 
    2021    int fd=0; 
     
    5556 
    5657    char keyid[]="Alpha (RSA, no passphrase) <alpha@test.com>"; 
    57     pubkey=ops_keyring_find_key_by_userid(&pub_keyring,keyid); 
    58     seckey=ops_keyring_find_key_by_userid(&sec_keyring,keyid); 
     58#endif 
     59    pubkey=ops_keyring_find_key_by_userid(&pub_keyring,alpha_user_id); 
     60    //    seckey=ops_keyring_find_key_by_userid(&sec_keyring,keyid); 
     61    seckey=ops_keyring_find_key_by_userid(&sec_keyring,alpha_user_id); 
    5962 
    6063    // Return success 
     
    6467int clean_suite_crypt_mpi(void) 
    6568    { 
     69         
     70#ifdef XXX 
    6671    char cmd[MAXBUF+1]; 
    67          
    6872    /* Close OPS */ 
    6973     
    7074    ops_keyring_free(&pub_keyring); 
    7175    ops_keyring_free(&sec_keyring); 
     76#endif 
     77 
    7278    ops_finish(); 
    7379 
     80#ifdef XXX 
    7481    /* Remove test dir and files */ 
    7582    snprintf(cmd,MAXBUF,"rm -rf %s", dir); 
     
    7986        return 1; 
    8087        } 
     88#endif 
    8189     
     90    reset_vars(); 
     91 
    8292    return 0; 
    8393    } 
     
    8595void test_crypt_mpi(void) 
    8696    { 
    87 #define BSZ (256/8+1+2) 
     97    // hardcoded using CAST 
     98#define BSZ (CAST_KEY_LENGTH+1+2) 
    8899 
    89100    unsigned char in[BSZ]; 
     
    92103    ops_boolean_t rtn; 
    93104     
    94     ops_pk_session_key_t *session_key=ops_create_pk_session_key(pubkey); 
     105    ops_pk_session_key_t *encrypted_pk_session_key=NULL; 
     106 
     107    encrypted_pk_session_key=ops_create_pk_session_key(pubkey); 
    95108 
    96109    // recreate what was encrypted 
    97     ops_create_m_buf(session_key, in); 
     110    //    ops_create_m_buf(session_key, in); 
    98111 
    99112    //    CU_ASSERT(session_key); 
     
    102115 
    103116    // decrypt it 
    104     rtn=ops_decrypt_mpi(out,BSZ, session_key->parameters.rsa.encrypted_m, &seckey->key.skey); 
     117    rtn=ops_decrypt_and_unencode_mpi(out,BSZ, encrypted_pk_session_key->parameters.rsa.encrypted_m, &seckey->key.skey); 
    105118 
    106119    // [0] is the symmetric algorithm 
  • openpgpsdk/trunk/tests/test_packet_types.c

    r484 r485  
    1616#include "tests.h" 
    1717 
    18 static unsigned char* literal_data=NULL; 
    19 static size_t sz_literal_data=0; 
    2018static unsigned char* mdc_data=NULL; 
    2119static size_t sz_mdc_data=0; 
     
    3331int init_suite_packet_types(void) 
    3432    { 
    35     char keydetails[MAXBUF+1]; 
    36     char keyring_name[MAXBUF+1]; 
    37     int fd=0; 
    38     char cmd[MAXBUF+1]; 
     33    //    char keydetails[MAXBUF+1]; 
     34    //    char keyring_name[MAXBUF+1]; 
     35    //    int fd=0; 
     36    //    char cmd[MAXBUF+1]; 
    3937 
    4038    // Initialise OPS  
    4139    ops_init(); 
    4240 
     41#ifdef XXX 
    4342    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"; 
    4443    // Create temp directory 
    4544    if (!mktmpdir()) 
    46        return 1; 
     45        return 1; 
    4746 
    4847    /* 
     
    6564 
    6665    // read keyrings 
     66 
    6767    snprintf(keyring_name,MAXBUF,"%s/pubring.gpg", dir); 
    6868    ops_keyring_read(&pub_keyring,keyring_name); 
    6969 
    70     // read keyring 
    7170    snprintf(keyring_name,MAXBUF,"%s/secring.gpg", dir); 
    7271    ops_keyring_read(&sec_keyring,keyring_name); 
     72#endif 
    7373 
    7474    // Return success 
     
    8282    ops_finish(); 
    8383 
     84    reset_vars(); 
     85 
    8486    return 0; 
    8587    } 
    8688 
    87 static ops_parse_cb_return_t 
    88 callback_literal_data(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) 
    89     { 
    90     ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content; 
    91  
    92     OPS_USED(cbinfo); 
    93  
    94     //    ops_print_packet(content_); 
    95  
    96     // Read data from packet into static buffer 
    97     switch(content_->tag) 
    98         { 
    99     case OPS_PTAG_CT_LITERAL_DATA_BODY: 
    100         sz_literal_data=content->literal_data_body.length; 
    101         literal_data=ops_mallocz(sz_literal_data+1); 
    102         memcpy(literal_data,content->literal_data_body.data,sz_literal_data); 
    103         break; 
    104  
    105     case OPS_PTAG_CT_LITERAL_DATA_HEADER: 
    106         // ignore 
    107         break; 
    108  
    109     default: 
    110         return callback_general(content_,cbinfo); 
    111         } 
    112  
    113     return OPS_RELEASE_MEMORY; 
    114     } 
    115   
    11689static ops_parse_cb_return_t 
    11790callback_mdc(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) 
     
    228201    ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); 
    229202    rtn=ops_parse(pinfo); 
     203    CU_ASSERT(rtn==1); 
    230204 
    231205    /* 
     
    274248    ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); 
    275249    rtn=ops_parse(pinfo); 
     250    CU_ASSERT(rtn==1); 
    276251 
    277252    /* 
     
    285260    ops_teardown_memory_read(pinfo,mem); 
    286261    free (in); 
    287     } 
    288  
    289 static void test_cfb() 
    290     { 
    291     // Used for trying low-level OpenSSL tests 
    292  
    293     ops_crypt_t crypt_aes; 
    294     ops_crypt_any(&crypt_aes, OPS_SA_AES_256); 
    295  
    296     ops_crypt_t crypt_cast; 
    297     ops_crypt_any(&crypt_cast, OPS_SA_CAST5); 
    298  
    299     ops_crypt_t* crypt; 
    300  
    301     /*  
    302        AES init 
    303        using empty IV and key for the moment  
    304     */ 
    305     unsigned char *iv=ops_mallocz(crypt_aes.blocksize); 
    306     unsigned char *key=ops_mallocz(crypt_aes.keysize); 
    307     snprintf((char *)key, crypt_aes.keysize, "AES_KEY"); 
    308     crypt_aes.set_iv(&crypt_aes, iv); 
    309     crypt_aes.set_key(&crypt_aes, key); 
    310     ops_encrypt_init(&crypt_aes); 
    311  
    312     /* 
    313      * CAST 
    314      */ 
    315     iv=ops_mallocz(crypt_cast.blocksize); 
    316     key=ops_mallocz(crypt_cast.keysize); 
    317     //    snprintf((char *)key, crypt_cast.keysize, "CAST_KEY"); 
    318     crypt_cast.set_iv(&crypt_cast, iv); 
    319     crypt_cast.set_key(&crypt_cast, key); 
    320     ops_encrypt_init(&crypt_cast); 
    321  
    322     crypt=&crypt_cast; 
    323  
    324     // Why does aes encrypt/decrypt work?? 
    325     //    crypt=&crypt_aes; 
    326  
    327     unsigned char *in=ops_mallocz(crypt->blocksize); 
    328     unsigned char *out=ops_mallocz(crypt->blocksize); 
    329     unsigned char *out2=ops_mallocz(crypt->blocksize); 
    330  
    331     snprintf((char *)in,crypt->blocksize,"hello"); 
    332         /* 
    333     printf("\n"); 
    334     printf("in:\t0x%.2x 0x%.2x 0x%.2x 0x%.2x   0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",  
    335            in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]); 
    336     printf("in:\t%c    %c    %c    %c      %c    %c    %c    %c\n",  
    337            in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]); 
    338         */ 
    339  
    340     crypt->block_encrypt(crypt, out, in); 
    341     //    AES_ecb_encrypt(in,out,crypt.data,AES_ENCRYPT); 
    342         /* 
    343     printf("out:\t0x%.2x 0x%.2x 0x%.2x 0x%.2x   0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",  
    344            out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7]); 
    345     printf("out:\t%c    %c    %c    %c      %c    %c    %c    %c\n",  
    346            out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7]); 
    347         */ 
    348  
    349     crypt->block_decrypt(crypt, out2, out); 
    350     //    AES_ecb_encrypt(out,out2,crypt.data,AES_DECRYPT); 
    351         /* 
    352     printf("out2:\t0x%.2x 0x%.2x 0x%.2x 0x%.2x   0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",  
    353            out2[0], out2[1], out2[2], out2[3], out2[4], out2[5], out2[6], out2[7]); 
    354     printf("out2:\t%c    %c    %c    %c      %c    %c    %c    %c\n",  
    355            out2[0], out2[1], out2[2], out2[3], out2[4], out2[5], out2[6], out2[7]); 
    356         */ 
    357     CU_ASSERT(memcmp((char *)in, (char *)out2, strlen((char *)in))==0); 
    358  
    359     cleanup(); 
    360262    } 
    361263 
     
    380282        ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); 
    381283        rtn=ops_parse(pinfo); 
     284    CU_ASSERT(rtn==1); 
    382285 
    383286        // This duplicates the hash done in ops_write_mdc so that we 
     
    459362 
    460363    rtn=ops_parse(pinfo); 
     364    CU_ASSERT(rtn==1); 
    461365 
    462366    /* 
     
    474378static void test_ops_encrypted_pk_sk() 
    475379    { 
    476     char *user_id="Alpha (RSA, no passphrase) <alpha@test.com>"; 
    477380    ops_pk_session_key_t *encrypted_pk_session_key; 
    478381    ops_create_info_t *cinfo; 
     
    485388 
    486389    // write 
    487     const ops_key_data_t *pub_key=ops_keyring_find_key_by_userid(&pub_keyring, user_id); 
     390    const ops_key_data_t *pub_key=ops_keyring_find_key_by_userid(&pub_keyring, alpha_user_id); 
     391    assert(pub_key); 
     392 
    488393    encrypted_pk_session_key=ops_create_pk_session_key(pub_key); 
    489394    ops_write_pk_session_key(cinfo,encrypted_pk_session_key); 
     
    494399    // read 
    495400    rtn=ops_parse(pinfo); 
     401    CU_ASSERT(rtn==1); 
    496402 
    497403    // test 
     
    513419    // add tests to suite 
    514420     
    515     if (NULL == CU_add_test(suite, "Test CFB", test_cfb)) 
    516             return NULL; 
    517  
    518421    if (NULL == CU_add_test(suite, "Tag 11: Literal Data packet in Text mode", test_literal_data_packet_text)) 
    519422            return NULL; 
  • openpgpsdk/trunk/tests/test_rsa_decrypt.c

    r476 r485  
    1919 
    2020#define MAXBUF 128 
    21 static char secring[MAXBUF+1]; 
     21//static char secring[MAXBUF+1]; 
    2222//static char dir[MAXBUF+1]; 
    23 static char keydetails[MAXBUF+1]; 
     23//static char keydetails[MAXBUF+1]; 
    2424static ops_keyring_t keyring; 
    2525static char *filename_rsa_noarmour_nopassphrase="rsa_noarmour_nopassphrase.txt"; 
     
    3333static char* text; 
    3434 
     35/* 
    3536static int create_testfile(const char *name) 
    3637    { 
     
    4849    return 1; 
    4950    } 
     51*/ 
    5052 
    5153static ops_parse_cb_return_t 
     
    170172int init_suite_rsa_decrypt(void) 
    171173    { 
     174#ifdef XXX 
    172175    int fd=0; 
    173176    char cmd[MAXBUF+1]; 
     
    255258    snprintf(secring,MAXBUF,"%s/secring.gpg", dir); 
    256259    ops_keyring_read(&keyring,secring); 
     260#endif 
    257261 
    258262    // Return success 
     
    262266int clean_suite_rsa_decrypt(void) 
    263267    { 
     268         
     269#ifdef XXX 
    264270    char cmd[MAXBUF+1]; 
    265          
    266271    /* Close OPS */ 
    267272     
     
    276281        return 1; 
    277282        } 
    278      
     283#endif 
     284     
     285    reset_vars(); 
     286 
    279287    return 0; 
    280288    } 
  • openpgpsdk/trunk/tests/test_rsa_encrypt.c

    r481 r485  
    88#include "openpgpsdk/util.h" 
    99#include "openpgpsdk/std_print.h" 
     10#include "openpgpsdk/readerwriter.h" 
    1011 
    1112#include "tests.h" 
    1213 
    1314#define MAXBUF 128 
    14 static char pub_keyring_name[MAXBUF+1]; 
    15 static char keydetails[MAXBUF+1]; 
    16 static ops_keyring_t pub_keyring; 
    1715static char *filename_rsa_noarmour_singlekey="rsa_noarmour_singlekey.txt"; 
    1816 
     17/* 
    1918static int create_testfile(const char *name) 
    2019    { 
     
    3231    return 1; 
    3332    } 
    34  
    35 #ifdef XXX 
     33*/ 
     34 
    3635static ops_parse_cb_return_t 
    37 callback(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) 
     36callback_ops_decrypt(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) 
    3837    { 
    3938    ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content; 
    4039    static ops_boolean_t skipping; 
    41     static const ops_key_data_t *encrypter; 
    42     const ops_key_data_t *keydata=NULL; 
    43     const ops_secret_key_t *secret; 
     40    static const ops_key_data_t *decrypter; 
     41    //    const ops_key_data_t *keydata=NULL; 
     42    //    const ops_secret_key_t *secret; 
    4443 
    4544    OPS_USED(cbinfo); 
     
    6867    case OPS_PTAG_CT_PK_SESSION_KEY: 
    6968                //      printf ("OPS_PTAG_CT_PK_SESSION_KEY\n"); 
    70         if(encrypter) 
     69        if(decrypter) 
    7170            break; 
    7271 
    73         encrypter=ops_keyring_find_key_by_id(&keyring, 
     72        decrypter=ops_keyring_find_key_by_id(&sec_keyring, 
    7473                                             content->pk_session_key.key_id); 
    75         if(!encrypter) 
     74        if(!decrypter) 
    7675            break; 
    7776        break; 
    7877 
    7978    case OPS_PARSER_CMD_GET_SECRET_KEY: 
    80         keydata=ops_keyring_find_key_by_id(&keyring,content->get_secret_key.pk_session_key->key_id); 
    81         if (!keydata || !ops_key_is_secret(keydata)) 
    82             return 0; 
    83  
    84         //      ops_set_secret_key(content,keydata); 
    85  
    86         // Do we need the passphrase and not have it? If so, get it 
    87         ops_parser_content_t pc; 
    88         char *passphrase; 
    89         memset(&pc,'\0',sizeof pc); 
    90         passphrase=NULL; 
    91         pc.content.secret_key_passphrase.passphrase=&passphrase; 
    92         pc.content.secret_key_passphrase.secret_key=&(keydata->key.skey); 
    93  
    94         /* Ugh. Need to duplicate this macro here to get the passphrase  
    95            Duplication to be removed when the callback gets moved to main code. 
    96            Can we make this inline code rather than a macro? 
    97         */ 
    98 #define CB(cbinfo,t,pc) do { (pc)->tag=(t); if((cbinfo)->cb(pc,(cbinfo)) == OPS_RELEASE_MEMORY) ops_parser_content_free(pc); } while(0) 
    99         CB(cbinfo,OPS_PARSER_CMD_GET_SK_PASSPHRASE,&pc); 
    100          
    101         /* now get the key from the data */ 
    102         secret=ops_get_secret_key_from_data(keydata); 
    103         while(!secret) 
    104             { 
    105             /* then it must be encrypted */ 
    106             secret=ops_decrypt_secret_key_from_data(keydata,passphrase); 
    107             free(passphrase); 
    108             } 
    109  
    110         *content->get_secret_key.secret_key=secret; 
    111          
    112         break; 
     79        return callback_cmd_get_secret_key(content_,cbinfo); 
    11380 
    11481    case OPS_PARSER_CMD_GET_SK_PASSPHRASE: 
    115         /* 
    116           Doing this so the test can be automated. 
    117           Will move this into separate stacked callback later 
    118         */ 
    119         *(content->secret_key_passphrase.passphrase)=ops_malloc_passphrase(current_passphrase); 
    120         return OPS_KEEP_MEMORY; 
    121         break; 
     82        return callback_cmd_get_secret_key_passphrase(content_,cbinfo); 
    12283 
    12384    case OPS_PTAG_CT_LITERAL_DATA_BODY: 
    124         text=ops_mallocz(content->literal_data_body.length+1); 
    125         memcpy(text,content->literal_data_body.data,content->literal_data_body.length); 
    126                 break; 
     85        return callback_literal_data(content_,cbinfo); 
     86        //      text=ops_mallocz(content->literal_data_body.length+1); 
     87        //      memcpy(text,content->literal_data_body.data,content->literal_data_body.length); 
     88        //              break; 
    12789 
    12890    case OPS_PARSER_PTAG: 
     
    147109    return OPS_RELEASE_MEMORY; 
    148110    } 
    149 #endif 
    150  
    151111 
    152112/* Decryption suite initialization. 
     
    157117int init_suite_rsa_encrypt(void) 
    158118    { 
    159     int fd=0; 
    160     char cmd[MAXBUF+1]; 
    161     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"; 
    162     char *rsa_pass="Key-Type: RSA\nKey-Usage: encrypt, sign\nName-Real: Bravo\nName-Comment: RSA, passphrase\nName-Email: bravo@test.com\nPassphrase: hello\nKey-Length: 1024\n"; 
    163      
    164     // Create temp directory 
    165     if (!mktmpdir()) 
    166         return 1; 
    167  
    168119    // Create RSA test files 
    169120 
     
    175126    */ 
    176127 
     128#ifdef XXX 
     129    int fd=0; 
     130    char cmd[MAXBUF+1]; 
     131    char keydetails[MAXBUF+1]; 
     132    char keyring_name[MAXBUF+1]; 
     133    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"; 
     134    char *rsa_pass="Key-Type: RSA\nKey-Usage: encrypt, sign\nName-Real: Bravo\nName-Comment: RSA, passphrase\nName-Email: bravo@test.com\nPassphrase: hello\nKey-Length: 1024\n"; 
     135     
     136    // Create temp directory 
     137    if (!mktmpdir()) 
     138        return 1; 
     139 
    177140    /* 
    178141     * Create a RSA keypair with no passphrase 
     
    193156    system(cmd); 
    194157 
    195 #ifdef XXX     
    196     // Now encrypt the test file with GPG 
    197     snprintf(cmd,MAXBUF,"gpg --quiet --encrypt --homedir=%s --recipient Alpha %s/%s", dir, dir, filename_rsa_noarmour_nopassphrase); 
    198     if (system(cmd)) 
    199         { 
    200         return 1; 
    201         } 
    202  
    203     // Now encrypt and ascii-armour the test file with GPG 
    204     snprintf(cmd,MAXBUF,"gpg --quiet --encrypt --armor --homedir=%s --recipient Alpha %s/%s", dir, dir, filename_rsa_armour_nopassphrase); 
    205     if (system(cmd)) 
    206         { 
    207         return 1; 
    208         } 
    209      
    210 #endif 
    211      
    212158    /* 
    213159     * Create a RSA keypair with passphrase 
     
    227173    system(cmd); 
    228174 
    229 #ifdef XXX     
    230     // Now encrypt the test file with GPG 
    231     snprintf(cmd,MAXBUF,"gpg --quiet --encrypt --homedir=%s --recipient Bravo %s/%s", dir, dir, filename_rsa_noarmour_passphrase); 
    232     if (system(cmd)) 
    233         { 
    234         return 1; 
    235         } 
    236  
    237     // Now encrypt and ascii-armour the test file with GPG 
    238     snprintf(cmd,MAXBUF,"gpg --quiet --encrypt --armor --homedir=%s --recipient Bravo %s/%s", dir, dir, filename_rsa_armour_passphrase); 
    239     if (system(cmd)) 
    240         { 
    241         return 1; 
    242         } 
    243 #endif 
    244  
    245175    // Initialise OPS  
    246176    ops_init(); 
    247177 
    248     // read keyring 
    249     snprintf(pub_keyring_name,MAXBUF,"%s/pubring.gpg", dir); 
    250     ops_keyring_read(&pub_keyring,pub_keyring_name); 
     178    // read keyrings 
     179    snprintf(keyring_name,MAXBUF,"%s/pubring.gpg", dir); 
     180    ops_keyring_read(&pub_keyring,keyring_name); 
     181 
     182    snprintf(keyring_name,MAXBUF,"%s/secring.gpg", dir); 
     183    ops_keyring_read(&sec_keyring,keyring_name); 
     184#endif 
    251185 
    252186    // Return success 
     
    258192    // char cmd[MAXBUF+1]; 
    259193         
     194#ifdef XXX 
    260195    /* Close OPS */ 
    261196     
    262197    ops_keyring_free(&pub_keyring); 
     198#endif 
     199 
    263200    ops_finish(); 
    264201 
     
    272209        } 
    273210   */  
     211 
     212    reset_vars(); 
     213 
    274214    return 0; 
    275215    } 
    276216 
     217static void test_rsa_decrypt(const char *encfile, const char*testtext) 
     218    { 
     219    int fd=0; 
     220    ops_parse_info_t *pinfo; 
     221    int rtn=0; 
     222 
     223    // open encrypted file 
     224    fd=open(encfile,O_RDONLY); 
     225    if(fd < 0) 
     226        { 
     227        perror(encfile); 
     228        exit(2); 
     229        } 
     230     
     231    // Set decryption reader and handling options 
     232 
     233    pinfo=ops_parse_info_new(); 
     234    ops_reader_set_fd(pinfo,fd); 
     235    ops_parse_cb_set(pinfo,callback_ops_decrypt,NULL); 
     236 
     237    //    current_passphrase=nopassphrase; 
     238     
     239    // Do the decryption 
     240 
     241    rtn=ops_parse(pinfo); 
     242    CU_ASSERT(rtn==1); 
     243 
     244    // Tidy up 
     245 
     246    close(fd); 
     247     
     248    // File contents should match 
     249    CU_ASSERT(memcmp(literal_data,testtext,sz_literal_data)==0); 
     250    } 
     251 
    277252static void test_rsa_encrypt(const int has_armour __attribute__((__unused__)), const ops_key_data_t *key __attribute__((__unused__)), const char *filename __attribute__((__unused__))) 
    278253    { 
    279 #ifdef NOTYETUSED 
     254    ops_memory_t *mem_ldt; 
     255    ops_create_info_t *cinfo_ldt; 
     256 
     257    //#ifdef NOTYETUSED 
    280258    char myfile[MAXBUF+1]; 
    281259    char encfile[MAXBUF+1]; 
     
    283261    int fd_in=0; 
    284262    int fd_out=0; 
    285     ops_create_info_t *cinfo; 
    286263    //    ops_crypt_t encrypt; 
    287264     
     
    303280        } 
    304281     
    305     // Set encryption writer and handling options 
    306  
    307     cinfo=ops_create_info_new(); 
    308     ops_writer_set_fd(cinfo,fd_out);  
    309282    // ops_parse_cb_set(pinfo,callback,NULL); 
    310283 
     
    320293    //    ops_encrypt_init(&encrypt); 
    321294 
    322     ops_writer_push_encrypt(cinfo,key); 
     295    //    ops_writer_push_encrypt(cinfo,key); 
    323296 
    324297    // Set up armour/passphrase options 
     
    341314                    break; 
    342315            assert(n>=0); 
     316#ifdef USING_PUSH 
    343317            ops_write(buf,n,cinfo); 
    344     } 
     318#else 
     319        // create a simple literal data packet as the encrypted payload 
     320        ops_setup_memory_write(&cinfo_ldt,&mem_ldt,n); 
     321        ops_write_literal_data((unsigned char *)buf, n, 
     322                           OPS_LDT_BINARY, cinfo_ldt); 
     323#endif 
     324    } 
     325 
     326 
     327    // write to file 
     328 
     329    // Set encryption writer and handling options 
     330 
     331    ops_create_info_t *cinfo; 
     332    cinfo=ops_create_info_new(); 
     333    ops_writer_set_fd(cinfo,fd_out);  
     334 
     335    /* 
     336     * write out the encrypted packet 
     337     */ 
     338    char *user_id="Alpha (RSA, no passphrase) <alpha@test.com>"; 
     339    const ops_key_data_t *pub_key=ops_keyring_find_key_by_userid(&pub_keyring, user_id); 
     340    ops_print_public_key_verbose(pub_key); 
     341 
     342    ops_pk_session_key_t* encrypted_pk_session_key; 
     343    encrypted_pk_session_key=ops_create_pk_session_key(pub_key); 
     344    ops_write_pk_session_key(cinfo,encrypted_pk_session_key); 
     345 
     346    //int rtn=0; 
     347    //    ops_parse_info_t *pinfo; 
     348    //    ops_memory_t *mem; 
     349    //    ops_setup_memory_write(&cinfo,&mem,MAXBUF); 
     350 
     351    //    ops_crypt_any(&encrypt, OPS_SA_CAST5); 
     352    ops_crypt_t encrypt; 
     353    ops_crypt_any(&encrypt, encrypted_pk_session_key->symmetric_algorithm); 
     354    unsigned char *iv=NULL; 
     355    iv=ops_mallocz(encrypt.blocksize); 
     356    encrypt.set_iv(&encrypt, iv); 
     357    key=ops_mallocz(encrypt.keysize); // using blank key for now 
     358    //    snprintf((char *)key, encrypt.keysize, "CAST_KEY"); 
     359    //    encrypt.set_key(&encrypt, key); 
     360    encrypt.set_key(&encrypt, &encrypted_pk_session_key->key[0]); 
     361    ops_encrypt_init(&encrypt); 
     362 
     363    ops_write_se_ip_data( ops_memory_get_data(mem_ldt), 
     364                          ops_memory_get_length(mem_ldt), 
     365                          &encrypt, cinfo); 
     366 
    345367     
    346368    // Tidy up 
     
    350372 
    351373     // File contents should match 
    352     char *text; 
    353374    char buffer[MAXBUF+1]; 
    354375    create_testtext(filename,&buffer[0],MAXBUF); 
    355     CU_ASSERT(strcmp(text,buffer)==0); 
    356 #endif 
     376    test_rsa_decrypt(encfile,buffer); 
     377    //    char *text; 
     378    //    CU_ASSERT(strcmp(text,buffer)==0); 
     379    //#endif 
    357380    } 
    358381 
     
    396419            return NULL; 
    397420 
    398 #ifdef TBD 
    399421    // add tests to suite 
    400422     
     
    402424            return NULL; 
    403425     
     426#ifdef TBD 
    404427    if (NULL == CU_add_test(suite, "Armoured, no passphrase", test_rsa_encrypt_armour_nopassphrase)) 
    405428            return NULL; 
  • openpgpsdk/trunk/tests/tests.c

    r484 r485  
    1313#include "tests.h" 
    1414 
     15extern CU_pSuite suite_crypto(); 
    1516extern CU_pSuite suite_packet_types(); 
    16 extern CU_pSuite suite_crypt_mpi(); 
     17//extern CU_pSuite suite_crypt_mpi(); 
    1718extern CU_pSuite suite_rsa_decrypt(); 
    1819extern CU_pSuite suite_rsa_encrypt(); 
     
    2223ops_keyring_t sec_keyring; 
    2324static char* no_passphrase=""; 
     25unsigned char* literal_data=NULL; 
     26size_t sz_literal_data=0; 
     27char *alpha_user_id="Alpha (RSA, no passphrase) <alpha@test.com>"; 
     28 
     29void setup_test_keys() 
     30    { 
     31    char keydetails[MAXBUF+1]; 
     32    char keyring_name[MAXBUF+1]; 
     33    int fd=0; 
     34    char cmd[MAXBUF+1]; 
     35 
     36    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"; 
     37    // Create temp directory 
     38    if (!mktmpdir()) 
     39        return; 
     40 
     41    /* 
     42     * Create a RSA keypair with no passphrase 
     43     */ 
     44 
     45    snprintf(keydetails,MAXBUF,"%s/%s",dir,"keydetails.alpha"); 
     46 
     47    if ((fd=open(keydetails,O_WRONLY | O_CREAT | O_EXCL, 0600))<0) 
     48        { 
     49        fprintf(stderr,"Can't create key details\n"); 
     50        return; 
     51        } 
     52 
     53    write(fd,rsa_nopass,strlen(rsa_nopass)); 
     54    close(fd); 
     55 
     56    snprintf(cmd,MAXBUF,"gpg --quiet --gen-key --expert --homedir=%s --batch %s",dir,keydetails); 
     57    system(cmd); 
     58     
     59    // read keyrings 
     60 
     61    snprintf(keyring_name,MAXBUF,"%s/pubring.gpg", dir); 
     62    ops_keyring_read(&pub_keyring,keyring_name); 
     63 
     64    snprintf(keyring_name,MAXBUF,"%s/secring.gpg", dir); 
     65    ops_keyring_read(&sec_keyring,keyring_name); 
     66 
     67    } 
     68 
     69static void cleanup() 
     70    { 
     71    char cmd[MAXBUF]; 
     72 
     73    return; 
     74 
     75    /* Remove test dir and files */ 
     76    snprintf(cmd,MAXBUF,"rm -rf %s", dir); 
     77    if (system(cmd)) 
     78        { 
     79        perror("Can't delete test directory "); 
     80        return; 
     81        } 
     82    } 
    2483 
    2584int main() 
    2685    { 
    2786 
     87    setup_test_keys(); 
     88 
    2889    if (CUE_SUCCESS != CU_initialize_registry()) 
    29         return CU_get_error(); 
     90        return CU_get_error(); 
     91 
     92    if (NULL == suite_crypto()) 
     93        { 
     94        CU_cleanup_registry(); 
     95        return CU_get_error(); 
     96        } 
     97 
     98    /* 
     99    if (NULL == suite_crypt_mpi()) 
     100        { 
     101        CU_cleanup_registry(); 
     102        return CU_get_error(); 
     103        } 
     104    */ 
    30105 
    31106    if (NULL == suite_packet_types()) 
     
    41116        return CU_get_error(); 
    42117        } 
     118    */ 
    43119 
    44120    if (NULL == suite_rsa_encrypt())  
     
    47123        return CU_get_error(); 
    48124        } 
    49     */ 
    50125 
    51126    // Run tests 
     
    53128    CU_basic_run_tests(); 
    54129    CU_cleanup_registry(); 
     130 
     131    cleanup(); 
     132 
    55133    return CU_get_error(); 
    56134    } 
     
    99177    } 
    100178 
     179void create_testfile(const char *name) 
     180    { 
     181    char filename[MAXBUF+1]; 
     182    char buffer[MAXBUF+1]; 
     183 
     184    int fd=0; 
     185    snprintf(filename,MAXBUF,"%s/%s",dir,name); 
     186    if ((fd=open(filename,O_WRONLY| O_CREAT | O_EXCL, 0600))<0) 
     187        return; 
     188 
     189    create_testtext(name,&buffer[0],MAXBUF); 
     190    write(fd,buffer,strlen(buffer)); 
     191    close(fd); 
     192    } 
     193 
    101194ops_parse_cb_return_t 
    102195callback_general(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) 
     
    220313    } 
    221314 
     315ops_parse_cb_return_t 
     316callback_literal_data(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) 
     317    { 
     318    ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content; 
     319 
     320    OPS_USED(cbinfo); 
     321 
     322    //    ops_print_packet(content_); 
     323 
     324    // Read data from packet into static buffer 
     325    switch(content_->tag) 
     326        { 
     327    case OPS_PTAG_CT_LITERAL_DATA_BODY: 
     328        sz_literal_data=content->literal_data_body.length; 
     329        literal_data=ops_mallocz(sz_literal_data+1); 
     330        memcpy(literal_data,content->literal_data_body.data,sz_literal_data); 
     331        break; 
     332 
     333    case OPS_PTAG_CT_LITERAL_DATA_HEADER: 
     334        // ignore 
     335        break; 
     336 
     337    default: 
     338        return callback_general(content_,cbinfo); 
     339        } 
     340 
     341    return OPS_RELEASE_MEMORY; 
     342    } 
     343  
     344void reset_vars() 
     345    { 
     346    if (literal_data) 
     347        { 
     348        free (literal_data); 
     349        literal_data=NULL; 
     350        sz_literal_data=0; 
     351        } 
     352    } 
  • openpgpsdk/trunk/tests/tests.h

    r484 r485  
    1818void create_testtext(const char *text, char *buf, const int maxlen); 
    1919void create_testdata(const char *text, unsigned char *buf, const int maxlen); 
     20void create_testfile(const char *name); 
    2021#define MAXBUF 128 
    2122 
     
    2627ops_parse_cb_return_t 
    2728callback_cmd_get_secret_key_passphrase(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo); 
     29ops_parse_cb_return_t 
     30callback_literal_data(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo); 
     31 
     32void reset_vars(); 
    2833 
    2934ops_keyring_t pub_keyring; 
    3035ops_keyring_t sec_keyring; 
     36unsigned char* literal_data; 
     37size_t sz_literal_data; 
     38char* alpha_user_id; 
    3139#endif 
    3240