Debian Codegraph @

debian/patches/0007-RadiusBLAST

raw
1 https://github.com/OpenVPN-Community/openvpn-radiusplugin/pull/9
2
3 commit bcd7cf10a3c8ad75cfe9231865df0aa43482bd91
4 Author: Samuel Thibault <[email protected]>
5 Date: Sun Oct 20 16:27:32 2024 +0200
6
7 Add MessageAuthenticator support
8
9 To address RadiusBLAST vulnerability.
10
11 diff --git a/RadiusClass/RadiusAttribute.cpp b/RadiusClass/RadiusAttribute.cpp
12 index da7d6d3..d3ca5d7 100755
13 --- a/RadiusClass/RadiusAttribute.cpp
14 +++ b/RadiusClass/RadiusAttribute.cpp
15 @@ -382,6 +382,14 @@ int RadiusAttribute::setValue(char *value)
16
17 }
18 break;
19 + case ATTRIB_Message_Authenticator:
20 + if(!(this->value=new Octet [16]))
21 + {
22 + return ALLOC_ERROR;
23 + }
24 + memcpy(this->value, value, 16);
25 + this->length=(Octet)16;
26 + break;
27
28 //for datatype integer/enum
29 case ATTRIB_NAS_Port:
30 diff --git a/RadiusClass/RadiusConfig.cpp b/RadiusClass/RadiusConfig.cpp
31 index 18702bd..54c19c9 100755
32 --- a/RadiusClass/RadiusConfig.cpp
33 +++ b/RadiusClass/RadiusConfig.cpp
34 @@ -171,6 +171,17 @@ int RadiusConfig::parseConfigFile(const char * configfile)
35 {
36 tmpServer->setWait(atoi(line.substr(5).c_str()));
37 }
38 + if (strncmp(line.c_str(),"requirema=",10)==0)
39 + {
40 + if (strcmp(line.substr(10).c_str(),"yes") == 0)
41 + tmpServer->setRequireMA(1);
42 + else if (strcmp(line.substr(10).c_str(),"no") == 0)
43 + tmpServer->setRequireMA(0);
44 + else if (strcmp(line.substr(10).c_str(),"auto") == 0)
45 + tmpServer->setRequireMA(-1);
46 + else
47 + return PARSING_ERROR;
48 + }
49 }
50 if(strstr(line.c_str(),"}"))
51 {
52 diff --git a/RadiusClass/RadiusPacket.cpp b/RadiusClass/RadiusPacket.cpp
53 index d97dcd1..3b1726c 100755
54 --- a/RadiusClass/RadiusPacket.cpp
55 +++ b/RadiusClass/RadiusPacket.cpp
56 @@ -65,6 +65,7 @@ RadiusPacket::RadiusPacket(Octet code)
57 this->length=sizeof(Octet)*(RADIUS_PACKET_AUTHENTICATOR_LEN+4);
58 this->sendbuffer=NULL;
59 this->sendbufferlen=0;
60 + this->message_authenticator=0;
61 this->recvbuffer=NULL;
62 this->recvbufferlen=0;
63 this->sock=0;
64 @@ -195,6 +196,12 @@ int RadiusPacket::shapeRadiusPacket(const char * sharedsecret)
65 //add the attributes to the buffer
66 for (multimap<Octet, RadiusAttribute>::iterator it = attribs.begin(); it != attribs.end(); it++)
67 {
68 + if (it->second.getType()==ATTRIB_Message_Authenticator)
69 + {
70 + // Record offset for filling it below.
71 + this->message_authenticator = this->sendbufferlen+2;
72 + }
73 +
74 //if the attribute is a password, build the hashedpassword
75 if (it->second.getType()==ATTRIB_User_Password)
76 {
77 @@ -406,6 +413,11 @@ int RadiusPacket::radiusSend(list<RadiusServer>::iterator server)
78 this->calcacctdigest(server->getSharedSecret().c_str());
79
80 }
81 +
82 + if (this->code==ACCESS_REQUEST && this->message_authenticator)
83 + {
84 + this->calcmadigest(server->getSharedSecret().c_str());
85 + }
86
87 //save the authenticator field for packet authentication on receiving a packet
88 memcpy(this->authenticator, this->req_authenticator, 16);
89 @@ -526,7 +538,7 @@ int RadiusPacket::radiusReceive(list<RadiusServer> *serverlist)
90 return UNSHAPE_ERROR;
91 }
92
93 - if (this->authenticateReceivedPacket(server->getSharedSecret().c_str())!=0)
94 + if (this->authenticateReceivedPacket(&*(server))!=0)
95 {
96
97 return WRONG_AUTHENTICATOR_IN_RECV_PACKET;
98 @@ -599,6 +611,44 @@ void RadiusPacket::calcacctdigest(const char *secret)
99 gcry_md_close(context);
100 }
101
102 +/** Sets the message authenticator field if the packet is
103 + * an access request. It is a MD5 hash over the whole packet
104 + * (the authenticator field itself is set to 0) with the shared
105 + * secret used as Hmac key.
106 + * The authenticator is updated in the message authenticator option.
107 + * @param secret The shared secret of the server in plaintext.
108 + */
109 +void RadiusPacket::calcmadigest(const char *secret)
110 +{
111 + //Octet digest[MD5_DIGEST_LENGTH];
112 + gcry_md_hd_t context;
113 +
114 + //Zero out the auth_vector in the received packet.
115 + //Then calculate the MD5 HMAC with the shared secret as key.
116 +
117 + memset((this->sendbuffer+this->message_authenticator), 0, 16);
118 + //build the hash
119 + if (!gcry_control (GCRYCTL_ANY_INITIALIZATION_P))
120 + { /* No other library has already initialized libgcrypt. */
121 +
122 + gcry_control(GCRYCTL_SET_THREAD_CBS,&gcry_threads_pthread);
123 +
124 + if (!gcry_check_version (NEED_LIBGCRYPT_VERSION) )
125 + {
126 + cerr << "libgcrypt is too old (need " << NEED_LIBGCRYPT_VERSION << ", have " << gcry_check_version (NULL) << ")\n";
127 + }
128 + /* Disable secure memory. */
129 + gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
130 + gcry_control (GCRYCTL_INITIALIZATION_FINISHED);
131 + }
132 + gcry_md_open (&context, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC);
133 + gcry_md_setkey(context, secret, strlen(secret));
134 + gcry_md_write(context, this->sendbuffer, this->length);
135 + //copy the digest to the packet
136 + memcpy(this->sendbuffer+this->message_authenticator, gcry_md_read(context, GCRY_MD_MD5), 16);
137 + gcry_md_close(context);
138 +}
139 +
140
141 /** Returns a pointer to the authenticator field.
142 * @return A pointer to the authenticator field.
143 @@ -653,9 +703,11 @@ pair<multimap<Octet,RadiusAttribute>::iterator,multimap<Octet,RadiusAttribute>::
144 * @return A an integer, 0 if the authenticator field is ok, else WRONG_AUTHENTICATOR_IN_RECV_PACKET.
145 */
146
147 -int RadiusPacket::authenticateReceivedPacket(const char *secret)
148 +int RadiusPacket::authenticateReceivedPacket(RadiusServer *server)
149 {
150 + const char *secret = server->getSharedSecret().c_str();
151 gcry_md_hd_t context;
152 + int res;
153
154 Octet * cpy_recvpacket;
155 //make a copy of the received packet
156 @@ -684,21 +736,53 @@ int RadiusPacket::authenticateReceivedPacket(const char *secret)
157 gcry_md_open (&context, GCRY_MD_MD5, 0);
158 gcry_md_write(context, cpy_recvpacket, this->recvbufferlen);
159 gcry_md_write(context, secret, strlen(secret));
160 -
161 - delete[] cpy_recvpacket;
162 -
163 +
164 //compare the received and the built authenticator
165 - if (memcmp(this->recvbuffer+4, gcry_md_read(context, GCRY_MD_MD5), 16)!=0)
166 + res = memcmp(this->recvbuffer+4, gcry_md_read(context, GCRY_MD_MD5), 16);
167 + gcry_md_close(context);
168 +
169 + if (res!=0)
170 {
171 - gcry_md_close(context);
172 + //Failed
173 + delete[] cpy_recvpacket;
174 return WRONG_AUTHENTICATOR_IN_RECV_PACKET;
175 }
176 - else
177 - {
178 +
179 + if (server->getRequireMA() == -1)
180 + //We did't know yet whether this server sends Message-Authenticator
181 + server->setRequireMA(this->recvbuffer[20] == 80);
182 +
183 + if (server->getRequireMA() == 1)
184 + {
185 + //This server normally sends Message-Authenticator, check that
186 + if (this->recvbufferlen < 20 + 18 || this->recvbuffer[20] != 80 || this->recvbuffer[21] != 18)
187 + {
188 + //No MA!
189 + delete[] cpy_recvpacket;
190 + return WRONG_AUTHENTICATOR_IN_RECV_PACKET;
191 + }
192 +
193 + //Clear MA hash for computation
194 + memset(cpy_recvpacket+20+2, 0, 16);
195 + //Hash
196 + gcry_md_open (&context, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC);
197 + gcry_md_setkey(context, secret, strlen(secret));
198 + gcry_md_write(context, cpy_recvpacket, this->recvbufferlen);
199 +
200 + //Compare
201 + res = memcmp(this->recvbuffer+20+2, gcry_md_read(context, GCRY_MD_MD5), 16);
202 gcry_md_close(context);
203 - return 0;
204 +
205 + if (res != 0)
206 + {
207 + //Failed
208 + delete[] cpy_recvpacket;
209 + return WRONG_AUTHENTICATOR_IN_RECV_PACKET;
210 + }
211 }
212 -
213 +
214 + delete[] cpy_recvpacket;
215 + return 0;
216 }
217
218
219 diff --git a/RadiusClass/RadiusPacket.h b/RadiusClass/RadiusPacket.h
220 index 7b02408..847c23d 100755
221 --- a/RadiusClass/RadiusPacket.h
222 +++ b/RadiusClass/RadiusPacket.h
223 @@ -73,10 +73,13 @@ private:
224
225 Octet *sendbuffer; /**<Buffer for sending the packet over the network.*/
226 int sendbufferlen; /**<Length of the buffer.*/
227 + int message_authenticator; /**<Offset of the message authenticator digest, if any.*/
228 Octet *recvbuffer; /**<Buffer for recveing the packet over the network.*/
229 int recvbufferlen; /**<Length of the buffer.*/
230 void calcacctdigest(const char *secret); /**Method to generate the hash
231 for the authenticator in Accounting-Requests.*/
232 + void calcmadigest(const char *secret); /**Method to generate the hash
233 + for the message authenticator in Access-Requests.*/
234
235 //private functions
236 void getRandom(int len, Octet *num);
237 @@ -101,7 +104,7 @@ public:
238
239 int getCode(void);
240
241 - int authenticateReceivedPacket(const char *secret);
242 + int authenticateReceivedPacket(RadiusServer *server);
243
244 pair<multimap<Octet,RadiusAttribute>::iterator,multimap<Octet,RadiusAttribute>::iterator> findAttributes(int type);
245
246 diff --git a/RadiusClass/RadiusServer.cpp b/RadiusClass/RadiusServer.cpp
247 index 27f2011..a90c9b3 100755
248 --- a/RadiusClass/RadiusServer.cpp
249 +++ b/RadiusClass/RadiusServer.cpp
250 @@ -32,13 +32,14 @@
251 * @param int wait : The time (in seconds) to wait on a response of the radius server.
252 */
253 RadiusServer::RadiusServer(string name, string secret,
254 - int authport, int acctport, int retry, int wait)
255 + int authport, int acctport, int retry, int wait, int requirema)
256 {
257 this->acctport=acctport;
258 this->authport=authport;
259 this->name=name;
260 this->retry=retry;
261 this->wait=wait;
262 + this->requirema=requirema;
263 this->sharedsecret=secret;
264
265
266 @@ -62,6 +63,7 @@ RadiusServer &RadiusServer::operator=(const RadiusServer &s)
267 this->retry=s.retry;
268 this->acctport=s.acctport;
269 this->authport=s.authport;
270 + this->requirema=s.requirema;
271 this->sharedsecret=s.sharedsecret;
272 return (*this);
273 }
274 @@ -186,6 +188,24 @@ void RadiusServer::setWait(int w)
275 }
276 }
277
278 +/** The getter method for the private member requirema
279 + * @return A integer 0 when MA Message-Authenticator is not required, 1 if it
280 + * is required, -1 if automatically determined from first answer.
281 + */
282 +int RadiusServer::getRequireMA(void)
283 +{
284 + return this->requirema;
285 +}
286 +
287 +
288 +/** The setter method for the private member requirema
289 + * @param ma integer 0 when MA Message-Authenticator is not required, 1 if it
290 + */
291 +void RadiusServer::setRequireMA(int ma)
292 +{
293 + this->requirema=ma;
294 +}
295 +
296 ostream& operator << (ostream& os, RadiusServer& server)
297 {
298 os << "\n\nRadiusServer:";
299 @@ -194,6 +214,7 @@ ostream& operator << (ostream& os, RadiusServer& server)
300 os << "\nAccounting-Port: " << server.acctport;
301 os << "\nRetries: " << server.retry;
302 os << "\nWait: " << server.wait;
303 + os << "\nRequireMA: " << server.requirema;
304 os << "\nSharedSecret: *******";
305 return os;
306
307 diff --git a/RadiusClass/RadiusServer.h b/RadiusClass/RadiusServer.h
308 index f29f5ca..73d0dbc 100755
309 --- a/RadiusClass/RadiusServer.h
310 +++ b/RadiusClass/RadiusServer.h
311 @@ -36,11 +36,12 @@ private:
312 int retry; /**< The number of retries how many times a radius ticket is send to the server, if it doesn#t answer.*/
313 string sharedsecret; /**< The sharedsecret, the maximum space is 16 chars.*/
314 int wait; /**< The time to wait for a response of the server.*/
315 + int requirema; /**< Whether to required Message-Authenticator from the server.*/
316
317 public:
318
319
320 - RadiusServer(string name="127.0.0.1",string secret = "", int authport=1812, int acctport=1813, int retry=3, int wait=1);
321 + RadiusServer(string name="127.0.0.1",string secret = "", int authport=1812, int acctport=1813, int retry=3, int wait=1, int requirema=-1);
322 ~RadiusServer();
323 RadiusServer &operator=(const RadiusServer &);
324
325 @@ -62,6 +63,9 @@ public:
326 string getName();
327 void setName(string);
328
329 + void setRequireMA(int);
330 + int getRequireMA(void);
331 +
332 friend ostream& operator << (ostream& os, RadiusServer& server);
333 };
334
335 diff --git a/RadiusClass/exampleconfig b/RadiusClass/exampleconfig
336 index 6a303d8..ec8e3ec 100644
337 --- a/RadiusClass/exampleconfig
338 +++ b/RadiusClass/exampleconfig
339 @@ -11,6 +11,7 @@ server
340 retry=1
341 wait=1
342 sharedsecret=testpw
343 + requirema=auto
344
345 }
346
347 @@ -22,4 +23,5 @@ server
348 retry=1
349 wait=1
350 sharedsecret=xxxx
351 + requirema=auto
352 }
353 diff --git a/UserAuth.cpp b/UserAuth.cpp
354 index 8b44256..525b80d 100755
355 --- a/UserAuth.cpp
356 +++ b/UserAuth.cpp
357 @@ -48,8 +48,10 @@ int UserAuth::sendAcceptRequestPacket(PluginContext * context)
358 {
359 list<RadiusServer> * serverlist;
360 list<RadiusServer>::iterator server;
361 + std::string zero("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16);
362 RadiusPacket packet(ACCESS_REQUEST);
363 - RadiusAttribute ra1(ATTRIB_User_Name,this->getUsername().c_str()),
364 + RadiusAttribute ra0(ATTRIB_Message_Authenticator,zero),
365 + ra1(ATTRIB_User_Name,this->getUsername().c_str()),
366 ra2(ATTRIB_User_Password),
367 ra3(ATTRIB_NAS_Port,this->getPortnumber()),
368 ra4(ATTRIB_Calling_Station_Id,this->getCallingStationId()),
369 @@ -72,8 +74,13 @@ int UserAuth::sendAcceptRequestPacket(PluginContext * context)
370
371 if (DEBUG (context->getVerbosity()))
372 cerr << getTime() << "RADIUS-PLUGIN: Build password packet: password: *****, sharedSecret: *****.\n";
373 -
374 +
375 //add the attributes
376 + if(packet.addRadiusAttribute(&ra0))
377 + {
378 + cerr << getTime() << "RADIUS-PLUGIN: Fail to add attribute ATTRIB_Message_Authenticator.\n";
379 + }
380 +
381 ra2.setValue(this->password);
382 if(packet.addRadiusAttribute(&ra1))
383 {
384 diff --git a/radiusplugin.cnf b/radiusplugin.cnf
385 index c7ff9a0..f6988bc 100755
386 --- a/radiusplugin.cnf
387 +++ b/radiusplugin.cnf
388 @@ -97,6 +97,9 @@ server
389 wait=1
390 # The shared secret.
391 sharedsecret=testpw
392 + # Whether to require Message-Authenticator (yes) or not (no), or require if first answer included it (auto)
393 + # For better security against RadiusBLAST, set to yes once it is confirmed that your radius server always sends it
394 + requirema=auto
395 }
396
397 server
398 @@ -113,5 +116,8 @@ server
399 wait=1
400 # The shared secret.
401 sharedsecret=testpw
402 + # Whether to require Message-Authenticator (yes) or not (no), or require if first answer included it (auto)
403 + # For better security against RadiusBLAST, set to yes once it is confirmed that your radius server always sends it
404 + requirema=auto
405 }
406