private async Task <ServerResponse> UnwrapResponseAsync(ServerSignedResponse resp) { var response = new ServerResponse(); try { var c = await GetSignatureHelperAsync(resp.Object.Fingerprint, response).ConfigureAwait(false); return(c.Verify <ServerSignedResponse, ServerResponse>(resp)); } catch (FingerprintException e) { _logger?.LogError(e, "Fingerprint Exception trying to unwrap the response"); response.ErrorMessage = e.Message; response.I18NErrorMessages = e.I18NErrorMessages; response.ResultCode = e.Code; return(response); } catch (Exception e) { _logger?.LogCritical(e, "Critical Exception trying to unwrap the response"); response.ErrorMessage = e.Message; response.ResultCode = ResultCodes.SystemError; return(response); } }
internal async Task <ServerResponse> UnwrapResponse(ServerSignedResponse resp) { ServerResponse response = new ServerResponse(); try { SignatureHelper c = await GetSignatureHelper(resp.Object.Fingerprint, response); return(c.Verify <ServerSignedResponse, ServerResponse>(resp)); } catch (Exception e) { response.PopulateFromException(e, Logger); return(response); } }
private async Task <SignatureHelper> GetSignatureHelper(string fingerprint, ServerResponse response) { SignatureHelper c = CertificateHelperFactory.Instance.VerifyKeys.FirstOrDefault(a => a.Key == fingerprint).Value; if (c == null) { await CertificateHelperFactory.Instance.ServerCertSemaphore.WaitAsync(); try { ServerSignedResponse <PublicKeyInfo> r = await Channel.GetServerPublicKey(fingerprint); if (r.Object.Object.ResultCode != ResultCodes.Ok) { throw new FingerprintException(("en", "Invalid or outdated Fingerprint, server returns: " + (r.Object.Object.ErrorMessage ?? "")), ("es", "Huella invalida o vieja, el servido retorna: " + ((r.Object.Object.I18NErrorMessages.ContainsKey("es") ? r.Object.Object.I18NErrorMessages["es"] : r.Object.Object.ErrorMessage) ?? ""))); } X509Certificate2 cert = new X509Certificate2(Convert.FromBase64String(r.Object.Object.Response.Key)); c = new SignatureHelper(cert, false); SignatureHelper verify = null; if (CertificateHelperFactory.Instance.VerifyKeys.ContainsKey(r.Object.Fingerprint)) { verify = CertificateHelperFactory.Instance.VerifyKeys[r.Object.Fingerprint]; } else if (r.Object.Fingerprint.Equals(r.Object.Object.Response.Fingerprint, StringComparison.InvariantCultureIgnoreCase)) { verify = c; } if (verify == null) { throw new FingerprintException(("en", "Fingerprint not found: " + r.Object.Fingerprint), ("es", "Huella no encontrada: " + r.Object.Fingerprint)); } verify.Verify <ServerSignedResponse <PublicKeyInfo>, ServerResponse <PublicKeyInfo> >(r); CertificateHelperFactory.Instance.VerifyKeys.Add(r.Object.Object.Response.Fingerprint, c); return(c); } finally { CertificateHelperFactory.Instance.ServerCertSemaphore.Release(); } } return(c); }