github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/apps/simpleexample/SimpleClientCpp/junkyard/server_test.cc.old (about)

     1  //
     2  // Copyright 2016, Google Corporation , All Rights Reserved.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  // or in the the file LICENSE-2.0.txt in the top level sourcedirectory
     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  // File: simple_server_test.cc
    15  
    16  #include "gtest/gtest.h"
    17  
    18  #include <gtest/gtest.h>
    19  #include <gflags/gflags.h>
    20  #include <stdio.h>
    21  #include <string>
    22  
    23  #include "helpers.h"
    24  
    25  bool ProcessRequest (int request_number, int request_size, byte* request,
    26                       int* reply_size, byte* reply) {
    27    printf("\nProcessRequest %s\n", (const char*)request);
    28    memset(reply, 0, *reply_size);
    29    sprintf((char*)reply, "This is a stupid reply %d\n", request_number);
    30    *reply_size = strlen((const char*)reply) + 1;
    31    if (request_number > 2)
    32      return false;
    33    return true;
    34  }
    35  
    36  void HandleConnection(SslChannel* channel,  SSL* ssl, int client) {
    37    byte request[4096];
    38    int request_size = 0;
    39    byte reply[4096];
    40    int reply_size;
    41    bool fContinue;
    42    int request_number = 0;
    43  
    44    printf("\nHandleConnection\n");
    45    for (;;) {
    46      memset(request, 0, 4096);
    47      request_size = SSL_read(ssl, request, 4096);
    48      printf("request %d: %s\n", request_size, (const char*)request);
    49  
    50      reply_size = 4096;
    51      fContinue = ProcessRequest(request_number++, request_size, request,
    52                       &reply_size, reply);
    53      SSL_write(ssl, reply, reply_size);
    54      if (!fContinue)
    55        break;
    56    }
    57    // SSL_free(ssl);
    58    // close(client);
    59  }
    60  
    61  int main(int an, char** av) {
    62    SslChannel channel;
    63    string path;
    64  
    65    string key_path("/Domains/test_keys");
    66    string ca_cert_string;
    67    string client_cert_string;
    68    string server_cert_string;
    69    string ca_key_string;
    70    string client_key_string;
    71    string server_key_string;
    72    string ca_key_type;
    73    string server_key_type;
    74    string client_key_type;
    75  
    76    // CA
    77    string ca_cert_file_name = key_path + "/ca_cert";
    78    string ca_key_file_name = key_path + "/ca_key";
    79    if(!ReadFile(ca_cert_file_name, &ca_cert_string)) {
    80      printf("can't read ca_cert.\n");
    81      return 1;
    82    }
    83    if(!ReadFile(ca_key_file_name, &ca_key_string)) {
    84      printf("can't read ca key.\n");
    85      return 1;
    86    }
    87    byte* ca_ptr = (byte*)ca_cert_string.data();
    88    X509* ca_cert = d2i_X509(nullptr, (const byte**)&ca_ptr,
    89          ca_cert_string.size());
    90  
    91    // server cert and keys
    92    string server_cert_file_name = key_path + "/server_cert";
    93    string server_key_file_name = key_path + "/server_key";
    94    if(!ReadFile(server_cert_file_name, &server_cert_string)) {
    95      printf("can't read server_cert.\n");
    96      return 1;
    97    }
    98    if(!ReadFile(server_key_file_name, &server_key_string)) {
    99      printf("Can't read server key.\n");
   100      return 1;
   101    }
   102    byte* server_ptr = (byte*)server_cert_string.data();
   103    X509* server_cert = d2i_X509(nullptr, (const byte**)&server_ptr,
   104          server_cert_string.size());
   105    if (server_cert == nullptr) {
   106      printf("server_cert doesnt translate.\n");
   107      return 1;
   108    }
   109  
   110    EVP_PKEY* server_key = nullptr;
   111  
   112    if (!DeserializePrivateKey(server_key_string, &server_key_type, &server_key)) {
   113      printf("Can't deserialize server key\n");
   114      return 1;
   115    }
   116  
   117    string network("tcp");
   118    string address("127.0.0.1");
   119    string port("2015");
   120  
   121    printf("Calling InitServerSslChannel %s key\n", server_key_type.c_str());
   122    if (!channel.InitServerSslChannel(network, address, port, ca_cert,
   123                                      server_cert, server_key_type, server_key,
   124                                      SSL_SERVER_VERIFY_CLIENT_VERIFY)) {
   125      printf("Can't InitServerSslChannel\n");
   126      return 1;
   127    }
   128    printf("Calling ServerLoop\n\n");
   129    channel.ServerLoop(&HandleConnection);
   130    return 0;
   131  }
   132