]> asedeno.scripts.mit.edu Git - PuTTY.git/blobdiff - cmdgen.c
Update docs for Ed25519 and ChaCha20-Poly1305.
[PuTTY.git] / cmdgen.c
index adfe027f5e727136bc52d9c3239ec307f8dba3c0..12e1ac9e19e31822e5d16f8095e982c21f37990b 100644 (file)
--- a/cmdgen.c
+++ b/cmdgen.c
@@ -91,7 +91,7 @@ static void no_progress(void *param, int action, int phase, int iprogress)
 {
 }
 
-void modalfatalbox(char *p, ...)
+void modalfatalbox(const char *p, ...)
 {
     va_list ap;
     fprintf(stderr, "FATAL ERROR: ");
@@ -102,7 +102,7 @@ void modalfatalbox(char *p, ...)
     cleanup_exit(1);
 }
 
-void nonfatal(char *p, ...)
+void nonfatal(const char *p, ...)
 {
     va_list ap;
     fprintf(stderr, "ERROR: ");
@@ -152,7 +152,8 @@ void help(void)
     showversion();
     usage(FALSE);
     fprintf(stderr,
-           "  -t    specify key type when generating (rsa, dsa, rsa1)\n"
+           "  -t    specify key type when generating (ed25519, ecdsa, rsa, "
+                                                       "dsa, rsa1)\n"
            "  -b    specify number of bits when generating key\n"
            "  -C    change or specify key comment\n"
            "  -P    change key passphrase\n"
@@ -173,56 +174,6 @@ void help(void)
            );
 }
 
-static int save_ssh2_pubkey(char *filename, char *comment,
-                           void *v_pub_blob, int pub_len)
-{
-    unsigned char *pub_blob = (unsigned char *)v_pub_blob;
-    char *p;
-    int i, column;
-    FILE *fp;
-
-    if (filename) {
-       fp = fopen(filename, "wb");
-       if (!fp)
-           return 0;
-    } else
-       fp = stdout;
-
-    fprintf(fp, "---- BEGIN SSH2 PUBLIC KEY ----\n");
-
-    if (comment) {
-       fprintf(fp, "Comment: \"");
-       for (p = 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");
-    if (filename)
-       fclose(fp);
-    return 1;
-}
-
 static int move(char *from, char *to)
 {
     int ret;
@@ -242,27 +193,6 @@ static int move(char *from, char *to)
     return TRUE;
 }
 
-static char *blobfp(char *alg, int bits, unsigned char *blob, int bloblen)
-{
-    char buffer[128];
-    unsigned char digest[16];
-    struct MD5Context md5c;
-    int i;
-
-    MD5Init(&md5c);
-    MD5Update(&md5c, blob, bloblen);
-    MD5Final(digest, &md5c);
-
-    sprintf(buffer, "%s ", alg);
-    if (bits > 0)
-       sprintf(buffer + strlen(buffer), "%d ", bits);
-    for (i = 0; i < 16; i++)
-       sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
-               digest[i]);
-
-    return dupstr(buffer);
-}
-
 int main(int argc, char **argv)
 {
     char *infile = NULL;
@@ -730,13 +660,7 @@ int main(int argc, char **argv)
             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;
-            }
+            ssh2key->alg = ec->signalg;
             ssh1key = NULL;
         } else if (keytype == ED25519) {
             struct ec_key *ec = snew(struct ec_key);
@@ -857,10 +781,12 @@ int main(int argc, char **argv)
                 if (ssh2blob) {
                     ssh2algf = find_pubkey_alg(ssh2alg);
                     if (ssh2algf)
-                        bits = ssh2algf->pubkey_bits(ssh2blob, ssh2bloblen);
+                        bits = ssh2algf->pubkey_bits(ssh2algf,
+                                                     ssh2blob, ssh2bloblen);
                     else
                         bits = -1;
                 }
+                sfree(ssh2alg);
            } else {
                ssh2key = ssh2_load_userkey(infilename, passphrase, &error);
            }
