Exemplo n.º 1
0
        /// <summary>
        /// Set a signature request.
        /// </summary>
        /// <remarks>
        /// Add or replaces a signature request.
        /// </remarks>
        /// <param name="signatureRequest">Signed signature request.</param>
        public void SetSignatureRequest(
            IRpcConnection connection,
            Secure<SignatureRequest> signatureRequest,
            Secure<SignatureRequestInfo> signatureRequestInfo)
        {
            Guid id = signatureRequest.Certificate.Id;

              if (signatureRequest.Certificate.Id != signatureRequestInfo.Certificate.Id)
              {
            Logger.Log(LogLevel.Warning,
              "Connection {0}: Certificate id {1} (unverified) tried to set a signature request, but the certificate id on the request info was {2}.",
              connection.Id,
              signatureRequest.Certificate.Id.ToString(),
              signatureRequestInfo.Certificate.Id);
            throw new PiArgumentException(ExceptionCode.SignatureRequestInvalid, "Signature request invalid.");
              }

              if (!signatureRequest.VerifySimple())
              {
            Logger.Log(LogLevel.Warning,
              "Connection {0}: Certificate id {1} (unverified) tried to set a signature request, but the signature on the request was invalid.",
              connection.Id,
              signatureRequest.Certificate.Id.ToString());
            throw new PiArgumentException(ExceptionCode.SignatureRequestInvalid, "Signature request invalid.");
              }

              if (!signatureRequestInfo.VerifySimple())
              {
            Logger.Log(LogLevel.Warning,
              "Connection {0}: Certificate id {1} (unverified) tried to set a signature request, but the signature on the request info was invalid.",
              connection.Id,
              signatureRequest.Certificate.Id.ToString());
            throw new PiArgumentException(ExceptionCode.SignatureRequestInvalid, "Signature request invalid.");
              }

              SignatureRequestInfo requestInfo = signatureRequestInfo.Value.Decrypt(this.serverCertificate);

              if (!requestInfo.Valid)
              {
            Logger.Log(LogLevel.Warning,
              "Connection {0}: Certificate id {1} (unverified) tried to set a signature request, but the request data was invalid.",
              connection.Id,
              signatureRequest.Certificate.Id.ToString());
            throw new PiArgumentException(ExceptionCode.InvalidSignatureRequest, "Signature request data not valid.");
              }

              MySqlCommand replaceCommand = new MySqlCommand("REPLACE INTO signaturerequest (Id, Value, Info) VALUES (@Id, @Value, @Info)", DbConnection);
              replaceCommand.Parameters.AddWithValue("@Id", id.ToByteArray());
              replaceCommand.Parameters.AddWithValue("@Value", signatureRequest.ToBinary());
              replaceCommand.Parameters.AddWithValue("@Info", signatureRequestInfo.ToBinary());
              replaceCommand.ExecuteNonQuery();

              Logger.Log(LogLevel.Info, "Connection {0}: Signature request for certificate id {1} stored.", connection.Id, signatureRequest.Certificate.Id.ToString());

              MySqlCommand deleteCommand = new MySqlCommand("DELETE FROM signatureresponse WHERE Id = @Id", DbConnection);
              deleteCommand.Parameters.AddWithValue("@Id", id.ToByteArray());
              deleteCommand.ExecuteNonQuery();

              if (signatureRequest.Certificate is AuthorityCertificate)
              {
            CertificateStorage.Add(signatureRequest.Certificate);
              }

              if (!requestInfo.EmailAddress.IsNullOrEmpty())
              {
            SendMail(
              requestInfo.EmailAddress,
              MailType.VoterRequestDeposited,
              requestInfo.EmailAddress,
              signatureRequest.Certificate.Id.ToString(),
              CertificateTypeText(signatureRequest.Certificate, Language.English),
              CertificateTypeText(signatureRequest.Certificate, Language.German),
              CertificateTypeText(signatureRequest.Certificate, Language.French));
              }

              SendMail(
            this.serverConfig.MailAdminAddress,
            MailType.AdminNewRequest,
            requestInfo.EmailAddress.IsNullOrEmpty() ? "?@?.?" : requestInfo.EmailAddress,
            signatureRequest.Certificate.Id.ToString(),
            signatureRequest.Certificate.TypeText);
        }