github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/apps/simpleexample/SimpleClientCpp/gen_keys_test.cc (about) 1 // 2 // Copyright 2014 John Manferdelli, 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: gen_keys_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 bool test_gen() { 27 string path("/Domains/test_keys"); 28 string ca_cert_string; 29 string client_cert_string; 30 string server_cert_string; 31 string ca_key_string; 32 string client_key_string; 33 string server_key_string; 34 string ca_key_type; 35 string server_key_type; 36 string client_key_type; 37 38 // CA 39 string ca_cert_file_name = path + "/ca_cert"; 40 string ca_key_file_name = path + "/ca_key"; 41 if(!ReadFile(ca_cert_file_name, &ca_cert_string)) { 42 printf("can't read ca_cert.\n"); 43 return false; 44 } 45 if(!ReadFile(ca_key_file_name, &ca_key_string)) { 46 printf("can't read ca key.\n"); 47 return false; 48 } 49 byte* ca_ptr = (byte*)ca_cert_string.data(); 50 X509* ca_cert = d2i_X509(nullptr, (const byte**)&ca_ptr, 51 ca_cert_string.size()); 52 if (ca_cert == nullptr) { 53 printf("ca_cert doesnt translate.\n"); 54 return false; 55 } 56 57 EVP_PKEY* ca_key = nullptr; 58 59 if (!DeserializePrivateKey(ca_key_string, &ca_key_type, &ca_key)) { 60 printf("Can't deserialize ca key\n"); 61 return false; 62 } 63 if (!VerifyX509CertificateChain(ca_cert, ca_cert)) { 64 printf("Can't verify ca cert\n"); 65 return false; 66 } 67 printf("CA verifies\n"); 68 69 // server 70 string server_cert_file_name = path + "/server_cert"; 71 string server_key_file_name = path + "/server_key"; 72 if(!ReadFile(server_cert_file_name, &server_cert_string)) { 73 printf("can't read server_cert.\n"); 74 return false; 75 } 76 if(!ReadFile(server_key_file_name, &server_key_string)) { 77 printf("Can't read server key.\n"); 78 return false; 79 } 80 byte* server_ptr = (byte*)server_cert_string.data(); 81 X509* server_cert = d2i_X509(nullptr, (const byte**)&server_ptr, 82 server_cert_string.size()); 83 if (server_cert == nullptr) { 84 printf("server_cert doesnt translate.\n"); 85 return false; 86 } 87 88 EVP_PKEY* server_key = nullptr; 89 90 if (!DeserializePrivateKey(server_key_string, &server_key_type, &server_key)) { 91 printf("Can't deserialize server key\n"); 92 return false; 93 } 94 if (!VerifyX509CertificateChain(ca_cert, server_cert)) { 95 printf("Can't verify server cert\n"); 96 return false; 97 } 98 printf("Server verifies\n"); 99 100 // client 101 string client_cert_file_name = path + "/client_cert"; 102 string client_key_file_name = path + "/client_key"; 103 if(!ReadFile(client_cert_file_name, &client_cert_string)) { 104 printf("can't read client_cert.\n"); 105 return false; 106 } 107 if(!ReadFile(client_key_file_name, &client_key_string)) { 108 printf("Can't read client key.\n"); 109 return false; 110 } 111 byte* client_ptr = (byte*)client_cert_string.data(); 112 X509* client_cert = d2i_X509(nullptr, (const byte**)&client_ptr, 113 client_cert_string.size()); 114 if (client_cert == nullptr) { 115 printf("client_cert doesnt translate.\n"); 116 return false; 117 } 118 119 EVP_PKEY* client_key = nullptr; 120 121 if (!DeserializePrivateKey(client_key_string, &client_key_type, &client_key)) { 122 printf("Can't deserialize client key\n"); 123 return false; 124 } 125 if (!VerifyX509CertificateChain(ca_cert, client_cert)) { 126 printf("Can't verify client cert\n"); 127 return false; 128 } 129 printf("Client verifies\n"); 130 131 return true; 132 } 133 134 135 TEST(test_gen, test_gen) { EXPECT_TRUE(test_gen()); } 136 137 int main(int an, char** av) { 138 ::testing::InitGoogleTest(&an, av); 139 int result = RUN_ALL_TESTS(); 140 return result; 141 } 142