github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/apps/simpleexample/SimpleClientCpp/junkyard/simple_client_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  // Project: New Cloudproxy Crypto
    15  // File: simple_client_test.cc
    16  
    17  #include "gtest/gtest.h"
    18  
    19  #include <gtest/gtest.h>
    20  #include <gflags/gflags.h>
    21  #include <stdio.h>
    22  #include <string>
    23  
    24  #include "helpers.h"
    25  
    26  
    27  #if 0
    28  // for testing
    29  #include <netinet/in.h>
    30  #include <sys/socket.h>
    31  #include <arpa/inet.h>
    32  void FakeClient(string& network, string& address, string& port) {
    33    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    34    struct sockaddr_in dest_addr;
    35    uint16_t s_port = atoi(port.c_str());
    36    dest_addr.sin_family = AF_INET;
    37    dest_addr.sin_port = htons(s_port);
    38    inet_aton(address.c_str(), &dest_addr.sin_addr);
    39  
    40    if (connect(sockfd, (struct sockaddr *) &dest_addr,
    41                sizeof(struct sockaddr)) == -1) {
    42      printf("Error: Cannot connect to host\n");
    43      return;
    44    }
    45  
    46    byte request[4096];
    47    byte reply[4096];
    48    const char* r = "this is a request\n";
    49    memcpy(request, (byte*)r, strlen(r) + 1);
    50  
    51    printf("Client sending %s\n", (const char*)request);
    52    if (write(sockfd, request, strlen(r) + 1) <= 0) {
    53      printf("client write failed\n");
    54    }
    55    if (read(sockfd, reply, 4096) <= 0) {
    56      printf("client read failed\n");
    57    }
    58    printf("client received: %s\n", (const char*)reply);
    59  }
    60  #endif
    61  
    62  DEFINE_string(key_type, "ECC", "key type for generated keys");
    63  
    64  int main(int an, char** av) {
    65    SslChannel channel;
    66    string path;
    67    string policy_cert_file_name(
    68        "/Domains/domain.simpleexample/policy_keys/cert");
    69    string policy_cert;
    70    string network("tcp");
    71    string address("127.0.0.1");
    72    string port("2015");
    73  
    74  #ifdef __linux__
    75    gflags::ParseCommandLineFlags(&an, &av, true);
    76  #else
    77    google::ParseCommandLineFlags(&an, &av, true);
    78  #endif
    79  
    80    // key type
    81    string key_type;
    82    int key_size;
    83    if(FLAGS_key_type == "ECC") {
    84      key_type = "ECC";
    85      key_size = 256;
    86    } else if (FLAGS_key_type == "RSA") {
    87      key_type = "RSA";
    88      key_size = 2048;
    89    } else {
    90      printf("Invalid key type\n");
    91      return 1;
    92    }
    93    printf("Key type is %s\n", key_type.c_str());
    94  
    95    // Self signed cert.
    96    X509_REQ* req = X509_REQ_new();;
    97    X509* cert = X509_new();
    98  
    99    string common_name("Fred");
   100    string issuer("Fred");
   101    string keyUsage("critical,digitalSignature,keyEncipherment,keyAgreement,keyCertSign");
   102    string extendedKeyUsage("serverAuth,clientAuth");
   103  
   104    EVP_PKEY* self = GenerateKey(key_type, key_size);
   105    if (!GenerateX509CertificateRequest(key_type, common_name,
   106              self, false, req)) {
   107      printf("Can't generate x509 request\n");
   108      return 1;
   109    }
   110    if (!SignX509Certificate(self, true, true, issuer, keyUsage,
   111                             extendedKeyUsage, 86400,
   112                             self, req, false, cert)) {
   113      printf("Can't sign x509 request\n");
   114      return 1;
   115    }
   116  
   117    printf("Calling InitClientSslChannel\n");
   118    if (!channel.InitClientSslChannel(network, address, port, cert,
   119                                      cert, key_type, self,
   120                                      SSL_NO_SERVER_VERIFY_NO_CLIENT_AUTH)) {
   121      printf("Can't InitClientSslChannel\n");
   122      return 1;
   123    }
   124  
   125    int size_send_buf = 4096;
   126    byte send_buf[4096];
   127    int size_get_buf = 4096;
   128    byte get_buf[4096];
   129    int msg_num = 1;
   130  
   131    // write/read
   132    printf("Client transcript\n\n");
   133    sprintf((char*)send_buf, "Client message %d\n", msg_num++);
   134    size_send_buf = SslWrite(channel.GetSslChannel(),
   135                             strlen((const char*)send_buf) + 1,
   136                             send_buf);
   137    size_get_buf = SslRead(channel.GetSslChannel(), 4096, get_buf);
   138    printf("server reply %d, %s\n", size_get_buf, (const char*)get_buf);
   139    sprintf((char*)send_buf, "Client message %d\n", msg_num++);
   140    size_send_buf = SslWrite(channel.GetSslChannel(),
   141                             strlen((const char*)send_buf) + 1,
   142                             send_buf);
   143    size_get_buf = SslWrite(channel.GetSslChannel(), 4096, get_buf);
   144    printf("server reply %d, %s\n", size_get_buf, (const char*)get_buf);
   145  
   146    return 0;
   147  }
   148