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 size_t slen = strlen(secret); 49 uint8_t b[16], plain0[256], *p, *c; 50 size_t off; 51 MD5_CTX ctx; 52 unsigned int i; 53 uint16_t salt; 54 55 if (plen > 239) 56 return (-1); 57 if (*clen < ROUNDUP(plen + 1, 16) + 2) 58 return (-1); 59 60 salt = arc4random(); 61 plain0[0] = plen; 62 memcpy(plain0 + 1, plain, plen); 63 memset(plain0 + 1 + plen, 0, ROUNDUP(plen + 1, 16) - (plen + 1)); 64 65 *clen = ROUNDUP(plen + 1, 16) + 2; 66 memcpy(cipher, &salt, 2); 67 for (off = 0; off < *clen - 2; off += 16) { 68 c = ((uint8_t *)cipher) + 2 + off; 69 p = ((uint8_t *)plain0) + off; 70 MD5_Init(&ctx); 71 MD5_Update(&ctx, secret, slen); 72 if (off == 0) { 73 MD5_Update(&ctx, ra, 16); 74 MD5_Update(&ctx, cipher, 2); 75 } else 76 MD5_Update(&ctx, c - 16, 16); 77 MD5_Final(b, &ctx); 78 for (i = 0; i < 16; i++) 79 c[i] = p[i] ^ b[i]; 80 } 81 82 return (0); 83 } 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 size_t slen = strlen(secret); 90 uint8_t b[16], plain0[256], *p, *c; 91 size_t off; 92 MD5_CTX ctx; 93 unsigned int i; 94 95 if (clen < 18 || clen > 255) 96 return (-1); 97 if ((clen - 2) % 16 != 0) 98 return (-1); 99 if (*plen < clen - 3) 100 return (-1); 101 102 for (off = 0; off < clen - 2; off += 16) { 103 c = ((uint8_t *)cipher) + 2 + off; 104 p = ((uint8_t *)plain0) + off; 105 MD5_Init(&ctx); 106 MD5_Update(&ctx, secret, slen); 107 if (off == 0) { 108 MD5_Update(&ctx, ra, 16); 109 MD5_Update(&ctx, cipher, 2); 110 } else 111 MD5_Update(&ctx, c - 16, 16); 112 MD5_Final(b, &ctx); 113 for (i = 0; i < 16; i++) 114 p[i] = c[i] ^ b[i]; 115 } 116 117 if (plain0[0] > clen - 3) 118 return (-1); 119 *plen = plain0[0]; 120 memcpy(plain, plain0 + 1, *plen); 121 122 return (0); 123 } 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 uint8_t cipher[256]; 130 size_t clen = sizeof(cipher); 131 132 if (radius_get_vs_raw_attr(packet, RADIUS_VENDOR_MICROSOFT, vtype, 133 cipher, &clen) != 0) 134 return (-1); 135 if (radius_decrypt_mppe_key_attr(buf, len, cipher, clen, 136 radius_get_request_authenticator_retval(packet), secret) != 0) 137 return (-1); 138 return (0); 139 } 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 uint8_t cipher[256]; 146 size_t clen = sizeof(cipher); 147 148 if (radius_encrypt_mppe_key_attr(cipher, &clen, buf, len, 149 radius_get_request_authenticator_retval(packet), secret) != 0) 150 return (-1); 151 if (radius_put_vs_raw_attr(packet, RADIUS_VENDOR_MICROSOFT, vtype, 152 cipher, clen) != 0) 153 return (-1); 154 155 return (0); 156 } 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 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 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 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 return (radius_put_mppe_key_attr(packet, RADIUS_VTYPE_MPPE_RECV_KEY, 187 buf, len, secret)); 188 } 189