wpa_supplicant / hostapd  2.5
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Data Structures | Macros | Enumerations | Functions
pcsc_funcs.c File Reference

WPA Supplicant / PC/SC smartcard interface for USIM, GSM SIM. More...

#include "includes.h"
#include <winscard.h>
#include "common.h"
#include "pcsc_funcs.h"

Data Structures

struct  scard_data
 

Macros

#define SIM_CMD_SELECT   0xa0, 0xa4, 0x00, 0x00, 0x02
 
#define SIM_CMD_RUN_GSM_ALG   0xa0, 0x88, 0x00, 0x00, 0x10
 
#define SIM_CMD_GET_RESPONSE   0xa0, 0xc0, 0x00, 0x00
 
#define SIM_CMD_READ_BIN   0xa0, 0xb0, 0x00, 0x00
 
#define SIM_CMD_READ_RECORD   0xa0, 0xb2, 0x00, 0x00
 
#define SIM_CMD_VERIFY_CHV1   0xa0, 0x20, 0x00, 0x01, 0x08
 
#define USIM_CLA   0x00
 
#define USIM_CMD_RUN_UMTS_ALG   0x00, 0x88, 0x00, 0x81, 0x22
 
#define USIM_CMD_GET_RESPONSE   0x00, 0xc0, 0x00, 0x00
 
#define SIM_RECORD_MODE_ABSOLUTE   0x04
 
#define USIM_FSP_TEMPL_TAG   0x62
 
#define USIM_TLV_FILE_DESC   0x82
 
#define USIM_TLV_FILE_ID   0x83
 
#define USIM_TLV_DF_NAME   0x84
 
#define USIM_TLV_PROPR_INFO   0xA5
 
#define USIM_TLV_LIFE_CYCLE_STATUS   0x8A
 
#define USIM_TLV_FILE_SIZE   0x80
 
#define USIM_TLV_TOTAL_FILE_SIZE   0x81
 
#define USIM_TLV_PIN_STATUS_TEMPLATE   0xC6
 
#define USIM_TLV_SHORT_FILE_ID   0x88
 
#define USIM_TLV_SECURITY_ATTR_8B   0x8B
 
#define USIM_TLV_SECURITY_ATTR_8C   0x8C
 
#define USIM_TLV_SECURITY_ATTR_AB   0xAB
 
#define USIM_PS_DO_TAG   0x90
 
#define AKA_RAND_LEN   16
 
#define AKA_AUTN_LEN   16
 
#define AKA_AUTS_LEN   14
 
#define RES_MAX_LEN   16
 
#define IK_LEN   16
 
#define CK_LEN   16
 
#define SCARD_FILE_MF   0x3F00
 
#define SCARD_FILE_GSM_DF   0x7F20
 
#define SCARD_FILE_UMTS_DF   0x7F50
 
#define SCARD_FILE_GSM_EF_IMSI   0x6F07
 
#define SCARD_FILE_GSM_EF_AD   0x6FAD
 
#define SCARD_FILE_EF_DIR   0x2F00
 
#define SCARD_FILE_EF_ICCID   0x2FE2
 
#define SCARD_FILE_EF_CK   0x6FE1
 
#define SCARD_FILE_EF_IK   0x6FE2
 
#define SCARD_CHV1_OFFSET   13
 
#define SCARD_CHV1_FLAG   0x80
 
#define mingw_load_symbols()   0
 
#define mingw_unload_symbols()   do { } while (0)
 

Enumerations

enum  sim_types { SCARD_GSM_SIM, SCARD_USIM }
 

Functions

struct scard_datascard_init (const char *reader)
 Initialize SIM/USIM connection using PC/SC. More...
 
int scard_set_pin (struct scard_data *scard, const char *pin)
 Set PIN (CHV1/PIN1) code for accessing SIM/USIM commands. More...
 
void scard_deinit (struct scard_data *scard)
 Deinitialize SIM/USIM connection. More...
 
int scard_get_pin_retry_counter (struct scard_data *scard)
 
int scard_get_imsi (struct scard_data *scard, char *imsi, size_t *len)
 Read IMSI from SIM/USIM card. More...
 
int scard_get_mnc_len (struct scard_data *scard)
 Read length of MNC in the IMSI from SIM/USIM card. More...
 
int scard_gsm_auth (struct scard_data *scard, const unsigned char *_rand, unsigned char *sres, unsigned char *kc)
 Run GSM authentication command on SIM card. More...
 
