[OpenPGP:SDK svn] r484 - in openpgpsdk/trunk: include/openpgpsdk src/advanced src/standard tests

Subversion ben at links.org
Fri Aug 10 13:07:33 BST 2007


Author: rachel
Date: 2007-08-10 13:07:33 +0100 (Fri, 10 Aug 2007)
New Revision: 484

Modified:
   openpgpsdk/trunk/include/openpgpsdk/std_print.h
   openpgpsdk/trunk/src/advanced/adv_create.c
   openpgpsdk/trunk/src/standard/std_print.c
   openpgpsdk/trunk/tests/test_packet_types.c
   openpgpsdk/trunk/tests/tests.c
   openpgpsdk/trunk/tests/tests.h
Log:
Added explicit test for Encrypted PK Session Key
Some refactoring


Modified: openpgpsdk/trunk/include/openpgpsdk/std_print.h
===================================================================
--- openpgpsdk/trunk/include/openpgpsdk/std_print.h	2007-08-09 16:05:16 UTC (rev 483)
+++ openpgpsdk/trunk/include/openpgpsdk/std_print.h	2007-08-10 12:07:33 UTC (rev 484)
@@ -4,6 +4,12 @@
 #ifndef OPS_STD_PRINT_H
 #define OPS_STD_PRINT_H
 
+#include "openpgpsdk/packet.h"
+#include "openpgpsdk/packet-parse.h"
+#include "openpgpsdk/keyring.h"
+
+void ops_print_pk_session_key(ops_content_tag_t tag,
+                          const ops_pk_session_key_t *key);
 void ops_print_public_key(const ops_key_data_t *key);
 
 void ops_print_public_key_verbose(const ops_key_data_t *key);

Modified: openpgpsdk/trunk/src/advanced/adv_create.c
===================================================================
--- openpgpsdk/trunk/src/advanced/adv_create.c	2007-08-09 16:05:16 UTC (rev 483)
+++ openpgpsdk/trunk/src/advanced/adv_create.c	2007-08-10 12:07:33 UTC (rev 484)
@@ -814,7 +814,8 @@
 	&& ops_write(pksk->key_id, 8, info)
 	&& ops_write_scalar(pksk->algorithm, 1, info)
 	&& ops_write_mpi(pksk->parameters.rsa.encrypted_m, info)
-	&& ops_write_scalar(0, 2, info);
+        //??	&& ops_write_scalar(0, 2, info);
+        ;
     }
 
 static ops_boolean_t encrypted_writer(const unsigned char *src,

Modified: openpgpsdk/trunk/src/standard/std_print.c
===================================================================
--- openpgpsdk/trunk/src/standard/std_print.c	2007-08-09 16:05:16 UTC (rev 483)
+++ openpgpsdk/trunk/src/standard/std_print.c	2007-08-10 12:07:33 UTC (rev 484)
@@ -487,7 +487,7 @@
     printf("<<<<< %s <<<<<\n",name);
     }
 
