| 1 |
#include "CUnit/Basic.h" |
|---|
| 2 |
|
|---|
| 3 |
#include <openssl/cast.h> |
|---|
| 4 |
|
|---|
| 5 |
#include "tests.h" |
|---|
| 6 |
#include "openpgpsdk/types.h" |
|---|
| 7 |
#include "openpgpsdk/keyring.h" |
|---|
| 8 |
#include "../src/advanced/keyring_local.h" |
|---|
| 9 |
#include "openpgpsdk/packet.h" |
|---|
| 10 |
#include "openpgpsdk/create.h" |
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
static const ops_key_data_t *pubkey; |
|---|
| 15 |
static const ops_key_data_t *seckey; |
|---|
| 16 |
|
|---|
| 17 |
int init_suite_crypt_mpi(void) |
|---|
| 18 |
{ |
|---|
| 19 |
#ifdef XXX |
|---|
| 20 |
static char keydetails[MAXBUF+1]; |
|---|
| 21 |
int fd=0; |
|---|
| 22 |
char cmd[MAXBUF+1]; |
|---|
| 23 |
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"; |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
if (!mktmpdir()) |
|---|
| 27 |
return 1; |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
snprintf(keydetails,sizeof keydetails,"%s/%s",dir,"keydetails.alpha"); |
|---|
| 34 |
|
|---|
| 35 |
if ((fd=open(keydetails,O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0600))<0) |
|---|
| 36 |
{ |
|---|
| 37 |
fprintf(stderr,"Can't create key details\n"); |
|---|
| 38 |
return 1; |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
write(fd,rsa_nopass,strlen(rsa_nopass)); |
|---|
| 42 |
close(fd); |
|---|
| 43 |
|
|---|
| 44 |
snprintf(cmd,sizeof cmd,"gpg --quiet --no-tty --homedir=%s --gen-key --expert --batch %s",dir,keydetails); |
|---|
| 45 |
system(cmd); |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
ops_init(); |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
snprintf(pubring,sizeof pubring,"%s/pubring.gpg", dir); |
|---|
| 52 |
ops_keyring_read(&pub_keyring,pubring); |
|---|
| 53 |
|
|---|
| 54 |
snprintf(secring,sizeof secring,"%s/secring.gpg", dir); |
|---|
| 55 |
ops_keyring_read(&sec_keyring,secring); |
|---|
| 56 |
|
|---|
| 57 |
char keyid[]="Alpha (RSA, no passphrase) <alpha@test.com>"; |
|---|
| 58 |
#endif |
|---|
| 59 |
pubkey=ops_keyring_find_key_by_userid(&pub_keyring,alpha_user_id); |
|---|
| 60 |
|
|---|
| 61 |
seckey=ops_keyring_find_key_by_userid(&sec_keyring,alpha_user_id); |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
return 0; |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
int clean_suite_crypt_mpi(void) |
|---|
| 68 |
{ |
|---|
| 69 |
|
|---|
| 70 |
#ifdef XXX |
|---|
| 71 |
char cmd[MAXBUF+1]; |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
ops_keyring_free(&pub_keyring); |
|---|
| 75 |
ops_keyring_free(&sec_keyring); |
|---|
| 76 |
#endif |
|---|
| 77 |
|
|---|
| 78 |
ops_finish(); |
|---|
| 79 |
|
|---|
| 80 |
#ifdef XXX |
|---|
| 81 |
|
|---|
| 82 |
snprintf(cmd,sizeof cmd,"rm -rf %s", dir); |
|---|
| 83 |
if (system(cmd)) |
|---|
| 84 |
{ |
|---|
| 85 |
perror("Can't delete test directory "); |
|---|
| 86 |
return 1; |
|---|
| 87 |
} |
|---|
| 88 |
#endif |
|---|
| 89 |
|
|---|
| 90 |
reset_vars(); |
|---|
| 91 |
|
|---|
| 92 |
return 0; |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
void test_crypt_mpi(void) |
|---|
| 96 |
{ |
|---|
| 97 |
|
|---|
| 98 |
#define BSZ (CAST_KEY_LENGTH+1+2) |
|---|
| 99 |
|
|---|
| 100 |
unsigned char in[BSZ]; |
|---|
| 101 |
unsigned char out[BSZ]; |
|---|
| 102 |
|
|---|
| 103 |
ops_boolean_t rtn; |
|---|
| 104 |
|
|---|
| 105 |
ops_pk_session_key_t *encrypted_pk_session_key=NULL; |
|---|
| 106 |
|
|---|
| 107 |
encrypted_pk_session_key=ops_create_pk_session_key(pubkey); |
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
rtn=ops_decrypt_and_unencode_mpi(out,BSZ, encrypted_pk_session_key->parameters.rsa.encrypted_m, &seckey->key.skey); |
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
CU_ASSERT(memcmp((char *)in,(char *)out,sizeof(in))==0); |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
CU_pSuite suite_crypt_mpi() |
|---|
| 128 |
{ |
|---|
| 129 |
CU_pSuite suite = NULL; |
|---|
| 130 |
|
|---|
| 131 |
suite = CU_add_suite("Crypt MPI Suite", init_suite_crypt_mpi, clean_suite_crypt_mpi); |
|---|
| 132 |
if (!suite) |
|---|
| 133 |
return NULL; |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
if (NULL == CU_add_test(suite, "encrypt_mpi, then decrypt_mpi", test_crypt_mpi)) |
|---|
| 138 |
return NULL; |
|---|
| 139 |
|
|---|
| 140 |
return suite; |
|---|
| 141 |
} |
|---|