private void GetDongleUserInfo() { if (this.dongleInfo.IsNullOrEmpty()) { return; } int size = IntPtrUtil.SizeOf(typeof(DongleUserInfoStru)); byte[] buffer = new byte[size]; for (int seq = 0; seq < this.dongleInfo.Length; seq++) { if (IsEmptyKey(this.dongleInfo[seq])) { continue; } DONGLE_HANDLER __hDongle = -1; if (Dongle_Open(ref __hDongle, seq) == SUCC) { if (Dongle_ReadFile(__hDongle, DongleConst.USER_INFO_DESCRIPTOR, 0, buffer, buffer.Length) == SUCC) { this.dongleInfo[seq].UserInfo = ParseDongleKeyInfo(buffer); } Dongle_Close(__hDongle); } } }
private bool Authen(DONGLE_HANDLER hDongle, DongleAuthenMode authenMode, byte[] pin) { uint flag = (authenMode == DongleAuthenMode.ADMIN) ? (uint)1 : (uint)0; int remainCount; this.lastErrorCode = Dongle_VerifyPIN(hDongle, flag, pin, out remainCount); return(IsSucc); }
/// <summary> /// unique Key, and login with new admin pin /// </summary> private bool Initialize(DONGLE_HANDLER hDongle, String userId, out byte[] newAdminPin, out byte[] appId) { // unique key if (!unique(hDongle, out newAdminPin, out appId)) { return(false); } // re-authen if (!Authen(hDongle, DongleAuthenMode.ADMIN, newAdminPin)) { return(false); } this.lastErrorCode = Dongle_SetUserID(hDongle, ToUint32(userId)); return(true); }
private bool CreateUserInfoFile(DONGLE_HANDLER hDongle) { DATA_FILE_ATTR dataAttr; dataAttr.m_Size = DongleConst.USER_INFO_FILE_LEN; //allow anonymous read dataAttr.m_Lic.m_Read_Priv = 0; //only admin could write dataAttr.m_Lic.m_Write_Priv = 2; IntPtr ptr = IntPtrUtil.CreateByStru(dataAttr); this.lastErrorCode = Dongle_CreateFile(hDongle, RockeyArmFileType.FILE_DATA, DongleConst.USER_INFO_DESCRIPTOR, ptr); IntPtrUtil.Free(ref ptr); return(IsSucc); }
public RockeyArmDongle(String encodingName, String seed, String errMsgFileName) { this.encoder = Encoding.GetEncoding(encodingName); if (String.IsNullOrEmpty(seed)) { seed = DongleConst.DEFAULT_SEED_KEY; } this.seed = this.Encoder.GetBytes(seed); if (!String.IsNullOrEmpty(errMsgFileName)) { this.errorMsgProp = new Properties(errMsgFileName); } this.lastErrorCode = 0; this.selectedIndex = -1; this.hDongle = -1; }
/// <summary> /// initialize a empty dongle /// after invoke SUCC,status of key is anonymous /// </summary> private bool unique(DONGLE_HANDLER hDongle, out byte[] newAdminPwd, out byte[] appId) { appId = new byte[8]; newAdminPwd = new byte[16]; logger.Debug("Initialize Key , seed = " + BitConverter.ToString(this.seed)); //unique key, requst admin privilege if (!Authen(hDongle, DongleAuthenMode.ADMIN, this.encoder.GetBytes(this.defaultAdminPin))) { logger.Debug("Authen failed use default admin pin!"); return(false); } this.lastErrorCode = Dongle_GenUniqueKey(hDongle, seed.Length, seed, appId, newAdminPwd); logger.Debug("new pid = " + this.Encoder.GetString(appId)); logger.Debug("new admin pwd = " + this.Encoder.GetString(newAdminPwd)); return(IsSucc); }
/// <summary> /// create RSA private key file /// </summary> private bool CreateAuthenKeyFile(DONGLE_HANDLER hDongle) { PRIKEY_FILE_ATTR priAttr = new PRIKEY_FILE_ATTR(); priAttr.m_Size = DongleConst.RSA_KEY_LEN * 8; priAttr.m_Type = RockeyArmFileType.FILE_PRIKEY_RSA; priAttr.m_Lic.m_Count = 0xFFFFFFFF; // un-limitted priAttr.m_Lic.m_IsDecOnRAM = 0; priAttr.m_Lic.m_IsReset = 0; priAttr.m_Lic.m_Priv = 0; IntPtr ptr = IntPtrUtil.CreateByStru(priAttr); try { this.lastErrorCode = Dongle_CreateFile(hDongle, RockeyArmFileType.FILE_PRIKEY_RSA, DongleConst.AUTHEN_KEY_DESCRIPTOR, ptr); } finally { IntPtrUtil.Free(ref ptr); } return(IsSucc); }
public bool CreateUserInfo(DONGLE_HANDLER hDongle, DongleUserInfo userInfo) { if (!CreateUserInfoFile(hDongle)) { return(false); } DongleUserInfoStru stru = CreateUserInfoStru(userInfo); IntPtr ptr = IntPtrUtil.CreateByStru(stru); byte[] dest = new byte[IntPtrUtil.SizeOf(stru.GetType())]; Marshal.Copy(ptr, dest, 0, dest.Length); this.lastErrorCode = Dongle_WriteFile( hDongle, RockeyArmFileType.FILE_DATA, DongleConst.USER_INFO_DESCRIPTOR, 0, dest, dest.Length); IntPtrUtil.Free(ref ptr); return(IsSucc); }
/// <summary> /// create User root key file,and Key is request 16 bytes at least /// </summary> private bool CreateUserRootKeyFile(DONGLE_HANDLER hDongle, ushort fileDescriptor, byte[] userRootkey) { KEY_FILE_ATTR keyAttr = new KEY_FILE_ATTR(); keyAttr.m_Size = 16; keyAttr.m_Lic.m_Priv_Enc = 0; IntPtr ptr = IntPtrUtil.CreateByStru(keyAttr); this.lastErrorCode = Dongle_CreateFile(hDongle, RockeyArmFileType.FILE_KEY, fileDescriptor, ptr); IntPtrUtil.Free(ref ptr); if (!IsSucc) { return(false); } // dongle key require 16 bytes key logger.Debug(String.Format("Create Key file, descriptor={0}, key={1}", fileDescriptor, BitConverter.ToString(userRootkey))); this.lastErrorCode = Dongle_WriteFile(hDongle, RockeyArmFileType.FILE_KEY, fileDescriptor, 0, userRootkey, 16); return(IsSucc); }
static extern uint Dongle_RsaGenPubPriKey(DONGLE_HANDLER hDongle, ushort descriptor, IntPtr pubKey, IntPtr priKey);
static extern uint Dongle_VerifyPIN(DONGLE_HANDLER hDongle, uint flag, byte[] pin, out int pRemainCount);
static extern uint Dongle_RFS(DONGLE_HANDLER hDongle);
static extern uint Dongle_ResetState(DONGLE_HANDLER hDongle);
static extern uint Dongle_SetUserID(DONGLE_HANDLER hDongle, uint dwUserID);
static extern uint Dongle_LEDControl(DONGLE_HANDLER hDongle, uint flag);
static extern uint Dongle_GenUniqueKey(DONGLE_HANDLER hDongle, int seedLen, byte[] seed, byte[] appId, byte[] newAdminPin);
static extern uint Dongle_GetUTCTime(DONGLE_HANDLER hDongle, ref uint pdwUTCTime);
static extern uint Dongle_Close(DONGLE_HANDLER hDongle);
static extern uint Dongle_CreateFile(DONGLE_HANDLER hDongle, uint fileType, ushort descriptor, IntPtr pFileAttr);
static extern uint Dongle_TDES(DONGLE_HANDLER hDongle, ushort descriptor, uint flag, byte[] plain, byte[] cipher, uint len);
static extern uint Dongle_ReadFile(DONGLE_HANDLER hDongle, ushort descriptor, short offset, byte[] buffer, int length);
static extern uint Dongle_RsaPri(DONGLE_HANDLER hDongle, ushort descriptor, uint flag, byte[] plain, uint plainLen, byte[] cipher, ref uint cipherLen);
static extern uint Dongle_Open(ref DONGLE_HANDLER hDongle, int seq);