-static void print_pk_session_key(ops_content_tag_t tag,
+void ops_print_pk_session_key(ops_content_tag_t tag,
 				 const ops_pk_session_key_t *key)
     {
     if(tag == OPS_PTAG_CT_PK_SESSION_KEY)
@@ -1115,11 +1115,11 @@
 
     case OPS_PTAG_CT_PK_SESSION_KEY:
     case OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY:
-	print_pk_session_key(content_->tag,&content->pk_session_key);
+	ops_print_pk_session_key(content_->tag,&content->pk_session_key);
 	break;
 
     case OPS_PARSER_CMD_GET_SECRET_KEY:
-	print_pk_session_key(OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY,
+	ops_print_pk_session_key(OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY,
 			     content->get_secret_key.pk_session_key);
 
 #ifdef XXX

Modified: openpgpsdk/trunk/tests/test_packet_types.c
===================================================================
--- openpgpsdk/trunk/tests/test_packet_types.c	2007-08-09 16:05:16 UTC (rev 483)
+++ openpgpsdk/trunk/tests/test_packet_types.c	2007-08-10 12:07:33 UTC (rev 484)
@@ -19,11 +19,12 @@
 static size_t sz_literal_data=0;
 static unsigned char* mdc_data=NULL;
 static size_t sz_mdc_data=0;
+static unsigned char* encrypted_pk_sk=NULL;
+static size_t sz_encrypted_pk_sk=0;
 
 #define MAXBUF 128
 
 static void cleanup();
-//static void print_hash(char* str, unsigned char* data);
 
 /* 
  * Packet Types initialisation.
@@ -31,9 +32,45 @@
 
 int init_suite_packet_types(void)
     {
+    char keydetails[MAXBUF+1];
+    char keyring_name[MAXBUF+1];
+    int fd=0;
+    char cmd[MAXBUF+1];
+
     // Initialise OPS 
     ops_init();
 
+    char *rsa_nopass="Key-Type: RSA\nKey-Usage: encrypt, sign\nName-Real: Alpha\nName-Comment: RSA, no passphrase\nName-Email: alpha at test.com\nKey-Length: 1024\n";
+    // Create temp directory
+    if (!mktmpdir())
+	return 1;
+
+    /*
+     * Create a RSA keypair with no passphrase
+     */
+
+    snprintf(keydetails,MAXBUF,"%s/%s",dir,"keydetails.alpha");
+
+    if ((fd=open(keydetails,O_WRONLY | O_CREAT | O_EXCL, 0600))<0)
+	{
+	fprintf(stderr,"Can't create key details\n");
+	return 1;
+	}
+
+    write(fd,rsa_nopass,strlen(rsa_nopass));
+    close(fd);
+
+    snprintf(cmd,MAXBUF,"gpg --quiet --gen-key --expert --homedir=%s --batch %s",dir,keydetails);
+    system(cmd);
+
+    // read keyrings
+    snprintf(keyring_name,MAXBUF,"%s/pubring.gpg", dir);
+    ops_keyring_read(&pub_keyring,keyring_name);
+
+    // read keyring
+    snprintf(keyring_name,MAXBUF,"%s/secring.gpg", dir);
+    ops_keyring_read(&sec_keyring,keyring_name);
+
     // Return success
     return 0;
     }
@@ -61,30 +98,16 @@
         {
     case OPS_PTAG_CT_LITERAL_DATA_BODY:
         sz_literal_data=content->literal_data_body.length;
-        //	literal_data=ops_mallocz(content->literal_data_body.length+1);
-        //        memcpy(literal_data,content->literal_data_body.data,content->literal_data_body.length);
         literal_data=ops_mallocz(sz_literal_data+1);
         memcpy(literal_data,content->literal_data_body.data,sz_literal_data);
         break;
 
-    case OPS_PARSER_PTAG:
     case OPS_PTAG_CT_LITERAL_DATA_HEADER:
         // ignore
         break;
 
-    case OPS_PARSER_ERROR:
-	printf("parse error: %s\n",content->error.error);
-	break;
-
-    case OPS_PARSER_ERRCODE:
-	printf("parse error: %s\n",
-	       ops_errcode(content->errcode.errcode));
-	break;
-
     default:
-	fprintf(stderr,"Unexpected packet tag=%d (0x%x)\n",content_->tag,
-		content_->tag);
-	assert(0);
+        return callback_general(content_,cbinfo);
         }
 
     return OPS_RELEASE_MEMORY;
@@ -99,7 +122,6 @@
 
 	//	ops_print_packet(content_);
 
-    // Read data from packet into static buffer
     switch(content_->tag)
         {
 	case OPS_PTAG_CT_MDC:
@@ -109,23 +131,41 @@
 		memcpy(mdc_data,content->mdc.data,sz_mdc_data);
 		break;
 
-    case OPS_PARSER_PTAG:
-        // ignore
+    default:
+        return callback_general(content_,cbinfo);
+        }
+
+    return OPS_RELEASE_MEMORY;
+    }
+ 
+static ops_parse_cb_return_t
+callback_encrypted_pk_session_key(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo)
+    {
+    ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content;
+
+    OPS_USED(cbinfo);
+
+	//	ops_print_packet(content_);
+
+    switch(content_->tag)
+        {
+    case OPS_PTAG_CT_PK_SESSION_KEY:
         break;
 
-    case OPS_PARSER_ERROR:
-	printf("parse error: %s\n",content->error.error);
-	break;
+	case OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY:
+        sz_encrypted_pk_sk=sizeof(*encrypted_pk_sk);
+		encrypted_pk_sk=ops_mallocz(sz_encrypted_pk_sk);
+		memcpy(encrypted_pk_sk,&content->pk_session_key,sz_encrypted_pk_sk);
+		break;
 
-    case OPS_PARSER_ERRCODE:
-	printf("parse error: %s\n",
-	       ops_errcode(content->errcode.errcode));
-	break;
+    case OPS_PARSER_CMD_GET_SK_PASSPHRASE:
+        return callback_cmd_get_secret_key_passphrase(content_,cbinfo);
 
+    case OPS_PARSER_CMD_GET_SECRET_KEY:
+        return callback_cmd_get_secret_key(content_,cbinfo);
+
     default:
-	fprintf(stderr,"Unexpected packet tag=%d (0x%x)\n",content_->tag,
-		content_->tag);
-	assert(0);
+        return callback_general(content_,cbinfo);
         }
 
     return OPS_RELEASE_MEMORY;
@@ -142,19 +182,13 @@
 
     switch(content_->tag)
         {
-    case OPS_PARSER_PTAG:
-        // ignore
-        break;
-
     case OPS_PTAG_CT_LITERAL_DATA_HEADER:
     case OPS_PTAG_CT_LITERAL_DATA_BODY:
         return callback_literal_data(content_,cbinfo);
         break;
 
     default:
-	fprintf(stderr,"Unexpected packet tag=%d (0x%x)\n",content_->tag,
-		content_->tag);
-	assert(0);
+        return callback_general(content_,cbinfo);
         }
 
     return OPS_RELEASE_MEMORY;
@@ -437,6 +471,37 @@
     ops_memory_free(mem_ldt);
     }
 
+static void test_ops_encrypted_pk_sk()
+    {
+    char *user_id="Alpha (RSA, no passphrase) <alpha at test.com>";
+    ops_pk_session_key_t *encrypted_pk_session_key;
+    ops_create_info_t *cinfo;
+    ops_parse_info_t *pinfo;
+    ops_memory_t *mem;
+    int rtn=0;
+
+    // setup for write
+    ops_setup_memory_write(&cinfo,&mem,MAXBUF);
+
+    // write
+    const ops_key_data_t *pub_key=ops_keyring_find_key_by_userid(&pub_keyring, user_id);
+    encrypted_pk_session_key=ops_create_pk_session_key(pub_key);
+    ops_write_pk_session_key(cinfo,encrypted_pk_session_key);
+
+    // setup for read
+    ops_setup_memory_read(&pinfo,mem,callback_encrypted_pk_session_key);
+
+    // read
+    rtn=ops_parse(pinfo);
+
+    // test
+    CU_ASSERT(memcmp(encrypted_pk_session_key, encrypted_pk_sk, sz_encrypted_pk_sk)==0);
+
+    // cleanup
+    cleanup();
+    ops_teardown_memory_read(pinfo,mem);
+    }
+
 CU_pSuite suite_packet_types()
 {
     CU_pSuite suite = NULL;
@@ -462,6 +527,9 @@
     if (NULL == CU_add_test(suite, "Tag 20: Sym. Encrypted Integrity Protected Data packet", test_ops_se_ip))
 	    return NULL;
 
+    if (NULL == CU_add_test(suite, "Tag 1: PK Encrypted Session Key packet", test_ops_encrypted_pk_sk))
+	    return NULL;
+
     return suite;
 }
 

Modified: openpgpsdk/trunk/tests/tests.c
===================================================================
--- openpgpsdk/trunk/tests/tests.c	2007-08-09 16:05:16 UTC (rev 483)
+++ openpgpsdk/trunk/tests/tests.c	2007-08-10 12:07:33 UTC (rev 484)
@@ -7,6 +7,8 @@
 
 #include "CUnit/Basic.h"
 #include "openpgpsdk/readerwriter.h"
+// \todo remove the need for this
+#include "../src/advanced/parse_local.h"
 
 #include "tests.h"
 
@@ -16,6 +18,9 @@
 extern CU_pSuite suite_rsa_encrypt();
 
 char dir[MAXBUF+1];
+ops_keyring_t pub_keyring;
+ops_keyring_t sec_keyring;
+static char* no_passphrase="";
 
 int main()
     {
@@ -93,5 +98,124 @@
         }
     }
 
