summaryrefslogtreecommitdiff
path: root/test/rma_auth.c
blob: 129cc703839c7e285a320c7be7eed66549e01f03 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* Copyright 2017 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Test RMA auth challenge/response
 */

#include <endian.h>
#include <stdio.h>
#include "common.h"
#include "chip/g/board_id.h"
#include "curve25519.h"
#include "base32.h"
#include "sha256.h"
#include "rma_auth.h"
#include "test_util.h"
#include "timer.h"
#include "util.h"

/* Placeholder implementations for testing */
static uint8_t placeholder_board_id[4] = {'Z', 'Z', 'C', 'R'};
static uint8_t placeholder_device_id[8] = {'T', 'H', 'X', 1, 1, 3, 8, 0xfe};
static int server_protocol_version = RMA_CHALLENGE_VERSION;
static uint8_t server_private_key[32] = RMA_TEST_SERVER_PRIVATE_KEY;
static int server_key_id = RMA_TEST_SERVER_KEY_ID;

void rand_bytes(void *buffer, size_t len)
{
	FILE *f = fopen("/dev/urandom", "rb");

	assert(f);
	assert(fread(buffer, len, 1, f) == 1);
	fclose(f);
}

int read_board_id(struct board_id *id)
{
	memcpy(&id->type, placeholder_board_id, sizeof(id->type));
	id->type_inv = ~id->type;
	id->flags = 0xFF00;
	return EC_SUCCESS;
}

int system_get_chip_unique_id(uint8_t **id)
{
	*id = placeholder_device_id;
	return sizeof(placeholder_device_id);
}

/**
 * Simulate the server side of a RMA challenge-response.
 *
 * @param out_auth_code		Buffer for generated authorization code
 *				(must be >= CR50_AUTH_CODE_CHARS + 1 chars)
 * @param challenge		Challenge from device
 * @return 0 if success, non-zero if error.
 */
int rma_server_side(char *out_auth_code, const char *challenge)
{
	int version, key_id;
	uint32_t device_id[2];
	uint8_t secret[32];
	uint8_t hmac[32];
	struct rma_challenge c;
	uint8_t *cptr = (uint8_t *)&c;

	/* Convert the challenge back into binary */
	if (base32_decode(cptr, 8 * sizeof(c), challenge, 9) != 8 * sizeof(c)) {
		printf("Error decoding challenge\n");
		return -1;
	}

	version = RMA_CHALLENGE_GET_VERSION(c.version_key_id);
	if (version != server_protocol_version) {
		printf("Unsupported challenge version %d\n", version);
		return -1;
	}

	key_id = RMA_CHALLENGE_GET_KEY_ID(c.version_key_id);

	printf("\nChallenge: %s\n", challenge);
	printf("  Version:      %d\n", version);
	printf("  Server KeyID: %d\n", key_id);
	printf("  BoardID:      %c%c%c%c\n",
	       isprint(c.board_id[0]) ? c.board_id[0] : '?',
	       isprint(c.board_id[1]) ? c.board_id[1] : '?',
	       isprint(c.board_id[2]) ? c.board_id[2] : '?',
	       isprint(c.board_id[3]) ? c.board_id[3] : '?');

	memcpy(device_id, c.device_id, sizeof(device_id));
	printf("  DeviceID:     0x%08x 0x%08x\n", device_id[0], device_id[1]);

	if (key_id != server_key_id) {
		printf("Unsupported KeyID %d\n", key_id);
		return -1;
	}

	/*
	 * Make sure the current user is authorized to reset this board.
	 *
	 * Since this is just a test, here we'll just make sure the BoardID
	 * and DeviceID match what we expected.
	 */
	if (memcmp(c.board_id, placeholder_board_id, sizeof(c.board_id))) {
		printf("BoardID mismatch\n");
		return -1;
	}
	if (memcmp(c.device_id, placeholder_device_id, sizeof(c.device_id))) {
		printf("DeviceID mismatch\n");
		return -1;
	}

	/* Calculate the shared secret */
	X25519(secret, server_private_key, c.device_pub_key);

	/*
	 * Auth code is a truncated HMAC of the ephemeral public key, BoardID,
	 * and DeviceID.
	 */
	hmac_SHA256(hmac, secret, sizeof(secret), cptr + 1, sizeof(c) - 1);
	if (base32_encode(out_auth_code, RMA_AUTHCODE_BUF_SIZE,
			  hmac, RMA_AUTHCODE_CHARS * 5, 0)) {
		printf("Error encoding auth code\n");
		return -1;
	}
	printf("Authcode: %s\n", out_auth_code);

	return 0;
};

