www.pudn.com > agsm2-1.2_src.zip > AuthPage.cpp


// AuthPage.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "agsm2.h" 
#include "AuthPage.h" 
#include "PcScCtrl.h" 
#include "agsm2Dlg.h" 
#include "Helper.h" 
#include "ref/aka.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CGsmAuthPage property page 
 
IMPLEMENT_DYNCREATE(CGsmAuthPage, CPropertyPage) 
 
CGsmAuthPage::CGsmAuthPage() : CPropertyPage(CGsmAuthPage::IDD) 
{ 
	//{{AFX_DATA_INIT(CGsmAuthPage) 
	m_editIMSI = _T(""); 
	m_Rand = _T(""); 
	m_SRES = _T(""); 
	m_Kc = _T(""); 
	//}}AFX_DATA_INIT 
} 
 
CGsmAuthPage::~CGsmAuthPage() 
{ 
} 
 
void CGsmAuthPage::DoDataExchange(CDataExchange* pDX) 
{ 
	CPropertyPage::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CGsmAuthPage) 
	DDX_Text(pDX, IDC_IMSI, m_editIMSI); 
	DDX_Text(pDX, IDC_EDIT_RAND, m_Rand); 
	DDX_Text(pDX, IDC_EDIT_SRES, m_SRES); 
	DDX_Text(pDX, IDC_EDIT_KC, m_Kc); 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(CGsmAuthPage, CPropertyPage) 
	//{{AFX_MSG_MAP(CGsmAuthPage) 
	ON_BN_CLICKED(IDC_ReadIMSI, OnReadIMSI) 
	ON_BN_CLICKED(IDC_GUID_GEN, OnGuidGen) 
	ON_BN_CLICKED(IDC_BUTTON_RUN_GSM_ALGORITHM, OnButtonRunGsmAlgorithm) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CGsmAuthPage message handlers 
void CGsmAuthPage::OnGuidGen()  
{ 
	// TODO: Add your control notification handler code here 
	// create random GUID 
	m_guid = GUID_NULL; 
	::CoCreateGuid(&m_guid); 
	if (m_guid == GUID_NULL) 
	{ 
		AfxMessageBox("Unable to create new GUID."); 
		return; 
	} 
	CString str; 
	str.Format("%08lX%04X%04x%02X%02X%02X%02X%02X%02X%02X%02X", 
					m_guid.Data1, m_guid.Data2, m_guid.Data3, 
					m_guid.Data4[0], m_guid.Data4[1], m_guid.Data4[2], m_guid.Data4[3], 
					m_guid.Data4[4], m_guid.Data4[5], m_guid.Data4[6], m_guid.Data4[7]); 
	TRACE(str); 
	m_Rand = str; 
	UpdateData(FALSE); 
 
} 
 
void CGsmAuthPage::OnButtonRunGsmAlgorithm()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(); 
	if(m_Rand.IsEmpty()) 
	{ 
		AfxMessageBox("Random must be format like this:\r\n{584A529C-3131-4f75-8EDC-F13CC14D4EC8}"); 
		return; 
	} 
	UCHAR RAND[20]; 
	int ret = sscanf(m_Rand,"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02x%02x", 
					&RAND[0], &RAND[1], &RAND[2],&RAND[3], 
					&RAND[4], &RAND[5], &RAND[6],&RAND[7], 
					&RAND[8], &RAND[9], &RAND[10],&RAND[11], 
					&RAND[12], &RAND[13], &RAND[14],&RAND[15]); 
 
    if(ret != 16) 
	{ 
		AfxMessageBox("Random must be 16 Byte(Hex)"); 
		return; 
	} 
 
	CPcScCtrl &ScReader = ((CAgsm2Dlg *)(AfxGetApp()->m_pMainWnd))->m_SCReader; 
	BYTE SRESKc[256]; 
	RESAPDU resApdu; 
	resApdu.data = SRESKc; 
	resApdu.len = 256; 
	try{ 
		ScReader.m_bGetRes = FALSE; 
		ScReader.Select(0x3f00,resApdu); 
		ScReader.Select(0x7f20,resApdu); 
		ScReader.m_bGetRes = TRUE; 
		ScReader.RunGsmAlgorithm((LPBYTE)&RAND[0],16,resApdu); 
	}catch(LONG e) 
	{ 
		if((e & 0xfffff000) == 0x6000 || (e & 0xfffff000) == 0x9000) 
			AfxMessageBox(ScReader.FormatErrMsg(e)); 
		else 
			Helper::ShowLastError(e); 
	} 
	CString SRES,Kc; 
	SRES.Format("%02X%02x%02x%02x", 
				SRESKc[0],SRESKc[1],SRESKc[2],SRESKc[3]); 
	Kc.Format("%02X%02x%02x%02x%02X%02x%02x%02x", 
				SRESKc[4],SRESKc[5],SRESKc[6],SRESKc[7], 
				SRESKc[8],SRESKc[9],SRESKc[10],SRESKc[11]); 
	m_SRES = (LPCTSTR)SRES; 
	m_Kc = (LPCTSTR)Kc; 
	UpdateData(FALSE); 
} 
 