+ops_parse_cb_return_t
+callback_general(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo)
+    {
+    ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content;
+    
+    OPS_USED(cbinfo);
+    
+    //    ops_print_packet(content_);
+    
+    switch(content_->tag)
+        {
+    case OPS_PARSER_PTAG:
+        // ignore
+        break;
+        
+    case OPS_PARSER_ERROR:
+        printf("parse error: %s\n",content->error.error);
+        break;
+        
+    case OPS_PARSER_ERRCODE:
+        printf("parse error: %s\n",
+               ops_errcode(content->errcode.errcode));
+        break;
+        
+    default:
+        fprintf(stderr,"Unexpected packet tag=%d (0x%x)\n",content_->tag,
+                content_->tag);
+        assert(0);
+        }
+    
+    return OPS_RELEASE_MEMORY;
+    }
 
+ops_parse_cb_return_t
+callback_cmd_get_secret_key(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo)
+    {
+    ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content;
+    const ops_key_data_t *keydata=NULL;
+    const ops_secret_key_t *secret;
+    /*
+    static const ops_key_data_t *decrypt_key;
+    */
 
+    OPS_USED(cbinfo);
+
+//    ops_print_packet(content_);
+
+    switch(content_->tag)
+	{
+    case OPS_PARSER_CMD_GET_SECRET_KEY:
+        keydata=ops_keyring_find_key_by_id(&sec_keyring,content->get_secret_key.pk_session_key->key_id);
+        if (!keydata || !ops_key_is_secret(keydata))
+            return 0;
+
+        // Do we need the passphrase and not have it? If so, get it
+        ops_parser_content_t pc;
+        char *passphrase;
+        memset(&pc,'\0',sizeof pc);
+        passphrase=NULL;
+        pc.content.secret_key_passphrase.passphrase=&passphrase;
+        //        pc.content.secret_key_passphrase.secret_key=&(keydata->key.skey);
+        pc.content.secret_key_passphrase.secret_key=ops_get_secret_key_from_data(keydata);
+
+        /* Ugh. Need to duplicate this macro here to get the passphrase 
+           Duplication to be removed when the callback gets moved to main code.
+           Can we make this inline code rather than a macro?
+        */
+#define CB(cbinfo,t,pc)	do { (pc)->tag=(t); if((cbinfo)->cb(pc,(cbinfo)) == OPS_RELEASE_MEMORY) ops_parser_content_free(pc); } while(0)
+        CB(cbinfo,OPS_PARSER_CMD_GET_SK_PASSPHRASE,&pc);
+        
+        /* now get the key from the data */
+        secret=ops_get_secret_key_from_data(keydata);
+        while(!secret)
+            {
+            /* then it must be encrypted */
+            secret=ops_decrypt_secret_key_from_data(keydata,passphrase);
+            free(passphrase);
+            }
+        
+        *content->get_secret_key.secret_key=secret;
+        break;
+
+    default:
+        return callback_general(content_,cbinfo);
+	}
+    
+    return OPS_RELEASE_MEMORY;
+    }
+
+ops_parse_cb_return_t
+callback_cmd_get_secret_key_passphrase(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo)
+    {
+    ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content;
+    /*
+    static const ops_key_data_t *decrypt_key;
+    const ops_key_data_t *keydata=NULL;
+    const ops_secret_key_t *secret;
+    */
+
+    OPS_USED(cbinfo);
+
+//    ops_print_packet(content_);
+
+    switch(content_->tag)
+        {
+    case OPS_PARSER_CMD_GET_SK_PASSPHRASE:
+        /*
+          Doing this so the test can be automated.
+          Will move this into separate stacked callback later
+        */
+        *(content->secret_key_passphrase.passphrase)=ops_malloc_passphrase(no_passphrase);
+        return OPS_KEEP_MEMORY;
+        break;
+        
+    default:
+        return callback_general(content_,cbinfo);
+	}
+    
+    return OPS_RELEASE_MEMORY;
+    }
+

Modified: openpgpsdk/trunk/tests/tests.h
===================================================================
--- openpgpsdk/trunk/tests/tests.h	2007-08-09 16:05:16 UTC (rev 483)
+++ openpgpsdk/trunk/tests/tests.h	2007-08-10 12:07:33 UTC (rev 484)
@@ -19,5 +19,14 @@
 void create_testdata(const char *text, unsigned char *buf, const int maxlen);
 #define MAXBUF 128
 
+ops_parse_cb_return_t
+callback_general(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo);
+ops_parse_cb_return_t
+callback_cmd_get_secret_key(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo);
+ops_parse_cb_return_t
+callback_cmd_get_secret_key_passphrase(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo);
+
+ops_keyring_t pub_keyring;
+ops_keyring_t sec_keyring;
 #endif
 



More information about the OpenPGPsdk-svn mailing list