Home | History | Annotate | Download | only in client
      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 "attestation/client/dbus_proxy.h"
     18 
     19 #include <brillo/bind_lambda.h>
     20 #include <brillo/dbus/dbus_method_invoker.h>
     21 
     22 #include "attestation/common/dbus_interface.h"
     23 
     24 namespace {
     25 
     26 // Use a two minute timeout because TPM operations can take a long time and
     27 // there may be a few of them queued up.
     28 const int kDBusTimeoutMS = 120000;
     29 
     30 }  // namespace
     31 
     32 namespace attestation {
     33 
     34 DBusProxy::DBusProxy() {}
     35 DBusProxy::~DBusProxy() {
     36   if (bus_) {
     37     bus_->ShutdownAndBlock();
     38   }
     39 }
     40 
     41 bool DBusProxy::Initialize() {
     42   dbus::Bus::Options options;
     43   options.bus_type = dbus::Bus::SYSTEM;
     44   bus_ = new dbus::Bus(options);
     45   object_proxy_ = bus_->GetObjectProxy(
     46       attestation::kAttestationServiceName,
     47       dbus::ObjectPath(attestation::kAttestationServicePath));
     48   return (object_proxy_ != nullptr);
     49 }
     50 
     51 void DBusProxy::CreateGoogleAttestedKey(
     52     const CreateGoogleAttestedKeyRequest& request,
     53     const CreateGoogleAttestedKeyCallback& callback) {
     54   auto on_error = [callback](brillo::Error* error) {
     55     CreateGoogleAttestedKeyReply reply;
     56     reply.set_status(STATUS_NOT_AVAILABLE);
     57     callback.Run(reply);
     58   };
     59   brillo::dbus_utils::CallMethodWithTimeout(
     60       kDBusTimeoutMS,
     61       object_proxy_,
     62       attestation::kAttestationInterface,
     63       attestation::kCreateGoogleAttestedKey,
     64       callback,
     65       base::Bind(on_error),
     66       request);
     67 }
     68 
     69 void DBusProxy::GetKeyInfo(const GetKeyInfoRequest& request,
     70                            const GetKeyInfoCallback& callback) {
     71   auto on_error = [callback](brillo::Error* error) {
     72     GetKeyInfoReply reply;
     73     reply.set_status(STATUS_NOT_AVAILABLE);
     74     callback.Run(reply);
     75   };
     76   brillo::dbus_utils::CallMethodWithTimeout(
     77       kDBusTimeoutMS,
     78       object_proxy_,
     79       attestation::kAttestationInterface,
     80       attestation::kGetKeyInfo,
     81       callback,
     82       base::Bind(on_error),
     83       request);
     84 }
     85 
     86 void DBusProxy::GetEndorsementInfo(const GetEndorsementInfoRequest& request,
     87                                    const GetEndorsementInfoCallback& callback) {
     88   auto on_error = [callback](brillo::Error* error) {
     89     GetEndorsementInfoReply reply;
     90     reply.set_status(STATUS_NOT_AVAILABLE);
     91     callback.Run(reply);
     92   };
     93   brillo::dbus_utils::CallMethodWithTimeout(
     94       kDBusTimeoutMS,
     95       object_proxy_,
     96       attestation::kAttestationInterface,
     97       attestation::kGetEndorsementInfo,
     98       callback,
     99       base::Bind(on_error),
    100       request);
    101 }
    102 
    103 void DBusProxy::GetAttestationKeyInfo(
    104     const GetAttestationKeyInfoRequest& request,
    105     const GetAttestationKeyInfoCallback& callback) {
    106   auto on_error = [callback](brillo::Error* error) {
    107     GetAttestationKeyInfoReply reply;
    108     reply.set_status(STATUS_NOT_AVAILABLE);
    109     callback.Run(reply);
    110   };
    111   brillo::dbus_utils::CallMethodWithTimeout(
    112       kDBusTimeoutMS,
    113       object_proxy_,
    114       attestation::kAttestationInterface,
    115       attestation::kGetAttestationKeyInfo,
    116       callback,
    117       base::Bind(on_error),
    118       request);
    119 }
    120 
    121 void DBusProxy::ActivateAttestationKey(
    122     const ActivateAttestationKeyRequest& request,
    123     const ActivateAttestationKeyCallback& callback) {
    124   auto on_error = [callback](brillo::Error* error) {
    125     ActivateAttestationKeyReply reply;
    126     reply.set_status(STATUS_NOT_AVAILABLE);
    127     callback.Run(reply);
    128   };
    129   brillo::dbus_utils::CallMethodWithTimeout(
    130       kDBusTimeoutMS,
    131       object_proxy_,
    132       attestation::kAttestationInterface,
    133       attestation::kActivateAttestationKey,
    134       callback,
    135       base::Bind(on_error),
    136       request);
    137 }
    138 
    139 void DBusProxy::CreateCertifiableKey(
    140     const CreateCertifiableKeyRequest& request,
    141     const CreateCertifiableKeyCallback& callback) {
    142   auto on_error = [callback](brillo::Error* error) {
    143     CreateCertifiableKeyReply reply;
    144     reply.set_status(STATUS_NOT_AVAILABLE);
    145     callback.Run(reply);
    146   };
    147   brillo::dbus_utils::CallMethodWithTimeout(
    148       kDBusTimeoutMS,
    149       object_proxy_,
    150       attestation::kAttestationInterface,
    151       attestation::kCreateCertifiableKey,
    152       callback,
    153       base::Bind(on_error),
    154       request);
    155 }
    156 
    157 void DBusProxy::Decrypt(const DecryptRequest& request,
    158                         const DecryptCallback& callback) {
    159   auto on_error = [callback](brillo::Error* error) {
    160     DecryptReply reply;
    161     reply.set_status(STATUS_NOT_AVAILABLE);
    162     callback.Run(reply);
    163   };
    164   brillo::dbus_utils::CallMethodWithTimeout(
    165       kDBusTimeoutMS,
    166       object_proxy_,
    167       attestation::kAttestationInterface,
    168       attestation::kDecrypt,
    169       callback,
    170       base::Bind(on_error),
    171       request);
    172 }
    173 
    174 void DBusProxy::Sign(const SignRequest& request, const SignCallback& callback) {
    175   auto on_error = [callback](brillo::Error* error) {
    176     SignReply reply;
    177     reply.set_status(STATUS_NOT_AVAILABLE);
    178     callback.Run(reply);
    179   };
    180   brillo::dbus_utils::CallMethodWithTimeout(
    181       kDBusTimeoutMS,
    182       object_proxy_,
    183       attestation::kAttestationInterface,
    184       attestation::kSign,
    185       callback,
    186       base::Bind(on_error),
    187       request);
    188 }
    189 
    190 void DBusProxy::RegisterKeyWithChapsToken(
    191     const RegisterKeyWithChapsTokenRequest& request,
    192     const RegisterKeyWithChapsTokenCallback& callback) {
    193   auto on_error = [callback](brillo::Error* error) {
    194     RegisterKeyWithChapsTokenReply reply;
    195     reply.set_status(STATUS_NOT_AVAILABLE);
    196     callback.Run(reply);
    197   };
    198   brillo::dbus_utils::CallMethodWithTimeout(
    199       kDBusTimeoutMS,
    200       object_proxy_,
    201       attestation::kAttestationInterface,
    202       attestation::kRegisterKeyWithChapsToken,
    203       callback,
    204       base::Bind(on_error),
    205       request);
    206 }
    207 
    208 }  // namespace attestation
    209