]> asedeno.scripts.mit.edu Git - PuTTY.git/blobdiff - cmdgen.c
Const-correctness in public-key functions.
[PuTTY.git] / cmdgen.c
index ae405208b36ab936b6b555f78ab5d74745952d4a..ea60c25f7dbdc82ad2defa6c4049c8ae05f71583 100644 (file)
--- a/cmdgen.c
+++ b/cmdgen.c
@@ -102,6 +102,16 @@ void modalfatalbox(char *p, ...)
     cleanup_exit(1);
 }
 
+void nonfatal(char *p, ...)
+{
+    va_list ap;
+    fprintf(stderr, "ERROR: ");
+    va_start(ap, p);
+    vfprintf(stderr, p, ap);
+    va_end(ap);
+    fputc('\n', stderr);
+}
+
 /*
  * Stubs to let everything else link sensibly.
  */
@@ -118,10 +128,7 @@ void sk_cleanup(void)
 
 void showversion(void)
 {
-    char *verstr = dupstr(ver);
-    verstr[0] = tolower((unsigned char)verstr[0]);
-    printf("PuTTYgen %s\n", verstr);
-    sfree(verstr);
+    printf("puttygen: %s\n", ver);
 }
 
 void usage(int standalone)
@@ -153,6 +160,10 @@ void help(void)
            "  -O    specify output type:\n"
            "           private             output PuTTY private key format\n"
            "           private-openssh     export OpenSSH private key\n"
+           "           private-openssh-pem export OpenSSH private key "
+                                             "(force old PEM format)\n"
+           "           private-openssh-new export OpenSSH private key "
+                                             "(force new format)\n"
            "           private-sshcom      export ssh.com private key\n"
            "           public              standard / ssh.com public key\n"
            "           public-openssh      OpenSSH public key\n"
@@ -257,12 +268,12 @@ static char *blobfp(char *alg, int bits, unsigned char *blob, int bloblen)
 int main(int argc, char **argv)
 {
     char *infile = NULL;
-    Filename infilename;
-    enum { NOKEYGEN, RSA1, RSA2, DSA } keytype = NOKEYGEN;    
+    Filename *infilename = NULL, *outfilename = NULL;
+    enum { NOKEYGEN, RSA1, RSA2, DSA, ECDSA } keytype = NOKEYGEN;
     char *outfile = NULL, *outfiletmp = NULL;
-    Filename outfilename;
-    enum { PRIVATE, PUBLIC, PUBLICO, FP, OPENSSH, SSHCOM } outtype = PRIVATE;
-    int bits = 1024;
+    enum { PRIVATE, PUBLIC, PUBLICO, FP, OPENSSH_PEM,
+           OPENSSH_NEW, SSHCOM } outtype = PRIVATE;
+    int bits = 2048;
     char *comment = NULL, *origcomment = NULL;
     int change_passphrase = FALSE;
     int errs = FALSE, nogo = FALSE;
@@ -316,25 +327,47 @@ int main(int argc, char **argv)
                            *p++ = '\0';
                            val = p;
                        } else
-                           val = NULL;
+                            val = NULL;
+
                        if (!strcmp(opt, "-help")) {
-                           help();
-                           nogo = TRUE;
+                            if (val) {
+                                errs = TRUE;
+                                fprintf(stderr, "puttygen: option `-%s'"
+                                        " expects no argument\n", opt);
+                            } else {
+                                help();
+                                nogo = TRUE;
+                            }
                        } else if (!strcmp(opt, "-version")) {
-                           showversion();
-                           nogo = TRUE;
+                            if (val) {
+                                errs = TRUE;
+                                fprintf(stderr, "puttygen: option `-%s'"
+                                        " expects no argument\n", opt);
+                            } else {
+                                showversion();
+                                nogo = TRUE;
+                            }
                        } else if (!strcmp(opt, "-pgpfp")) {
-                            /* support "-pgpfp" for consistency with others */
-                            pgp_fingerprints();
-                            nogo = TRUE;
+                            if (val) {
+                                errs = TRUE;
+                                fprintf(stderr, "puttygen: option `-%s'"
+                                        " expects no argument\n", opt);
+                            } else {
+                                /* support --pgpfp for consistency */
+                                pgp_fingerprints();
+                                nogo = TRUE;
+                            }
                         }
                        /*
-                        * A sample option requiring an argument:
+                        * For long options requiring an argument, add
+                        * code along the lines of
                         * 
                         * else if (!strcmp(opt, "-output")) {
-                        *     if (!val)
-                        *         errs = TRUE, error(err_optnoarg, opt);
-                        *     else
+                        *     if (!val) {
+                        *         errs = TRUE;
+                         *         fprintf(stderr, "puttygen: option `-%s'"
+                         *                 " expects an argument\n", opt);
+                        *     } else
                         *         ofile = val;
                         * }
                         */
