github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/apps/simpleexample/SimpleClientCpp/junkyard/simple_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 #if 0 26 // For testing. 27 28 #include <netinet/in.h> 29 #include <sys/socket.h> 30 #include <arpa/inet.h> 31 void FakeServer(string& network, string& address, string& port) { 32 int sockfd = socket(AF_INET, SOCK_STREAM, 0); 33 struct sockaddr_in dest_addr; 34 uint16_t s_port = atoi(port.c_str()); 35 dest_addr.sin_family = AF_INET; 36 dest_addr.sin_port = htons(s_port); 37 inet_aton(address.c_str(), &dest_addr.sin_addr); 38 39 if (bind(sockfd, (struct sockaddr*)&dest_addr, sizeof(dest_addr)) < 0) { 40 printf("Unable to bind\n"); 41 return; 42 } 43 44 if (listen(sockfd, 1) < 0) { 45 printf("Unable to listen\n"); 46 return; 47 } 48 49 byte request[4096]; 50 byte reply[4096]; 51 52 struct sockaddr_in addr; 53 uint len = sizeof(addr); 54 55 int client = accept(sockfd, (struct sockaddr*)&addr, &len); 56 if (client < 0) { 57 printf("Unable to accept\n"); 58 } 59 for(;;) { 60 int in_size = read(client, request, 4096); 61 if (in_size <= 0) { 62 printf("server read failed\n"); 63 return; 64 } 65 printf("Server received: %s\n", (const char*)request); 66 const char* r = "This is a stupid reply"; 67 memcpy(reply, r, strlen(r) + 1); 68 printf("Server sending: %s\n", (const char*)reply); 69 write(client, reply, strlen(r) + 1); 70 break; 71 } 72 } 73 #endif 74 75 bool ProcessRequest (int request_number, int request_size, byte* request, 76 int* reply_size, byte* reply) { 77 printf("\nProcessRequest %s\n", (const char*)request); 78 memset(reply, 0, *reply_size); 79 sprintf((char*)reply, "This is a stupid reply %d\n", request_number); 80 *reply_size = strlen((const char*)reply) + 1; 81 if (request_number > 2) 82 return false; 83 return true; 84 } 85 86 void HandleConnection(SslChannel* channel, SSL* ssl, int client) { 87 byte request[4096]; 88 int request_size = 0; 89 byte reply[4096]; 90 int reply_size; 91 bool fContinue; 92 int request_number = 0; 93 94 printf("\nHandleConnection\n"); 95 for (;;) { 96 memset(request, 0, 4096); 97 request_size = SSL_read(ssl, request, 4096); 98 printf("request %d: %s\n", request_size, (const char*)request); 99 100 reply_size = 4096; 101 fContinue = ProcessRequest(request_number++, request_size, request, 102 &reply_size, reply); 103 SSL_write(ssl, reply, reply_size); 104 if (!fContinue) 105 break; 106 } 107 // SSL_free(ssl); 108 // close(client); 109 } 110 111 DEFINE_string(key_type, "ECC", "key type for generated keys"); 112 113 int main(int an, char** av) { 114 SslChannel channel; 115 string path; 116 string policy_cert_file_name( 117 "/Domains/domain.simpleexample/policy_keys/cert"); 118 string policy_cert; 119 string network("tcp"); 120 string address("127.0.0.1"); 121 string port("2015"); 122 123 #ifdef __linux__ 124 gflags::ParseCommandLineFlags(&an, &av, true); 125 #else 126 google::ParseCommandLineFlags(&an, &av, true); 127 #endif 128 129 // key type 130 string key_type; 131 int key_size; 132 if(FLAGS_key_type == "ECC") { 133 key_type = "ECC"; 134 key_size = 256; 135 } else if (FLAGS_key_type == "RSA") { 136 key_type = "RSA"; 137 key_size = 2048; 138 } else { 139 printf("Invalid key type\n"); 140 return 1; 141 } 142 printf("Key type is %s\n", key_type.c_str()); 143 144 // Self signed cert. 145 X509_REQ* req = X509_REQ_new();; 146 X509* cert = X509_new(); 147 string common_name("Fred"); 148 string issuer("Fred"); 149 string keyUsage("critical,digitalSignature,keyEncipherment,keyAgreement,keyCertSign"); 150 string extendedKeyUsage("serverAuth,clientAuth"); 151 152 EVP_PKEY* self = GenerateKey(key_type, key_size); 153 if (!GenerateX509CertificateRequest(key_type, common_name, self, 154 false, req)) { 155 printf("Can't generate x509 request\n"); 156 return 1; 157 } 158 159 // EC_KEY_METHOD* meth = EC_KEY_get_method(EVP_PKEY_get1_EC_KEY(self)); 160 161 if (!SignX509Certificate(self, true, true, issuer, keyUsage, 162 extendedKeyUsage, 86400, 163 self, req, false, cert)) { 164 printf("Can't sign x509 request\n"); 165 return 1; 166 } 167 168 printf("Calling InitServerSslChannel\n"); 169 if (!channel.InitServerSslChannel(network, address, port, cert, 170 cert, key_type, self, 171 SSL_NO_SERVER_VERIFY_NO_CLIENT_AUTH)) { 172 printf("Can't InitServerSslChannel\n"); 173 return 1; 174 } 175 printf("Calling ServerLoop\n\n"); 176 channel.ServerLoop(&HandleConnection); 177 return 0; 178 } 179