github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/apps/simpleexample/SimpleClient/simpleclient.go (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  //     http://www.apache.org/licenses/LICENSE-2.0
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // File: simpleclient.go
    14  
    15  package main
    16  
    17  import (
    18  	"flag"
    19  	"fmt"
    20  	"io/ioutil"
    21  	"log"
    22  	"os"
    23  	"path"
    24  
    25  	"github.com/jlmucb/cloudproxy/go/apps/simpleexample/common"
    26  	taosupport "github.com/jlmucb/cloudproxy/go/support_libraries/tao_support"
    27  	"github.com/jlmucb/cloudproxy/go/tao"
    28  )
    29  
    30  var caAddr = flag.String("caAddr", "localhost:8124", "The address to listen on")
    31  var simpleCfg = flag.String("domain_config",
    32  	"./tao.config",
    33  	"path to tao configuration")
    34  var simpleClientPath = flag.String("path",
    35  	"./SimpleClient",
    36  	"path to SimpleClient files")
    37  var testRollback = flag.Bool("test_rollback", false, "test rollback?")
    38  var serverHost = flag.String("host", "localhost", "address for client/server")
    39  var serverPort = flag.String("port", "8123", "port for client/server")
    40  var useSimpleDomainService = flag.Bool("use_simpledomainservice", true,
    41  	"whether to use simple domain service")
    42  var serverAddr string
    43  
    44  func main() {
    45  
    46  	// This holds the cloudproxy specific data for simpleclient
    47  	// including the Program Cert and Program Private key.
    48  	var clientProgramData taosupport.TaoProgramData
    49  
    50  	// Make sure we zero keys when we're done.
    51  	defer clientProgramData.ClearTaoProgramData()
    52  
    53  	// Parse flags
    54  	flag.Parse()
    55  	serverAddr = *serverHost + ":" + *serverPort
    56  
    57  	// If TaoParadigm completes without error, clientProgramData contains all the
    58  	// Cloudproxy information needed throughout simpleclient execution.
    59  	err := taosupport.TaoParadigm(simpleCfg, simpleClientPath,
    60  		*useSimpleDomainService, *caAddr, &clientProgramData)
    61  	if err != nil {
    62  		log.Fatalln("simpleclient: Can't establish Tao: ", err)
    63  	}
    64  	fmt.Printf("simpleclient: TaoParadigm complete, name: %s\n",
    65  		clientProgramData.TaoName)
    66  
    67  	if *testRollback {
    68  		err = tao.Parent().InitCounter("label", 0)
    69  		if err != nil {
    70  			fmt.Printf("simpleClient: Error return from InitCounter %s\n", err)
    71  		} else {
    72  			fmt.Printf("simpleClient: InitCounter, no error\n")
    73  		}
    74  		c, err := tao.Parent().GetCounter("label")
    75  		if err != nil {
    76  			fmt.Printf("simpleClient: Error Return from GetCounter %d %s\n", c, err)
    77  		} else {
    78  			fmt.Printf("simpleclient: GetCounter successful %d\n", c)
    79  		}
    80  		data := []byte{
    81  			0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5,
    82  			0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5}
    83  		sealed, err := tao.Parent().RollbackProtectedSeal("label", data,
    84  			tao.SealPolicyDefault) // REMOVE
    85  		if err != nil {
    86  			fmt.Printf("simpleClient: Error Return from RollbackProtectedSeal %s\n", err)
    87  		} else {
    88  			fmt.Printf("simpleClient: RollbackProtectedSeal successful %x\n", sealed)
    89  		}
    90  		c, err = tao.Parent().GetCounter("label")
    91  		if err != nil {
    92  			fmt.Printf("simpleClient: Error Return from GetCounter %d %s\n", c, err)
    93  		} else {
    94  			fmt.Printf("simpleclient: GetCounter successful %d\n", c)
    95  		}
    96  		recoveredData, _, err := tao.Parent().RollbackProtectedUnseal(sealed)
    97  		if err != nil {
    98  			fmt.Printf("simpleClient: Error Return from RollbackProtectedUnseal %s\n", err)
    99  		} else {
   100  			fmt.Printf("simpleClient: RollbackProtectedUnseal successful %x\n", recoveredData)
   101  		}
   102  		fmt.Printf("data: %x, recovered data: %x\n", data, recoveredData)
   103  	}
   104  
   105  	// Open the Tao Channel using the Program key. This program does all the
   106  	// standard channel negotiation and presents the secure server name
   107  	// after negotiation is complete.
   108  	ms, serverName, err := taosupport.OpenTaoChannel(&clientProgramData,
   109  		&serverAddr)
   110  	if err != nil {
   111  		log.Fatalln("simpleclient: Can't establish Tao Channel")
   112  	}
   113  	log.Printf("simpleclient: establish Tao Channel with %s, %s\n",
   114  		serverAddr, serverName)
   115  
   116  	// Send a simple request and get response.
   117  	// We have a simple service protobuf for requests and reponsed between
   118  	// simpleclient and simpleserver.  There's only on request: tell me the
   119  	// secret.
   120  	secretRequest := "SecretRequest"
   121  
   122  	msg := new(simpleexample_messages.SimpleMessage)
   123  	msg.RequestType = &secretRequest
   124  	simpleexample_messages.SendRequest(ms, msg)
   125  	if err != nil {
   126  		log.Fatalln("simpleclient: Error in response to SendRequest\n")
   127  	}
   128  	respmsg, err := simpleexample_messages.GetResponse(ms)
   129  	if err != nil {
   130  		log.Fatalln("simpleclient: Error in response to GetResponse\n")
   131  	}
   132  
   133  	// This is the secret.
   134  	retrieveSecret := respmsg.Data[0]
   135  
   136  	// Encrypt and store the secret in simpleclient's save area.
   137  	out, err := clientProgramData.ProgramCryptingKey.Encrypt(retrieveSecret)
   138  	if err != nil {
   139  		log.Fatalln("simpleclient: can't encrypt retrieved secret\n")
   140  	}
   141  	err = ioutil.WriteFile(path.Join(*simpleClientPath,
   142  		"retrieved_secret"), out, os.ModePerm)
   143  	if err != nil {
   144  		log.Fatalln("simpleclient: error saving retrieved secret\n")
   145  	}
   146  
   147  	// Close down.
   148  	log.Printf("simpleclient: secret is %s, done\n", retrieveSecret)
   149  }