/// <summary> /// Format data for sending to client and assign an event handler for the Completed event /// </summary> /// <param name="ResponseCode"></param> /// <param name="Data"></param> internal void SendData(ServerResponseCode ResponseCode, string Data) { // Get a new SocketAsyncEventArgs object: SocketAsyncEventArgs e = new SocketAsyncEventArgs(); // Create response and buffer it: byte[] buffer = ServerResponse(ResponseCode, Data); e.SetBuffer(buffer, 0, buffer.Length); // Send a reference to this object for use in next callback: e.UserToken = this; // Store our last response: LastServerResponse = ResponseCode; // Assign delegate to handle the Completed event switch (ResponseCode) { case ServerResponseCode.None: throw new ArgumentException(); case ServerResponseCode.DISCON: case ServerResponseCode.SERVICE_NOT_AVAILABLE: e.Completed += this.rejectConnectionDelegate; break; default: e.Completed += this.DataSentDelegate; break; } // Blast that data down the wire yo: m_WorkerSocket.SendAsync(e); }
/// <summary> /// Server-response-code generator: /// </summary> /// <param name="ResponseCode"></param> /// <param name="Data"></param> /// <returns></returns> private static byte[] ServerResponse(ServerResponseCode ResponseCode, string Data) { if (Data != "") { Data = " " + Data; // this will be ok for null too won't it? } return(Encoding.UTF8.GetBytes( (int)ResponseCode + " " + ResponseCode.ToString() + Data + "\r\n")); }
private void OnCmdSaveTo(bool isReregister) { List <LedRegistationInfo> list = new List <LedRegistationInfo>(); ServerResponseCode strResult = ServerResponseCode.ScreenAlreadyExists; foreach (DataOneRegistationInfo oneInfo in Uc_CareRegisters) { if (string.IsNullOrEmpty(oneInfo.Led_name)) { strResult = ServerResponseCode.SnEmpty; list.Clear(); break; } list.Add(GetLedRegistationInfo(oneInfo)); } if (list.Count > 0) { if (isReregister) { foreach (LedRegistationInfo regInfo in list) { regInfo.IsReregistering = true; } } strResult = MonitorAllConfig.Instance().SaveResgiterTo(UserId, list, list[0].IsReregistering); if (strResult == ServerResponseCode.ScreenRegisteredSuccessfully) { foreach (LedRegistationInfo regInfo in list) { DataOneRegistationInfo oneInfo = Uc_CareRegisters.Find(a => a.Sn == regInfo.sn_num); LedRegistationInfoResponse ledReg = MonitorAllConfig.Instance().LedRegistationUiList.Find(a => a.SN == regInfo.sn_num); ledReg.IsRegisted = true; ledReg.Name = oneInfo.Led_name; ledReg.User = UserId; ledReg.Latitude = oneInfo.Latitude; ledReg.Longitude = oneInfo.Longitude; oneInfo.IsRegister = true; oneInfo.UserId = UserId; } MonitorAllConfig.Instance().LedRegistationInfoEventMethod(false); } } Messenger.Default.Send <string>(strResult.ToString(), MsgToken.MSG_NOTIFYDIALOG_OK); }
/// <summary> /// The verify license. /// </summary> /// <param name="responseCode"> /// The response code. /// </param> /// <param name="signedData"> /// The signed data. /// </param> /// <param name="signature"> /// The signature. /// </param> public void VerifyLicense(ServerResponseCode responseCode, string signedData, string signature) { Parcel data = Parcel.Obtain(); try { data.WriteInterfaceToken(Descriptor); data.WriteInt((int)responseCode); data.WriteString(signedData); data.WriteString(signature); this.remote.Transact(TransactionVerifyLicense, data, null, TransactionFlags.Oneway); } finally { data.Recycle(); } }
/// <summary> /// Runs in IPC thread pool. Post it to the Handler, so we can guarantee /// either this or the timeout runs. /// </summary> /// <param name="responseCode"> /// The response code from the server. /// </param> /// <param name="signedData"> /// The data from the server. /// </param> /// <param name="signature"> /// The signature to use to verify the data. /// </param> public override void VerifyLicense(ServerResponseCode responseCode, string signedData, string signature) { this.checker.handler.Post( delegate { Debug.WriteLine("Received license response. " + responseCode); // Make sure it hasn't already timed out. if (this.checker.checksInProgress.Contains(this.licenseValidator)) { this.ClearTimeout(); this.licenseValidator.Verify( this.checker.publicKey, responseCode, signedData, signature); this.checker.FinishCheck(this.licenseValidator); } // Write debug data to the log this.DebugServerResponseCode(responseCode); }); }
private void DebugServerResponseCode(ServerResponseCode responseCode) { bool logResponse; string stringError = null; switch (responseCode) { case ServerResponseCode.ErrorContactingServer: logResponse = true; stringError = "ERROR_CONTACTING_SERVER"; break; case ServerResponseCode.InvalidPackageName: logResponse = true; stringError = "ERROR_INVALID_PACKAGE_NAME"; break; case ServerResponseCode.NonMatchingUid: logResponse = true; stringError = "ERROR_NON_MATCHING_UID"; break; default: logResponse = false; break; } if (logResponse) { string androidId = Settings.Secure.GetString( this.checker.context.ContentResolver, Settings.Secure.AndroidId); Debug.WriteLine("License Server Failure: " + stringError); Debug.WriteLine("Android ID: " + androidId); Debug.WriteLine("Time: " + DateTime.Now); } }
/// <summary> /// The verify license. /// </summary> /// <param name="responseCode"> /// The response code. /// </param> /// <param name="signedData"> /// The signed data. /// </param> /// <param name="signature"> /// The signature. /// </param> public abstract void VerifyLicense(ServerResponseCode responseCode, string signedData, string signature);
/// <summary> /// Format data for sending to client and assign an event handler for the Completed event /// </summary> /// <param name="ResponseCode"></param> /// <param name="Data"></param> internal void SendData(ServerResponseCode ResponseCode) { SendData(ResponseCode, ""); }
/// <summary> /// Verifies the response from server and calls appropriate callback method. /// </summary> /// <param name="publicKey"> /// public key associated with the developer account /// </param> /// <param name="responseCode"> /// server response code /// </param> /// <param name="signedData"> /// signed data from server /// </param> /// <param name="signature"> /// server signature /// </param> public void Verify(IPublicKey publicKey, ServerResponseCode responseCode, string signedData, string signature) { string userId = null; // Skip signature check for unsuccessful requests ResponseData data = null; if (responseCode == ServerResponseCode.Licensed || responseCode == ServerResponseCode.NotLicensed || responseCode == ServerResponseCode.LicensedOldKey) { // Verify signature. try { Signature sig = Signature.GetInstance(SignatureAlgorithm); sig.InitVerify(publicKey); sig.Update(new Java.Lang.String(signedData).GetBytes()); if (!sig.Verify(Convert.FromBase64String(signature))) { System.Diagnostics.Debug.WriteLine("Signature verification failed."); this.HandleInvalidResponse(); return; } } catch (NoSuchAlgorithmException e) { // This can't happen on an Android compatible device. throw new RuntimeException(e); } catch (InvalidKeyException) { this.HandleApplicationError(CallbackErrorCode.InvalidPublicKey); return; } catch (SignatureException e) { throw new RuntimeException(e); } catch (FormatException) { System.Diagnostics.Debug.WriteLine("Could not Base64-decode signature."); this.HandleInvalidResponse(); return; } // Parse and validate response. try { data = ResponseData.Parse(signedData); } catch (IllegalArgumentException) { System.Diagnostics.Debug.WriteLine("Could not parse response."); this.HandleInvalidResponse(); return; } if (data.ResponseCode != responseCode) { System.Diagnostics.Debug.WriteLine("Response codes don't match."); this.HandleInvalidResponse(); return; } if (data.NumberUsedOnce != this.numberUsedOnce) { System.Diagnostics.Debug.WriteLine("NumberUsedOnce doesn't match."); this.HandleInvalidResponse(); return; } if (data.PackageName != this.packageName) { System.Diagnostics.Debug.WriteLine("Package name doesn't match."); this.HandleInvalidResponse(); return; } if (data.VersionCode != this.versionCode) { System.Diagnostics.Debug.WriteLine("Version codes don't match."); this.HandleInvalidResponse(); return; } // Application-specific user identifier. userId = data.UserId; if (string.IsNullOrEmpty(userId)) { System.Diagnostics.Debug.WriteLine("User identifier is empty."); this.HandleInvalidResponse(); return; } } switch (responseCode) { case ServerResponseCode.Licensed: case ServerResponseCode.LicensedOldKey: PolicyServerResponse limiterResponse = this.deviceLimiter.IsDeviceAllowed(userId); this.HandleResponse(limiterResponse, data); break; case ServerResponseCode.NotLicensed: this.HandleResponse(PolicyServerResponse.NotLicensed, data); break; case ServerResponseCode.ErrorContactingServer: System.Diagnostics.Debug.WriteLine("Error contacting licensing server."); this.HandleResponse(PolicyServerResponse.Retry, data); break; case ServerResponseCode.ServerFailure: System.Diagnostics.Debug.WriteLine("An error has occurred on the licensing server."); this.HandleResponse(PolicyServerResponse.Retry, data); break; case ServerResponseCode.OverQuota: System.Diagnostics.Debug.WriteLine( "Licensing server is refusing to talk to this device, over quota."); this.HandleResponse(PolicyServerResponse.Retry, data); break; case ServerResponseCode.InvalidPackageName: this.HandleApplicationError(CallbackErrorCode.InvalidPackageName); break; case ServerResponseCode.NonMatchingUid: this.HandleApplicationError(CallbackErrorCode.ErrorNonMatchingUid); break; case ServerResponseCode.NotMarketManaged: this.HandleApplicationError(CallbackErrorCode.NotMarketManaged); break; default: System.Diagnostics.Debug.WriteLine("Unknown response code for license check."); this.HandleInvalidResponse(); break; } }
/// <summary> /// Server-response-code generator: /// </summary> /// <param name="ResponseCode"></param> /// <param name="Data"></param> /// <returns></returns> private static byte[] ServerResponse(ServerResponseCode ResponseCode, string Data) { if (Data != "") Data = " " + Data; // this will be ok for null too won't it? return Encoding.UTF8.GetBytes( (int)ResponseCode + " " + ResponseCode.ToString() + Data + "\r\n"); }