1 // Copyright 2012 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
15 #include <gtest/gtest.h>
16 #include <openssl/err.h>
17
18 #include <polo/pairing/polochallengeresponse.h>
19 #include <polo/util/poloutil.h>
20
21 namespace polo {
22 namespace pairing {
23
24 class PoloChallengeResponseTest : public ::testing::Test {
25 protected:
PoloChallengeResponseTest()26 PoloChallengeResponseTest() : nonce(4) { }
27
SetUp()28 virtual void SetUp() {
29 // Test certificates generated using:
30 // openssl req -x509 -nodes -days 365 -newkey rsa:1024 -out cert.pem
31
32 char client_pem[] = "-----BEGIN CERTIFICATE-----\n"
33 "MIICsDCCAhmgAwIBAgIJAI1seGT4bQoOMA0GCSqGSIb3DQEBBAUAMEUxCzAJBgNV\n"
34 "BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX\n"
35 "aWRnaXRzIFB0eSBMdGQwHhcNMTAxMjEyMTYwMzI3WhcNMTExMjEyMTYwMzI3WjBF\n"
36 "MQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50\n"
37 "ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB\n"
38 "gQDa7AitkkzqAZjsoJ3Y5eeq2LZtkF8xMWKuZMOaKDzOaTOBpfiFXbIsrOrHJvh0\n"
39 "WIUI7MEu4KTknpqyTEhwqyYozeOoJnhVVaKE03TQTMKgLhc4PwO35NJXHkFxJts1\n"
40 "OSCFZ7SQm8OMIr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58 "wtuIAI+Yty2UAjacvt3yqG2J1r55kIOsYeDoy1E5Hpo8gwIDAQABo4GnMIGkMB0G\n"
59 "A1UdDgQWBBRgMM6zsFJ2DGv7B1URsUmx1BBAPzB1BgNVHSMEbjBsgBRgMM6zsFJ2\n"
60 "DGv7B1URsUmx1BBAP6FJpEcwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUt\n"
61 "U3RhdGUxITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZIIJAPa14A4W\n"
62 "CQpNMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEEBQADgYEAoU/4pb2QTEgCwhzG\n"
63 "k6BPIz2WhOeIAAZ9fQmVxL5pbcgIUC4SnoJ3MtwB02Abbk5pIeSgtgJ50R4SmluM\n"
64 "T+0G1p772RqN+tLWihJqWgmODhfppUm9pp07UfL6yn4wAnyvzevadVXl6GCPocL9\n"
65 "cvcuBiBPlRU/giP3n15OtJ6KL9U=\n"
66 "-----END CERTIFICATE-----\n";
67
68 SSL_load_error_strings();
69
70 client_bio = BIO_new_mem_buf(client_pem, -1);
71 client_cert = PEM_read_bio_X509(client_bio, NULL, NULL, NULL);
72
73 server_bio = BIO_new_mem_buf(server_pem, -1);
74 server_cert = PEM_read_bio_X509(server_bio, NULL, NULL, NULL);
75
76 nonce[0] = 0x1;
77 nonce[1] = 0x2;
78 nonce[2] = 0x3;
79 nonce[3] = 0x4;
80
81 response = new PoloChallengeResponse(client_cert, server_cert);
82 }
83
TearDown()84 virtual void TearDown() {
85 X509_free(client_cert);
86 BIO_free(client_bio);
87
88 X509_free(server_cert);
89 BIO_free(server_bio);
90
91 delete response;
92 }
93
94 BIO* client_bio;
95 X509* client_cert;
96 BIO* server_bio;
97 X509* server_cert;
98 Nonce nonce;
99 PoloChallengeResponse* response;
100 };
101
TEST_F(PoloChallengeResponseTest,GetAlpha)102 TEST_F(PoloChallengeResponseTest, GetAlpha) {
103 const Alpha* alpha = response->GetAlpha(nonce);
104 ASSERT_TRUE(alpha);
105
106 ASSERT_EQ("E4DA87E4A544B30C98FC8A4731C10828506A97BA143950D7C68D9BF58ED4C397",
107 util::PoloUtil::BytesToHexString(&(*alpha)[0], alpha->size()));
108 delete alpha;
109 }
110
TEST_F(PoloChallengeResponseTest,TestGetGamma)111 TEST_F(PoloChallengeResponseTest, TestGetGamma) {
112 const Gamma* gamma = response->GetGamma(nonce);
113 ASSERT_TRUE(gamma);
114
115 ASSERT_EQ("E4DA87E401020304",
116 util::PoloUtil::BytesToHexString(&(*gamma)[0], gamma->size()));
117 delete gamma;
118 }
119
TEST_F(PoloChallengeResponseTest,TestExtractNonce)120 TEST_F(PoloChallengeResponseTest, TestExtractNonce) {
121 const Gamma* gamma = response->GetGamma(nonce);
122 ASSERT_TRUE(gamma);
123 ASSERT_EQ("E4DA87E401020304",
124 util::PoloUtil::BytesToHexString(&(*gamma)[0], gamma->size()));
125
126 const Nonce* extracted = response->ExtractNonce(*gamma);
127 ASSERT_TRUE(extracted);
128 ASSERT_EQ("01020304",
129 util::PoloUtil::BytesToHexString(&(*extracted)[0],
130 extracted->size()));
131
132 delete gamma;
133 delete extracted;
134 }
135
TEST_F(PoloChallengeResponseTest,TestCheckGamma)136 TEST_F(PoloChallengeResponseTest, TestCheckGamma) {
137 Gamma gamma(8);
138 gamma[0] = 0xE4;
139 gamma[1] = 0xDA;
140 gamma[2] = 0x87;
141 gamma[3] = 0xE4;
142 gamma[4] = 0x01;
143 gamma[5] = 0x02;
144 gamma[6] = 0x03;
145 gamma[7] = 0x04;
146
147 ASSERT_TRUE(response->CheckGamma(gamma));
148 }
149
150 } // namespace pairing
151 } // namespace polo
152