@@ -409,6 +442,8 @@ int main(int argc, char **argv)
                            keytype = RSA1, sshver = 1;
                        else if (!strcmp(p, "dsa") || !strcmp(p, "dss"))
                            keytype = DSA, sshver = 2;
+                        else if (!strcmp(p, "ecdsa"))
+                            keytype = ECDSA, sshver = 2;
                        else {
                            fprintf(stderr,
                                    "puttygen: unknown key type `%s'\n", p);
@@ -430,8 +465,11 @@ int main(int argc, char **argv)
                            outtype = PRIVATE;
                        else if (!strcmp(p, "fingerprint"))
                            outtype = FP;
-                       else if (!strcmp(p, "private-openssh"))
-                           outtype = OPENSSH, sshver = 2;
+                       else if (!strcmp(p, "private-openssh") ||
+                                 !strcmp(p, "private-openssh-pem"))
+                           outtype = OPENSSH_PEM, sshver = 2;
+                       else if (!strcmp(p, "private-openssh-new"))
+                           outtype = OPENSSH_NEW, sshver = 2;
                        else if (!strcmp(p, "private-sshcom"))
                            outtype = SSHCOM, sshver = 2;
                        else {
@@ -469,6 +507,11 @@ int main(int argc, char **argv)
        }
     }
 
+    if (keytype == ECDSA && (bits != 256 && bits != 384 && bits != 521)) {
+        fprintf(stderr, "puttygen: invalid bits for ECDSA, choose 256, 384 or 521\n");
+        errs = TRUE;
+    }
+
     if (errs)
        return 1;
 
