/// <summary> /// Installs CA certificate on Inkjet printer /// </summary> /// <param name="printerIp"></param> /// <param name="file"></param> /// <param name="authorization"></param> /// <param name="nameCollection"></param> public void InstallCAFileInk(Uri printerIp, string file, string authorization, NameValueCollection nameCollection) { Uri uploadUrl = null; var fileExtension = Path.GetExtension(file); if (fileExtension != null && fileExtension.Equals(".cer", StringComparison.OrdinalIgnoreCase)) { uploadUrl = new Uri($"{ (object)printerIp}{ (object)Properties.Resources.URLInstallInkCer}"); } else { uploadUrl = new Uri($"{printerIp}{Properties.Resources.URLInstallInkPfx}"); } HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uploadUrl); webRequest.Accept = "text/html, application/xhtml+xml, */*"; webRequest.Headers.Add(HttpRequestHeader.Pragma, "no-cache"); if (!string.IsNullOrEmpty(authorization)) { webRequest.Headers.Add(HttpRequestHeader.Authorization, authorization); } try { var response = HttpWebEngine.UploadFile(webRequest, file, "certificate", nameCollection, _userAgent); if ((response.StatusCode == HttpStatusCode.OK) || (response.StatusCode == HttpStatusCode.Created)) { if (fileExtension != null && fileExtension.Equals(".cer", StringComparison.OrdinalIgnoreCase)) { ExecutionServices.SystemTrace.LogDebug("Certificate installed!"); VerifyCAUploadInk(printerIp); } else { XmlDocument xDoc = new XmlDocument(); xDoc.LoadXml(response.Response); string certError = xDoc.DocumentElement.GetElementsByTagName("cert:InterfaceError").Item(0).InnerText; if (certError.Contains("invalidCertData")) { ExecutionServices.SystemTrace.LogDebug("File is unsupported file format"); throw new SiriusInvalidOperationException("Certificate is invalid"); } } } else { ExecutionServices.SystemTrace.LogDebug("Upload Failed"); throw new WebException("Upload Failed"); } } catch (WebException ex) { ExecutionServices.SystemTrace.LogDebug("Upload Failed"); throw new WebException("Upload Failed", ex); } }
/// <summary> /// Installs CA certificate on the printer /// </summary> /// <param name="printerIp"></param> /// <param name="file"></param> /// <param name="authorization"></param> /// <param name="nameCollection"></param> public void InstallCAFilePhoenix(Uri printerIp, string file, string authorization, NameValueCollection nameCollection) { Uri uploadUrl = new Uri($"{printerIp}{Properties.Resources.URLInstallPhoenix}"); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uploadUrl); webRequest.Accept = "text/html, application/xhtml+xml, */*"; webRequest.Headers.Add(HttpRequestHeader.Pragma, "no-cache"); if (!string.IsNullOrEmpty(authorization)) { webRequest.Headers.Add(HttpRequestHeader.Authorization, authorization); webRequest.AllowAutoRedirect = false; } try { var response = HttpWebEngine.UploadFile(webRequest, file, "upFile", nameCollection, _userAgent); if (!string.IsNullOrEmpty(authorization)) { if (response.StatusCode == HttpStatusCode.Found) { response = GetUploadStatus(printerIp, authorization, file); if (response.Response.Contains("The format of the file is invalid.")) { ExecutionServices.SystemTrace.LogDebug("The format of the file is invalid."); throw new FormatException("The format of the file is invalid."); } } } if (response.StatusCode == HttpStatusCode.OK) { response = GetUploadStatus(printerIp, authorization, file); if (response.Response.Contains("The format of the file is invalid.")) { ExecutionServices.SystemTrace.LogDebug("The format of the file is invalid."); throw new FormatException("The format of the file is invalid."); } ExecutionServices.SystemTrace.LogDebug("Certificate installed!"); if (!VerifyCAUploadPhoenix(printerIp, authorization, true)) { ExecutionServices.SystemTrace.LogDebug("Certificate upload verification failed"); throw new WebException("Certificate upload verification failed"); } } else { throw new WebException("Upload Failed"); } } catch (WebException ex) { throw new WebException("Upload Failed", ex); } }
/// <summary> /// Close the Wizard page of CA certificate installation for VEP printers /// </summary> /// <param name="printerIp"></param> /// <param name="authorization"></param> /// <param name="wizardid"></param> /// <param name="paramName"></param> /// <param name="hideValue"></param> private void CloseWizard(Uri printerIp, string authorization, string wizardid, string paramName, string hideValue) { Uri uploadUrl = new Uri($"{printerIp}{Properties.Resources.URLJediError}"); HttpWebRequest oKRequest = (HttpWebRequest)WebRequest.Create(uploadUrl); oKRequest.Headers.Add(HttpRequestHeader.CacheControl, "no-cache"); CookieCollection cookies = new CookieCollection { new Cookie("wizardid", wizardid) { Domain = "localhost" } }; oKRequest.CookieContainer = new CookieContainer(); oKRequest.CookieContainer.Add(cookies); oKRequest.Accept = Properties.Resources.WebRequestAccept; oKRequest.ContentType = "application/x-www-form-urlencoded"; oKRequest.Headers.Add("Accept-Encoding", "gzip, deflate"); oKRequest.Headers.Add("Accept-Language", "en-US"); oKRequest.Host = uploadUrl.Host; oKRequest.KeepAlive = true; string optionData = "ok=OK"; if (!string.IsNullOrEmpty(authorization)) { oKRequest.Headers.Add(HttpRequestHeader.Authorization, authorization); oKRequest.AllowAutoRedirect = false; } try { var request = HttpWebEngine.Post(oKRequest, optionData, _userAgent); if (request.StatusCode != HttpStatusCode.OK) { throw new WebException("Certificate Wizard closure failed"); } } catch (WebException ex) { throw new WebException("Certificate Wizard closure failed", ex); } //Step 2 : Upload Empty File NameValueCollection nvc = new NameValueCollection { { "Hide", hideValue }, { "Cancel", "Cancel" } }; uploadUrl = new Uri($"{printerIp}{Properties.Resources.URLJediUpload}"); HttpWebRequest cancelRequest = (HttpWebRequest)WebRequest.Create(uploadUrl); cancelRequest.Headers.Add(HttpRequestHeader.CacheControl, "no-cache"); cancelRequest.CookieContainer = new CookieContainer(); cancelRequest.CookieContainer.Add(cookies); cancelRequest.Accept = Properties.Resources.WebRequestAccept; cancelRequest.Headers.Add("Accept-Encoding", "gzip, deflate"); cancelRequest.ContentType = "application/x-www-form-urlencoded"; cancelRequest.Headers.Add("Accept-Language", "en-US"); cancelRequest.Host = uploadUrl.Host; cancelRequest.KeepAlive = true; cancelRequest.ServicePoint.Expect100Continue = false; if (!string.IsNullOrEmpty(authorization)) { cancelRequest.Headers.Add(HttpRequestHeader.Authorization, authorization); cancelRequest.AllowAutoRedirect = false; } try { var uploadResponse = HttpWebEngine.UploadFile(cancelRequest, "", paramName, nvc, _userAgent); if (uploadResponse.StatusCode != HttpStatusCode.OK) { throw new WebException("Certificate Wizard closure failed"); } } catch (WebException ex) { throw new WebException("Certificate Wizard closure failed", ex); } }
/// <summary> /// Installs CA certificate on VEP printer /// </summary> /// <param name="printerIp"></param> /// <param name="file"></param> /// <param name="authorization"></param> /// <param name="allowIntermediate"></param> public void InstallCAFileJedi(Uri printerIp, string file, string authorization, bool allowIntermediate) { //Retrieve Session ID HttpWebResult webResultGetReqeust = GetSessionIdRequest(printerIp, authorization); var cookies = webResultGetReqeust.Cookies; for (int i = 0; i < webResultGetReqeust.Headers.Count; i++) { if (webResultGetReqeust.Headers.Get(i).Contains("sessionId")) { string strTemp = webResultGetReqeust.Headers.Get(i); strTemp = strTemp.Replace("sessionId=", ""); strTemp = strTemp.Replace("; path=/;", ""); cookies.Add(new Cookie("sessionId", strTemp) { Domain = "localhost" }); } } //Retrieve WizardID and HideValue var getResponse = GetWizardIdResponse(printerIp, authorization, cookies); string wizardid = getResponse.Headers.Get("Set-Cookie"); int startindex = getResponse.Response.IndexOf("Hide\" VALUE=\"", StringComparison.OrdinalIgnoreCase) + 13; string truncatedResponse = getResponse.Response.Substring(startindex); int endIndex = truncatedResponse.IndexOf("\">", StringComparison.OrdinalIgnoreCase); string hideValue = truncatedResponse.Substring(0, endIndex); //Step 1 : Post Wizard page Uri uploadUrl = new Uri($"{printerIp}{Properties.Resources.URLJediCookie}"); HttpWebRequest optionRequest = (HttpWebRequest)WebRequest.Create(uploadUrl); optionRequest.Headers.Add(HttpRequestHeader.CacheControl, "no-cache"); wizardid = wizardid.Replace("wizardid=", ""); wizardid = wizardid.Replace(";PATH=/;SECURE", ""); cookies.Add(new Cookie("wizardid", wizardid) { Domain = "localhost" }); optionRequest.CookieContainer = new CookieContainer(); optionRequest.CookieContainer.Add(cookies); optionRequest.Accept = Properties.Resources.WebRequestAccept; optionRequest.ContentType = "application/x-www-form-urlencoded"; optionRequest.Headers.Add("Accept-Encoding", "gzip, deflate"); optionRequest.Headers.Add("Accept-Language", "en-US"); optionRequest.Host = uploadUrl.Host; optionRequest.KeepAlive = true; string postOptionData = Properties.Resources.OptionRequestJediInstall + hideValue; if (!string.IsNullOrEmpty(authorization)) { optionRequest.Headers.Add(HttpRequestHeader.Authorization, authorization); optionRequest.AllowAutoRedirect = false; } try { var optionResponse = HttpWebEngine.Post(optionRequest, postOptionData, _userAgent); if (optionResponse.StatusCode != HttpStatusCode.OK) { throw new WebException("Certificate option page POST failed"); } } catch (WebException ex) { throw new WebException("Certificate option page POST failed", ex); } //Step 2 : Upload File NameValueCollection nvc = new NameValueCollection(); if (allowIntermediate) { nvc.Add("Intermediate_CA", "on"); } nvc.Add("Hide", hideValue); nvc.Add("Finish", "Finish"); uploadUrl = new Uri($"{printerIp}{Properties.Resources.URLJediUpload}"); HttpWebRequest installRequest = (HttpWebRequest)WebRequest.Create(uploadUrl); installRequest.Headers.Add(HttpRequestHeader.CacheControl, "no-cache"); wizardid = wizardid.Replace("wizardid=", ""); wizardid = wizardid.Replace(";PATH=/", ""); cookies.Add(new Cookie("wizardid", wizardid) { Domain = "localhost" }); installRequest.CookieContainer = new CookieContainer(); installRequest.CookieContainer.Add(cookies); installRequest.Accept = Properties.Resources.WebRequestAccept; installRequest.Headers.Add("Accept-Encoding", "gzip, deflate"); installRequest.ContentType = "application/x-www-form-urlencoded"; installRequest.Headers.Add("Accept-Language", "en-US"); installRequest.Host = uploadUrl.Host; installRequest.KeepAlive = true; installRequest.ServicePoint.Expect100Continue = false; if (!string.IsNullOrEmpty(authorization)) { installRequest.Headers.Add(HttpRequestHeader.Authorization, authorization); installRequest.AllowAutoRedirect = false; } try { var uploadResponse = HttpWebEngine.UploadFile(installRequest, file, ".Install_FileName_handle", nvc, _userAgent); if (uploadResponse.StatusCode == HttpStatusCode.OK) { var responseError = !allowIntermediate ? "The format of the file is invalid." : "The certificate entered was invalid. Please try again and be sure to include the entire certificate correctly."; if (uploadResponse.Response.Contains(responseError)) { CloseWizard(printerIp, authorization, wizardid, ".Install_FileName_handle", hideValue); ExecutionServices.SystemTrace.LogDebug(responseError); return; } CloseWizard(printerIp, authorization, wizardid); ExecutionServices.SystemTrace.LogDebug("Certificate installed!"); if (!VerifyCAUploadJedi(printerIp, authorization, true)) { throw new WebException("Certificate upload verification failed"); } } else { throw new WebException("Upload Failed"); } } catch (WebException ex) { throw new WebException("Upload Failed", ex); } }