Procházet zdrojové kódy

Printing informations about the just broken RSA key.

In line to what eemerged in the last meeting with emanuele, I'm printing out the
information about the private key just broken.
Doing some cleanup meanwhile.
Michele Orrù před 11 roky
rodič
revize
97ebc3ccc7
3 změnil soubory, kde provedl 68 přidání a 13 odebrání
  1. 61 6
      src/qa.c
  2. 3 2
      src/questions/pollard.c
  3. 4 5
      src/questions/wiener.c

+ 61 - 6
src/qa.c

@@ -1,3 +1,11 @@
+/**
+ * \file qa.c
+ * \brief QA controller and engine.
+ *
+ * After retrieving a valid configuration from the frontend, this file takes
+ * care of running the actual mainloop.
+ */
+
 #include <assert.h>
 #include <error.h>
 #include <stdio.h>
@@ -15,13 +23,26 @@
 #include "qa/questions/questions.h"
 #include "qa/qa_sock.h"
 
-void qa_abort(const char *reason)
+/**
+ * \Handle unexpected error.
+ *
+ * Function handling fatal errors: exit immediately, reporting eventual errors
+ * coming from openssl/bio or the standard errno.
+ */
+void
+qa_abort(const char *reason)
 {
   //ERR_print_errors_fp(stderr);
   exit(EXIT_FAILURE);
 }
 
-X509* get_local_cert(const char *src)
+/**
+ * \brief Loads a valid ssl certificate from file.
+ *
+ * \return NULL in case of error, a X509* structure otherwise.
+ */
+X509*
+get_local_cert(const char *src)
 {
   X509 *crt;
   FILE *fp;
@@ -34,12 +55,45 @@ X509* get_local_cert(const char *src)
   return crt;
 }
 
+/**
+ * \brief Print out a valid RSA Private Key.
+ *
+ */
+static void
+print_rsa_private(RSA *rsa)
+{
+  size_t i;
+  char *dec, *hex;
+  const struct {
+    const char *desc;
+    BIGNUM *n;
+  } items[5] = {
+    {"Public Modulus", rsa->n},
+    {"Prime Factor p", rsa->p},
+    {"Prime Factor q", rsa->q},
+    {"Public Exponent", rsa->e},
+    {"Private Exponent", rsa->d},
+  };
+
+
+  assert(rsa); /* && rsa->p && rsa->q && rsa->e); */
+  for (i=0; i!=5; i++) {
+    if (!items[i].n) continue;
+    dec = BN_bn2dec(items[i].n);
+    hex = BN_bn2hex(items[i].n);
+    fprintf(stdout, "\t%-22s : %-15s (0x%s)\n", items[i].desc, dec, hex);
+    OPENSSL_free(dec);
+    OPENSSL_free(hex);
+  }
+}
+
 /**
  * \brief Given an initial configuration, stuctures the program flow.
  *
  * \param[in] args   Initial configuration given from a frontend.
  */
-int qa_init(const struct qa_conf* conf)
+int
+qa_init(const struct qa_conf* conf)
 {
   X509 *crt = NULL;
 
@@ -73,7 +127,8 @@ int qa_init(const struct qa_conf* conf)
   return 0;
 }
 
-void qa_dispose(X509 *crt)
+void
+qa_dispose(X509 *crt)
 {
   RSA *pub = X509_get_pubkey(crt)->pkey.rsa;
   RSA *priv;
@@ -93,7 +148,7 @@ void qa_dispose(X509 *crt)
     }
 
     /*
-     * Run test. If the test is undecidible or either okk, go on. Otherwise,
+     * Run test. If the test is undecidible or either ok, go on. Otherwise,
      * print an error message and go to the next question.
      */
     if (q->test && q->test(crt) < 0) {
@@ -108,7 +163,7 @@ void qa_dispose(X509 *crt)
     if (q->ask_rsa &&
         (priv = q->ask_rsa(pub))) {
       fprintf(stderr, "[\\] Key Broken using %s.\n", q->pretty_name);
-
+      print_rsa_private(priv);
     }
 
     /*

+ 3 - 2
src/questions/pollard.c

@@ -97,9 +97,10 @@ pollard1_question_ask_rsa(const RSA *rsa)
   /* Either p or q found :) */
   if (!BN_is_zero(B)) {
     ret = RSA_new();
-    ret->n = rsa->e;
-    ret->p = p = BN_dup(gcd);
+    ret->n = rsa->n;
+    ret->e = rsa->e;
     ret->q = q = BN_new();
+    ret->p = p = BN_dup(gcd);
     BN_div(q, NULL, n, gcd, ctx);
   }
 

+ 4 - 5
src/questions/wiener.c

@@ -30,7 +30,6 @@ wiener_question_ask_rsa(const RSA *rsa)
   /* key data */
   RSA *ret = NULL;
   BIGNUM *n, *e, *d, *phi;
-  BIGNUM *p, *q;
   /* continued fractions coefficient, and mod */
   cf_t* cf;
   bigfraction_t *it;
@@ -92,10 +91,10 @@ wiener_question_ask_rsa(const RSA *rsa)
     ret = RSA_new();
     ret->n = rsa->n;
     ret->e = rsa->e;
-    ret->p = p = BN_new();
-    ret->q = q = BN_new();
-    BN_usub(p, b2, tmp);
-    BN_uadd(q, b2, tmp);
+    ret->p = BN_new();
+    ret->q = BN_new();
+    BN_usub(ret->p, b2, tmp);
+    BN_uadd(ret->q, b2, tmp);
     break;
   }