1 /*-
2  * Copyright (c) 2010 Alistair Crooks <agc@NetBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include <sys/types.h>
26 
27 #include <netdb.h>
28 
29 #include <netpgp.h>
30 #include <regex.h>
31 #include <sha1.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <time.h>
36 #include <unistd.h>
37 
38 #include "libpaa.h"
39 
40 #define DEFAULT_HASH_ALG "SHA256"
41 
42 int
43 main(int argc, char **argv)
44 {
45 	paa_server_info_t	server;
46 	paa_challenge_t		challenge;
47 	paa_identity_t		id;
48 	netpgp_t		netpgp;
49 	char			buf[2048];
50 	int			secretc;
51 	int			cc;
52 	int			i;
53 
54 	(void) memset(&server, 0x0, sizeof(server));
55 	(void) memset(&challenge, 0x0, sizeof(challenge));
56 	(void) memset(&id, 0x0, sizeof(id));
57 	(void) memset(&netpgp, 0x0, sizeof(netpgp));
58 	secretc = 64;
59 	while ((i = getopt(argc, argv, "S:c:d:r:u:")) != -1) {
60 		switch(i) {
61 		case 'S':
62 			netpgp_setvar(&netpgp, "ssh keys", "1");
63 			netpgp_setvar(&netpgp, "sshkeyfile", optarg);
64 			break;
65 		case 'c':
66 			secretc = atoi(optarg);
67 			break;
68 		case 'd':
69 			challenge.domain = optarg;
70 			break;
71 		case 'r':
72 			challenge.realm = optarg;
73 			break;
74 		case 'u':
75 			netpgp_setvar(&netpgp, "userid", optarg);
76 			break;
77 		}
78 	}
79 	netpgp_setvar(&netpgp, "hash", DEFAULT_HASH_ALG);
80 	netpgp_setvar(&netpgp, "need seckey", "1");
81 	netpgp_setvar(&netpgp, "need userid", "1");
82 	netpgp_set_homedir(&netpgp, getenv("HOME"),
83 			netpgp_getvar(&netpgp, "ssh keys") ? "/.ssh" : "/.gnupg", 1);
84 	if (!netpgp_init(&netpgp)) {
85 		(void) fprintf(stderr, "can't initialise netpgp\n");
86 		exit(EXIT_FAILURE);
87 	}
88 	if (!paa_server_init(&server, secretc)) {
89 		(void) fprintf(stderr, "can't initialise paa server\n");
90 		exit(EXIT_FAILURE);
91 	}
92 	/* format the challenge */
93 	cc = paa_format_challenge(&challenge, &server, buf, sizeof(buf));
94 	/* write challenge to temp file */
95 	paa_write_file("challenge", buf, cc);
96 	/* get the client to authenticate via paa, writing to temp response file */
97 	system("client/paaclient -r authentication@bigco.com < challenge > response");
98 	/* read in response */
99 	cc = paa_read_file("response", buf, sizeof(buf));
100 	if (!paa_check_response(&challenge, &id, &netpgp, buf)) {
101 		(void) fprintf(stderr, "server: paa_check failed\n");
102 		exit(EXIT_FAILURE);
103 	}
104 	printf("paa_check_response verified challenge: signature authenticated:\n");
105 	paa_print_identity(stdout, &id);
106 	printf("Changing buf[%d] from '%c' to '%c'\n", cc / 2, buf[cc / 2], buf[cc / 2] - 1);
107 	buf[cc / 2] = buf[cc / 2] - 1;
108 	if (paa_check_response(&challenge, &id, &netpgp, buf)) {
109 		(void) fprintf(stderr, "server: unexpected paa_check pass\n");
110 		exit(EXIT_FAILURE);
111 	}
112 	printf("paa_check_response verified challenge: signature not authenticated:\n");
113 	exit(EXIT_SUCCESS);
114 }
115