@@ -501,7 +544,8 @@ int main(int argc, char **argv)
      * We must save the private part when generating a new key.
      */
     if (keytype != NOKEYGEN &&
-       (outtype != PRIVATE && outtype != OPENSSH && outtype != SSHCOM)) {
+       (outtype != PRIVATE && outtype != OPENSSH_PEM &&
+         outtype != OPENSSH_NEW && outtype != SSHCOM)) {
        fprintf(stderr, "puttygen: this would generate a new key but "
                "discard the private part\n");
        return 1;
@@ -514,7 +558,7 @@ int main(int argc, char **argv)
     if (infile) {
        infilename = filename_from_str(infile);
 
-       intype = key_type(&infilename);
+       intype = key_type(infilename);
 
        switch (intype) {
            /*
@@ -555,7 +599,8 @@ int main(int argc, char **argv)
            break;
 
          case SSH_KEYTYPE_SSH2:
-         case SSH_KEYTYPE_OPENSSH:
+         case SSH_KEYTYPE_OPENSSH_PEM:
+         case SSH_KEYTYPE_OPENSSH_NEW:
          case SSH_KEYTYPE_SSHCOM:
            if (sshver == 1) {
                fprintf(stderr, "puttygen: conversion from SSH-2 to SSH-1 keys"
@@ -579,7 +624,8 @@ int main(int argc, char **argv)
      */
     if ((intype == SSH_KEYTYPE_SSH1 && outtype == PRIVATE) ||
        (intype == SSH_KEYTYPE_SSH2 && outtype == PRIVATE) ||
-       (intype == SSH_KEYTYPE_OPENSSH && outtype == OPENSSH) ||
+       (intype == SSH_KEYTYPE_OPENSSH_PEM && outtype == OPENSSH_PEM) ||
+       (intype == SSH_KEYTYPE_OPENSSH_NEW && outtype == OPENSSH_NEW) ||
        (intype == SSH_KEYTYPE_SSHCOM && outtype == SSHCOM)) {
        if (!outfile) {
            outfile = infile;
@@ -597,7 +643,8 @@ int main(int argc, char **argv)
             * Bomb out rather than automatically choosing to write
             * a private key file to stdout.
             */
-           if (outtype==PRIVATE || outtype==OPENSSH || outtype==SSHCOM) {
+           if (outtype == PRIVATE || outtype == OPENSSH_PEM ||
+                outtype == OPENSSH_NEW || outtype == SSHCOM) {
                fprintf(stderr, "puttygen: need to specify an output file\n");
                return 1;
            }
@@ -610,8 +657,11 @@ int main(int argc, char **argv)
      * out a private key format, or (b) the entire input key file
      * is encrypted.
      */
-    if (outtype == PRIVATE || outtype == OPENSSH || outtype == SSHCOM ||
-       intype == SSH_KEYTYPE_OPENSSH || intype == SSH_KEYTYPE_SSHCOM)
+    if (outtype == PRIVATE || outtype == OPENSSH_PEM ||
+        outtype == OPENSSH_NEW || outtype == SSHCOM ||
+       intype == SSH_KEYTYPE_OPENSSH_PEM ||
+       intype == SSH_KEYTYPE_OPENSSH_NEW ||
+        intype == SSH_KEYTYPE_SSHCOM)
        load_encrypted = TRUE;
     else
        load_encrypted = FALSE;
@@ -635,6 +685,8 @@ int main(int argc, char **argv)
        tm = ltime();
        if (keytype == DSA)
            strftime(default_comment, 30, "dsa-key-%Y%m%d", &tm);
+        else if (keytype == ECDSA)
+            strftime(default_comment, 30, "ecdsa-key-%Y%m%d", &tm);
        else
            strftime(default_comment, 30, "rsa-key-%Y%m%d", &tm);
 
@@ -646,7 +698,7 @@ int main(int argc, char **argv)
            return 1;
        }
        random_add_heavynoise(entropy, bits / 8);
-       memset(entropy, 0, bits/8);
+       smemclr(entropy, bits/8);
        sfree(entropy);
 
        if (keytype == DSA) {
@@ -656,6 +708,19 @@ int main(int argc, char **argv)
            ssh2key->data = dsskey;
            ssh2key->alg = &ssh_dss;
            ssh1key = NULL;
+        } else if (keytype == ECDSA) {
+            struct ec_key *ec = snew(struct ec_key);
+            ec_generate(ec, bits, progressfn, &prog);
+            ssh2key = snew(struct ssh2_userkey);
+            ssh2key->data = ec;
+            if (bits == 256) {
+                ssh2key->alg = &ssh_ecdsa_nistp256;
+            } else if (bits == 384) {
+                ssh2key->alg = &ssh_ecdsa_nistp384;
+            } else {
+                ssh2key->alg = &ssh_ecdsa_nistp521;
+            }
+            ssh1key = NULL;
        } else {
            struct RSAKey *rsakey = snew(struct RSAKey);
            rsa_generate(rsakey, bits, progressfn, &prog);
@@ -685,11 +750,11 @@ int main(int argc, char **argv)
         * Find out whether the input key is encrypted.
         */
        if (intype == SSH_KEYTYPE_SSH1)
-           encrypted = rsakey_encrypted(&infilename, &origcomment);
+           encrypted = rsakey_encrypted(infilename, &origcomment);
        else if (intype == SSH_KEYTYPE_SSH2)
-           encrypted = ssh2_userkey_encrypted(&infilename, &origcomment);
+           encrypted = ssh2_userkey_encrypted(infilename, &origcomment);
        else
-           encrypted = import_encrypted(&infilename, intype, &origcomment);
+           encrypted = import_encrypted(infilename, intype, &origcomment);
 
        /*
         * If so, ask for a passphrase.
@@ -699,7 +764,7 @@ int main(int argc, char **argv)
            int ret;
            p->to_server = FALSE;
            p->name = dupstr("SSH key passphrase");
-           add_prompt(p, dupstr("Enter passphrase to load key: "), FALSE, 512);
+           add_prompt(p, dupstr("Enter passphrase to load key: "), FALSE);
            ret = console_get_userpass_input(p, NULL, 0);
            assert(ret >= 0);
            if (!ret) {
@@ -724,7 +789,7 @@ int main(int argc, char **argv)
                unsigned char *blob;
                int n, l, bloblen;
 
-               ret = rsakey_pubblob(&infilename, &vblob, &bloblen,
+               ret = rsakey_pubblob(infilename, &vblob, &bloblen,
                                     &origcomment, &error);
                blob = (unsigned char *)vblob;
 
@@ -745,8 +810,11 @@ int main(int argc, char **argv)
                }
                ssh1key->comment = dupstr(origcomment);
                ssh1key->private_exponent = NULL;
+               ssh1key->p = NULL;
+               ssh1key->q = NULL;
+               ssh1key->iqmp = NULL;
            } else {
-               ret = loadrsakey(&infilename, ssh1key, passphrase, &error);
+               ret = loadrsakey(infilename, ssh1key, passphrase, &error);
            }
            if (ret > 0)
                error = NULL;
@@ -756,15 +824,17 @@ int main(int argc, char **argv)
 
          case SSH_KEYTYPE_SSH2:
            if (!load_encrypted) {
-               ssh2blob = ssh2_userkey_loadpub(&infilename, &ssh2alg,
+               ssh2blob = ssh2_userkey_loadpub(infilename, &ssh2alg,
                                                &ssh2bloblen, NULL, &error);
-               ssh2algf = find_pubkey_alg(ssh2alg);
-               if (ssh2algf)
-                   bits = ssh2algf->pubkey_bits(ssh2blob, ssh2bloblen);
-               else
-                   bits = -1;
+                if (ssh2blob) {
+                    ssh2algf = find_pubkey_alg(ssh2alg);
+                    if (ssh2algf)
+                        bits = ssh2algf->pubkey_bits(ssh2blob, ssh2bloblen);
+                    else
+                        bits = -1;
+                }
            } else {
-               ssh2key = ssh2_load_userkey(&infilename, passphrase, &error);
+               ssh2key = ssh2_load_userkey(infilename, passphrase, &error);
            }
            if ((ssh2key && ssh2key != SSH2_WRONG_PASSPHRASE) || ssh2blob)
                error = NULL;
@@ -776,9 +846,10 @@ int main(int argc, char **argv)
            }
            break;
 
-         case SSH_KEYTYPE_OPENSSH:
+         case SSH_KEYTYPE_OPENSSH_PEM:
+         case SSH_KEYTYPE_OPENSSH_NEW:
          case SSH_KEYTYPE_SSHCOM:
-           ssh2key = import_ssh2(&infilename, intype, passphrase, &error);
+           ssh2key = import_ssh2(infilename, intype, passphrase, &error);
            if (ssh2key) {
                if (ssh2key != SSH2_WRONG_PASSPHRASE)
                    error = NULL;
@@ -824,8 +895,8 @@ int main(int argc, char **argv)
 
        p->to_server = FALSE;
        p->name = dupstr("New SSH key passphrase");
-       add_prompt(p, dupstr("Enter passphrase to save key: "), FALSE, 512);
-       add_prompt(p, dupstr("Re-enter passphrase to verify: "), FALSE, 512);
+       add_prompt(p, dupstr("Enter passphrase to save key: "), FALSE);
+       add_prompt(p, dupstr("Re-enter passphrase to verify: "), FALSE);
        ret = console_get_userpass_input(p, NULL, 0);
        assert(ret >= 0);
        if (!ret) {
@@ -839,7 +910,7 @@ int main(int argc, char **argv)
                return 1;
            }
            if (passphrase) {
-               memset(passphrase, 0, strlen(passphrase));
+               smemclr(passphrase, strlen(passphrase));
                sfree(passphrase);
            }
            passphrase = dupstr(p->prompts[0]->result);
@@ -865,19 +936,19 @@ int main(int argc, char **argv)
        outfilename = filename_from_str(outfile ? outfile : "");
 
     switch (outtype) {
-       int ret;
+       int ret, real_outtype;
 
       case PRIVATE:
        if (sshver == 1) {
            assert(ssh1key);
-           ret = saversakey(&outfilename, ssh1key, passphrase);
+           ret = saversakey(outfilename, ssh1key, passphrase);
            if (!ret) {
                fprintf(stderr, "puttygen: unable to save SSH-1 private key\n");
                return 1;
            }
        } else {
            assert(ssh2key);
-           ret = ssh2_save_userkey(&outfilename, ssh2key, passphrase);
+           ret = ssh2_save_userkey(outfilename, ssh2key, passphrase);
            if (!ret) {
                fprintf(stderr, "puttygen: unable to save SSH-2 private key\n");
                return 1;
@@ -997,11 +1068,27 @@ int main(int argc, char **argv)
        }
        break;
        
-      case OPENSSH:
+      case OPENSSH_PEM:
+      case OPENSSH_NEW:
       case SSHCOM:
        assert(sshver == 2);
        assert(ssh2key);
-       ret = export_ssh2(&outfilename, outtype, ssh2key, passphrase);
+       random_ref(); /* both foreign key types require randomness,
+                       * for IV or padding */
+        switch (outtype) {
+          case OPENSSH_PEM:
+            real_outtype = SSH_KEYTYPE_OPENSSH_PEM;
+            break;
+          case OPENSSH_NEW:
+            real_outtype = SSH_KEYTYPE_OPENSSH_NEW;
+            break;
+          case SSHCOM:
+            real_outtype = SSH_KEYTYPE_SSHCOM;
+            break;
+          default:
+            assert(0 && "control flow goof");
+        }
+       ret = export_ssh2(outfilename, real_outtype, ssh2key, passphrase);
        if (!ret) {
            fprintf(stderr, "puttygen: unable to export key\n");
            return 1;
@@ -1014,7 +1101,7 @@ int main(int argc, char **argv)
     }
 
     if (passphrase) {
-       memset(passphrase, 0, strlen(passphrase));
+       smemclr(passphrase, strlen(passphrase));
        sfree(passphrase);
     }