summaryrefslogtreecommitdiff
path: root/sbin/photurisd/handle_value_response.c
blob: cd07867fa9a17c9d82a99eb2679568dc1edfd12b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Niels Provos.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * handle_value_response:
 * receive a VALUE_RESPONSE packet; return -1 on failure, 0 on success
 *
 */

#ifndef lint
static char rcsid[] = "$Id: handle_value_response.c,v 1.1 1998/11/14 23:37:24 deraadt Exp $";
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "config.h"
#include "photuris.h"
#include "packets.h"
#include "state.h"
#include "cookie.h"
#include "buffer.h"
#include "scheme.h"
#include "packet.h"
#include "schedule.h"
#include "exchange.h"
#include "secrets.h"
#include "spi.h"
#include "errlog.h"
#ifdef DEBUG
#include "config.h"
#endif

int
handle_value_response(u_char *packet, int size, char *address, 
		      char *local_address)

{
        struct packet_sub parts[] = {
	     { "Exchange Value", FLD_VARPRE, 0, 0, },
	     { "Offered Attributes", FLD_ATTRIB, FMD_ATT_FILL, 0, },
	     { NULL }
	};
	struct packet vr_msg = {
	     "Value Response",
	     VALUE_RESPONSE_MIN, 0, parts
	};
	struct value_response *header;
	struct stateob *st;
	mpz_t test;

	if (size < VALUE_RESPONSE_MIN)
	     return -1;	/* packet too small  */

	if (packet_check(packet, size, &vr_msg) == -1) {
	     log_error(0, "bad packet structure in handle_value_response()");
	     return -1;
	}

	header = (struct value_response *) packet;

	st = state_find_cookies(address, header->icookie, header->rcookie);
	if (st == NULL)
	     return -1;     /* Silently discard */

	if (st->phase != VALUE_REQUEST)
	     return -1;     /* We don't want this packet */

	/* Now check the exchange value for defects */
	mpz_init_set_varpre(test, parts[0].where);
	if (!exchange_check_value(test, st->generator, st->modulus)) {
	     mpz_clear(test);
	     return 0;
	}
	mpz_clear(test);

	/* Reserved Field for TBV */
	bcopy(header->reserved, st->uSPITBV, 3);

	/* Fill the state object */
	st->uSPIoattrib = calloc(parts[1].size, sizeof(u_int8_t));
	if (st->uSPIoattrib == NULL) {
	     state_value_reset(st);
	     state_unlink(st);
	     return -1;
	}
	bcopy(parts[1].where, st->uSPIoattrib, parts[1].size);  
	st->uSPIoattribsize = parts[1].size;  

#ifdef DEBUG 
	{
	     int i = BUFFER_SIZE; 
	     bin2hex(buffer, &i, parts[0].where, parts[0].size);
	     printf("Got exchange value 0x%s\n", buffer); 
	}
#endif 

	/* Set exchange value */
	st->texchangesize = parts[0].size;
	st->texchange = calloc(st->texchangesize, sizeof(u_int8_t));
	if (st->texchange == NULL) {
	     log_error(1, "calloc() in handle_value_response()");
	     return -1;
	}
	bcopy(parts[0].where, st->texchange, st->texchangesize);

	/* Compute the shared secret now */
	compute_shared_secret(st, &(st->shared), &(st->sharedsize));
#ifdef DEBUG  
	{
	     int i = BUFFER_SIZE;
	     bin2hex(buffer, &i, st->shared, st->sharedsize);
	     printf("Shared secret is: 0x%s\n", buffer);  
	}
#endif  

	/* Create SPI + choice of attributes */
	if (make_spi(st, local_address, st->oSPI, &(st->olifetime),
		     &(st->oSPIattrib), &(st->oSPIattribsize)) == -1) {
	     log_error(0, "make_spi() in handle_value_response()");
	     return -1;
	}
	
	/* Initialize Privacy Keys from Exchange Values */
	init_privacy_key(st, 0);   /* User -> Owner direction */
	init_privacy_key(st, 1);   /* Owner -> User direction */

	packet_size = PACKET_BUFFER_SIZE;
	if (photuris_identity_request(st, packet_buffer, &packet_size) == -1)
	     return -1;

	packet_save(st, packet_buffer, packet_size);

	send_packet();

	st->retries = 0;
	st->phase = IDENTITY_REQUEST;

	schedule_remove(TIMEOUT, st->icookie);
	schedule_insert(TIMEOUT, retrans_timeout, st->icookie, COOKIE_SIZE);
	return 0;
}