github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/src/apps/encode_auth.cc (about)

     1  //  Copyright (c) 2014, 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  #include <iostream>
    15  
    16  #include <gflags/gflags.h>
    17  #include <glog/logging.h>
    18  #include <google/protobuf/io/coded_stream.h>
    19  #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
    20  #include <google/protobuf/stubs/common.h>
    21  
    22  #include "auth.h"
    23  #include "tao/util.h"
    24  
    25  using google::protobuf::io::ArrayInputStream;
    26  using google::protobuf::io::CodedInputStream;
    27  using google::protobuf::io::CodedOutputStream;
    28  using google::protobuf::io::StringOutputStream;
    29  using std::string;
    30  using tao::Base64WEncode;
    31  using tao::Bytes;
    32  using tao::InitializeApp;
    33  using tao::make_unique;
    34  using tao::MarshalKeyPrin;
    35  using tao::MarshalSpeaksfor;
    36  using tao::Prin;
    37  using tao::PrinExt;
    38  using tao::SubPrin;
    39  using tao::Term;
    40  
    41  int main(int argc, char **argv) {
    42    InitializeApp(&argc, &argv, false);
    43  
    44    std::vector<std::unique_ptr<PrinExt>> v;
    45    v.push_back(make_unique<PrinExt>("Validated", std::vector<std::unique_ptr<Term>>()));
    46  
    47    Prin p("key", make_unique<Bytes>("These are not key bytes"),
    48           make_unique<SubPrin>(std::move(v)));
    49  
    50    string serialized_prin;
    51    {
    52      StringOutputStream raw_output_stream(&serialized_prin);
    53      CodedOutputStream output_stream(&raw_output_stream);
    54      p.Marshal(&output_stream);
    55    }
    56  
    57    string encoded_prin;
    58    if (!Base64WEncode(serialized_prin, &encoded_prin)) {
    59      LOG(FATAL) << "Could not encode the prin in Base64W";
    60    }
    61  
    62    std::cout << "A Prin encoded with Base64W:" << std::endl;
    63    std::cout << encoded_prin << std::endl;
    64  
    65    Prin unmarshalled_prin;
    66    {
    67      ArrayInputStream raw_input_stream(serialized_prin.data(),
    68                                        serialized_prin.size());
    69      CodedInputStream input_stream(&raw_input_stream);
    70      if (!unmarshalled_prin.Unmarshal(&input_stream)) {
    71        LOG(FATAL) << "Unmarshalling failed";
    72      }
    73    }
    74  
    75    if (unmarshalled_prin.type_ != "key") {
    76      LOG(ERROR) << "The unmarshalled prin had incorrect type '"
    77                 << unmarshalled_prin.type_ << "'";
    78    }
    79  
    80    auto unmarshalled_bytes =
    81        reinterpret_cast<Bytes*>(unmarshalled_prin.key_.get());
    82    if (unmarshalled_bytes->elt_ != "These are not key bytes") {
    83      LOG(ERROR) << "The unmarshalled bytes did not match the original bytes";
    84    }
    85  
    86    if (unmarshalled_prin.ext_->elts_.size() != 1) {
    87      LOG(ERROR) << "The unmarshalled prin did not have one extension";
    88    }
    89  
    90    // A fake key for the parent.
    91    string taoKeyName("test tao key");
    92    string taoName;
    93    if (!MarshalKeyPrin(taoKeyName, &taoName)) {
    94      LOG(FATAL) << "Could not marshal a fake key auth.Prin value";
    95    }
    96  
    97    // A dummy key string to encode as bytes in MarshalSpeaksfor
    98    string testKey("test key");
    99    string speaksfor;
   100    if (!MarshalSpeaksfor(testKey, taoName, &speaksfor)) {
   101      LOG(FATAL) << "Could not marshal a speaksfor statement";
   102    }
   103  
   104    string encoded;
   105    if (!Base64WEncode(speaksfor, &encoded)) {
   106      LOG(FATAL) << "Could not encode the speaksfor in Base64W";
   107    }
   108  
   109    std::cout << "A Speaksfor encoded with Base64W:" << std::endl;
   110    std::cout << encoded << std::endl;
   111    return 0;
   112  }