Home | History | Annotate | Download | only in trunks
      1 //
      2 // Copyright (C) 2015 The Android Open Source Project
      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 #include "trunks/blob_parser.h"
     18 
     19 #include <base/logging.h>
     20 #include <base/stl_util.h>
     21 
     22 #include "trunks/error_codes.h"
     23 
     24 namespace trunks {
     25 
     26 bool BlobParser::SerializeKeyBlob(const TPM2B_PUBLIC& public_info,
     27                                   const TPM2B_PRIVATE& private_info,
     28                                   std::string* key_blob) {
     29   CHECK(key_blob) << "KeyBlob not defined.";
     30   key_blob->clear();
     31   if ((public_info.size == 0) && (private_info.size == 0)) {
     32     return true;
     33   }
     34   TPM_RC result = Serialize_TPM2B_PUBLIC(public_info, key_blob);
     35   if (result != TPM_RC_SUCCESS) {
     36     LOG(ERROR) << "Error serializing public info: " << GetErrorString(result);
     37     return false;
     38   }
     39   result = Serialize_TPM2B_PRIVATE(private_info, key_blob);
     40   if (result != TPM_RC_SUCCESS) {
     41     LOG(ERROR) << "Error serializing private info: " << GetErrorString(result);
     42     return false;
     43   }
     44   return true;
     45 }
     46 
     47 bool BlobParser::ParseKeyBlob(const std::string& key_blob,
     48                               TPM2B_PUBLIC* public_info,
     49                               TPM2B_PRIVATE* private_info) {
     50   CHECK(public_info) << "Public info not defined.";
     51   CHECK(private_info) << "Private info not defined.";
     52   if (key_blob.empty()) {
     53     public_info->size = 0;
     54     private_info->size = 0;
     55     return true;
     56   }
     57   std::string mutable_key_blob = key_blob;
     58   TPM_RC result = Parse_TPM2B_PUBLIC(&mutable_key_blob, public_info, nullptr);
     59   if (result != TPM_RC_SUCCESS) {
     60     LOG(ERROR) << "Error parsing public info: " << GetErrorString(result);
     61     return false;
     62   }
     63   result = Parse_TPM2B_PRIVATE(&mutable_key_blob, private_info, nullptr);
     64   if (result != TPM_RC_SUCCESS) {
     65     LOG(ERROR) << "Error parsing private info: " << GetErrorString(result);
     66     return false;
     67   }
     68   return true;
     69 }
     70 
     71 bool BlobParser::SerializeCreationBlob(const TPM2B_CREATION_DATA& creation_data,
     72                                        const TPM2B_DIGEST& creation_hash,
     73                                        const TPMT_TK_CREATION& creation_ticket,
     74                                        std::string* creation_blob) {
     75   CHECK(creation_blob) << "CreationBlob not defined.";
     76   creation_blob->clear();
     77   TPM_RC result = Serialize_TPM2B_CREATION_DATA(creation_data, creation_blob);
     78   if (result != TPM_RC_SUCCESS) {
     79     LOG(ERROR) << "Error serializing creation_data: " << GetErrorString(result);
     80     return false;
     81   }
     82   result = Serialize_TPM2B_DIGEST(creation_hash, creation_blob);
     83   if (result != TPM_RC_SUCCESS) {
     84     LOG(ERROR) << "Error serializing creation_hash: " << GetErrorString(result);
     85     return false;
     86   }
     87   result = Serialize_TPMT_TK_CREATION(creation_ticket, creation_blob);
     88   if (result != TPM_RC_SUCCESS) {
     89     LOG(ERROR) << "Error serializing creation_ticket: "
     90                << GetErrorString(result);
     91     return false;
     92   }
     93   return true;
     94 }
     95 
     96 bool BlobParser::ParseCreationBlob(const std::string& creation_blob,
     97                                    TPM2B_CREATION_DATA* creation_data,
     98                                    TPM2B_DIGEST* creation_hash,
     99                                    TPMT_TK_CREATION* creation_ticket) {
    100   CHECK(creation_data) << "CreationData not defined.";
    101   CHECK(creation_hash) << "CreationHash not defined.";
    102   CHECK(creation_ticket) << "CreationTicket not defined.";
    103   if (creation_blob.empty()) {
    104     return false;
    105   }
    106   std::string mutable_creation_blob = creation_blob;
    107   TPM_RC result =
    108       Parse_TPM2B_CREATION_DATA(&mutable_creation_blob, creation_data, nullptr);
    109   if (result != TPM_RC_SUCCESS) {
    110     LOG(ERROR) << "Error parsing creation_data: " << GetErrorString(result);
    111     return false;
    112   }
    113   result = Parse_TPM2B_DIGEST(&mutable_creation_blob, creation_hash, nullptr);
    114   if (result != TPM_RC_SUCCESS) {
    115     LOG(ERROR) << "Error parsing creation_hash: " << GetErrorString(result);
    116     return false;
    117   }
    118   result =
    119       Parse_TPMT_TK_CREATION(&mutable_creation_blob, creation_ticket, nullptr);
    120   if (result != TPM_RC_SUCCESS) {
    121     LOG(ERROR) << "Error parsing creation_ticket: " << GetErrorString(result);
    122     return false;
    123   }
    124   return true;
    125 }
    126 
    127 }  // namespace trunks
    128