try to discover if there is input waiting on stdin

otherwise give the user a hint
resolves #12
This commit is contained in:
Klas Lindfors
2015-01-12 16:24:47 +01:00
parent 98320c2c0d
commit b1a673b1f9
3 changed files with 38 additions and 0 deletions
+17
View File
@@ -57,6 +57,23 @@ FILE *open_file(const char *file_name, int mode) {
return file; return file;
} }
bool input_ready(FILE *file) {
int fd = fileno(file);
fd_set rfds;
struct timeval tv;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 0;
if(select(fd + 1, &rfds, NULL, NULL, &tv) == 1) {
return true;
}
return false;
}
unsigned char get_algorithm(EVP_PKEY *key) { unsigned char get_algorithm(EVP_PKEY *key) {
int type = EVP_PKEY_type(key->type); int type = EVP_PKEY_type(key->type);
switch(type) { switch(type) {
+1
View File
@@ -45,5 +45,6 @@ unsigned char get_algorithm(EVP_PKEY*);
FILE *open_file(const char*, int); FILE *open_file(const char*, int);
int get_object_id(enum enum_slot slot); int get_object_id(enum enum_slot slot);
bool set_component_with_len(unsigned char**, const BIGNUM*, int); bool set_component_with_len(unsigned char**, const BIGNUM*, int);
bool input_ready(FILE*);
#endif #endif
+20
View File
@@ -290,6 +290,10 @@ static bool import_key(ykpiv_state *state, enum enum_key_format key_format,
return false; return false;
} }
if(input_file == stdin && !input_ready(input_file)) {
fprintf(stderr, "Please paste the private key...\n");
}
if(key_format == key_format_arg_PEM) { if(key_format == key_format_arg_PEM) {
private_key = PEM_read_PrivateKey(input_file, NULL, NULL, password); private_key = PEM_read_PrivateKey(input_file, NULL, NULL, password);
if(!private_key) { if(!private_key) {
@@ -412,6 +416,10 @@ static bool import_cert(ykpiv_state *state, enum enum_key_format cert_format,
return false; return false;
} }
if(input_file == stdin && !input_ready(input_file)) {
fprintf(stderr, "Please paste the certificate...\n");
}
if(cert_format == key_format_arg_PEM) { if(cert_format == key_format_arg_PEM) {
cert = PEM_read_X509(input_file, NULL, NULL, password); cert = PEM_read_X509(input_file, NULL, NULL, password);
if(!cert) { if(!cert) {
@@ -555,6 +563,10 @@ static bool request_certificate(ykpiv_state *state, enum enum_key_format key_for
goto request_out; goto request_out;
} }
if(input_file == stdin && !input_ready(input_file)) {
fprintf(stderr, "Please paste the public key...\n");
}
if(key_format == key_format_arg_PEM) { if(key_format == key_format_arg_PEM) {
public_key = PEM_read_PUBKEY(input_file, NULL, NULL, NULL); public_key = PEM_read_PUBKEY(input_file, NULL, NULL, NULL);
if(!public_key) { if(!public_key) {
@@ -734,6 +746,10 @@ static bool selfsign_certificate(ykpiv_state *state, enum enum_key_format key_fo
goto selfsign_out; goto selfsign_out;
} }
if(input_file == stdin && !input_ready(input_file)) {
fprintf(stderr, "Please paste the public key...\n");
}
if(key_format == key_format_arg_PEM) { if(key_format == key_format_arg_PEM) {
public_key = PEM_read_PUBKEY(input_file, NULL, NULL, NULL); public_key = PEM_read_PUBKEY(input_file, NULL, NULL, NULL);
if(!public_key) { if(!public_key) {
@@ -1007,6 +1023,10 @@ static bool sign_file(ykpiv_state *state, const char *input, const char *output,
return false; return false;
} }
if(input_file == stdin && !input_ready(input_file)) {
fprintf(stderr, "Please paste the input...\n");
}
output_file = open_file(output, OUTPUT); output_file = open_file(output, OUTPUT);
if(!output_file) { if(!output_file) {
return false; return false;