2 * Copyright (c) 2010 The FreeBSD Foundation
5 * This software was developed by Shteryana Sotirova Shopova under
6 * sponsorship from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/types.h>
32 #include <sys/socket.h>
39 #elif defined(HAVE_INTTYPES_H)
45 #include <netinet/in.h>
48 #include <openssl/evp.h>
55 #define SNMP_PRIV_AES_IV_SIZ 16
56 #define SNMP_EXTENDED_KEY_SIZ 64
57 #define SNMP_AUTH_KEY_LOOPCNT 1048576
58 #define SNMP_AUTH_BUF_SIZE 72
60 static const uint8_t ipad = 0x36;
61 static const uint8_t opad = 0x5c;
66 snmp_digest_init(const struct snmp_user *user, EVP_MD_CTX *ctx,
67 const EVP_MD **dtype, uint32_t *keylen)
69 if (user->auth_proto == SNMP_AUTH_HMAC_MD5) {
71 *keylen = SNMP_AUTH_HMACMD5_KEY_SIZ;
72 } else if (user->auth_proto == SNMP_AUTH_HMAC_SHA) {
74 *keylen = SNMP_AUTH_HMACSHA_KEY_SIZ;
75 } else if (user->auth_proto == SNMP_AUTH_NOAUTH)
78 snmp_error("unknown authentication option - %d",
83 if (EVP_DigestInit(ctx, *dtype) != 1)
90 snmp_pdu_calc_digest(const struct snmp_pdu *pdu, uint8_t *digest)
92 uint8_t md[EVP_MAX_MD_SIZE], extkey[SNMP_EXTENDED_KEY_SIZ];
93 uint8_t key1[SNMP_EXTENDED_KEY_SIZ], key2[SNMP_EXTENDED_KEY_SIZ];
94 uint32_t i, keylen, olen;
99 err = snmp_digest_init(&pdu->user, &ctx, &dtype, &keylen);
101 return (SNMP_CODE_BADDIGEST);
103 return (SNMP_CODE_OK);
105 memset(pdu->digest_ptr, 0, sizeof(pdu->msg_digest));
106 memcpy(extkey, pdu->user.auth_key, keylen);
107 memset(extkey + keylen, 0, sizeof(extkey) - keylen);
109 for (i = 0; i < SNMP_EXTENDED_KEY_SIZ; i++) {
110 key1[i] = extkey[i] ^ ipad;
111 key2[i] = extkey[i] ^ opad;
114 if (EVP_DigestUpdate(&ctx, key1, SNMP_EXTENDED_KEY_SIZ) != 1 ||
115 EVP_DigestUpdate(&ctx, pdu->outer_ptr, pdu->outer_len) != 1 ||
116 EVP_DigestFinal(&ctx, md, &olen) != 1)
119 if (EVP_DigestInit(&ctx, dtype) != 1 ||
120 EVP_DigestUpdate(&ctx, key2, SNMP_EXTENDED_KEY_SIZ) != 1 ||
121 EVP_DigestUpdate(&ctx, md, olen) != 1 ||
122 EVP_DigestFinal(&ctx, md, &olen) != 1)
125 if (olen < SNMP_USM_AUTH_SIZE) {
126 snmp_error("bad digest size - %d", olen);
127 EVP_MD_CTX_cleanup(&ctx);
128 return (SNMP_CODE_BADDIGEST);
131 memcpy(digest, md, SNMP_USM_AUTH_SIZE);
132 EVP_MD_CTX_cleanup(&ctx);
133 return (SNMP_CODE_OK);
136 EVP_MD_CTX_cleanup(&ctx);
137 return (SNMP_CODE_BADDIGEST);
141 snmp_pdu_cipher_init(const struct snmp_pdu *pdu, int32_t len,
142 const EVP_CIPHER **ctype, uint8_t *piv)
147 if (pdu->user.priv_proto == SNMP_PRIV_DES) {
150 *ctype = EVP_des_cbc();
151 memcpy(piv, pdu->msg_salt, sizeof(pdu->msg_salt));
152 for (i = 0; i < 8; i++)
153 piv[i] = piv[i] ^ pdu->user.priv_key[8 + i];
154 } else if (pdu->user.priv_proto == SNMP_PRIV_AES) {
155 *ctype = EVP_aes_128_cfb128();
156 netint = htonl(pdu->engine.engine_boots);
157 memcpy(piv, &netint, sizeof(netint));
158 piv += sizeof(netint);
159 netint = htonl(pdu->engine.engine_time);
160 memcpy(piv, &netint, sizeof(netint));
161 piv += sizeof(netint);
162 memcpy(piv, pdu->msg_salt, sizeof(pdu->msg_salt));
163 } else if (pdu->user.priv_proto == SNMP_PRIV_NOPRIV)
166 snmp_error("unknown privacy option - %d", pdu->user.priv_proto);
174 snmp_pdu_encrypt(const struct snmp_pdu *pdu)
177 uint8_t iv[SNMP_PRIV_AES_IV_SIZ];
178 const EVP_CIPHER *ctype;
181 err = snmp_pdu_cipher_init(pdu, pdu->scoped_len, &ctype, iv);
183 return (SNMP_CODE_EDECRYPT);
185 return (SNMP_CODE_OK);
187 if (EVP_EncryptInit(&ctx, ctype, pdu->user.priv_key, iv) != 1)
188 return (SNMP_CODE_FAILED);
190 if (EVP_EncryptUpdate(&ctx, pdu->scoped_ptr, &olen, pdu->scoped_ptr,
191 pdu->scoped_len) != 1 ||
192 EVP_EncryptFinal(&ctx, pdu->scoped_ptr + olen, &olen) != 1) {
193 EVP_CIPHER_CTX_cleanup(&ctx);
194 return (SNMP_CODE_FAILED);
197 EVP_CIPHER_CTX_cleanup(&ctx);
198 return (SNMP_CODE_OK);
202 snmp_pdu_decrypt(const struct snmp_pdu *pdu)
205 uint8_t iv[SNMP_PRIV_AES_IV_SIZ];
206 const EVP_CIPHER *ctype;
209 err = snmp_pdu_cipher_init(pdu, pdu->scoped_len, &ctype, iv);
211 return (SNMP_CODE_EDECRYPT);
213 return (SNMP_CODE_OK);
215 if (EVP_DecryptInit(&ctx, ctype, pdu->user.priv_key, iv) != 1 ||
216 EVP_CIPHER_CTX_set_padding(&ctx, 0) != 1)
217 return (SNMP_CODE_EDECRYPT);
219 if (EVP_DecryptUpdate(&ctx, pdu->scoped_ptr, &olen, pdu->scoped_ptr,
220 pdu->scoped_len) != 1 ||
221 EVP_DecryptFinal(&ctx, pdu->scoped_ptr + olen, &olen) != 1) {
222 EVP_CIPHER_CTX_cleanup(&ctx);
223 return (SNMP_CODE_EDECRYPT);
226 EVP_CIPHER_CTX_cleanup(&ctx);
227 return (SNMP_CODE_OK);
230 /* [RFC 3414] - A.2. Password to Key Algorithm */
232 snmp_passwd_to_keys(struct snmp_user *user, char *passwd)
234 int err, loop, i, pwdlen;
235 uint32_t keylen, olen;
238 uint8_t authbuf[SNMP_AUTH_BUF_SIZE];
240 if (passwd == NULL || user == NULL)
241 return (SNMP_CODE_FAILED);
243 err = snmp_digest_init(user, &ctx, &dtype, &keylen);
245 return (SNMP_CODE_BADDIGEST);
247 return (SNMP_CODE_OK);
249 memset(user->auth_key, 0, sizeof(user->auth_key));
250 pwdlen = strlen(passwd);
252 for (loop = 0; loop < SNMP_AUTH_KEY_LOOPCNT; loop += i) {
253 for (i = 0; i < SNMP_EXTENDED_KEY_SIZ; i++)
254 authbuf[i] = passwd[(loop + i) % pwdlen];
255 if (EVP_DigestUpdate(&ctx, authbuf, SNMP_EXTENDED_KEY_SIZ) != 1)
259 if (EVP_DigestFinal(&ctx, user->auth_key, &olen) != 1)
262 EVP_MD_CTX_cleanup(&ctx);
263 return (SNMP_CODE_OK);
266 EVP_MD_CTX_cleanup(&ctx);
267 return (SNMP_CODE_BADDIGEST);
270 /* [RFC 3414] - 2.6. Key Localization Algorithm */
272 snmp_get_local_keys(struct snmp_user *user, uint8_t *eid, uint32_t elen)
275 uint32_t keylen, olen;
278 uint8_t authbuf[SNMP_AUTH_BUF_SIZE];
280 if (user == NULL || eid == NULL || elen > SNMP_ENGINE_ID_SIZ)
281 return (SNMP_CODE_FAILED);
283 memset(user->priv_key, 0, sizeof(user->priv_key));
284 memset(authbuf, 0, sizeof(authbuf));
286 err = snmp_digest_init(user, &ctx, &dtype, &keylen);
288 return (SNMP_CODE_BADDIGEST);
290 return (SNMP_CODE_OK);
292 memcpy(authbuf, user->auth_key, keylen);
293 memcpy(authbuf + keylen, eid, elen);
294 memcpy(authbuf + keylen + elen, user->auth_key, keylen);
296 if (EVP_DigestUpdate(&ctx, authbuf, 2 * keylen + elen) != 1 ||
297 EVP_DigestFinal(&ctx, user->auth_key, &olen) != 1) {
298 EVP_MD_CTX_cleanup(&ctx);
299 return (SNMP_CODE_BADDIGEST);
301 EVP_MD_CTX_cleanup(&ctx);
303 if (user->priv_proto != SNMP_PRIV_NOPRIV)
304 memcpy(user->priv_key, user->auth_key, sizeof(user->priv_key));
306 return (SNMP_CODE_OK);
310 snmp_calc_keychange(struct snmp_user *user, uint8_t *keychange)
312 int32_t err, rvalue[SNMP_AUTH_HMACSHA_KEY_SIZ / 4];
313 uint32_t i, keylen, olen;
317 err = snmp_digest_init(user, &ctx, &dtype, &keylen);
319 return (SNMP_CODE_BADDIGEST);
321 return (SNMP_CODE_OK);
323 for (i = 0; i < keylen / 4; i++)
324 rvalue[i] = random();
326 memcpy(keychange, user->auth_key, keylen);
327 memcpy(keychange + keylen, rvalue, keylen);
329 if (EVP_DigestUpdate(&ctx, keychange, 2 * keylen) != 1 ||
330 EVP_DigestFinal(&ctx, keychange, &olen) != 1) {
331 EVP_MD_CTX_cleanup(&ctx);
332 return (SNMP_CODE_BADDIGEST);
335 EVP_MD_CTX_cleanup(&ctx);
336 return (SNMP_CODE_OK);
339 #else /* !HAVE_LIBCRYPTO */
342 snmp_pdu_calc_digest(const struct snmp_pdu *pdu, uint8_t *digest __unused)
344 if (pdu->user.auth_proto != SNMP_AUTH_NOAUTH)
345 return (SNMP_CODE_BADSECLEVEL);
348 return (SNMP_CODE_OK);
352 snmp_pdu_encrypt(const struct snmp_pdu *pdu)
354 if (pdu->user.priv_proto != SNMP_PRIV_NOPRIV)
355 return (SNMP_CODE_BADSECLEVEL);
357 return (SNMP_CODE_OK);
361 snmp_pdu_decrypt(const struct snmp_pdu *pdu)
363 if (pdu->user.priv_proto != SNMP_PRIV_NOPRIV)
364 return (SNMP_CODE_BADSECLEVEL);
366 return (SNMP_CODE_OK);
370 snmp_passwd_to_keys(struct snmp_user *user, char *passwd __unused)
372 if (user->auth_proto == SNMP_AUTH_NOAUTH &&
373 user->priv_proto == SNMP_PRIV_NOPRIV)
374 return (SNMP_CODE_OK);
376 errno = EPROTONOSUPPORT;
378 return (SNMP_CODE_FAILED);
382 snmp_get_local_keys(struct snmp_user *user, uint8_t *eid __unused,
383 uint32_t elen __unused)
385 if (user->auth_proto == SNMP_AUTH_NOAUTH &&
386 user->priv_proto == SNMP_PRIV_NOPRIV)
387 return (SNMP_CODE_OK);
389 errno = EPROTONOSUPPORT;
391 return (SNMP_CODE_FAILED);
395 snmp_calc_keychange(struct snmp_user *user __unused,
396 uint8_t *keychange __unused)
398 errno = EPROTONOSUPPORT;
399 return (SNMP_CODE_FAILED);
402 #endif /* HAVE_LIBCRYPTO */