Parcourir la source

Refactoring: adding ask_rsa() to question_t as callback for rsa keys specifically.

Changin this API was made just to avoid the annoyng preface of
get_pubkey->rsa. And being faster whenever testing.
Enabling tests with automake.
Michele Orrù il y a 11 ans
Parent
commit
6966588152

+ 5 - 1
src/qa.c

@@ -41,6 +41,7 @@ X509* get_local_cert(const char *src)
 int qa_init(const struct qa_conf* conf)
 {
   X509 *crt;
+  RSA *rsa;
   struct qa_question *q;
 
   /* bind stdout/stderr to a BIO shit to be used externally */
@@ -61,11 +62,14 @@ int qa_init(const struct qa_conf* conf)
   if (!crt)
     error(EXIT_FAILURE, errno, "oops");
 
+  rsa = X509_get_pubkey(crt)->pkey.rsa;
+
   register_all_questions();
   for (q=questions.lh_first; q; q = q->qs.le_next) {
     if (q->setup)    q->setup();
     if (q->test)     q->test(crt);
-    q->ask(crt);
+    if (q->ask_rsa)  q->ask_rsa(rsa);
+    if (q->ask_crt)  q->ask_crt(crt);
     if (q->teardown) q->teardown();
   }
 

+ 4 - 9
src/questions/dixon.c

@@ -176,13 +176,8 @@ int dixon_question_teardown(void) {
   return 0;
 }
 
-int dixon_question_test(X509* cert) {
-  return 1;
-}
-
 
-int dixon_question_ask(X509* cert) {
-  RSA *rsa;
+int dixon_question_ask_rsa(RSA *rsa) {
   /* key data */
   BIGNUM
     *n,
@@ -200,7 +195,6 @@ int dixon_question_ask(X509* cert) {
   char *even_powers;
   size_t i, j, k;
 
-  rsa = X509_get_pubkey(cert)->pkey.rsa;
   n = rsa->n;
   U_bucket = malloc(sizeof(ssize_t) * U_SIZE);
   even_powers = malloc(sizeof(char) * B_size);
@@ -278,6 +272,7 @@ qa_question_t DixonQuestion = {
   .name = "Dixon",
   .setup = dixon_question_setup,
   .teardown = dixon_question_teardown,
-  .test = dixon_question_test,
-  .ask = dixon_question_ask
+  .test =  NULL,
+  .ask_rsa = dixon_question_ask_rsa,
+  .ask_crt = NULL
 };

+ 19 - 4
src/questions/example.c

@@ -1,4 +1,5 @@
 #include <openssl/bio.h>
+#include <openssl/rsa.h>
 #include <openssl/x509.h>
 
 
@@ -11,10 +12,18 @@ int example_question_setup(void) {
   return 0;
 }
 
-int example_question_teardown(void) { return 0; }
+int example_question_teardown(void)
+{
+
+  return 0;
+}
+
 /* XXX. apparently openssl does not allow const X509* in get_pkey() func */
-int example_question_test(X509* cert) { return 1; }
-int example_question_ask(X509* cert)
+int example_question_test(X509* cert) {
+  return 1;
+}
+
+int example_question_ask_crt(X509* cert)
 {
   EVP_PKEY* pkey;
 
@@ -23,6 +32,11 @@ int example_question_ask(X509* cert)
   return 1;
 }
 
+int example_question_ask_rsa(RSA *rsa)
+{
+  return 0;
+}
+
 
 
 qa_question_t ExampleQuestion = {
@@ -30,5 +44,6 @@ qa_question_t ExampleQuestion = {
   .setup = example_question_setup,
   .teardown = example_question_teardown,
   .test = example_question_test,
-  .ask = example_question_ask
+  .ask_crt = example_question_ask_crt,
+  .ask_rsa = example_question_ask_rsa
 };

+ 3 - 3
src/questions/include/questions.h

@@ -10,13 +10,13 @@ typedef struct qa_question {
   const char* name;
   int (* setup) (void);
   int (* teardown) ();
-  int (* test) (X509* cert);
-  int (* ask) (X509* cert);
+  int (* test) (X509 *cert);
+  int (* ask_rsa) (RSA *rsa);
+  int (* ask_crt) (X509 *cert);
 
   LIST_ENTRY(qa_question) qs;
 } qa_question_t;
 
-
 LIST_HEAD(listhead, qa_question) questions;
 
 void register_all_questions(void);

+ 4 - 13
src/questions/pollard.c

@@ -48,14 +48,6 @@ int pollard1_question_teardown(void)
 }
 
 
-int pollard1_question_test(X509 *cert)
-{
-  return 0;
-}
-
-
-int BN_sqrtmod(BIGNUM*, BIGNUM*, BIGNUM*, BN_CTX*);
-
 /**
  * \brief Pollard (p-1) factorization.
  *
@@ -68,17 +60,15 @@ int BN_sqrtmod(BIGNUM*, BIGNUM*, BIGNUM*, BN_CTX*);
  * about 3^(−3) = 1/27 that a B value of n^(1/6) will yield a factorisation.»
  *
  */
-int pollard1_question_ask(X509 *cert)
+int pollard1_question_ask_rsa(RSA *rsa)
 {
   int ret = 1;
-  RSA *rsa;
   BIGNUM *a, *B, *a1;
   BIGNUM *gcd, *rem;
   BIGNUM *n;
   BIGNUM *p, *q;
   BN_CTX *ctx;
 
-  rsa = X509_get_pubkey(cert)->pkey.rsa;
   n = rsa->n;
   a = BN_new();
   B = BN_new();
@@ -125,6 +115,7 @@ struct qa_question PollardQuestion = {
   .name = "Pollard's (p-1) factorization",
   .setup = pollard1_question_setup,
   .teardown = pollard1_question_teardown,
-  .test = pollard1_question_test,
-  .ask = pollard1_question_ask,
+  .test = NULL,
+  .ask_rsa = pollard1_question_ask_rsa,
+  .ask_crt = NULL
 };

+ 6 - 6
src/questions/tests/Makefile.am

@@ -1,12 +1,12 @@
 # unittesting my ass
-AM_CFLAGS = -I ../../include/
+AM_CFLAGS = -I ../../include/ -ggdb -Wall
 AM_LDFLAGS = -lcrypto -lssl
+LDADD = ../libquestions.a
 
-check_PROGRAMS = test_qarith test_qstrings
+check_PROGRAMS = test_qarith test_qstrings test_wiener test_pollard
+TESTS = $(check_PROGRAMS)
 
 test_qstrings_SOURCES = test_qstrings.c
-test_qstrings_LDADD = ../libquestions.a
 test_qarith_SOURCES = test_qarith.c
-test_qarith_LDADD = ../libquestions.a
-
-TESTS = $(check_PROGRAMS)
+test_wiener_SOURCES = test_wiener.c
+test_pollard_SOURCES = test_pollard.c

+ 9 - 7
src/questions/tests/test_pollard.c

@@ -1,22 +1,24 @@
 #include <assert.h>
 
+#include <openssl/rsa.h>
 #include <openssl/x509.h>
 #include <openssl/pem.h>
 
-#include  "questions.h"
-#include "qpollard.h"
+#include "qa/questions/questions.h"
+#include "qa/questions/qpollard.h"
 
 void test_pollard(void)
 {
   X509 *crt;
-  FILE *fp = fopen("test/pollard.pem", "r");
+  RSA *rsa;
+  FILE *fp = fopen("pollard.pem", "r");
+
   if (!fp) exit(EXIT_FAILURE);
   crt = PEM_read_X509(fp, NULL, 0, NULL);
-  if (!crt) {
-    exit(EXIT_FAILURE);
-  }
+  if (!crt) exit(EXIT_FAILURE);
 
-  PollardQuestion.ask(crt);
+  rsa = X509_get_pubkey(crt)->pkey.rsa;
+  PollardQuestion.ask_rsa(rsa);
 }
 
 int main(int argc, char **argv)

+ 9 - 7
src/questions/tests/test_wiener.c

@@ -11,8 +11,8 @@
 #include <openssl/x509.h>
 #include <openssl/err.h>
 
-#include "questions.h"
-#include "qwiener.h"
+#include "qa/questions/questions.h"
+#include "qa/questions/qwiener.h"
 
 /**
  * \brief Testing the continued fractions generator.
@@ -116,7 +116,8 @@ void test_cf(void)
 void test_wiener(void)
 {
   X509 *crt;
-  FILE *fp = fopen("tests/wiener_test.crt", "r");
+  RSA *rsa;
+  FILE *fp = fopen("wiener_test.crt", "r");
 
   if (!fp) exit(EXIT_FAILURE);
   crt = PEM_read_X509(fp, NULL, 0, NULL);
@@ -124,17 +125,18 @@ void test_wiener(void)
     exit(EXIT_FAILURE);
   }
 
-  assert(WienerQuestion.test(crt));
-  assert(WienerQuestion.ask(crt));
+  rsa = X509_get_pubkey(crt)->pkey.rsa;
+  /* assert(WienerQuestion.test(crt)); */
+  assert(WienerQuestion.ask_rsa(rsa));
 }
 
 int main(int argc, char ** argv)
 {
-  WienerQuestion.setup();
+  if (WienerQuestion.setup) WienerQuestion.setup();
 
   test_cf();
   test_wiener();
 
-  WienerQuestion.teardown();
+  if (WienerQuestion.teardown) WienerQuestion.teardown();
   return 0;
 }

+ 6 - 15
src/questions/wiener.c

@@ -146,17 +146,8 @@ bigfraction_t* cf_next(cf_t *f)
 /*
  *  Weiner Attack Implementation
  */
-
-int wiener_question_setup(void) { return 0; }
-
-int wiener_question_teardown(void) { return 0; }
-
-int wiener_question_test(X509* cert) { return 1; }
-
-
-int wiener_question_ask(X509* cert)
+int wiener_question_ask_rsa(RSA *rsa)
 {
-  RSA *rsa;
   /* key data */
   BIGNUM *n, *e, *d, *phi;
   BIGNUM *p, *q;
@@ -170,7 +161,6 @@ int wiener_question_ask(X509* cert)
   BN_CTX *ctx;
   int bits;
 
-  rsa = X509_get_pubkey(cert)->pkey.rsa;
   phi = BN_new();
   tmp = BN_new();
   rem = BN_new();
@@ -241,8 +231,9 @@ int wiener_question_ask(X509* cert)
 
 qa_question_t WienerQuestion = {
   .name = "Wiener",
-  .setup = wiener_question_setup,
-  .teardown = wiener_question_teardown,
-  .test = wiener_question_test,
-  .ask = wiener_question_ask
+  .setup = NULL,
+  .teardown = NULL,
+  .test = NULL,
+  .ask_rsa = wiener_question_ask_rsa,
+  .ask_crt = NULL,
 };