github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/apps/simpleexample/SimpleClientCpp/junkyard/helpers_test.cc.old (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: support_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 readwritetest() { 27 string filename("/tmp/test1"); 28 string in("12345"); 29 string out; 30 31 if (!WriteFile(filename, in)) { 32 return false; 33 } 34 if (!ReadFile(filename, &out)) { 35 return false; 36 } 37 if (out != in) { 38 return false; 39 } 40 return true; 41 } 42 43 // Cert request test 44 bool cert_test() { 45 X509_REQ* req = X509_REQ_new();; 46 X509* cert = X509_new(); 47 48 int size = 256; 49 string key_type("ECC"); 50 string common_name("Fred"); 51 string issuer("Fred"); 52 string keyUsage("critical,digitalSignature,keyEncipherment,keyAgreement,keyCertSign"); 53 string extendedKeyUsage("serverAuth,clientAuth"); 54 55 EVP_PKEY* self = GenerateKey(key_type, size); 56 if (self == nullptr) { 57 printf("Can't generate key.\n"); 58 return false; 59 } 60 if (!GenerateX509CertificateRequest(key_type, common_name, 61 self, false, req)) { 62 printf("Can't generate x509 request\n"); 63 return false; 64 } 65 66 if (!SignX509Certificate(self, true, true, issuer, keyUsage, 67 extendedKeyUsage, 86400, 68 self, req, false, cert)) { 69 printf("Can't sign x509 request\n"); 70 return false; 71 } 72 73 if(!VerifyX509CertificateChain(cert, cert)) { 74 printf("cert DOES NOT verifies\n"); 75 return false; 76 } 77 printf("cert verifies\n"); 78 return true; 79 } 80 81 // Verify chains test 82 bool verify_chains_test() { 83 return true; 84 } 85 86 // Key bytes test 87 bool key_bytes_test() { 88 string key_type("RSA"); 89 int key_size = 2048; 90 EVP_PKEY* key = GenerateKey(key_type, key_size); 91 if (key == nullptr) { 92 printf("GenerateKey fails\n"); 93 return false; 94 } 95 EVP_PKEY_free(key); 96 key = nullptr; 97 98 key_type = "ECC"; 99 key_size = 256; 100 key = GenerateKey(key_type, key_size); 101 if (key == nullptr) { 102 printf("GenerateKey fails\n"); 103 return false; 104 } 105 106 EVP_PKEY_free(key); 107 key = nullptr; 108 return true; 109 } 110 111 // Serialize/Deserialize tests 112 bool serialize_test() { 113 string key_type("RSA"); 114 int key_size = 2048; 115 string out_buf; 116 117 EVP_PKEY* key = GenerateKey(key_type, key_size); 118 if (key == nullptr) { 119 printf("GenerateKey fails\n"); 120 return false; 121 } 122 if (!SerializePrivateKey(key_type, key, &out_buf)) { 123 printf("SerializePrivateKey fails\n"); 124 return false; 125 } 126 string new_key_type; 127 EVP_PKEY* new_key = nullptr; 128 if (!DeserializePrivateKey(out_buf, &new_key_type, &new_key)) { 129 printf("DeserializePrivateKey fails\n"); 130 return false; 131 } 132 if (key->type != new_key->type) { 133 printf("Key type mismatch\n"); 134 return false; 135 } 136 RSA* rsa_key = EVP_PKEY_get1_RSA(key); 137 RSA* new_rsa_key = EVP_PKEY_get1_RSA(new_key); 138 if (rsa_key == nullptr || new_rsa_key == nullptr) { 139 return false; 140 } 141 if (rsa_key->n == nullptr || new_rsa_key->n == nullptr) { 142 return false; 143 } 144 if (BN_cmp(rsa_key->n, new_rsa_key->n) != 0) { 145 return false; 146 } 147 if (rsa_key->e == nullptr || new_rsa_key->e == nullptr) { 148 return false; 149 } 150 if (BN_cmp(rsa_key->e, new_rsa_key->e) != 0) { 151 return false; 152 } 153 if (rsa_key->d != nullptr && new_rsa_key->d != nullptr) { 154 if (BN_cmp(rsa_key->d, new_rsa_key->d) != 0) { 155 return false; 156 } 157 } 158 if (rsa_key->p != nullptr && new_rsa_key->p != nullptr) { 159 if (BN_cmp(rsa_key->p, new_rsa_key->p) != 0) { 160 return false; 161 } 162 } 163 if (rsa_key->q != nullptr && new_rsa_key->q != nullptr) { 164 if (BN_cmp(rsa_key->q, new_rsa_key->q) != 0) { 165 return false; 166 } 167 } 168 EVP_PKEY_free(key); 169 EVP_PKEY_free(new_key); 170 key = nullptr; 171 new_key = nullptr; 172 printf("RSA done\n"); 173 174 key_type = "ECC"; 175 key_size = 256; 176 out_buf.clear(); 177 new_key_type.clear(); 178 179 key = GenerateKey(key_type, key_size); 180 if (key == nullptr) { 181 printf("GenerateKey fails\n"); 182 return false; 183 } 184 if (!SerializePrivateKey(key_type, key, &out_buf)) { 185 printf("SerializePrivateKey fails\n"); 186 return false; 187 } 188 new_key = nullptr; 189 if (!DeserializePrivateKey(out_buf, &new_key_type, &new_key)) { 190 printf("DeserializePrivateKey fails\n"); 191 return false; 192 } 193 EC_KEY* ec_key = EVP_PKEY_get1_EC_KEY(key); 194 EC_KEY* new_ec_key = EVP_PKEY_get1_EC_KEY(new_key); 195 if (key->type != new_key->type) { 196 printf("Key type mismatch\n"); 197 return false; 198 } 199 if (ec_key == nullptr || new_ec_key == nullptr) { 200 return false; 201 } 202 const EC_GROUP* group = EC_KEY_get0_group(ec_key); 203 const EC_GROUP* new_group = EC_KEY_get0_group(new_ec_key); 204 if (group == nullptr || new_group == nullptr) { 205 return false; 206 } 207 208 byte out[4096]; 209 byte* ptr = out; 210 int n = i2d_ECPrivateKey(ec_key, &ptr); 211 if (n <= 0) { 212 printf("Can't i2d ECC private key\n"); 213 return false; 214 } 215 byte new_out[4096]; 216 byte* new_ptr = new_out; 217 int new_n = i2d_ECPrivateKey(new_ec_key, &new_ptr); 218 if (new_n <= 0) { 219 printf("Can't i2d ECC private key\n"); 220 return false; 221 } 222 if (n != new_n) { 223 return false; 224 } 225 if (memcmp(out, new_out, n) != 0) { 226 return false; 227 } 228 229 #if 0 230 BIGNUM order; 231 BIGNUM cofactor; 232 BN_CTX* bn_ctx = BN_CTX_new(); 233 const EC_POINT* generator = EC_GROUP_get0_generator(group); 234 int o1 = EC_GROUP_get_order(group, &order, bn_ctx); 235 int o2 = EC_GROUP_get_cofactor(group, &cofactor, bn_ctx); 236 BN_CTX_free(bn_ctx); 237 #endif 238 239 EVP_PKEY_free(key); 240 EVP_PKEY_free(new_key); 241 key = nullptr; 242 new_key = nullptr; 243 printf("ECC done\n"); 244 return true; 245 } 246 247 bool crypt_test() { 248 int n; 249 string key_type("RSA"); 250 int key_size = 2048; 251 EVP_PKEY* key = GenerateKey(key_type, key_size); 252 if (key == nullptr) { 253 return false; 254 } 255 256 byte in[20] = { 257 0,1,2,3,4,5,6,7,8,9, 258 0,1,2,3,4,5,6,7,8,9, 259 }; 260 byte out[512]; 261 262 RSA* rsa = EVP_PKEY_get1_RSA(key); 263 if (rsa == nullptr) { 264 return false; 265 } 266 n = RSA_public_encrypt(20, in, out, rsa, RSA_PKCS1_OAEP_PADDING); 267 if (n <= 0) { 268 return false; 269 } 270 271 byte recover[512]; 272 int m = RSA_private_decrypt(n, out, recover, rsa, RSA_PKCS1_OAEP_PADDING); 273 if (m != 20 || memcmp(in, recover, m) !=0) { 274 return false; 275 } 276 277 key_type = "ECC"; 278 key_size = 256; 279 key = GenerateKey(key_type, key_size); 280 if (key == nullptr) { 281 return false; 282 } 283 EC_KEY* ec_key = EVP_PKEY_get1_EC_KEY(key); 284 if (ec_key == nullptr) { 285 return false; 286 } 287 288 byte sig[8192]; 289 unsigned int len = 8192; 290 n = ECDSA_sign(0, in, 20, sig, &len, ec_key); 291 if (n <= 0) { 292 return false; 293 } 294 295 m = ECDSA_verify(0, in, 20, sig, len, ec_key); 296 if (m <= 0) { 297 return false; 298 } 299 return true; 300 } 301 302 303 TEST(cert_test, cert_test) { EXPECT_TRUE(cert_test()); } 304 TEST(ReadWriteTest, ReadWriteTest) { EXPECT_TRUE(readwritetest()); } 305 TEST(crypt_test, crypt_test) { EXPECT_TRUE(crypt_test()); } 306 TEST(verify_chains_test, verify_chains_test) { EXPECT_TRUE(verify_chains_test()); } 307 TEST(key_bytes_test, key_bytes_test) { EXPECT_TRUE(key_bytes_test()); } 308 TEST(serialize_test, serialize_test) { EXPECT_TRUE(serialize_test()); } 309 310 int main(int an, char** av) { 311 ::testing::InitGoogleTest(&an, av); 312 int result = RUN_ALL_TESTS(); 313 return result; 314 } 315