#define FORCE_TIME(t) { ts.val = (t); force_time(ts); }

/*
 * rma_try_authcode expects a buffer that is at least RMA_AUTHCODE_CHARS long,
 * so copy the input string to a buffer before calling the function.
 */
static int rma_try_authcode_pad(const char *code)
{
	char authcode[RMA_AUTHCODE_BUF_SIZE];

	memset(authcode, 0, sizeof(authcode));
	strncpy(authcode, code, sizeof(authcode));

	return rma_try_authcode(authcode);
}

static int test_rma_auth(void)
{
	const char *challenge;
	char authcode[RMA_AUTHCODE_BUF_SIZE];
	timestamp_t ts;

	/* Test rate limiting */
	FORCE_TIME(9 * SECOND);
	TEST_ASSERT(rma_create_challenge() == EC_ERROR_TIMEOUT);
	TEST_ASSERT(rma_try_authcode_pad("Bad") == EC_ERROR_ACCESS_DENIED);
	TEST_ASSERT(strlen(rma_get_challenge()) == 0);

	FORCE_TIME(10 * SECOND);
	TEST_ASSERT(rma_create_challenge() == 0);
	TEST_ASSERT(strlen(rma_get_challenge()) == RMA_CHALLENGE_CHARS);

	/* Test using up tries */
	TEST_ASSERT(rma_try_authcode_pad("Bad") == EC_ERROR_INVAL);
	TEST_ASSERT(strlen(rma_get_challenge()) == RMA_CHALLENGE_CHARS);
	TEST_ASSERT(rma_try_authcode_pad("BadCodeZ") == EC_ERROR_INVAL);
	TEST_ASSERT(strlen(rma_get_challenge()) == RMA_CHALLENGE_CHARS);
	TEST_ASSERT(rma_try_authcode_pad("BadLongCode") == EC_ERROR_INVAL);
	/* Out of tries now */
	TEST_ASSERT(strlen(rma_get_challenge()) == 0);
	TEST_ASSERT(rma_try_authcode_pad("Bad") == EC_ERROR_ACCESS_DENIED);

	FORCE_TIME(19 * SECOND);
	TEST_ASSERT(rma_create_challenge() == EC_ERROR_TIMEOUT);
	TEST_ASSERT(strlen(rma_get_challenge()) == 0);

	FORCE_TIME(21 * SECOND);
	TEST_ASSERT(rma_create_challenge() == 0);
	challenge = rma_get_challenge();
	TEST_ASSERT(strlen(challenge) == RMA_CHALLENGE_CHARS);
	TEST_ASSERT(rma_server_side(authcode, challenge) == 0);
	TEST_ASSERT(rma_try_authcode(authcode) == EC_SUCCESS);

	/*
	 * Make sure the server-side checks for fields work.  That is, test
	 * our ability to test those fields...
	 */
	server_protocol_version++;
	TEST_ASSERT(rma_server_side(authcode, challenge) == -1);
	server_protocol_version--;

	server_key_id++;
	TEST_ASSERT(rma_server_side(authcode, challenge) == -1);
	server_key_id--;

	placeholder_board_id[0]++;
	TEST_ASSERT(rma_server_side(authcode, challenge) == -1);
	placeholder_board_id[0]--;

	placeholder_device_id[0]++;
	TEST_ASSERT(rma_server_side(authcode, challenge) == -1);
	placeholder_device_id[0]--;

	return EC_SUCCESS;
}

void run_test(void)
{
	test_reset();

	RUN_TEST(test_rma_auth);

	test_print_result();
}