]> asedeno.scripts.mit.edu Git - PuTTY.git/blobdiff - windows/winpgen.c
Surround process protection with an #ifndef UNPROTECT
[PuTTY.git] / windows / winpgen.c
index 093479ff77969b55464dc42872def2f2af4772f1..916cca643ff4700ba3209b1596822478121ef099 100644 (file)
@@ -27,7 +27,7 @@ static char *cmdline_keyfile = NULL;
 /*
  * Print a modal (Really Bad) message box and perform a fatal exit.
  */
-void modalfatalbox(char *fmt, ...)
+void modalfatalbox(const char *fmt, ...)
 {
     va_list ap;
     char *stuff;
@@ -44,7 +44,7 @@ void modalfatalbox(char *fmt, ...)
 /*
  * Print a non-fatal message box and do not exit.
  */
-void nonfatal(char *fmt, ...)
+void nonfatal(const char *fmt, ...)
 {
     va_list ap;
     char *stuff;
@@ -141,7 +141,7 @@ struct PassphraseProcStruct {
 /*
  * Dialog-box function for the passphrase box.
  */
-static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
+static INT_PTR CALLBACK PassphraseProc(HWND hwnd, UINT msg,
                                   WPARAM wParam, LPARAM lParam)
 {
     static char **passphrase = NULL;
@@ -233,7 +233,7 @@ static int prompt_keyfile(HWND hwnd, char *dlgtitle,
 /*
  * Dialog-box function for the Licence box.
  */
-static int CALLBACK LicenceProc(HWND hwnd, UINT msg,
+static INT_PTR CALLBACK LicenceProc(HWND hwnd, UINT msg,
                                WPARAM wParam, LPARAM lParam)
 {
     switch (msg) {
@@ -272,7 +272,7 @@ static int CALLBACK LicenceProc(HWND hwnd, UINT msg,
 /*
  * Dialog-box function for the About box.
  */
-static int CALLBACK AboutProc(HWND hwnd, UINT msg,
+static INT_PTR CALLBACK AboutProc(HWND hwnd, UINT msg,
                              WPARAM wParam, LPARAM lParam)
 {
     switch (msg) {
@@ -383,69 +383,23 @@ static void hidemany(HWND hwnd, const int *ids, int hideit)
 
 static void setupbigedit1(HWND hwnd, int id, int idstatic, struct RSAKey *key)
 {
-    char *buffer;
-    char *dec1, *dec2;
-
-    dec1 = bignum_decimal(key->exponent);
-    dec2 = bignum_decimal(key->modulus);
-    buffer = dupprintf("%d %s %s %s", bignum_bitcount(key->modulus),
-                      dec1, dec2, key->comment);
+    char *buffer = ssh1_pubkey_str(key);
     SetDlgItemText(hwnd, id, buffer);
     SetDlgItemText(hwnd, idstatic,
                   "&Public key for pasting into authorized_keys file:");
-    sfree(dec1);
-    sfree(dec2);
     sfree(buffer);
 }
 
 static void setupbigedit2(HWND hwnd, int id, int idstatic,
                          struct ssh2_userkey *key)
 {
-    unsigned char *pub_blob;
-    char *buffer, *p;
-    int pub_len;
-    int i;
-
-    pub_blob = key->alg->public_blob(key->data, &pub_len);
-    buffer = snewn(strlen(key->alg->name) + 4 * ((pub_len + 2) / 3) +
-                  strlen(key->comment) + 3, char);
-    strcpy(buffer, key->alg->name);
-    p = buffer + strlen(buffer);
-    *p++ = ' ';
-    i = 0;
-    while (i < pub_len) {
-       int n = (pub_len - i < 3 ? pub_len - i : 3);
-       base64_encode_atom(pub_blob + i, n, p);
-       i += n;
-       p += 4;
-    }
-    *p++ = ' ';
-    strcpy(p, key->comment);
+    char *buffer = ssh2_pubkey_openssh_str(key);
     SetDlgItemText(hwnd, id, buffer);
     SetDlgItemText(hwnd, idstatic, "&Public key for pasting into "
                   "OpenSSH authorized_keys file:");
-    sfree(pub_blob);
     sfree(buffer);
 }
 
-static int save_ssh1_pubkey(char *filename, struct RSAKey *key)
-{
-    char *dec1, *dec2;
-    FILE *fp;
-
-    fp = fopen(filename, "wb");
-    if (!fp)
-       return 0;
-    dec1 = bignum_decimal(key->exponent);
-    dec2 = bignum_decimal(key->modulus);
-    fprintf(fp, "%d %s %s %s\n",
-           bignum_bitcount(key->modulus), dec1, dec2, key->comment);
-    fclose(fp);
-    sfree(dec1);
-    sfree(dec2);
-    return 1;
-}
-
 /*
  * Warn about the obsolescent key file format.
  */
@@ -466,53 +420,6 @@ void old_keyfile_warning(void)
     MessageBox(NULL, message, mbtitle, MB_OK);
 }
 
-static int save_ssh2_pubkey(char *filename, struct ssh2_userkey *key)
-{
-    unsigned char *pub_blob;
-    char *p;
-    int pub_len;
-    int i, column;
-    FILE *fp;
-
-    pub_blob = key->alg->public_blob(key->data, &pub_len);
-
-    fp = fopen(filename, "wb");
-    if (!fp)
-       return 0;
-
-    fprintf(fp, "---- BEGIN SSH2 PUBLIC KEY ----\n");
-
-    fprintf(fp, "Comment: \"");
-    for (p = key->comment; *p; p++) {
-       if (*p == '\\' || *p == '\"')
-           fputc('\\', fp);
-       fputc(*p, fp);
-    }
-    fprintf(fp, "\"\n");
-
-    i = 0;
-    column = 0;
-    while (i < pub_len) {
-       char buf[5];
-       int n = (pub_len - i < 3 ? pub_len - i : 3);
-       base64_encode_atom(pub_blob + i, n, buf);
-       i += n;
-       buf[4] = '\0';
-       fputs(buf, fp);
-       if (++column >= 16) {
-           fputc('\n', fp);
-           column = 0;
-       }
-    }
-    if (column > 0)
-       fputc('\n', fp);
-    
-    fprintf(fp, "---- END SSH2 PUBLIC KEY ----\n");
-    fclose(fp);
-    sfree(pub_blob);
-    return 1;
-}
-
 enum {
     controlidstart = 100,
     IDC_QUIT,
@@ -799,9 +706,7 @@ void load_key_file(HWND hwnd, struct MainDlgState *state,
 
                savecomment = state->ssh2key.comment;
                state->ssh2key.comment = NULL;
-               fp =
-                   state->ssh2key.alg->
-                   fingerprint(state->ssh2key.data);
+               fp = ssh2_fingerprint(state->ssh2key.alg, state->ssh2key.data);
                state->ssh2key.comment = savecomment;
 
                SetDlgItemText(hwnd, IDC_FINGERPRINT, fp);
@@ -844,7 +749,7 @@ void load_key_file(HWND hwnd, struct MainDlgState *state,
 /*
  * Dialog-box function for the main PuTTYgen dialog box.
  */
-static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
+static INT_PTR CALLBACK MainDlgProc(HWND hwnd, UINT msg,
                                WPARAM wParam, LPARAM lParam)
 {
     static const char generating_msg[] =
@@ -885,7 +790,7 @@ static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
            AppendMenu(menu1, MF_ENABLED, IDC_SAVE, "&Save private key");
            AppendMenu(menu1, MF_SEPARATOR, 0, 0);
            AppendMenu(menu1, MF_ENABLED, IDC_QUIT, "E&xit");
-           AppendMenu(menu, MF_POPUP | MF_ENABLED, (UINT) menu1, "&File");
+           AppendMenu(menu, MF_POPUP | MF_ENABLED, (UINT_PTR) menu1, "&File");
            state->filemenu = menu1;
 
            menu1 = CreateMenu();
@@ -896,7 +801,7 @@ static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
            AppendMenu(menu1, MF_ENABLED, IDC_KEYSSH2DSA, "SSH-2 &DSA key");
             AppendMenu(menu1, MF_ENABLED, IDC_KEYSSH2ECDSA, "SSH-2 &ECDSA key");
             AppendMenu(menu1, MF_ENABLED, IDC_KEYSSH2ED25519, "SSH-2 ED&25519 key");
-           AppendMenu(menu, MF_POPUP | MF_ENABLED, (UINT) menu1, "&Key");
+           AppendMenu(menu, MF_POPUP | MF_ENABLED, (UINT_PTR) menu1, "&Key");
            state->keymenu = menu1;
 
            menu1 = CreateMenu();
@@ -908,7 +813,7 @@ static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
                       "Export &OpenSSH key (force new file format)");
            AppendMenu(menu1, MF_ENABLED, IDC_EXPORT_SSHCOM,
                       "Export &ssh.com key");
-           AppendMenu(menu, MF_POPUP | MF_ENABLED, (UINT) menu1,
+           AppendMenu(menu, MF_POPUP | MF_ENABLED, (UINT_PTR) menu1,
                       "Con&versions");
            state->cvtmenu = menu1;
 
@@ -916,7 +821,7 @@ static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
            AppendMenu(menu1, MF_ENABLED, IDC_ABOUT, "&About");
            if (has_help())
                AppendMenu(menu1, MF_ENABLED, IDC_GIVEHELP, "&Help");
-           AppendMenu(menu, MF_POPUP | MF_ENABLED, (UINT) menu1, "&Help");
+           AppendMenu(menu, MF_POPUP | MF_ENABLED, (UINT_PTR) menu1, "&Help");
 
            SetMenu(hwnd, menu);
        }
@@ -973,12 +878,13 @@ static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
                       "&Save private key", IDC_SAVE);
            endbox(&cp);
            beginbox(&cp, "Parameters", IDC_BOX_PARAMS);
-           radioline(&cp, "Type of key to generate:", IDC_TYPESTATIC, 4,
+           radioline(&cp, "Type of key to generate:", IDC_TYPESTATIC, 5,
+                     "&RSA", IDC_KEYSSH2RSA,
+                      "&DSA", IDC_KEYSSH2DSA,
+                      "&ECDSA", IDC_KEYSSH2ECDSA,
+                      "ED&25519", IDC_KEYSSH2ED25519,
                      "SSH-&1 (RSA)", IDC_KEYSSH1,
-                     "SSH-2 &RSA", IDC_KEYSSH2RSA,
-                      "SSH-2 &DSA", IDC_KEYSSH2DSA,
-                      "SSH-2 &ECDSA", IDC_KEYSSH2ECDSA,
-                      "SSH-2 ED&25519", IDC_KEYSSH2ED25519, NULL);
+                      NULL);
            staticedit(&cp, "Number of &bits in a generated key:",
                       IDC_BITSSTATIC, IDC_BITS, 20);
            endbox(&cp);
@@ -1062,15 +968,12 @@ static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
                state = (struct MainDlgState *)
                    GetWindowLongPtr(hwnd, GWLP_USERDATA);
                if (!IsDlgButtonChecked(hwnd, LOWORD(wParam)))
-                   CheckRadioButton(hwnd, IDC_KEYSSH1, IDC_KEYSSH2DSA,
+                   CheckRadioButton(hwnd,
+                                     IDC_KEYSSH1, IDC_KEYSSH2ED25519,
                                     LOWORD(wParam));
-               CheckMenuRadioItem(state->keymenu, IDC_KEYSSH1, IDC_KEYSSH2DSA,
+               CheckMenuRadioItem(state->keymenu,
+                                   IDC_KEYSSH1, IDC_KEYSSH2ED25519,
                                   LOWORD(wParam), MF_BYCOMMAND);
-                CheckRadioButton(hwnd, IDC_KEYSSH1, IDC_KEYSSH2ECDSA,
-                                 LOWORD(wParam));
-                CheckMenuRadioItem(state->keymenu, IDC_KEYSSH1,
-                                   IDC_KEYSSH2ECDSA,
-                                   LOWORD(wParam), MF_BYCOMMAND);
            }
            break;
          case IDC_QUIT:
@@ -1326,15 +1229,27 @@ static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
                        if (ret != IDYES)
                            break;
                    }
-                   if (state->ssh2) {
-                       ret = save_ssh2_pubkey(filename, &state->ssh2key);
-                   } else {
-                       ret = save_ssh1_pubkey(filename, &state->key);
-                   }
-                   if (ret <= 0) {
-                       MessageBox(hwnd, "Unable to save key file",
-                                  "PuTTYgen Error", MB_OK | MB_ICONERROR);
-                   }
+                    fp = fopen(filename, "w");
+                    if (!fp) {
+                        MessageBox(hwnd, "Unable to open key file",
+                                   "PuTTYgen Error", MB_OK | MB_ICONERROR);
+                    } else {
+                        if (state->ssh2) {
+                            int bloblen;
+                            unsigned char *blob;
+                            blob = state->ssh2key.alg->public_blob
+                                (state->ssh2key.data, &bloblen);
+                            ssh2_write_pubkey(fp, state->ssh2key.comment,
+                                              blob, bloblen,
+                                              SSH_KEYTYPE_SSH2_PUBLIC_RFC4716);
+                        } else {
+                            ssh1_write_pubkey(fp, &state->key);
+                        }
+                        if (fclose(fp) < 0) {
+                            MessageBox(hwnd, "Unable to save key file",
+                                       "PuTTYgen Error", MB_OK | MB_ICONERROR);
+                        }
+                    }
                }
            }
            break;
@@ -1369,12 +1284,7 @@ static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
                state->ssh2key.alg = &ssh_dss;
             } else if (state->keytype == ECDSA) {
                 state->ssh2key.data = &state->eckey;
-                if (state->eckey.publicKey.curve->fieldBits == 256)
-                    state->ssh2key.alg = &ssh_ecdsa_nistp256;
-                else if (state->eckey.publicKey.curve->fieldBits == 384)
-                    state->ssh2key.alg = &ssh_ecdsa_nistp384;
-                else
-                    state->ssh2key.alg = &ssh_ecdsa_nistp521;
+                state->ssh2key.alg = state->eckey.signalg;
             } else if (state->keytype == ED25519) {
                 state->ssh2key.data = &state->eckey;
                 state->ssh2key.alg = &ssh_ecdsa_ed25519;
@@ -1429,7 +1339,7 @@ static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
            *state->commentptr = NULL;
            if (state->ssh2) {
                char *fp;
-               fp = state->ssh2key.alg->fingerprint(state->ssh2key.data);
+               fp = ssh2_fingerprint(state->ssh2key.alg, state->ssh2key.data);
                SetDlgItemText(hwnd, IDC_FINGERPRINT, fp);
                sfree(fp);
            } else {
@@ -1459,7 +1369,7 @@ static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
       case WM_HELP:
         {
             int id = ((LPHELPINFO)lParam)->iCtrlId;
-            char *topic = NULL;
+            const char *topic = NULL;
             switch (id) {
               case IDC_GENERATING:
               case IDC_PROGRESS: