roughtime.googlesource.com/roughtime.git@v0.0.0-20201210012726-dd529367052d/simple_server.cc (about)

     1  /* Copyright 2016 The Roughtime Authors.
     2   *
     3   * Licensed under the Apache License, Version 2.0 (the "License");
     4   * you may not use this file except in compliance with the License.
     5   * You may obtain a copy of the License at
     6   *
     7   *   http://www.apache.org/licenses/LICENSE-2.0
     8   *
     9   * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License. */
    14  
    15  #include "simple_server.h"
    16  
    17  #include <openssl/curve25519.h>
    18  
    19  #include "logging.h"
    20  #include "server.h"
    21  
    22  namespace roughtime {
    23  
    24  SimpleServer::SimpleServer(std::unique_ptr<Identity> identity,
    25                             std::unique_ptr<TimeSource> time_source, int fd)
    26      : fd_(fd), server_(std::move(identity), std::move(time_source)) {}
    27  
    28  bool SimpleServer::ProcessBatch() {
    29    UdpProcessor::Stats stats;
    30    return udp_processor_.ProcessBatch(fd_, &server_, &stats);
    31  }
    32  
    33  void SimpleServer::RunUntilError() {
    34    UdpProcessor::Stats stats;
    35    while (udp_processor_.ProcessBatch(fd_, &server_, &stats)) {
    36    }
    37  }
    38  
    39  // static
    40  std::unique_ptr<Identity> SimpleServer::MakeIdentity(
    41      const uint8_t root_private_key[ED25519_PRIVATE_KEY_LEN], rough_time_t mint,
    42      rough_time_t maxt) {
    43    ROUGHTIME_CHECK(mint <= maxt);
    44    uint8_t delegated_private_key[ED25519_PRIVATE_KEY_LEN];
    45    uint8_t delegated_public_key[ED25519_PUBLIC_KEY_LEN];
    46    ED25519_keypair(delegated_public_key, delegated_private_key);
    47  
    48    auto identity = std::unique_ptr<Identity>(new Identity());
    49    CreateCertificate(identity->certificate, root_private_key, mint, maxt,
    50                      delegated_public_key);
    51    memcpy(identity->private_key, delegated_private_key,
    52           sizeof(delegated_private_key));
    53    return identity;
    54  }
    55  
    56  }  // namespace roughtime