iPXE
eap_md5.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2024 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <ipxe/md5.h>
30 #include <ipxe/chap.h>
31 #include <ipxe/eap.h>
32 
33 /** @file
34  *
35  * EAP MD5-Challenge authentication method
36  *
37  */
38 
39 /**
40  * Handle EAP MD5-Challenge
41  *
42  * @v supplicant EAP supplicant
43  * @v req Request type data
44  * @v req_len Length of request type data
45  * @ret rc Return status code
46  */
47 static int eap_rx_md5 ( struct eap_supplicant *supplicant,
48  const void *req, size_t req_len ) {
49  struct net_device *netdev = supplicant->netdev;
50  const struct eap_md5 *md5req = req;
51  struct {
52  uint8_t len;
54  } __attribute__ (( packed )) md5rsp;
55  struct chap_response chap;
56  void *secret;
57  int secret_len;
58  int rc;
59 
60  /* Sanity checks */
61  if ( req_len < sizeof ( *md5req ) ) {
62  DBGC ( netdev, "EAP %s underlength MD5-Challenge:\n",
63  netdev->name );
64  DBGC_HDA ( netdev, 0, req, req_len );
65  rc = -EINVAL;
66  goto err_sanity;
67  }
68  if ( ( req_len - sizeof ( *md5req ) ) < md5req->len ) {
69  DBGC ( netdev, "EAP %s truncated MD5-Challenge:\n",
70  netdev->name );
71  DBGC_HDA ( netdev, 0, req, req_len );
72  rc = -EINVAL;
73  goto err_sanity;
74  }
75 
76  /* Construct response */
77  if ( ( rc = chap_init ( &chap, &md5_algorithm ) ) != 0 ) {
78  DBGC ( netdev, "EAP %s could not initialise CHAP: %s\n",
79  netdev->name, strerror ( rc ) );
80  goto err_chap;
81  }
82  chap_set_identifier ( &chap, supplicant->id );
84  &password_setting, &secret );
85  if ( secret_len < 0 ) {
86  rc = secret_len;
87  DBGC ( netdev, "EAP %s has no secret: %s\n",
88  netdev->name, strerror ( rc ) );
89  goto err_secret;
90  }
91  chap_update ( &chap, secret, secret_len );
92  chap_update ( &chap, md5req->value, md5req->len );
93  chap_respond ( &chap );
94  assert ( chap.response_len == sizeof ( md5rsp.value ) );
95  md5rsp.len = sizeof ( md5rsp.value );
96  memcpy ( md5rsp.value, chap.response, sizeof ( md5rsp.value ) );
97 
98  /* Transmit response */
99  if ( ( rc = eap_tx_response ( supplicant, &md5rsp,
100  sizeof ( md5rsp ) ) ) != 0 )
101  goto err_tx;
102 
103  err_tx:
104  free ( secret );
105  err_secret:
106  chap_finish ( &chap );
107  err_chap:
108  err_sanity:
109  return rc;
110 }
111 
112 /** EAP MD5-Challenge method */
113 struct eap_method eap_md5_method __eap_method = {
114  .type = EAP_TYPE_MD5,
115  .rx = eap_rx_md5,
116 };
#define __attribute__(x)
Definition: compiler.h:10
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
A CHAP response.
Definition: chap.h:18
uint8_t * response
CHAP response.
Definition: chap.h:24
int fetch_raw_setting_copy(struct settings *settings, const struct setting *setting, void **data)
Fetch value of setting.
Definition: settings.c:821
Error codes.
#define EAP_TYPE_MD5
EAP MD5 challenge request/response.
Definition: eap.h:53
#define DBGC(...)
Definition: compiler.h:505
void chap_respond(struct chap_response *chap)
Respond to the CHAP challenge.
Definition: chap.c:104
EAP MD5 challenge request/response type data.
Definition: eap.h:56
An EAP supplicant.
Definition: eap.h:138
static int eap_rx_md5(struct eap_supplicant *supplicant, const void *req, size_t req_len)
Handle EAP MD5-Challenge.
Definition: eap_md5.c:47
struct eap_method eap_md5_method __eap_method
EAP MD5-Challenge method.
Definition: eap_md5.c:113
uint8_t id
ID for current request/response.
Definition: eap.h:144
static struct settings * netdev_settings(struct net_device *netdev)
Get per-netdevice configuration settings block.
Definition: netdevice.h:583
uint8_t type
Type.
Definition: eap.h:179
void * memcpy(void *dest, const void *src, size_t len) __nonnull
int eap_tx_response(struct eap_supplicant *supplicant, const void *rsp, size_t rsp_len)
Transmit EAP response.
Definition: eap.c:47
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define DBGC_HDA(...)
Definition: compiler.h:506
int chap_init(struct chap_response *chap, struct digest_algorithm *digest)
Initialise CHAP challenge/response.
Definition: chap.c:51
static struct net_device * netdev
Definition: gdbudp.c:52
uint8_t len
Value length.
Definition: eap.h:58
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
void chap_finish(struct chap_response *chap)
Free resources used by a CHAP response.
Definition: chap.c:122
CHAP protocol.
A network device.
Definition: netdevice.h:352
unsigned char uint8_t
Definition: stdint.h:10
size_t response_len
Length of CHAP response.
Definition: chap.h:26
static void chap_set_identifier(struct chap_response *chap, unsigned int identifier)
Add identifier data to the CHAP challenge.
Definition: chap.h:46
char name[NETDEV_NAME_LEN]
Name of this network device.
Definition: netdevice.h:362
uint8_t value[0]
Value.
Definition: eap.h:60
#define MD5_DIGEST_SIZE
MD5 digest size.
Definition: md5.h:72
struct net_device * netdev
Network device.
Definition: eap.h:140
MD5 algorithm.
void chap_update(struct chap_response *chap, const void *data, size_t len)
Add data to the CHAP challenge.
Definition: chap.c:85
String functions.
Extensible Authentication Protocol.
struct digest_algorithm md5_algorithm
MD5 algorithm.
Definition: md5.c:286
An EAP method.
Definition: eap.h:177