mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-10 01:48:00 +00:00
cmdgen: option to specify the random number device to use.
E.g. you might pass '--random-device=/dev/urandom'. Mostly because I got sick of waiting for /dev/random to finish blocking while I was trying to generate throwaway keys for testing bug fixes in cmdgen itself. But it might also be useful on systems that call their random device by a different name that we haven't encountered. (Since cmdgen also reads the saved PuTTY random seed file, setting this option to /dev/zero will not render key generation deterministic. It's tempting to provide _some_ way to do that, for testing purposes and clearly marked as dangerous of course, but I think it would take more faff than this.)
This commit is contained in:
parent
ad87950539
commit
435b29da88
17
cmdgen.c
17
cmdgen.c
@ -37,7 +37,7 @@
|
|||||||
* run tests.
|
* run tests.
|
||||||
*/
|
*/
|
||||||
#define get_random_data get_random_data_diagnostic
|
#define get_random_data get_random_data_diagnostic
|
||||||
char *get_random_data(int len)
|
char *get_random_data(int len, const char *device)
|
||||||
{
|
{
|
||||||
char *buf = snewn(len, char);
|
char *buf = snewn(len, char);
|
||||||
memset(buf, 'x', len);
|
memset(buf, 'x', len);
|
||||||
@ -177,6 +177,8 @@ void help(void)
|
|||||||
" specify file containing old key passphrase\n"
|
" specify file containing old key passphrase\n"
|
||||||
" --new-passphrase file\n"
|
" --new-passphrase file\n"
|
||||||
" specify file containing new key passphrase\n"
|
" specify file containing new key passphrase\n"
|
||||||
|
" --random-device device\n"
|
||||||
|
" specify device to read entropy from (e.g. /dev/urandom)\n"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,6 +247,7 @@ int main(int argc, char **argv)
|
|||||||
char *old_passphrase = NULL, *new_passphrase = NULL;
|
char *old_passphrase = NULL, *new_passphrase = NULL;
|
||||||
int load_encrypted;
|
int load_encrypted;
|
||||||
progfn_t progressfn = is_interactive() ? progress_update : no_progress;
|
progfn_t progressfn = is_interactive() ? progress_update : no_progress;
|
||||||
|
const char *random_device = NULL;
|
||||||
|
|
||||||
/* ------------------------------------------------------------------
|
/* ------------------------------------------------------------------
|
||||||
* Parse the command line to figure out what we've been asked to do.
|
* Parse the command line to figure out what we've been asked to do.
|
||||||
@ -338,6 +341,16 @@ int main(int argc, char **argv)
|
|||||||
if (!new_passphrase)
|
if (!new_passphrase)
|
||||||
errs = TRUE;
|
errs = TRUE;
|
||||||
}
|
}
|
||||||
|
} else if (!strcmp(opt, "-random-device")) {
|
||||||
|
if (!val && argc > 1)
|
||||||
|
--argc, val = *++argv;
|
||||||
|
if (!val) {
|
||||||
|
errs = TRUE;
|
||||||
|
fprintf(stderr, "puttygen: option `-%s'"
|
||||||
|
" expects an argument\n", opt);
|
||||||
|
} else {
|
||||||
|
random_device = val;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
errs = TRUE;
|
errs = TRUE;
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
@ -677,7 +690,7 @@ int main(int argc, char **argv)
|
|||||||
strftime(default_comment, 30, "rsa-key-%Y%m%d", &tm);
|
strftime(default_comment, 30, "rsa-key-%Y%m%d", &tm);
|
||||||
|
|
||||||
random_ref();
|
random_ref();
|
||||||
entropy = get_random_data(bits / 8);
|
entropy = get_random_data(bits / 8, random_device);
|
||||||
if (!entropy) {
|
if (!entropy) {
|
||||||
fprintf(stderr, "puttygen: failed to collect entropy, "
|
fprintf(stderr, "puttygen: failed to collect entropy, "
|
||||||
"could not generate key\n");
|
"could not generate key\n");
|
||||||
|
2
putty.h
2
putty.h
@ -1352,7 +1352,7 @@ void filename_free(Filename *fn);
|
|||||||
int filename_serialise(const Filename *f, void *data);
|
int filename_serialise(const Filename *f, void *data);
|
||||||
Filename *filename_deserialise(void *data, int maxsize, int *used);
|
Filename *filename_deserialise(void *data, int maxsize, int *used);
|
||||||
char *get_username(void); /* return value needs freeing */
|
char *get_username(void); /* return value needs freeing */
|
||||||
char *get_random_data(int bytes); /* used in cmdgen.c */
|
char *get_random_data(int bytes, const char *device); /* used in cmdgen.c */
|
||||||
char filename_char_sanitise(char c); /* rewrite special pathname chars */
|
char filename_char_sanitise(char c); /* rewrite special pathname chars */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
15
unix/uxgen.c
15
unix/uxgen.c
@ -3,21 +3,27 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "putty.h"
|
#include "putty.h"
|
||||||
|
|
||||||
char *get_random_data(int len)
|
char *get_random_data(int len, const char *device)
|
||||||
{
|
{
|
||||||
char *buf = snewn(len, char);
|
char *buf = snewn(len, char);
|
||||||
int fd;
|
int fd;
|
||||||
int ngot, ret;
|
int ngot, ret;
|
||||||
|
|
||||||
fd = open("/dev/random", O_RDONLY);
|
if (!device)
|
||||||
|
device = "/dev/random";
|
||||||
|
|
||||||
|
fd = open(device, O_RDONLY);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
sfree(buf);
|
sfree(buf);
|
||||||
perror("puttygen: unable to open /dev/random");
|
fprintf(stderr, "puttygen: %s: open: %s\n",
|
||||||
|
device, strerror(errno));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,7 +33,8 @@ char *get_random_data(int len)
|
|||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
close(fd);
|
close(fd);
|
||||||
sfree(buf);
|
sfree(buf);
|
||||||
perror("puttygen: unable to read /dev/random");
|
fprintf(stderr, "puttygen: %s: read: %s\n",
|
||||||
|
device, strerror(errno));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
ngot += ret;
|
ngot += ret;
|
||||||
|
Loading…
Reference in New Issue
Block a user