void CGsmAuthPage::OnReadIMSI()  
{ 
	// TODO: Add your control notification handler code here 
	BYTE ResData[255]; 
	RESAPDU resApdu; 
	resApdu.data = ResData; 
	resApdu.len = sizeof(ResData); 
	CPcScCtrl &ScReader = ((CAgsm2Dlg *)(AfxGetApp()->m_pMainWnd))->m_SCReader; 
 
	try{ 
		ScReader.m_bGetRes = FALSE; 
		ScReader.Select(0x3f00,resApdu); 
		resApdu.len = sizeof(ResData); 
		ScReader.Select(0x7f20,resApdu); 
		resApdu.len = sizeof(ResData); 
		ScReader.Select(0x6f07,resApdu); 
		ScReader.m_bGetRes = TRUE; 
	 
		resApdu.len = 9 + 2; 
		ScReader.ReadBinary(0,0,resApdu); 
		CString str = Helper::BcdToAscii(ResData+1, ResData[0]); 
		str.Delete(0); 
		m_editIMSI = str; 
		UpdateData(FALSE); 
	}catch(LONG e) 
	{ 
		if((e & 0xfffff000) == 0x6000 || (e & 0xfffff000) == 0x9000) 
			AfxMessageBox(ScReader.FormatErrMsg(e)); 
		else 
			Helper::ShowLastError(e); 
	} 
	 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// C3GPPAuthPage property page 
 
IMPLEMENT_DYNCREATE(C3GPPAuthPage, CPropertyPage) 
 
C3GPPAuthPage::C3GPPAuthPage() : CPropertyPage(C3GPPAuthPage::IDD) 
{ 
	//{{AFX_DATA_INIT(C3GPPAuthPage) 
	m_editIMSI = _T(""); 
	m_Rand = _T(""); 
	m_CK = _T(""); 
	m_IK = _T(""); 
	m_RES = _T(""); 
	m_useBuiltInAKA = FALSE; 
	m_sqn = _T(""); 
	//}}AFX_DATA_INIT 
} 
 
C3GPPAuthPage::~C3GPPAuthPage() 
{ 
} 
 
void C3GPPAuthPage::DoDataExchange(CDataExchange* pDX) 
{ 
	CPropertyPage::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(C3GPPAuthPage) 
	DDX_Control(pDX, IDC_BUTTON_RUN_3GPP_AKA, m_RunAKA); 
	DDX_Control(pDX, IDC_CHECK_USE_BUILTIN, m_useBuiltIn); 
	DDX_Text(pDX, IDC_IMSI, m_editIMSI); 
	DDX_Text(pDX, IDC_EDIT_RAND, m_Rand); 
	DDV_MaxChars(pDX, m_Rand, 32); 
	DDX_Text(pDX, IDC_EDIT_CK, m_CK); 
	DDV_MaxChars(pDX, m_CK, 32); 
	DDX_Text(pDX, IDC_EDIT_IK, m_IK); 
	DDV_MaxChars(pDX, m_IK, 32); 
	DDX_Text(pDX, IDC_EDIT_RES, m_RES); 
	DDV_MaxChars(pDX, m_RES, 32); 
	DDX_Check(pDX, IDC_CHECK_USE_BUILTIN, m_useBuiltInAKA); 
	DDX_Text(pDX, IDC_EDIT_SQN, m_sqn); 
	DDV_MaxChars(pDX, m_sqn, 12); 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(C3GPPAuthPage, CPropertyPage) 
	//{{AFX_MSG_MAP(C3GPPAuthPage) 
	ON_BN_CLICKED(IDC_ReadIMSI, OnReadIMSI) 
	ON_BN_CLICKED(IDC_GUID_GEN, OnGuidGen) 
	ON_BN_CLICKED(IDC_BUTTON_RUN_3GPP_AKA, OnButtonRun3gppAka) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// C3GPPAuthPage message handlers 
 
 
 
void C3GPPAuthPage::OnReadIMSI()  
{ 
	// TODO: Add your control notification handler code here 
	BYTE ResData[255]; 
	RESAPDU resApdu; 
	resApdu.data = ResData; 
	resApdu.len = sizeof(ResData); 
	CPcScCtrl &ScReader = ((CAgsm2Dlg *)(AfxGetApp()->m_pMainWnd))->m_SCReader; 
 
	try{ 
		ScReader.m_bGetRes = FALSE; 
		ScReader.Select(0x3f00,resApdu); 
		resApdu.len = sizeof(ResData); 
		ScReader.Select(0x7f20,resApdu); 
		resApdu.len = sizeof(ResData); 
		ScReader.Select(0x6f07,resApdu); 
		ScReader.m_bGetRes = TRUE; 
	 
		resApdu.len = 9 + 2; 
		ScReader.ReadBinary(0,0,resApdu); 
		CString str = Helper::BcdToAscii(ResData+1, ResData[0]); 
		str.Delete(0); 
		m_editIMSI = str; 
		UpdateData(FALSE); 
	}catch(LONG e) 
	{ 
		if((e & 0xfffff000) == 0x6000 || (e & 0xfffff000) == 0x9000) 
			AfxMessageBox(ScReader.FormatErrMsg(e)); 
		else 
			Helper::ShowLastError(e); 
	} 
	 
} 
static uchar K[]={0xad,0x1b,0x5a,0x15,0x9b,0xe8,0x6b,0x2c, 
					0xa6,0x6c,0x7a,0xe4,0x0b,0xba,0x9b,0x9d}; 
static uchar seed[]={0xb0,0xab,0xb9,0x9d,0x6a,0xc6,0xa7,0x4e, 
					 0xb9,0x8e,0xb6,0xc2,0xda,0xb1,0xa5,0x51}; 
static uchar Fmk[L_FMK] = { 'A', 'H', 'A', 'G' }; 
static uchar AMF[2] = {0,1}; 
static uchar SQNhe[L_SQN]={0x00,0x00,0x00,0x00,0x00,0x00}; 
static uchar fi0=0x41; 
static uchar fi1=0x42; 
static uchar fi1star=0x43; 
static uchar fi2=0x44; 
static uchar fi3=0x45; 
static uchar fi4=0x46; 
static uchar fi5=0x47; 
static uchar fi5star=0x48; 
static uchar RAND[L_RAND]; 
 
void C3GPPAuthPage::OnGuidGen()  
{ 
	// TODO: Add your control notification handler code here 
	f0(seed,fi0,Fmk,RAND); 
	f0(seed,fi0,Fmk,&RAND[L_RAND/2]); 
 
	char str[L_RAND*2+1]; 
	char *buf = str; 
	int i; 
	for(i=0; im_pMainWnd))->m_SCReader; 
	LONG lResult = ScReader.SCardControl(IOCTL_USCR_CCID, 
						cmd,sizeof(cmd),Res,64,&ret); 
	if(lResult == 0) 
	{ 
		char str[256]; 
		if(ret == 2) 
		{ 
			AfxMessageBox(ScReader.FormatErrMsg(Res)); 
		}else if(ret == 16 && Res[0] == 0xdc && Res[1] == 14) 
		{//Recv AUTS 
			//verify MAC 
			f5star(K,fi5star,RAND,Fmk,AK); 
			uchar SQNms[L_SQN]; 
			int i; 
			uchar *p = Res+2; 
			for(i=0; i= 44 && Res[0] == 0xdb) 
		{ 
			char *s = str; 
			uchar *p = Res+2; 
			for(i=0; im_pMainWnd))->m_SCReader; 
	int n = ScReader.m_asReaderNames.GetSize(); 
	if(n <=0 ) 
		return TRUE; 
	if(strstr(ScReader.m_asReaderNames[ScReader.GetCurReader()],"ED11")) 
	{ 
		m_useBuiltIn.EnableWindow(); 
		m_useBuiltInAKA = TRUE; 
		m_RunAKA.EnableWindow(); 
		UpdateData(FALSE); 
	}else{ 
		m_useBuiltIn.EnableWindow(FALSE); 
		m_RunAKA.EnableWindow(FALSE); 
	} 
 
	return CPropertyPage::OnSetActive(); 
}