17c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet// Copyright 2012 Google Inc. All Rights Reserved.
27c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet//
37c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet// Licensed under the Apache License, Version 2.0 (the "License");
47c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet// you may not use this file except in compliance with the License.
57c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet// You may obtain a copy of the License at
67c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet//
77c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet//     http://www.apache.org/licenses/LICENSE-2.0
87c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet//
97c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet// Unless required by applicable law or agreed to in writing, software
107c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet// distributed under the License is distributed on an "AS IS" BASIS,
117c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet// See the License for the specific language governing permissions and
137c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet// limitations under the License.
147c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
157c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet#include <gtest/gtest.h>
167c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet#include <openssl/err.h>
177c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
187c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet#include <polo/pairing/polochallengeresponse.h>
197c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet#include <polo/util/poloutil.h>
207c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
217c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetnamespace polo {
227c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetnamespace pairing {
237c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
247c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetclass PoloChallengeResponseTest : public ::testing::Test {
257c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet protected:
267c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  PoloChallengeResponseTest() : nonce(4) { }
277c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
287c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  virtual void SetUp() {
297c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    // Test certificates generated using:
307c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    // openssl req -x509 -nodes -days 365 -newkey rsa:1024 -out cert.pem
317c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
327c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    char client_pem[] = "-----BEGIN CERTIFICATE-----\n"
337c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "MIICsDCCAhmgAwIBAgIJAI1seGT4bQoOMA0GCSqGSIb3DQEBBAUAMEUxCzAJBgNV\n"
347c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX\n"
357c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "aWRnaXRzIFB0eSBMdGQwHhcNMTAxMjEyMTYwMzI3WhcNMTExMjEyMTYwMzI3WjBF\n"
367c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "MQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50\n"
377c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB\n"
387c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "gQDa7AitkkzqAZjsoJ3Y5eeq2LZtkF8xMWKuZMOaKDzOaTOBpfiFXbIsrOrHJvh0\n"
397c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "WIUI7MEu4KTknpqyTEhwqyYozeOoJnhVVaKE03TQTMKgLhc4PwO35NJXHkFxJts1\n"
407c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "OSCFZ7SQm8OMIr6eEMLh6v7UQQ/GryNY+v5SYiVsbfgW3QIDAQABo4GnMIGkMB0G\n"
417c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "A1UdDgQWBBRBiLSqlUt+9ZXMBLBp141te487bTB1BgNVHSMEbjBsgBRBiLSqlUt+\n"
427c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "9ZXMBLBp141te487baFJpEcwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUt\n"
437c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "U3RhdGUxITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZIIJAI1seGT4\n"
447c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "bQoOMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEEBQADgYEAchrbHb8S0WCGRupi\n"
457c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "lxwnD6aVVmVsnNiOaLSI1I6RCKeS0SG/fseThd9nh92WZh6Rbx3U3rAMD08wDfSt\n"
467c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "S9h7bukJ0X9Rs/BTirzT7Cl09PUjoawP8MeLEDFRUzcBsSYr/k/IPAWOrazWQ2tu\n"
477c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "XO5L5nPKzpxd3tF4Aj4/3kBm4nw=\n"
487c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "-----END CERTIFICATE-----\n";
497c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
507c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    char server_pem[] = "-----BEGIN CERTIFICATE-----\n"
517c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "MIICsDCCAhmgAwIBAgIJAPa14A4WCQpNMA0GCSqGSIb3DQEBBAUAMEUxCzAJBgNV\n"
527c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX\n"
537c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "aWRnaXRzIFB0eSBMdGQwHhcNMTAxMjEyMTYwNzMzWhcNMTExMjEyMTYwNzMzWjBF\n"
547c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "MQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50\n"
557c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB\n"
567c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "gQDBkfualV4+vxIEBg1TWXy2T1nf0Dch8XoQG824o3EAzuIRHdBGHvzRNfmQOlje\n"
577c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "XVU/Cds376EYOblxoZNVNQYMf1fkwTUnDWXNl3wR5A4m4Govi2y61b7NA8/AMxO9\n"
587c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "wtuIAI+Yty2UAjacvt3yqG2J1r55kIOsYeDoy1E5Hpo8gwIDAQABo4GnMIGkMB0G\n"
597c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "A1UdDgQWBBRgMM6zsFJ2DGv7B1URsUmx1BBAPzB1BgNVHSMEbjBsgBRgMM6zsFJ2\n"
607c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "DGv7B1URsUmx1BBAP6FJpEcwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUt\n"
617c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "U3RhdGUxITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZIIJAPa14A4W\n"
627c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "CQpNMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEEBQADgYEAoU/4pb2QTEgCwhzG\n"
637c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "k6BPIz2WhOeIAAZ9fQmVxL5pbcgIUC4SnoJ3MtwB02Abbk5pIeSgtgJ50R4SmluM\n"
647c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "T+0G1p772RqN+tLWihJqWgmODhfppUm9pp07UfL6yn4wAnyvzevadVXl6GCPocL9\n"
657c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "cvcuBiBPlRU/giP3n15OtJ6KL9U=\n"
667c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        "-----END CERTIFICATE-----\n";
677c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
687c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    SSL_load_error_strings();
697c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
707c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    client_bio = BIO_new_mem_buf(client_pem, -1);
717c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    client_cert = PEM_read_bio_X509(client_bio, NULL, NULL, NULL);
727c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
737c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    server_bio = BIO_new_mem_buf(server_pem, -1);
747c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    server_cert = PEM_read_bio_X509(server_bio, NULL, NULL, NULL);
757c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
767c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    nonce[0] = 0x1;
777c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    nonce[1] = 0x2;
787c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    nonce[2] = 0x3;
797c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    nonce[3] = 0x4;
807c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
817c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    response = new PoloChallengeResponse(client_cert, server_cert);
827c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  }
837c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
847c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  virtual void TearDown() {
857c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    X509_free(client_cert);
867c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    BIO_free(client_bio);
877c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
887c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    X509_free(server_cert);
897c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    BIO_free(server_bio);
907c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
917c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    delete response;
927c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  }
937c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
947c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  BIO* client_bio;
957c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  X509* client_cert;
967c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  BIO* server_bio;
977c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  X509* server_cert;
987c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  Nonce nonce;
997c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  PoloChallengeResponse* response;
1007c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet};
1017c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
1027c9978567a202d6aa98beac5da5e1b3b34792862Jerome PoichetTEST_F(PoloChallengeResponseTest, GetAlpha) {
1037c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  const Alpha* alpha = response->GetAlpha(nonce);
1047c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  ASSERT_TRUE(alpha);
1057c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
1067c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  ASSERT_EQ("E4DA87E4A544B30C98FC8A4731C10828506A97BA143950D7C68D9BF58ED4C397",
1077c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet            util::PoloUtil::BytesToHexString(&(*alpha)[0], alpha->size()));
1087c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  delete alpha;
1097c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet}
1107c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
1117c9978567a202d6aa98beac5da5e1b3b34792862Jerome PoichetTEST_F(PoloChallengeResponseTest, TestGetGamma) {
1127c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  const Gamma* gamma = response->GetGamma(nonce);
1137c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  ASSERT_TRUE(gamma);
1147c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
1157c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  ASSERT_EQ("E4DA87E401020304",
1167c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet            util::PoloUtil::BytesToHexString(&(*gamma)[0], gamma->size()));
1177c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  delete gamma;
1187c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet}
1197c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
1207c9978567a202d6aa98beac5da5e1b3b34792862Jerome PoichetTEST_F(PoloChallengeResponseTest, TestExtractNonce) {
1217c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  const Gamma* gamma = response->GetGamma(nonce);
1227c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  ASSERT_TRUE(gamma);
1237c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  ASSERT_EQ("E4DA87E401020304",
1247c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet            util::PoloUtil::BytesToHexString(&(*gamma)[0], gamma->size()));
1257c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
1267c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  const Nonce* extracted = response->ExtractNonce(*gamma);
1277c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  ASSERT_TRUE(extracted);
1287c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  ASSERT_EQ("01020304",
1297c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet            util::PoloUtil::BytesToHexString(&(*extracted)[0],
1307c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet                                             extracted->size()));
1317c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
1327c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  delete gamma;
1337c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  delete extracted;
1347c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet}
1357c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
1367c9978567a202d6aa98beac5da5e1b3b34792862Jerome PoichetTEST_F(PoloChallengeResponseTest, TestCheckGamma) {
1377c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  Gamma gamma(8);
1387c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  gamma[0] = 0xE4;
1397c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  gamma[1] = 0xDA;
1407c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  gamma[2] = 0x87;
1417c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  gamma[3] = 0xE4;
1427c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  gamma[4] = 0x01;
1437c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  gamma[5] = 0x02;
1447c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  gamma[6] = 0x03;
1457c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  gamma[7] = 0x04;
1467c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
1477c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  ASSERT_TRUE(response->CheckGamma(gamma));
1487c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet}
1497c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
1507c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet}  // namespace pairing
1517c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet}  // namespace polo
152