xref: /openbsd/regress/lib/libradius/test24.c (revision 5af055cd)
1 #include "incs.h"
2 
3 #include <openssl/hmac.h>
4 
5 /*
6  * User-Password attribute
7  */
8 
9 void test24(void)
10 {
11 	uint8_t cipher[256],cipher1[256];
12 	size_t clen;
13 	char plain[256];
14 	RADIUS_PACKET *packet;
15 
16 	uint8_t ra[16] = {
17 		0xf3, 0xa4, 0x7a, 0x1f, 0x6a, 0x6d, 0x76, 0x71, 0x0b, 0x94, 0x7a, 0xb9, 0x30, 0x41, 0xa0, 0x39,
18 	};
19 
20 	uint8_t encryptedpass[16] = {
21 		0x33, 0x65, 0x75, 0x73, 0x77, 0x82, 0x89, 0xb5, 0x70, 0x88, 0x5e, 0x15, 0x08, 0x48, 0x25, 0xc5,
22 	};
23 
24 	clen = sizeof(cipher);
25 	CHECK(radius_encrypt_user_password_attr(cipher, &clen, "challenge", ra, "xyzzy5461") == 0);
26 	CHECK(clen == 16);
27 	CHECK(memcmp(cipher, encryptedpass, 16) == 0);
28 
29 	CHECK(radius_decrypt_user_password_attr(plain, sizeof(plain), cipher, clen, ra, "xyzzy5461") == 0);
30 	CHECK(strcmp(plain, "challenge") == 0);
31 
32 	clen = 15;
33 	CHECK(radius_encrypt_user_password_attr(cipher, &clen, "challenge", ra, "xyzzy5461") != 0);
34 	CHECK(radius_decrypt_user_password_attr(plain, 16, cipher, 16, ra, "xyzzy5461") != 0);
35 	CHECK(radius_decrypt_user_password_attr(plain, 256, cipher, 17, ra, "xyzzy5461") != 0);
36 
37 	packet = radius_new_request_packet(RADIUS_CODE_ACCESS_REQUEST);
38 
39 	CHECK(radius_put_user_password_attr(packet, "foobarbaz", "sharedsecret") == 0);
40 	clen = sizeof(cipher1);
41 	CHECK(radius_get_raw_attr(packet, RADIUS_TYPE_USER_PASSWORD, cipher1, &clen) == 0);
42 	CHECK(clen == 16);
43 	radius_encrypt_user_password_attr(cipher, &clen, "foobarbaz", radius_get_authenticator_retval(packet), "sharedsecret");
44 	CHECK(memcmp(cipher1, cipher, 16) == 0);
45 
46 	CHECK(radius_get_user_password_attr(packet, plain, sizeof(plain), "sharedsecret") == 0);
47 	CHECK(strcmp(plain, "foobarbaz") == 0);
48 }
49 
50 ADD_TEST(test24)
51