]> SAFE projects GIT repository - jmp/mailleur/commitdiff
dig_getchallange procure ready
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Sun, 6 Jul 2025 18:55:56 +0000 (14:55 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Sun, 6 Jul 2025 18:55:56 +0000 (14:55 -0400)
lib/unidig.c
lib/unidig.h

index e4316c3ed4117684850d0df99f8507eb73a728ab..e93f51b75c10241a535f3e31e6450d1a581ffc99 100644 (file)
@@ -14,6 +14,9 @@
 #include       "subcnv.h"
 #include       "unidig.h"
 
+#define ALGO    "md5-sess"
+#define MAXBUF  2000
+#define CHARSET "utf-8"
 /*
 \f
 */
@@ -278,6 +281,76 @@ while (proceed==true) {
   }
 return hashmd5;
 
+#undef  OPEP
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
+/*      Procedure to generate a unique DIGEST-M5        */
+/*      challenge as an B64 string.                     */
+/*                                                      */
+/********************************************************/
+PUBLIC char *dig_getchallenge()
+
+{
+#define OPEP    "unidig.c:dig_getchallenge,"
+
+static char *comp[]={
+        "realm=\"%s\"",         //the LOCAL realm
+        ",nonce=\"%s\"",
+        ",maxbuf=%d",
+        ",qop=%s",
+        ",algorithm=%s",
+        ",charset=%s",
+        (char*)0
+        };
+
+char *challenge;
+
+challenge=(char *)calloc(MAXBUF,sizeof(char));
+for (int num=0;comp[num]!=(char *)0;num++) {
+  char loc[MAXBUF/2];
+
+  (void) memset(loc,'\000',sizeof(loc));
+  switch (num) {
+    case 0      :       //realm
+      (void) snprintf(loc,sizeof(loc),comp[num],REALM);
+      break;
+    case 1      :  {     //nonce
+      char *nonce;
+      char *b64;
+
+      nonce=cnv_getrndstr(20);
+      b64=cnv_setb64(nonce); 
+      (void) snprintf(loc,sizeof(loc),comp[num],b64);
+      b64=rou_freestr(b64);
+      nonce=rou_freestr(nonce);
+      }
+      break;
+    case 2      :       //maxbuf
+      (void) snprintf(loc,sizeof(loc),comp[num],MAXBUF);
+      break;
+    case 3      :       //qop
+      (void) snprintf(loc,sizeof(loc),comp[num],"auth");
+      break;
+    case 4      :       //algorithm
+      (void) snprintf(loc,sizeof(loc),comp[num],ALGO);
+      break;
+    case 5      :       //charset
+      (void) snprintf(loc,sizeof(loc),comp[num],CHARSET);
+      break;
+    default     :       //none expect field BUG!
+      (void) rou_alert(0,"%s field[%d]=%s not treated (BUG!)",OPEP,num,comp[num]);
+      challenge=rou_freestr(challenge);
+      break;
+    }
+  if (strlen(loc)>0)
+    (void) strcat(challenge,loc); 
+  }
+return challenge;
+
 #undef  OPEP
 }
 /*
index fa856c48ee64e532fc9ca9316e16585fc493a600..cecc3c157f7ebf9bed7aa282cab1edae6222c9a6 100644 (file)
@@ -8,6 +8,8 @@
 #ifndef        UNIDIG
 #define UNIDIG
 
+#define REALM    APPNAME"-email"
+
 typedef struct  {       //DIGEST-MD5 challange response structure
         char *username; //username requesting authentication
         char *realm;    //Challenge realm
@@ -37,6 +39,10 @@ extern MD5TYP *dig_hashmd5(unsigned char *seq);
 //Procedure to crypt a string with MD5 hash function
 extern char *dig_cryptmd5(const void *key,unsigned char *seq);
 
+//Procedure to generate a DISGEST-MD5 challaneg as a
+//b64 string.
+extern char *dig_getchallenge();
+
 //Procedure to compute local response to challenge and
 //check if the remote session is the same
 extern _Bool dig_checkresp(RSPTYP *resp,char *secret);