Changeset 485
- Timestamp:
- 08/13/07 18:48:48
- Files:
-
- openpgpsdk/trunk/include/openpgpsdk/crypto.h (modified) (1 diff)
- openpgpsdk/trunk/include/openpgpsdk/std_print.h (modified) (1 diff)
- openpgpsdk/trunk/src/advanced/adv_create.c (modified) (6 diffs)
- openpgpsdk/trunk/src/advanced/adv_crypto.c (modified) (5 diffs)
- openpgpsdk/trunk/src/advanced/adv_openssl_crypto.c (modified) (3 diffs)
- openpgpsdk/trunk/src/advanced/adv_packet-parse.c (modified) (7 diffs)
- openpgpsdk/trunk/src/advanced/adv_symmetric.c (modified) (1 diff)
- openpgpsdk/trunk/src/standard/std_print.c (modified) (2 diffs)
- openpgpsdk/trunk/tests/Makefile.template (modified) (1 diff)
- openpgpsdk/trunk/tests/test_crypt_mpi.c (modified) (9 diffs)
- openpgpsdk/trunk/tests/test_crypto.c (added)
- openpgpsdk/trunk/tests/test_packet_types.c (modified) (13 diffs)
- openpgpsdk/trunk/tests/test_rsa_decrypt.c (modified) (7 diffs)
- openpgpsdk/trunk/tests/test_rsa_encrypt.c (modified) (17 diffs)
- openpgpsdk/trunk/tests/tests.c (modified) (7 diffs)
- openpgpsdk/trunk/tests/tests.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openpgpsdk/trunk/include/openpgpsdk/crypto.h
r482 r485 114 114 void ops_reader_pop_hash(ops_parse_info_t *pinfo); 115 115 116 int ops_decrypt_ mpi(unsigned char *buf,unsigned buflen,const BIGNUM *encmpi,116 int ops_decrypt_and_unencode_mpi(unsigned char *buf,unsigned buflen,const BIGNUM *encmpi, 117 117 const ops_secret_key_t *skey); 118 118 ops_boolean_t ops_encrypt_mpi(const unsigned char *buf, size_t buflen, openpgpsdk/trunk/include/openpgpsdk/std_print.h
r484 r485 9 9 #include "openpgpsdk/keyring.h" 10 10 11 void print_bn( const char *name, 12 const BIGNUM *bn); 11 13 void ops_print_pk_session_key(ops_content_tag_t tag, 12 14 const ops_pk_session_key_t *key); openpgpsdk/trunk/src/advanced/adv_create.c
r484 r485 1 1 /** \file 2 2 */ 3 4 #include <openssl/cast.h> 3 5 4 6 #include <openpgpsdk/create.h> … … 751 753 { return ops_stacked_write(src,length,errors,winfo); } 752 754 753 void ops_create_m_buf(ops_pk_session_key_t *session_key, unsigned char *buf)755 static void create_unencoded_m_buf(ops_pk_session_key_t *session_key, unsigned char *m_buf) 754 756 { 755 757 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 758 763 // as defined in RFC Section 5.1 "Public-Key Encrypted Session Key Packet" 759 764 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++) 764 769 { 765 770 checksum+=session_key->key[i]; 766 buf[1+i]=session_key->key[i];771 m_buf[1+i]=session_key->key[i]; 767 772 } 768 773 checksum = checksum % 65536; 769 774 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 779 ops_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; 772 827 } 773 828 774 829 ops_pk_session_key_t *ops_create_pk_session_key(const ops_key_data_t *key) 775 830 { 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 777 848 ops_pk_session_key_t *session_key=ops_mallocz(sizeof *session_key); 778 849 … … 780 851 session_key->version=OPS_PKSK_V3; 781 852 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 */ 782 860 783 861 assert(key->key.pkey.algorithm == OPS_PKA_RSA); 784 862 session_key->algorithm=key->key.pkey.algorithm; 863 /* 785 864 session_key->symmetric_algorithm=OPS_SA_AES_256; 786 865 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; 793 889 794 890 return session_key; … … 842 938 843 939 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 844 953 if (!ops_stacked_write(encbuf,len,errors,winfo)) 845 954 return ops_false; … … 930 1039 ops_writer_push(info,encrypted_writer,encrypted_finaliser, 931 1040 encrypted_destroyer,arg); 1041 1042 /* 1043 fprintf(stderr,"writing %ld + %d + %ld\n", sz_preamble, len, ops_memory_get_length(mem_mdc)); 1044 */ 932 1045 933 1046 if (!ops_write(preamble, sz_preamble,info) … … 997 1110 998 1111 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); 1000 1114 1001 1115 return ops_write_ptag(OPS_PTAG_CT_SE_DATA, info) openpgpsdk/trunk/src/advanced/adv_crypto.c
r473 r485 7 7 #include <openpgpsdk/final.h> 8 8 9 int ops_decrypt_ mpi(unsigned char *buf,unsigned buflen,const BIGNUM *encmpi,9 int ops_decrypt_and_unencode_mpi(unsigned char *buf,unsigned buflen,const BIGNUM *encmpi, 10 10 const ops_secret_key_t *skey) 11 11 { … … 23 23 assert(skey->public_key.algorithm == OPS_PKA_RSA); 24 24 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 25 33 n=ops_rsa_private_decrypt(mpibuf,encmpibuf,(BN_num_bits(encmpi)+7)/8, 26 34 &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 */ 27 43 28 44 if(n <= 0) … … 38 54 39 55 if(mpibuf[0] != 0 || mpibuf[1] != 2) 40 return ops_false;56 return ops_false; 41 57 42 58 // Skip the random bytes. 43 59 for(i=2 ; i < n && mpibuf[i] ; ++i) 44 ;60 ; 45 61 46 62 if(i == n || i < 10) 47 return ops_false;63 return ops_false; 48 64 49 65 // Skip the zero 50 66 ++i; 51 67 68 // this is the unencoded m buf 52 69 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 */ 54 79 55 80 return n-i; 56 81 } 57 82 58 ops_boolean_t ops_encrypt_mpi(const unsigned char *buf, size_t buflen, 83 ops_boolean_t ops_encrypt_mpi(const unsigned char *encoded_m_buf, 84 const size_t sz_encoded_m_buf, 59 85 const ops_public_key_t *pkey, 60 86 ops_pk_session_key_parameters_t *skp) 61 87 { 88 assert(sz_encoded_m_buf==(size_t) BN_num_bytes(pkey->key.rsa.n)); 89 62 90 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; 65 95 unsigned i; 66 96 … … 69 99 assert(pkey->algorithm == OPS_PKA_RSA); 70 100 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 ?? 72 114 73 115 // these two bytes defined by RFC 74 padded[0]=0; 75 padded[1]=2; 116 EM[0]=0x00; 117 EM[1]=0x02; 118 76 119 // add non-zero random bytes of length k - mLen -3 77 for(i=2 ; i < n-buflen-1 ; ++i)78 do79 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); 81 124 82 125 assert (i >= 8+2); 83 126 84 padded[i++]=0;127 EM[i++]=0; 85 128 86 memcpy( padded+i, buf, buflen);129 memcpy(EM+i, M, mLen); 87 130 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); 89 142 90 143 if(n <= 0) … … 93 146 skp->rsa.encrypted_m=BN_bin2bn(encmpibuf, n, NULL); 94 147 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 95 156 return ops_true; 96 157 } openpgpsdk/trunk/src/advanced/adv_openssl_crypto.c
r470 r485 4 4 #include <openpgpsdk/configure.h> 5 5 #include <openpgpsdk/crypto.h> 6 #include <openpgpsdk/std_print.h> 6 7 #include <openssl/md5.h> 7 8 #include <openssl/sha.h> … … 167 168 n=RSA_private_decrypt(length,in,out,orsa,RSA_NO_PADDING); 168 169 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 } 169 178 orsa->n=orsa->d=orsa->p=orsa->q=NULL; 170 179 RSA_free(orsa); … … 179 188 int n; 180 189 190 // printf("ops_rsa_public_encrypt: length=%ld\n", length); 191 181 192 orsa=RSA_new(); 182 193 orsa->n=rsa->n; 183 194 orsa->e=rsa->e; 184 195 196 // printf("len: %ld\n", length); 197 // ops_print_bn("n: ", orsa->n); 198 // ops_print_bn("e: ", orsa->e); 185 199 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 } 186 207 187 208 orsa->n=orsa->e=NULL; openpgpsdk/trunk/src/advanced/adv_packet-parse.c
r480 r485 2 2 * \brief Parser for OpenPGP packets 3 3 */ 4 5 #include <openssl/cast.h> 4 6 5 7 #include <openpgpsdk/packet.h> … … 2166 2168 2167 2169 static int parse_pk_session_key(ops_region_t *region, 2168 ops_parse_info_t *pinfo)2170 ops_parse_info_t *pinfo) 2169 2171 { 2170 2172 unsigned char c[1]; 2171 2173 ops_parser_content_t content; 2172 2174 ops_parser_content_t pc; 2173 unsigned char buf[8192];2175 // unsigned char buf[8192]; 2174 2176 int n; 2175 2177 BIGNUM *enc_m; 2176 2178 unsigned k; 2177 2179 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]; 2178 2187 2179 2188 if(!limited_read(c,1,region,pinfo)) … … 2189 2198 return 0; 2190 2199 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 2191 2209 if(!limited_read(c,1,region,pinfo)) 2192 2210 return 0; … … 2231 2249 } 2232 2250 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); 2234 2253 2235 2254 if(n < 1) 2236 2255 ERRP(pinfo,"decrypted message too short"); 2237 2256 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); 2239 2260 k=ops_key_size(C.pk_session_key.symmetric_algorithm); 2240 2261 2241 2262 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); 2244 2265 2245 2266 assert(k <= sizeof C.pk_session_key.key); 2246 2267 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 */ 2250 2282 2251 2283 // XXX: Check checksum! … … 2254 2286 2255 2287 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); 2256 2290 pinfo->decrypt.set_key(&pinfo->decrypt,C.pk_session_key.key); 2257 2291 ops_encrypt_init(&pinfo->decrypt); 2258 2292 return 1; 2259 2293 } … … 2298 2332 if(buf[b-2] != buf[b] || buf[b-1] != buf[b+1]) 2299 2333 { 2334 fprintf(stderr,"Bad symmetric decrypt (%02x%02x vs %02x%02x)\n", 2335 buf[b-2],buf[b-1],buf[b],buf[b+1]); 2300 2336 // 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]); 2302 2338 return 0; 2303 2339 } … … 2327 2363 if (memcmp(mdc_hash,hashed,OPS_SHA1_HASH_SIZE)) 2328 2364 { 2329 fprintf(stderr,"Hash is bad ");2365 fprintf(stderr,"Hash is bad\n"); 2330 2366 // ERRP(pinfo,"Bad hash in MDC"); 2331 2367 return 0; openpgpsdk/trunk/src/advanced/adv_symmetric.c
r480 r485 102 102 arg->decrypted, 103 103 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 */ 104 116 } 105 117 else openpgpsdk/trunk/src/standard/std_print.c
r484 r485 18 18 static int indent=0; 19 19 20 staticvoid print_bn( const char *name,20 void print_bn( const char *name, 21 21 const BIGNUM *bn); 22 22 #ifdef NOTYETUSED … … 225 225 } 226 226 227 staticvoid print_bn( const char *name, const BIGNUM *bn)227 void print_bn( const char *name, const BIGNUM *bn) 228 228 { 229 229 print_indent(); openpgpsdk/trunk/tests/Makefile.template
r480 r485 14 14 TESTSRC=tests.c \ 15 15 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 17 19 TESTOBJ=$(TESTSRC:.c=.o) 18 20 openpgpsdk/trunk/tests/test_crypt_mpi.c
r474 r485 1 1 #include "CUnit/Basic.h" 2 3 #include <openssl/cast.h> 2 4 3 5 #include "tests.h" … … 8 10 #include "openpgpsdk/create.h" 9 11 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]; 14 14 static const ops_key_data_t *pubkey; 15 15 static const ops_key_data_t *seckey; … … 17 17 int init_suite_crypt_mpi(void) 18 18 { 19 #ifdef XXX 19 20 static char keydetails[MAXBUF+1]; 20 21 int fd=0; … … 55 56 56 57 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); 59 62 60 63 // Return success … … 64 67 int clean_suite_crypt_mpi(void) 65 68 { 69 70 #ifdef XXX 66 71 char cmd[MAXBUF+1]; 67 68 72 /* Close OPS */ 69 73 70 74 ops_keyring_free(&pub_keyring); 71 75 ops_keyring_free(&sec_keyring); 76 #endif 77 72 78 ops_finish(); 73 79 80 #ifdef XXX 74 81 /* Remove test dir and files */ 75 82 snprintf(cmd,MAXBUF,"rm -rf %s", dir); … … 79 86 return 1; 80 87 } 88 #endif 81 89 90 reset_vars(); 91 82 92 return 0; 83 93 } … … 85 95 void test_crypt_mpi(void) 86 96 { 87 #define BSZ (256/8+1+2) 97 // hardcoded using CAST 98 #define BSZ (CAST_KEY_LENGTH+1+2) 88 99 89 100 unsigned char in[BSZ]; … … 92 103 ops_boolean_t rtn; 93 104 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); 95 108 96 109 // recreate what was encrypted 97 ops_create_m_buf(session_key, in);110 // ops_create_m_buf(session_key, in); 98 111 99 112 // CU_ASSERT(session_key); … … 102 115 103 116 // 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); 105 118 106 119 // [0] is the symmetric algorithm openpgpsdk/trunk/tests/test_packet_types.c
r484 r485 16 16 #include "tests.h" 17 17 18 static unsigned char* literal_data=NULL;19 static size_t sz_literal_data=0;20 18 static unsigned char* mdc_data=NULL; 21 19 static size_t sz_mdc_data=0; … … 33 31 int init_suite_packet_types(void) 34 32 { 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]; 39 37 40 38 // Initialise OPS 41 39 ops_init(); 42 40 41 #ifdef XXX 43 42 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 43 // Create temp directory 45 44 if (!mktmpdir()) 46 return 1;45 return 1; 47 46 48 47 /* … … 65 64 66 65 // read keyrings 66 67 67 snprintf(keyring_name,MAXBUF,"%s/pubring.gpg", dir); 68 68 ops_keyring_read(&pub_keyring,keyring_name); 69 69 70 // read keyring71 70 snprintf(keyring_name,MAXBUF,"%s/secring.gpg", dir); 72 71 ops_keyring_read(&sec_keyring,keyring_name); 72 #endif 73 73 74 74 // Return success … … 82 82 ops_finish(); 83 83 84 reset_vars(); 85 84 86 return 0; 85 87 } 86 88 87 static ops_parse_cb_return_t88 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 buffer97 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 // ignore107 break;108 109 default:110 return callback_general(content_,cbinfo);111 }112 113 return OPS_RELEASE_MEMORY;114 }115 116 89 static ops_parse_cb_return_t 117 90 callback_mdc(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) … … 228 201 ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); 229 202 rtn=ops_parse(pinfo); 203 CU_ASSERT(rtn==1); 230 204 231 205 /* … … 274 248 ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); 275 249 rtn=ops_parse(pinfo); 250 CU_ASSERT(rtn==1); 276 251 277 252 /* … … 285 260 ops_teardown_memory_read(pinfo,mem); 286 261 free (in); 287 }288 289 static void test_cfb()290 {291 // Used for trying low-level OpenSSL tests292 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 init303 using empty IV and key for the moment304 */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 * CAST314 */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();360 262 } 361 263 … … 380 282 ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); 381 283 rtn=ops_parse(pinfo); 284 CU_ASSERT(rtn==1); 382 285 383 286 // This duplicates the hash done in ops_write_mdc so that we … … 459 362 460 363 rtn=ops_parse(pinfo); 364 CU_ASSERT(rtn==1); 461 365 462 366 /* … … 474 378 static void test_ops_encrypted_pk_sk() 475 379 { 476 char *user_id="Alpha (RSA, no passphrase) <alpha@test.com>";477 380 ops_pk_session_key_t *encrypted_pk_session_key; 478 381 ops_create_info_t *cinfo; … … 485 388 486 389 // 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 488 393 encrypted_pk_session_key=ops_create_pk_session_key(pub_key); 489 394 ops_write_pk_session_key(cinfo,encrypted_pk_session_key); … … 494 399 // read 495 400 rtn=ops_parse(pinfo); 401 CU_ASSERT(rtn==1); 496 402 497 403 // test … … 513 419 // add tests to suite 514 420 515 if (NULL == CU_add_test(suite, "Test CFB", test_cfb))516 return NULL;517 518 421 if (NULL == CU_add_test(suite, "Tag 11: Literal Data packet in Text mode", test_literal_data_packet_text)) 519 422 return NULL; openpgpsdk/trunk/tests/test_rsa_decrypt.c
r476 r485 19 19 20 20 #define MAXBUF 128 21 static char secring[MAXBUF+1];21 //static char secring[MAXBUF+1]; 22 22 //static char dir[MAXBUF+1]; 23 static char keydetails[MAXBUF+1];23 //static char keydetails[MAXBUF+1]; 24 24 static ops_keyring_t keyring; 25 25 static char *filename_rsa_noarmour_nopassphrase="rsa_noarmour_nopassphrase.txt"; … … 33 33 static char* text; 34 34 35 /* 35 36 static int create_testfile(const char *name) 36 37 { … … 48 49 return 1; 49 50 } 51 */ 50 52 51 53 static ops_parse_cb_return_t … … 170 172 int init_suite_rsa_decrypt(void) 171 173 { 174 #ifdef XXX 172 175 int fd=0; 173 176 char cmd[MAXBUF+1]; … … 255 258 snprintf(secring,MAXBUF,"%s/secring.gpg", dir); 256 259 ops_keyring_read(&keyring,secring); 260 #endif 257 261 258 262 // Return success … … 262 266 int clean_suite_rsa_decrypt(void) 263 267 { 268 269 #ifdef XXX 264 270 char cmd[MAXBUF+1]; 265 266 271 /* Close OPS */ 267 272 … … 276 281 return 1; 277 282 } 278 283 #endif 284 285 reset_vars(); 286 279 287 return 0; 280 288 } openpgpsdk/trunk/tests/test_rsa_encrypt.c
r481 r485 8 8 #include "openpgpsdk/util.h" 9 9 #include "openpgpsdk/std_print.h" 10 #include "openpgpsdk/readerwriter.h" 10 11 11 12 #include "tests.h" 12 13 13 14 #define MAXBUF 128 14 static char pub_keyring_name[MAXBUF+1];15 static char keydetails[MAXBUF+1];16 static ops_keyring_t pub_keyring;17 15 static char *filename_rsa_noarmour_singlekey="rsa_noarmour_singlekey.txt"; 18 16 17 /* 19 18 static int create_testfile(const char *name) 20 19 { … … 32 31 return 1; 33 32 } 34 35 #ifdef XXX 33 */ 34 36 35 static ops_parse_cb_return_t 37 callback (const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo)36 callback_ops_decrypt(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) 38 37 { 39 38 ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content; 40 39 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; 44 43 45 44 OPS_USED(cbinfo); … … 68 67 case OPS_PTAG_CT_PK_SESSION_KEY: 69 68 // printf ("OPS_PTAG_CT_PK_SESSION_KEY\n"); 70 if( encrypter)69 if(decrypter) 71 70 break; 72 71 73 encrypter=ops_keyring_find_key_by_id(&keyring,72 decrypter=ops_keyring_find_key_by_id(&sec_keyring, 74 73 content->pk_session_key.key_id); 75 if(! encrypter)74 if(!decrypter) 76 75 break; 77 76 break; 78 77 79 78 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); 113 80 114 81 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); 122 83 123 84 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; 127 89 128 90 case OPS_PARSER_PTAG: … … 147 109 return OPS_RELEASE_MEMORY; 148 110 } 149 #endif150 151 111 152 112 /* Decryption suite initialization. … … 157 117 int init_suite_rsa_encrypt(void) 158 118 { 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 directory165 if (!mktmpdir())166 return 1;167 168 119 // Create RSA test files 169 120 … … 175 126 */ 176 127 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 177 140 /* 178 141 * Create a RSA keypair with no passphrase … … 193 156 system(cmd); 194 157 195 #ifdef XXX196 // Now encrypt the test file with GPG197 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 GPG204 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 #endif211 212 158 /* 213 159 * Create a RSA keypair with passphrase … … 227 173 system(cmd); 228 174 229 #ifdef XXX230 // Now encrypt the test file with GPG231 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 GPG238 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 #endif244 245 175 // Initialise OPS 246 176 ops_init(); 247 177 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 251 185 252 186 // Return success … … 258 192 // char cmd[MAXBUF+1]; 259 193 194 #ifdef XXX 260 195 /* Close OPS */ 261 196 262 197 ops_keyring_free(&pub_keyring); 198 #endif 199 263 200 ops_finish(); 264 201 … … 272 209 } 273 210 */ 211 212 reset_vars(); 213 274 214 return 0; 275 215 } 276 216 217 static 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 277 252 static void test_rsa_encrypt(const int has_armour __attribute__((__unused__)), const ops_key_data_t *key __attribute__((__unused__)), const char *filename __attribute__((__unused__))) 278 253 { 279 #ifdef NOTYETUSED 254 ops_memory_t *mem_ldt; 255 ops_create_info_t *cinfo_ldt; 256 257 //#ifdef NOTYETUSED 280 258 char myfile[MAXBUF+1]; 281 259 char encfile[MAXBUF+1]; … … 283 261 int fd_in=0; 284 262 int fd_out=0; 285 ops_create_info_t *cinfo;286 263 // ops_crypt_t encrypt; 287 264 … … 303 280 } 304 281 305 // Set encryption writer and handling options306 307 cinfo=ops_create_info_new();308 ops_writer_set_fd(cinfo,fd_out);309 282 // ops_parse_cb_set(pinfo,callback,NULL); 310 283 … … 320 293 // ops_encrypt_init(&encrypt); 321 294 322 ops_writer_push_encrypt(cinfo,key);295 // ops_writer_push_encrypt(cinfo,key); 323 296 324 297 // Set up armour/passphrase options … … 341 314 break; 342 315 assert(n>=0); 316 #ifdef USING_PUSH 343 317 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 345 367 346 368 // Tidy up … … 350 372 351 373 // File contents should match 352 char *text;353 374 char buffer[MAXBUF+1]; 354 375 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 357 380 } 358 381 … … 396 419 return NULL; 397 420 398 #ifdef TBD399 421 // add tests to suite 400 422 … … 402 424 return NULL; 403 425 426 #ifdef TBD 404 427 if (NULL == CU_add_test(suite, "Armoured, no passphrase", test_rsa_encrypt_armour_nopassphrase)) 405 428 return NULL; openpgpsdk/trunk/tests/tests.c
r484 r485 13 13 #include "tests.h" 14 14 15 extern CU_pSuite suite_crypto(); 15 16 extern CU_pSuite suite_packet_types(); 16 extern CU_pSuite suite_crypt_mpi();17 //extern CU_pSuite suite_crypt_mpi(); 17 18 extern CU_pSuite suite_rsa_decrypt(); 18 19 extern CU_pSuite suite_rsa_encrypt(); … … 22 23 ops_keyring_t sec_keyring; 23 24 static char* no_passphrase=""; 25 unsigned char* literal_data=NULL; 26 size_t sz_literal_data=0; 27 char *alpha_user_id="Alpha (RSA, no passphrase) <alpha@test.com>"; 28 29 void 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 69 static 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 } 24 83 25 84 int main() 26 85 { 27 86 87 setup_test_keys(); 88 28 89 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 */ 30 105 31 106 if (NULL == suite_packet_types()) … … 41 116 return CU_get_error(); 42 117 } 118 */ 43 119 44 120 if (NULL == suite_rsa_encrypt()) … … 47 123 return CU_get_error(); 48 124 } 49 */50 125 51 126 // Run tests … … 53 128 CU_basic_run_tests(); 54 129 CU_cleanup_registry(); 130 131 cleanup(); 132 55 133 return CU_get_error(); 56 134 } … … 99 177 } 100 178 179 void 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 101 194 ops_parse_cb_return_t 102 195 callback_general(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) … … 220 313 } 221 314 315 ops_parse_cb_return_t 316 callback_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 344 void 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 18 18 void create_testtext(const char *text, char *buf, const int maxlen); 19 19 void create_testdata(const char *text, unsigned char *buf, const int maxlen); 20 void create_testfile(const char *name); 20 21 #define MAXBUF 128 21 22 … … 26 27 ops_parse_cb_return_t 27 28 callback_cmd_get_secret_key_passphrase(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo); 29 ops_parse_cb_return_t 30 callback_literal_data(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo); 31 32 void reset_vars(); 28 33 29 34 ops_keyring_t pub_keyring; 30 35 ops_keyring_t sec_keyring; 36 unsigned char* literal_data; 37 size_t sz_literal_data; 38 char* alpha_user_id; 31 39 #endif 32 40