int scard_umts_auth (struct scard_data *scard, const unsigned char *_rand, const unsigned char *autn, unsigned char *res, size_t *res_len, unsigned char *ik, unsigned char *ck, unsigned char *auts)
 Run UMTS authentication command on USIM card. More...
 
int scard_supports_umts (struct scard_data *scard)
 

Detailed Description

WPA Supplicant / PC/SC smartcard interface for USIM, GSM SIM.

This file implements wrapper functions for accessing GSM SIM and 3GPP USIM cards through PC/SC smartcard library. These functions are used to implement authentication routines for EAP-SIM and EAP-AKA.

Function Documentation

void scard_deinit ( struct scard_data scard)

Deinitialize SIM/USIM connection.

Parameters
scardPointer to private data from scard_init()

This function closes the SIM/USIM connect opened with scard_init().

int scard_get_imsi ( struct scard_data scard,
char *  imsi,
size_t *  len 
)

Read IMSI from SIM/USIM card.

Parameters
scardPointer to private data from scard_init()
imsiBuffer for IMSI
lenLength of imsi buffer; set to IMSI length on success
Returns
0 on success, -1 if IMSI file cannot be selected, -2 if IMSI file selection returns invalid result code, -3 if parsing FSP template file fails (USIM only), -4 if IMSI does not fit in the provided imsi buffer (len is set to needed length), -5 if reading IMSI file fails.

This function can be used to read IMSI from the SIM/USIM card. If the IMSI file is PIN protected, scard_set_pin() must have been used to set the correct PIN code before calling scard_get_imsi().

int scard_get_mnc_len ( struct scard_data scard)

Read length of MNC in the IMSI from SIM/USIM card.

Parameters
scardPointer to private data from scard_init()
Returns
length (>0) on success, -1 if administrative data file cannot be selected, -2 if administrative data file selection returns invalid result code, -3 if parsing FSP template file fails (USIM only), -4 if length of the file is unexpected, -5 if reading file fails, -6 if MNC length is not in range (i.e. 2 or 3), -7 if MNC length is not available.
int scard_gsm_auth ( struct scard_data scard,
const unsigned char *  _rand,
unsigned char *  sres,
unsigned char *  kc 
)

Run GSM authentication command on SIM card.

Parameters
scardPointer to private data from scard_init()
_rand16-byte RAND value from HLR/AuC
sres4-byte buffer for SRES
kc8-byte buffer for Kc
Returns
0 on success, -1 if SIM/USIM connection has not been initialized, -2 if authentication command execution fails, -3 if unknown response code for authentication command is received, -4 if reading of response fails, -5 if if response data is of unexpected length

This function performs GSM authentication using SIM/USIM card and the provided RAND value from HLR/AuC. If authentication command can be completed successfully, SRES and Kc values will be written into sres and kc buffers.

struct scard_data* scard_init ( const char *  reader)

Initialize SIM/USIM connection using PC/SC.

Parameters
readerReader name prefix to search for
Returns
Pointer to private data structure, or NULL on failure

This function is used to initialize SIM/USIM connection. PC/SC is used to open connection to the SIM/USIM card. In addition, local flag is set if a PIN is needed to access some of the card functions. Once the connection is not needed anymore, scard_deinit() can be used to close it.

int scard_set_pin ( struct scard_data scard,
const char *  pin 
)

Set PIN (CHV1/PIN1) code for accessing SIM/USIM commands.

Parameters
scardPointer to private data from scard_init()
pinPIN code as an ASCII string (e.g., "1234")
Returns
0 on success, -1 on failure
int scard_umts_auth ( struct scard_data scard,
const unsigned char *  _rand,
const unsigned char *  autn,
unsigned char *  res,
size_t *  res_len,
unsigned char *  ik,
unsigned char *  ck,
unsigned char *  auts 
)

Run UMTS authentication command on USIM card.

Parameters
scardPointer to private data from scard_init()
_rand16-byte RAND value from HLR/AuC
autn16-byte AUTN value from HLR/AuC
res16-byte buffer for RES
res_lenVariable that will be set to RES length
ik16-byte buffer for IK
ck16-byte buffer for CK
auts14-byte buffer for AUTS
Returns
0 on success, -1 on failure, or -2 if USIM reports synchronization failure

This function performs AKA authentication using USIM card and the provided RAND and AUTN values from HLR/AuC. If authentication command can be completed successfully, RES, IK, and CK values will be written into provided buffers and res_len is set to length of received RES value. If USIM reports synchronization failure, the received AUTS value will be written into auts buffer. In this case, RES, IK, and CK are not valid.