Home | History | Annotate | Download | only in unittests
      1 /*############################################################################
      2 # Copyright 2017 Intel Corporation
      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 #
      8 #     http://www.apache.org/licenses/LICENSE-2.0
      9 #
     10 # Unless required by applicable law or agreed to in writing, software
     11 # distributed under the License is distributed on an "AS IS" BASIS,
     12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 # See the License for the specific language governing permissions and
     14 # limitations under the License.
     15 ############################################################################*/
     16 
     17 /*!
     18  * \file
     19  * \brief Member credentials storage helper API unit tests.
     20  */
     21 #include <cstring>
     22 
     23 #include "epid/common-testhelper/epid_gtest-testhelper.h"
     24 #include "gtest/gtest.h"
     25 
     26 #include "epid/common-testhelper/epid2params_wrapper-testhelper.h"
     27 #include "epid/common-testhelper/errors-testhelper.h"
     28 #include "epid/common-testhelper/prng-testhelper.h"
     29 #include "epid/member/tpm2/unittests/tpm2-testhelper.h"
     30 #include "epid/member/unittests/member-testhelper.h"
     31 
     32 extern "C" {
     33 #include "epid/member/src/storage.h"
     34 #include "epid/member/tpm2/nv.h"
     35 }
     36 
     37 namespace {
     38 
     39 TEST_F(EpidMemberTest, NvWriteMembershipCredentialFailsGivenNullPointer) {
     40   Prng my_prng;
     41   Epid2ParamsObj epid2params;
     42   Tpm2CtxObj tpm(&Prng::Generate, &my_prng, nullptr, epid2params);
     43 
     44   uint32_t nv_index = 0x01c10100;
     45   GroupPubKey const pub_key = this->kGroupPublicKey;
     46   // PrivKey can be trimed to MembershipCredential
     47   MembershipCredential const credential =
     48       *(MembershipCredential*)&this->kMemberPrivateKey;
     49 
     50   EXPECT_EQ(kEpidBadArgErr, EpidNvWriteMembershipCredential(
     51                                 nullptr, &pub_key, &credential, nv_index));
     52   EXPECT_EQ(kEpidBadArgErr, EpidNvWriteMembershipCredential(
     53                                 tpm, nullptr, &credential, nv_index));
     54   EXPECT_EQ(kEpidBadArgErr,
     55             EpidNvWriteMembershipCredential(tpm, &pub_key, nullptr, nv_index));
     56   // cleanup nv_index for next test
     57   Tpm2NvUndefineSpace(tpm, nv_index);
     58 }
     59 
     60 TEST_F(EpidMemberTest, NvReadMembershipCredentialFailsGivenNoCredentials) {
     61   Prng my_prng;
     62   Epid2ParamsObj epid2params;
     63   Tpm2CtxObj tpm(&Prng::Generate, &my_prng, nullptr, epid2params);
     64 
     65   uint32_t nv_index = 0x01c10101;
     66   GroupPubKey pub_key = this->kGroupPublicKey;
     67   // PrivKey can be trimed to MembershipCredential
     68   MembershipCredential credential =
     69       *(MembershipCredential*)&this->kMemberPrivateKey;
     70 
     71   EXPECT_EQ(kEpidBadArgErr, EpidNvReadMembershipCredential(
     72                                 tpm, nv_index, &pub_key, &credential));
     73 }
     74 
     75 TEST_F(EpidMemberTest, NvReadMembershipCredentialFailsGivenNullPointer) {
     76   Prng my_prng;
     77   Epid2ParamsObj epid2params;
     78   Tpm2CtxObj tpm(&Prng::Generate, &my_prng, nullptr, epid2params);
     79 
     80   uint32_t nv_index = 0x01c10100;
     81   GroupPubKey pub_key = this->kGroupPublicKey;
     82   // PrivKey can be trimed to MembershipCredential
     83   MembershipCredential credential =
     84       *(MembershipCredential*)&this->kMemberPrivateKey;
     85 
     86   // write credentials
     87   EXPECT_EQ(kEpidNoErr, EpidNvWriteMembershipCredential(tpm, &pub_key,
     88                                                         &credential, nv_index));
     89 
     90   EXPECT_EQ(kEpidBadArgErr, EpidNvReadMembershipCredential(
     91                                 nullptr, nv_index, &pub_key, &credential));
     92   EXPECT_EQ(kEpidBadArgErr, EpidNvReadMembershipCredential(
     93                                 tpm, nv_index, nullptr, &credential));
     94   EXPECT_EQ(kEpidBadArgErr,
     95             EpidNvReadMembershipCredential(tpm, nv_index, &pub_key, nullptr));
     96   // cleanup nv_index for next test
     97   Tpm2NvUndefineSpace(tpm, nv_index);
     98 }
     99 
    100 TEST_F(EpidMemberTest, WrittenMembershipCredentialCanBeRead) {
    101   Prng my_prng;
    102   Epid2ParamsObj epid2params;
    103   Tpm2CtxObj tpm(&Prng::Generate, &my_prng, nullptr, epid2params);
    104 
    105   uint32_t nv_index = 0x01c10100;
    106   GroupPubKey pub_key = this->kGroupPublicKey;
    107   // PrivKey can be trimed to MembershipCredential
    108   MembershipCredential credential_expected =
    109       *(MembershipCredential*)&this->kMemberPrivateKey;
    110   MembershipCredential credential;
    111 
    112   // write credentials
    113   EXPECT_EQ(kEpidNoErr, EpidNvWriteMembershipCredential(
    114                             tpm, &pub_key, &credential_expected, nv_index));
    115 
    116   // read credentials
    117   EXPECT_EQ(kEpidNoErr, EpidNvReadMembershipCredential(tpm, nv_index, &pub_key,
    118                                                        &credential));
    119 
    120   EXPECT_EQ(this->kGroupPublicKey, pub_key);
    121   EXPECT_EQ(credential_expected, credential);
    122   // cleanup nv_index for next test
    123   Tpm2NvUndefineSpace(tpm, nv_index);
    124 }
    125 
    126 }  // namespace
    127