Ejemplo n.º 1
0
 public KioskServer(CertificateStorage certificateStorage, Certificate serverCertificate)
 {
     Requests = new Queue<RequestContainer>();
       this.certificateStorageBinary = certificateStorage.ToBinary();
       this.serverCertificateBinary = serverCertificate.ToBinary();
       this.tcpServer = new TcpServer(this);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Add an authority.
        /// </summary>
        /// <param name="certificate">Authority to be added.</param>
        /// <returns>Index of the authority.</returns>
        public int AddAuthority(
            IRpcConnection connection,
            Certificate certificate)
        {
            if (certificate == null)
            throw new ArgumentNullException("certificate");
              if (certificate.Validate(this.certificateStorage) != CertificateValidationResult.Valid)
            throw new PiSecurityException(ExceptionCode.InvalidCertificate, "Authority certificate not valid.");
              if (!(certificate is AuthorityCertificate))
            throw new PiSecurityException(ExceptionCode.NoAuthorizedAuthority, "No an authority certificate.");

              MySqlTransaction transaction = DbConnection.BeginTransaction();

              MySqlCommand countCommand = new MySqlCommand("SELECT count(*) FROM authority WHERE VotingId = @VotingId", DbConnection, transaction);
              countCommand.Add("@VotingId", this.parameters.VotingId.ToByteArray());
              if ((long)countCommand.ExecuteScalar() >= this.parameters.AuthorityCount)
            throw new PiArgumentException(ExceptionCode.AlreadyEnoughAuthorities, "Already enough authorities.");

              MySqlCommand addedCommand = new MySqlCommand("SELECT count(*) FROM authority WHERE VotingId = @VotingId AND AuthorityId = @AuthorityId", DbConnection, transaction);
              addedCommand.Add("@VotingId", this.parameters.VotingId.ToByteArray());
              addedCommand.Add("@AuthorityId", certificate.Id.ToByteArray());
              if (addedCommand.ExecuteHasRows())
            throw new PiArgumentException(ExceptionCode.AuthorityAlreadyInVoting, "Already an authority of the voting.");

              MySqlCommand indexCommand = new MySqlCommand("SELECT max(AuthorityIndex) + 1 FROM authority WHERE VotingId = @VotingId", DbConnection, transaction);
              indexCommand.Add("@VotingId", this.parameters.VotingId.ToByteArray());
              object authorityIndexNull = indexCommand.ExecuteScalar();
              int authorityIndex = authorityIndexNull == DBNull.Value ? 1 : Convert.ToInt32((long)authorityIndexNull);

              MySqlCommand insertCommand = new MySqlCommand("INSERT INTO authority (VotingId, AuthorityIndex, AuthorityId, Certificate) VALUES (@VotingId, @AuthorityIndex, @AuthorityId, @Certificate)", DbConnection, transaction);
              insertCommand.Parameters.AddWithValue("@VotingId", this.parameters.VotingId.ToByteArray());
              insertCommand.Parameters.AddWithValue("@AuthorityIndex", authorityIndex);
              insertCommand.Parameters.AddWithValue("@AuthorityId", certificate.Id.ToByteArray());
              insertCommand.Parameters.AddWithValue("@Certificate", certificate.ToBinary());
              insertCommand.ExecuteNonQuery();

              Logger.Log(LogLevel.Info, "Connection {0}: Authority id {1} added to voting id {2}", connection.Id, certificate.Id.ToString(), Id.ToString());

              transaction.Commit();

              return authorityIndex;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Add a root certificate to storage.
        /// </summary>
        /// <param name="certificate">Root certificate to add.</param>
        public void AddRoot(Certificate certificate)
        {
            if (!Has(certificate.Id))
              {
            MySqlCommand command = new MySqlCommand("INSERT INTO certificate (Id, Value, Root) VALUES (@Id, @Value, @Root)", DbConnection);
            command.Parameters.AddWithValue("@Id", certificate.Id.ToByteArray());
            command.Parameters.AddWithValue("@Value", certificate.ToBinary());
            command.Parameters.AddWithValue("@Root", 1);
            command.ExecuteNonQuery();
              }
              else
              {
            Certificate storedCertificate = Get(certificate.Id);
            storedCertificate.Merge(certificate);

            MySqlCommand command = new MySqlCommand("UPDATE certificate SET Value = @Value, Root = @Root WHERE Id = @Id", DbConnection);
            command.Parameters.AddWithValue("@Id", certificate.Id.ToByteArray());
            command.Parameters.AddWithValue("@Value", certificate.ToBinary());
            command.Parameters.AddWithValue("@Root", 1);
            command.ExecuteNonQuery();
              }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Add a certificate to the storage.
        /// </summary>
        /// <param name="certificate">Certificate to add.</param>
        public void Add(Certificate certificate)
        {
            if (certificate == null)
            throw new ArgumentNullException("certificate");

              if (Has(certificate.Id))
              {
            Certificate storedCertificate = Get(certificate.Id);
            storedCertificate.Merge(certificate);

            MySqlCommand command = new MySqlCommand("UPDATE certificate SET Value = @Value WHERE Id = @Id", DbConnection);
            command.Parameters.AddWithValue("@Id", certificate.Id.ToByteArray());
            command.Parameters.AddWithValue("@Value", certificate.ToBinary());
            command.ExecuteNonQuery();
              }
              else
              {
            MySqlCommand command = new MySqlCommand("INSERT INTO certificate (Id, Value, Root) VALUES (@Id, @Value, @Root)", DbConnection);
            command.Parameters.AddWithValue("@Id", certificate.Id.ToByteArray());
            command.Parameters.AddWithValue("@Value", certificate.ToBinary());
            command.Parameters.AddWithValue("@Root", 0);
            command.ExecuteNonQuery();
              }
        }