GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libradius/radius_mppe.c Lines: 73 80 91.3 %
Date: 2017-11-13 Branches: 25 32 78.1 %

Line Branch Exec Source
1
/*	$OpenBSD: radius_mppe.c,v 1.1 2015/07/20 23:52:29 yasuoka Exp $ */
2
3
/*-
4
 * Copyright (c) 2013 Internet Initiative Japan Inc.
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE"AUTHOR" AND CONTRIBUTORS AS IS'' AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 */
28
29
#include <sys/types.h>
30
#include <sys/socket.h>
31
#include <netinet/in.h>
32
33
#include <stdbool.h>
34
#include <stdio.h>
35
#include <stdlib.h>
36
#include <string.h>
37
38
#include <openssl/md5.h>
39
40
#include "radius.h"
41
42
#include "radius_local.h"
43
44
int
45
radius_encrypt_mppe_key_attr(void *cipher, size_t * clen, const void *plain,
46
    size_t plen, const void *ra, const char *secret)
47
{
48
24
	size_t		 slen = strlen(secret);
49
12
	uint8_t		 b[16], plain0[256], *p, *c;
50
	size_t		 off;
51
12
	MD5_CTX		 ctx;
52
	unsigned int	 i;
53
	uint16_t	 salt;
54
55
12
	if (plen > 239)
56
		return (-1);
57
12
	if (*clen < ROUNDUP(plen + 1, 16) + 2)
58
3
		return (-1);
59
60
9
	salt = arc4random();
61
9
	plain0[0] = plen;
62
9
	memcpy(plain0 + 1, plain, plen);
63
9
	memset(plain0 + 1 + plen, 0, ROUNDUP(plen + 1, 16) - (plen + 1));
64
65
9
	*clen = ROUNDUP(plen + 1, 16) + 2;
66
9
	memcpy(cipher, &salt, 2);
67
54
	for (off = 0; off < *clen - 2; off += 16) {
68
18
		c = ((uint8_t *)cipher) + 2 + off;
69
18
		p = ((uint8_t *)plain0) + off;
70
18
		MD5_Init(&ctx);
71
18
		MD5_Update(&ctx, secret, slen);
72
18
		if (off == 0) {
73
9
			MD5_Update(&ctx, ra, 16);
74
9
			MD5_Update(&ctx, cipher, 2);
75
9
		} else
76
9
			MD5_Update(&ctx, c - 16, 16);
77
18
		MD5_Final(b, &ctx);
78
612
		for (i = 0; i < 16; i++)
79
288
			c[i] = p[i] ^ b[i];
80
	}
81
82
9
	return (0);
83
12
}
84
85
int
86
radius_decrypt_mppe_key_attr(void *plain, size_t * plen, const void *cipher,
87
    size_t clen, const void *ra, const char *secret)
88
{
89
48
	size_t		 slen = strlen(secret);
90
24
	uint8_t		 b[16], plain0[256], *p, *c;
91
	size_t		 off;
92
24
	MD5_CTX		 ctx;
93
	unsigned int	 i;
94
95
24
	if (clen < 18 || clen > 255)
96
		return (-1);
97
24
	if ((clen - 2) % 16 != 0)
98
3
		return (-1);
99
21
	if (*plen < clen - 3)
100
3
		return (-1);
101
102
108
	for (off = 0; off < clen - 2; off += 16) {
103
36
		c = ((uint8_t *)cipher) + 2 + off;
104
36
		p = ((uint8_t *)plain0) + off;
105
36
		MD5_Init(&ctx);
106
36
		MD5_Update(&ctx, secret, slen);
107
36
		if (off == 0) {
108
18
			MD5_Update(&ctx, ra, 16);
109
18
			MD5_Update(&ctx, cipher, 2);
110
18
		} else
111
18
			MD5_Update(&ctx, c - 16, 16);
112
36
		MD5_Final(b, &ctx);
113
1224
		for (i = 0; i < 16; i++)
114
576
			p[i] = c[i] ^ b[i];
115
	}
116
117
18
	if (plain0[0] > clen - 3)
118
		return (-1);
119
18
	*plen = plain0[0];
120
18
	memcpy(plain, plain0 + 1, *plen);
121
122
18
	return (0);
123
24
}
124
125
static int
126
radius_get_mppe_key_attr(const RADIUS_PACKET * packet, uint8_t vtype,
127
    void *buf, size_t * len, const char *secret)
128
{
129
12
	uint8_t	 cipher[256];
130
6
	size_t	 clen = sizeof(cipher);
131
132
12
	if (radius_get_vs_raw_attr(packet, RADIUS_VENDOR_MICROSOFT, vtype,
133
12
	    cipher, &clen) != 0)
134
		return (-1);
135
18
	if (radius_decrypt_mppe_key_attr(buf, len, cipher, clen,
136
12
		radius_get_request_authenticator_retval(packet), secret) != 0)
137
		return (-1);
138
6
	return (0);
139
6
}
140
141
static int
142
radius_put_mppe_key_attr(RADIUS_PACKET * packet, uint8_t vtype,
143
    const void *buf, size_t len, const char *secret)
144
{
145
12
	uint8_t		 cipher[256];
146
6
	size_t		 clen = sizeof(cipher);
147
148
18
	if (radius_encrypt_mppe_key_attr(cipher, &clen, buf, len,
149
12
		radius_get_request_authenticator_retval(packet), secret) != 0)
150
		return (-1);
151
12
	if (radius_put_vs_raw_attr(packet, RADIUS_VENDOR_MICROSOFT, vtype,
152
12
	    cipher, clen) != 0)
153
		return (-1);
154
155
6
	return (0);
156
6
}
157
158
int
159
radius_get_mppe_send_key_attr(const RADIUS_PACKET * packet, void *buf,
160
    size_t * len, const char *secret)
161
{
162
6
	return (radius_get_mppe_key_attr(packet, RADIUS_VTYPE_MPPE_SEND_KEY,
163
	    buf, len, secret));
164
}
165
166
int
167
radius_put_mppe_send_key_attr(RADIUS_PACKET * packet, const void *buf,
168
    size_t len, const char *secret)
169
{
170
6
	return (radius_put_mppe_key_attr(packet, RADIUS_VTYPE_MPPE_SEND_KEY,
171
	    buf, len, secret));
172
}
173
174
int
175
radius_get_mppe_recv_key_attr(const RADIUS_PACKET * packet, void *buf,
176
    size_t * len, const char *secret)
177
{
178
6
	return (radius_get_mppe_key_attr(packet, RADIUS_VTYPE_MPPE_RECV_KEY,
179
	    buf, len, secret));
180
}
181
182
int
183
radius_put_mppe_recv_key_attr(RADIUS_PACKET * packet, const void *buf,
184
    size_t len, const char *secret)
185
{
186
6
	return (radius_put_mppe_key_attr(packet, RADIUS_VTYPE_MPPE_RECV_KEY,
187
	    buf, len, secret));
188
}