summaryrefslogtreecommitdiffstats
path: root/contrib/bsnmp/lib/snmpcrypto.c
blob: 81d8eacba9976dafe15e6c5405f9c53d4780a61a (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*-
 * Copyright (c) 2010 The FreeBSD Foundation
 * All rights reserved.
 *
 * This software was developed by Shteryana Sotirova Shopova under
 * sponsorship from the FreeBSD Foundation.
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
 *
 * $FreeBSD$
 */
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#elif defined(HAVE_INTTYPES_H)
#include <inttypes.h>
#endif
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <netinet/in.h>

#ifdef HAVE_LIBCRYPTO
#include <openssl/evp.h>
#endif

#include "asn1.h"
#include "snmp.h"
#include "snmppriv.h"

#define	SNMP_PRIV_AES_IV_SIZ		16
#define	SNMP_EXTENDED_KEY_SIZ		64
#define	SNMP_AUTH_KEY_LOOPCNT		1048576
#define	SNMP_AUTH_BUF_SIZE		72

static const uint8_t ipad = 0x36;
static const uint8_t opad = 0x5c;

#ifdef HAVE_LIBCRYPTO

static int32_t
snmp_digest_init(const struct snmp_user *user, EVP_MD_CTX *ctx,
    const EVP_MD **dtype, uint32_t *keylen)
{
	if (user->auth_proto == SNMP_AUTH_HMAC_MD5) {
		*dtype = EVP_md5();
		*keylen = SNMP_AUTH_HMACMD5_KEY_SIZ;
	} else if (user->auth_proto == SNMP_AUTH_HMAC_SHA) {
		*dtype = EVP_sha1();
		*keylen = SNMP_AUTH_HMACSHA_KEY_SIZ;
	} else if (user->auth_proto == SNMP_AUTH_NOAUTH)
		return (0);
	else {
		snmp_error("unknown authentication option - %d",
		    user->auth_proto);
		return (-1);
	}

	if (EVP_DigestInit(ctx, *dtype) != 1)
		return (-1);

	return (1);
}

enum snmp_code
snmp_pdu_calc_digest(struct asn_buf *b, const struct snmp_pdu *pdu,
    uint8_t *digest)
{
	uint8_t md[EVP_MAX_MD_SIZE], extkey[SNMP_EXTENDED_KEY_SIZ];
	uint8_t key1[SNMP_EXTENDED_KEY_SIZ], key2[SNMP_EXTENDED_KEY_SIZ];
	uint32_t i, keylen, olen;
	int32_t err;
	const EVP_MD *dtype;
	EVP_MD_CTX ctx;

	err = snmp_digest_init(&pdu->user, &ctx, &dtype, &keylen);
	if (err < 0)
		return (SNMP_CODE_BADDIGEST);
	else if (err == 0)
		return (SNMP_CODE_OK);

	memset(pdu->digest_ptr, 0, sizeof(pdu->msg_digest));
	memcpy(extkey, pdu->user.auth_key, keylen);
	memset(extkey + keylen, 0, sizeof(extkey) - keylen);

	for (i = 0; i < SNMP_EXTENDED_KEY_SIZ; i++) {
		key1[i] = extkey[i] ^ ipad;
		key2[i] = extkey[i] ^ opad;
	}

	if (EVP_DigestUpdate(&ctx, key1, SNMP_EXTENDED_KEY_SIZ) != 1 ||
	    EVP_DigestUpdate(&ctx, pdu->outer_ptr, pdu->outer_len) != 1 ||
	    EVP_DigestFinal(&ctx, md, &olen) != 1)
		goto failed;

	if (EVP_DigestInit(&ctx, dtype) != 1 ||
	    EVP_DigestUpdate(&ctx, key2, SNMP_EXTENDED_KEY_SIZ) != 1 ||
	    EVP_DigestUpdate(&ctx, md, olen) != 1 ||
	    EVP_DigestFinal(&ctx, md, &olen) != 1)
		goto failed;

	if (olen < SNMP_USM_AUTH_SIZE) {
		snmp_error("bad digest size - %d", olen);
		EVP_MD_CTX_cleanup(&ctx);
		return (SNMP_CODE_BADDIGEST);
	}

	memcpy(digest, md, SNMP_USM_AUTH_SIZE);
	EVP_MD_CTX_cleanup(&ctx);
	return (SNMP_CODE_OK);

failed:
	EVP_MD_CTX_cleanup(&ctx);
	return (SNMP_CODE_BADDIGEST);
}

static int32_t
snmp_pdu_cipher_init(const struct snmp_pdu *pdu, int32_t len,
    EVP_CIPHER_CTX *ctx, const EVP_CIPHER **ctype, uint8_t *piv)
{
	int i;
	uint32_t netint;

	if (pdu->user.priv_proto == SNMP_PRIV_DES) {
		if (len  % 8 != 0)
			return (-1);
		*ctype = EVP_des_cbc();
		memcpy(piv, pdu->msg_salt, sizeof(pdu->msg_salt));
		for (i = 0; i < 8; i++)
			piv[i] = piv[i] ^ pdu->user.priv_key[8 + i];
	} else if (pdu->user.priv_proto == SNMP_PRIV_AES) {
		*ctype = EVP_aes_128_cfb128();
		netint = htonl(pdu->engine.engine_boots);
		memcpy(piv, &netint, sizeof(netint));
		piv += sizeof(netint);
		netint = htonl(pdu->engine.engine_time);
		memcpy(piv, &netint, sizeof(netint));
		piv += sizeof(netint);
		memcpy(piv, pdu->msg_salt, sizeof(pdu->msg_salt));
	} else if (pdu->user.priv_proto == SNMP_PRIV_NOPRIV)
		return (0);
	else {
		snmp_error("unknown privacy option - %d", pdu->user.priv_proto);
		return (-1);
	}

	return (1);
}

enum snmp_code
snmp_pdu_encrypt(struct asn_buf *b, const struct snmp_pdu *pdu)
{
	int32_t err, olen;
	uint8_t iv[SNMP_PRIV_AES_IV_SIZ];
	const EVP_CIPHER *ctype;
	EVP_CIPHER_CTX ctx;

	err = snmp_pdu_cipher_init(pdu, pdu->scoped_len, &ctx, &ctype, iv);
	if (err < 0)
		return (SNMP_CODE_EDECRYPT);
	else if (err == 0)
		return (SNMP_CODE_OK);

	if (EVP_EncryptInit(&ctx, ctype, pdu->user.priv_key, iv) != 1)
		return (SNMP_CODE_FAILED);

	if (EVP_EncryptUpdate(&ctx, pdu->scoped_ptr, &olen, pdu->scoped_ptr,
	    pdu->scoped_len) != 1 ||
	    EVP_EncryptFinal(&ctx, pdu->scoped_ptr + olen, &olen) != 1) {
		EVP_CIPHER_CTX_cleanup(&ctx);
		return (SNMP_CODE_FAILED);
	}

	EVP_CIPHER_CTX_cleanup(&ctx);
	return (SNMP_CODE_OK);
}

enum snmp_code
snmp_pdu_decrypt(struct asn_buf *b, const struct snmp_pdu *pdu)
{
	int32_t err, olen;
	uint8_t iv[SNMP_PRIV_AES_IV_SIZ];
	const EVP_CIPHER *ctype;
	EVP_CIPHER_CTX ctx;

	err = snmp_pdu_cipher_init(pdu, pdu->scoped_len, &ctx, &ctype, iv);
	if (err < 0)
		return (SNMP_CODE_EDECRYPT);
	else if (err == 0)
		return (SNMP_CODE_OK);

	if (EVP_DecryptInit(&ctx, ctype, pdu->user.priv_key, iv) != 1 ||
	    EVP_CIPHER_CTX_set_padding(&ctx, 0) != 1)
		return (SNMP_CODE_EDECRYPT);

	if (EVP_DecryptUpdate(&ctx, pdu->scoped_ptr, &olen, pdu->scoped_ptr,
	    pdu->scoped_len) != 1 ||
	    EVP_DecryptFinal(&ctx, pdu->scoped_ptr + olen, &olen) != 1) {
		EVP_CIPHER_CTX_cleanup(&ctx);
		return (SNMP_CODE_EDECRYPT);
	}

	EVP_CIPHER_CTX_cleanup(&ctx);
	return (SNMP_CODE_OK);
}

/* [RFC 3414] - A.2. Password to Key Algorithm */
enum snmp_code
snmp_passwd_to_keys(struct snmp_user *user, char *passwd)
{
	int err, loop, i, pwdlen;
	uint32_t  keylen, olen;
	const EVP_MD *dtype;
	EVP_MD_CTX ctx;
	uint8_t authbuf[SNMP_AUTH_BUF_SIZE];

	if (passwd == NULL || user == NULL)
		return (SNMP_CODE_FAILED);

	err = snmp_digest_init(user, &ctx, &dtype, &keylen);
	if (err < 0)
		return (SNMP_CODE_BADDIGEST);
	else if (err == 0)
		return (SNMP_CODE_OK);

	memset(user->auth_key, 0, sizeof(user->auth_key));
	pwdlen = strlen(passwd);

	for (loop = 0; loop < SNMP_AUTH_KEY_LOOPCNT; loop += i) {
		for (i = 0; i < SNMP_EXTENDED_KEY_SIZ; i++)
			authbuf[i] = passwd[(loop + i) % pwdlen];
		if (EVP_DigestUpdate(&ctx, authbuf, SNMP_EXTENDED_KEY_SIZ) != 1)
			goto failed;
	}

	if (EVP_DigestFinal(&ctx, user->auth_key, &olen) != 1)
		goto failed;

	EVP_MD_CTX_cleanup(&ctx);
	return (SNMP_CODE_OK);

failed:
	EVP_MD_CTX_cleanup(&ctx);
	return (SNMP_CODE_BADDIGEST);
}

/* [RFC 3414] - 2.6. Key Localization Algorithm */
enum snmp_code
snmp_get_local_keys(struct snmp_user *user, uint8_t *eid, uint32_t elen)
{
	int err;
	uint32_t  keylen, olen;
	const EVP_MD *dtype;
	EVP_MD_CTX ctx;
	uint8_t authbuf[SNMP_AUTH_BUF_SIZE];

	if (user == NULL || eid == NULL || elen > SNMP_ENGINE_ID_SIZ)
		return (SNMP_CODE_FAILED);

	memset(user->priv_key, 0, sizeof(user->priv_key));
	memset(authbuf, 0, sizeof(authbuf));

	err = snmp_digest_init(user, &ctx, &dtype, &keylen);
	if (err < 0)
		return (SNMP_CODE_BADDIGEST);
	else if (err == 0)
		return (SNMP_CODE_OK);

	memcpy(authbuf, user->auth_key, keylen);
	memcpy(authbuf + keylen, eid, elen);
	memcpy(authbuf + keylen + elen, user->auth_key, keylen);

	if (EVP_DigestUpdate(&ctx, authbuf, 2 * keylen + elen) != 1 ||
	    EVP_DigestFinal(&ctx, user->auth_key, &olen) != 1) {
		EVP_MD_CTX_cleanup(&ctx);
		return (SNMP_CODE_BADDIGEST);
	}
	EVP_MD_CTX_cleanup(&ctx);

	if (user->priv_proto != SNMP_PRIV_NOPRIV)
		memcpy(user->priv_key, user->auth_key, sizeof(user->priv_key));

	return (SNMP_CODE_OK);
}

enum snmp_code
snmp_calc_keychange(struct snmp_user *user, uint8_t *keychange)
{
	int32_t i, err, rvalue[SNMP_AUTH_HMACSHA_KEY_SIZ / 4];
	uint32_t  keylen, olen;
	const EVP_MD *dtype;
	EVP_MD_CTX ctx;

	err = snmp_digest_init(user, &ctx, &dtype, &keylen);
	if (err < 0)
		return (SNMP_CODE_BADDIGEST);
	else if (err == 0)
		return (SNMP_CODE_OK);

	for (i = 0; i < keylen / 4; i++)
		rvalue[i] = random();
	
	memcpy(keychange, user->auth_key, keylen);
	memcpy(keychange + keylen, rvalue, keylen);

	if (EVP_DigestUpdate(&ctx, keychange, 2 * keylen) != 1 ||
	    EVP_DigestFinal(&ctx, keychange, &olen) != 1) {
		EVP_MD_CTX_cleanup(&ctx);
		return (SNMP_CODE_BADDIGEST);
	}

	EVP_MD_CTX_cleanup(&ctx);
	return (SNMP_CODE_OK);
}

#else /* !HAVE_LIBCRYPTO */

enum snmp_code
snmp_pdu_calc_digest(struct asn_buf *b __unused, const struct snmp_pdu *pdu,
    uint8_t *digest __unused)
{
	if  (pdu->user.auth_proto != SNMP_AUTH_NOAUTH)
		return (SNMP_CODE_BADSECLEVEL);


	return (SNMP_CODE_OK);
}

enum snmp_code
snmp_pdu_encrypt(struct asn_buf *b __unused, const struct snmp_pdu *pdu)
{
	if (pdu->user.priv_proto != SNMP_PRIV_NOPRIV)
		return (SNMP_CODE_BADSECLEVEL);

	return (SNMP_CODE_OK);
}

enum snmp_code
snmp_pdu_decrypt(struct asn_buf *b __unused, const struct snmp_pdu *pdu)
{
	if (pdu->user.priv_proto != SNMP_PRIV_NOPRIV)
		return (SNMP_CODE_BADSECLEVEL);

	return (SNMP_CODE_OK);
}

int
snmp_passwd_to_keys(struct snmp_user *user, char *passwd __unused)
{
	if (user->auth_proto == SNMP_AUTH_NOAUTH &&
	    user->priv_proto == SNMP_PRIV_NOPRIV)
		return (SNMP_CODE_OK);

	errno = EPROTONOSUPPORT;

	return (SNMP_CODE_FAILED);
}

int
snmp_get_local_keys(struct snmp_user *user, uint8_t *eid __unused,
    uint32_t elen __unused)
{
	if (user->auth_proto == SNMP_AUTH_NOAUTH &&
	    user->priv_proto == SNMP_PRIV_NOPRIV)
		return (SNMP_CODE_OK);

	errno = EPROTONOSUPPORT;

	return (SNMP_CODE_FAILED);
}

enum snmp_code
snmp_calc_keychange(struct snmp_user *user __unused,
    uint8_t *keychange __unused)
{
	errno = EPROTONOSUPPORT;
	return (SNMP_CODE_FAILED);
}

#endif /* HAVE_LIBCRYPTO */
OpenPOWER on IntegriCloud