@@ -990,80 +916,33 @@ int main(int argc, char **argv)
 
       case PUBLIC:
       case PUBLICO:
-       if (sshver == 1) {
-           FILE *fp;
-           char *dec1, *dec2;
+        {
+            FILE *fp;
 
-           assert(ssh1key);
+            if (outfile)
+                fp = f_open(outfilename, "w", FALSE);
+            else
+                fp = stdout;
 
-           if (outfile)
-               fp = f_open(outfilename, "w", FALSE);
-           else
-               fp = stdout;
-           dec1 = bignum_decimal(ssh1key->exponent);
-           dec2 = bignum_decimal(ssh1key->modulus);
-           fprintf(fp, "%d %s %s %s\n", bignum_bitcount(ssh1key->modulus),
-                   dec1, dec2, ssh1key->comment);
-           sfree(dec1);
-           sfree(dec2);
-           if (outfile)
-               fclose(fp);
-       } else if (outtype == PUBLIC) {
-           if (!ssh2blob) {
-               assert(ssh2key);
-               ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
-                                                    &ssh2bloblen);
-           }
-           save_ssh2_pubkey(outfile, ssh2key ? ssh2key->comment : origcomment,
-                            ssh2blob, ssh2bloblen);
-       } else if (outtype == PUBLICO) {
-           char *buffer, *p;
-           int i;
-           FILE *fp;
+            if (sshver == 1) {
+                ssh1_write_pubkey(fp, ssh1key);
+            } else {
+                if (!ssh2blob) {
+                    assert(ssh2key);
+                    ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
+                                                         &ssh2bloblen);
+                }
 
-           if (!ssh2blob) {
-               assert(ssh2key);
-               ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
-                                                    &ssh2bloblen);
-           }
-           if (!ssh2alg) {
-               assert(ssh2key);
-               ssh2alg = ssh2key->alg->name;
-           }
-           if (ssh2key)
-               comment = ssh2key->comment;
-           else
-               comment = origcomment;
-
-           buffer = snewn(strlen(ssh2alg) +
-                          4 * ((ssh2bloblen+2) / 3) +
-                          strlen(comment) + 3, char);
-           strcpy(buffer, ssh2alg);
-           p = buffer + strlen(buffer);
-           *p++ = ' ';
-           i = 0;
-           while (i < ssh2bloblen) {
-               int n = (ssh2bloblen - i < 3 ? ssh2bloblen - i : 3);
-               base64_encode_atom(ssh2blob + i, n, p);
-               i += n;
-               p += 4;
-           }
-           if (*comment) {
-               *p++ = ' ';
-               strcpy(p, comment);
-           } else
-               *p++ = '\0';
+                ssh2_write_pubkey(fp, ssh2key ? ssh2key->comment : origcomment,
+                                  ssh2blob, ssh2bloblen,
+                                  (outtype == PUBLIC ?
+                                   SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 :
+                                   SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH));
+            }
 
-           if (outfile)
-               fp = f_open(outfilename, "w", FALSE);
-           else
-               fp = stdout;
-           fprintf(fp, "%s\n", buffer);
            if (outfile)
                fclose(fp);
-
-           sfree(buffer);
-       }
+        }
        break;
 
       case FP:
@@ -1077,10 +956,11 @@ int main(int argc, char **argv)
                rsa_fingerprint(fingerprint, 128, ssh1key);
            } else {
                if (ssh2key) {
-                   fingerprint = ssh2key->alg->fingerprint(ssh2key->data);
+                   fingerprint = ssh2_fingerprint(ssh2key->alg,
+                                                   ssh2key->data);
                } else {
                    assert(ssh2blob);
-                   fingerprint = blobfp(ssh2alg, bits, ssh2blob, ssh2bloblen);
+                   fingerprint = ssh2_fingerprint_blob(ssh2blob, ssh2bloblen);
                }
            }