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