2 * Copyright (C) 2005, 2007, 2008 Free Software Foundation
4 * This file is part of GNUTLS.
6 * GNUTLS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GNUTLS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #define _MAX(x,y) (x>y?x:y)
30 main (int argc
, char **argv
)
32 printf ("\nPSK not supported. This program is a dummy.\n\n");
37 psktool_version (void)
39 fprintf (stderr
, "GNU TLS dummy psktool.\n");
47 #include <gnutls/gnutls.h>
48 #include <gnutls/extra.h>
51 #include <gc.h> /* for randomize */
53 #include <sys/types.h>
63 static int write_key (const char *username
, const char *key
, int key_size
,
67 psktool_version (void)
69 const char *v
= gnutls_check_version (NULL
);
71 printf ("psktool (GnuTLS) %s\n", LIBGNUTLS_VERSION
);
72 if (strcmp (v
, LIBGNUTLS_VERSION
) != 0)
73 printf ("libgnutls %s\n", v
);
77 #define KPASSWD "/etc/passwd.psk"
78 #define MAX_KEY_SIZE 64
80 main (int argc
, char **argv
)
85 unsigned char key
[MAX_KEY_SIZE
];
86 char hex_key
[MAX_KEY_SIZE
* 2 + 1];
88 size_t hex_key_size
= sizeof (hex_key
);
90 if ((ret
= gnutls_global_init ()) < 0)
92 fprintf (stderr
, "global_init: %s\n", gnutls_strerror (ret
));
98 if (gaa (argc
, argv
, &info
) != -1)
100 fprintf (stderr
, "Error in the arguments.\n");
104 if (info
.passwd
== NULL
)
105 info
.passwd
= KPASSWD
;
107 if (info
.username
== NULL
)
110 pwd
= getpwuid (getuid ());
114 fprintf (stderr
, "No such user\n");
118 info
.username
= pwd
->pw_name
;
120 fprintf (stderr
, "Please specify a user\n");
125 if (info
.key_size
> MAX_KEY_SIZE
)
127 fprintf (stderr
, "Key size is too long\n");
131 if (info
.netconf_hint
)
135 if (info
.key_size
!= 0 && info
.key_size
!= 20)
137 fprintf (stderr
, "For netconf, key size must always be 20.\n");
141 passwd
= getpass ("Enter password: ");
144 fprintf (stderr
, "Please specify a password\n");
148 ret
= gnutls_psk_netconf_derive_key (passwd
,
155 if (info
.key_size
< 1)
158 printf ("Generating a random key for user '%s'\n", info
.username
);
160 ret
= gc_pseudo_random ((char *) key
, info
.key_size
);
163 fprintf (stderr
, "Not enough randomness\n");
168 dkey
.size
= info
.key_size
;
171 ret
= gnutls_hex_encode (&dkey
, hex_key
, &hex_key_size
);
172 if (info
.netconf_hint
)
173 gnutls_free (dkey
.data
);
176 fprintf (stderr
, "HEX encoding error\n");
180 ret
= write_key (info
.username
, hex_key
, hex_key_size
, info
.passwd
);
182 printf ("Key stored to %s\n", info
.passwd
);
188 filecopy (char *src
, char *dst
)
194 fd
= fopen (dst
, "w");
197 fprintf (stderr
, "Cannot open '%s' for write\n", dst
);
201 fd2
= fopen (src
, "r");
209 line
[sizeof (line
) - 1] = 0;
212 p
= fgets (line
, sizeof (line
) - 1, fd2
);
227 write_key (const char *username
, const char *key
, int key_size
,
236 /* delete previous entry */
241 if (strlen (passwd_file
) > sizeof (tmpname
) + 5)
243 fprintf (stderr
, "file '%s' is tooooo long\n", passwd_file
);
246 strcpy (tmpname
, passwd_file
);
247 strcat (tmpname
, ".tmp");
249 if (stat (tmpname
, &st
) != -1)
251 fprintf (stderr
, "file '%s' is locked\n", tmpname
);
255 if (filecopy (passwd_file
, tmpname
) != 0)
257 fprintf (stderr
, "Cannot copy '%s' to '%s'\n", passwd_file
, tmpname
);
261 fd
= fopen (passwd_file
, "w");
264 fprintf (stderr
, "Cannot open '%s' for write\n", passwd_file
);
269 fd2
= fopen (tmpname
, "r");
272 fprintf (stderr
, "Cannot open '%s' for read\n", tmpname
);
280 p
= fgets (line
, sizeof (line
) - 1, fd2
);
284 pp
= strchr (line
, ':');
290 _MAX (strlen (username
), (unsigned int) (pp
- p
))) == 0)
293 fprintf (fd
, "%s:%s\n", username
, key
);
304 fprintf (fd
, "%s:%s\n", username
, key
);
316 #endif /* ENABLE_PSK */