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 FILE_SECBOOT ( PERMITTED );
26 
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <ipxe/md5.h>
31 #include <ipxe/chap.h>
32 #include <ipxe/eap.h>
33 
34 /** @file
35  *
36  * EAP MD5-Challenge authentication method
37  *
38  */
39 
40 /**
41  * Handle EAP MD5-Challenge
42  *
43  * @v supplicant EAP supplicant
44  * @v req Request type data
45  * @v req_len Length of request type data
46  * @ret rc Return status code
47  */
48 static int eap_rx_md5 ( struct eap_supplicant *supplicant,
49  const void *req, size_t req_len ) {
50  struct net_device *netdev = supplicant->netdev;
51  const struct eap_md5 *md5req = req;
52  struct {
53  uint8_t len;
55  } __attribute__ (( packed )) md5rsp;
56  struct chap_response chap;
57  void *secret;
58  int secret_len;
59  int rc;
60 
61  /* Sanity checks */
62  if ( req_len < sizeof ( *md5req ) ) {
63  DBGC ( netdev, "EAP %s underlength MD5-Challenge:\n",
64  netdev->name );
65  DBGC_HDA ( netdev, 0, req, req_len );
66  rc = -EINVAL;
67  goto err_sanity;
68  }
69  if ( ( req_len - sizeof ( *md5req ) ) < md5req->len ) {
70  DBGC ( netdev, "EAP %s truncated MD5-Challenge:\n",
71  netdev->name );
72  DBGC_HDA ( netdev, 0, req, req_len );
73  rc = -EINVAL;
74  goto err_sanity;
75  }
76 
77  /* Construct response */
78  if ( ( rc = chap_init ( &chap, &md5_algorithm ) ) != 0 ) {
79  DBGC ( netdev, "EAP %s could not initialise CHAP: %s\n",
80  netdev->name, strerror ( rc ) );
81  goto err_chap;
82  }
83  chap_set_identifier ( &chap, supplicant->id );
85  &password_setting, &secret );
86  if ( secret_len < 0 ) {
87  rc = secret_len;
88  DBGC ( netdev, "EAP %s has no secret: %s\n",
89  netdev->name, strerror ( rc ) );
90  goto err_secret;
91  }
92  chap_update ( &chap, secret, secret_len );
93  chap_update ( &chap, md5req->value, md5req->len );
94  chap_respond ( &chap );
95  assert ( chap.response_len == sizeof ( md5rsp.value ) );
96  md5rsp.len = sizeof ( md5rsp.value );
97  memcpy ( md5rsp.value, chap.response, sizeof ( md5rsp.value ) );
98 
99  /* Transmit response */
100  if ( ( rc = eap_tx_response ( supplicant, &md5rsp,
101  sizeof ( md5rsp ) ) ) != 0 )
102  goto err_tx;
103 
104  err_tx:
105  free ( secret );
106  err_secret:
107  chap_finish ( &chap );
108  err_chap:
109  err_sanity:
110  return rc;
111 }
112 
113 /** EAP MD5-Challenge method */
114 struct eap_method eap_md5_method __eap_method = {
115  .type = EAP_TYPE_MD5,
116  .rx = eap_rx_md5,
117 };
#define __attribute__(x)
Definition: compiler.h:10
#define EINVAL
Invalid argument.
Definition: errno.h:429
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
A CHAP response.
Definition: chap.h:19
uint8_t * response
CHAP response.
Definition: chap.h:25
int fetch_raw_setting_copy(struct settings *settings, const struct setting *setting, void **data)
Fetch value of setting.
Definition: settings.c:822
Error codes.
#define EAP_TYPE_MD5
EAP MD5 challenge request/response.
Definition: eap.h:54
#define DBGC(...)
Definition: compiler.h:505
void chap_respond(struct chap_response *chap)
Respond to the CHAP challenge.
Definition: chap.c:105
EAP MD5 challenge request/response type data.
Definition: eap.h:57
An EAP supplicant.
Definition: eap.h:139
static int eap_rx_md5(struct eap_supplicant *supplicant, const void *req, size_t req_len)
Handle EAP MD5-Challenge.
Definition: eap_md5.c:48
struct eap_method eap_md5_method __eap_method
EAP MD5-Challenge method.
Definition: eap_md5.c:114
uint8_t id
ID for current request/response.
Definition: eap.h:145
static struct settings * netdev_settings(struct net_device *netdev)
Get per-netdevice configuration settings block.
Definition: netdevice.h:587
uint8_t type
Type.
Definition: eap.h:180
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:48
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
FILE_SECBOOT(PERMITTED)
#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:52
static struct net_device * netdev
Definition: gdbudp.c:52
uint8_t len
Value length.
Definition: eap.h:59
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:55
void chap_finish(struct chap_response *chap)
Free resources used by a CHAP response.
Definition: chap.c:123
CHAP protocol.
A network device.
Definition: netdevice.h:353
unsigned char uint8_t
Definition: stdint.h:10
size_t response_len
Length of CHAP response.
Definition: chap.h:27
static void chap_set_identifier(struct chap_response *chap, unsigned int identifier)
Add identifier data to the CHAP challenge.
Definition: chap.h:47
char name[NETDEV_NAME_LEN]
Name of this network device.
Definition: netdevice.h:363
uint8_t value[0]
Value.
Definition: eap.h:61
#define MD5_DIGEST_SIZE
MD5 digest size.
Definition: md5.h:73
struct net_device * netdev
Network device.
Definition: eap.h:141
MD5 algorithm.
void chap_update(struct chap_response *chap, const void *data, size_t len)
Add data to the CHAP challenge.
Definition: chap.c:86
String functions.
Extensible Authentication Protocol.
struct digest_algorithm md5_algorithm
MD5 algorithm.
Definition: md5.c:287
An EAP method.
Definition: eap.h:178