2008-02-28 02:34:43 +01:00
|
|
|
/*
|
|
|
|
* WPA Supplicant - ASCII passphrase to WPA PSK tool
|
|
|
|
* Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
|
|
|
|
*
|
2012-02-11 15:46:35 +01:00
|
|
|
* This software may be distributed under the terms of the BSD license.
|
|
|
|
* See README for more details.
|
2008-02-28 02:34:43 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
2022-11-21 17:30:27 +01:00
|
|
|
#include <termios.h>
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
#include "common.h"
|
2009-11-29 22:04:43 +01:00
|
|
|
#include "crypto/sha1.h"
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2022-11-21 17:30:27 +01:00
|
|
|
struct termios term;
|
2008-02-28 02:34:43 +01:00
|
|
|
unsigned char psk[32];
|
|
|
|
int i;
|
|
|
|
char *ssid, *passphrase, buf[64], *pos;
|
2016-12-05 14:36:56 +01:00
|
|
|
size_t len;
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
printf("usage: wpa_passphrase <ssid> [passphrase]\n"
|
|
|
|
"\nIf passphrase is left out, it will be read from "
|
|
|
|
"stdin\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssid = argv[1];
|
|
|
|
|
|
|
|
if (argc > 2) {
|
|
|
|
passphrase = argv[2];
|
|
|
|
} else {
|
2022-11-21 17:30:27 +01:00
|
|
|
bool ctrl_echo;
|
|
|
|
|
2019-12-23 16:20:20 +01:00
|
|
|
fprintf(stderr, "# reading passphrase from stdin\n");
|
2022-11-21 17:30:27 +01:00
|
|
|
if (tcgetattr(STDIN_FILENO, &term) < 0) {
|
|
|
|
perror("tcgetattr");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
ctrl_echo = term.c_lflag & ECHO;
|
|
|
|
term.c_lflag &= ~ECHO;
|
|
|
|
if (ctrl_echo && tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0) {
|
|
|
|
perror("tcsetattr:error disabling echo");
|
|
|
|
return 1;
|
|
|
|
}
|
2008-02-28 02:34:43 +01:00
|
|
|
if (fgets(buf, sizeof(buf), stdin) == NULL) {
|
2019-12-23 16:20:20 +01:00
|
|
|
fprintf(stderr, "Failed to read passphrase\n");
|
2008-02-28 02:34:43 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2022-11-21 17:30:27 +01:00
|
|
|
term.c_lflag |= ECHO;
|
|
|
|
if (ctrl_echo && tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0) {
|
|
|
|
perror("tcsetattr:error enabling echo");
|
|
|
|
return 1;
|
|
|
|
}
|
2008-02-28 02:34:43 +01:00
|
|
|
buf[sizeof(buf) - 1] = '\0';
|
|
|
|
pos = buf;
|
|
|
|
while (*pos != '\0') {
|
|
|
|
if (*pos == '\r' || *pos == '\n') {
|
|
|
|
*pos = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
passphrase = buf;
|
|
|
|
}
|
|
|
|
|
2016-12-05 14:36:56 +01:00
|
|
|
len = os_strlen(passphrase);
|
|
|
|
if (len < 8 || len > 63) {
|
2019-12-23 16:20:20 +01:00
|
|
|
fprintf(stderr, "Passphrase must be 8..63 characters\n");
|
2008-02-28 02:34:43 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2016-12-05 14:36:56 +01:00
|
|
|
if (has_ctrl_char((u8 *) passphrase, len)) {
|
2019-12-23 16:20:20 +01:00
|
|
|
fprintf(stderr, "Invalid passphrase character\n");
|
2016-12-05 14:36:56 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2008-02-28 02:34:43 +01:00
|
|
|
|
2022-04-29 16:11:54 +02:00
|
|
|
if (pbkdf2_sha1(passphrase, (u8 *) ssid, os_strlen(ssid), 4096, psk, 32)
|
|
|
|
!= 0) {
|
|
|
|
fprintf(stderr, "Error in pbkdf2_sha1()\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
printf("network={\n");
|
|
|
|
printf("\tssid=\"%s\"\n", ssid);
|
|
|
|
printf("\t#psk=\"%s\"\n", passphrase);
|
|
|
|
printf("\tpsk=");
|
|
|
|
for (i = 0; i < 32; i++)
|
|
|
|
printf("%02x", psk[i]);
|
|
|
|
printf("\n");
|
|
|
|
printf("}\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|