github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/tao/linux_host_tao_rpc.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  //
     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  
    15  package tao
    16  
    17  // This provides a server stub for LinuxHost's Tao RPC interface. This code is
    18  // (mostly) extremely dull and, ideally, would be generated automatically. The
    19  // only mildly interesting thing it does is hold some state associated with each
    20  // connection, and pass that as a parameter to each server function.
    21  
    22  import (
    23  	"errors"
    24  	"io"
    25  	"net/rpc"
    26  
    27  	"github.com/golang/protobuf/proto"
    28  	"github.com/jlmucb/cloudproxy/go/tao/auth"
    29  	"github.com/jlmucb/cloudproxy/go/util/protorpc"
    30  )
    31  
    32  // LinuxHostTaoServer is a server stub for LinuxHost's Tao RPC interface.
    33  type LinuxHostTaoServer struct {
    34  	lh    *LinuxHost
    35  	child *LinuxHostChild
    36  }
    37  
    38  type linuxHostTaoServerStub LinuxHostTaoServer
    39  
    40  // NewLinuxHostTaoServer returns a new server stub for LinuxHost's Tao RPC
    41  // interface.
    42  func NewLinuxHostTaoServer(host *LinuxHost, child *LinuxHostChild) LinuxHostTaoServer {
    43  	return LinuxHostTaoServer{host, child}
    44  }
    45  
    46  // Serve listens on sock for new connections and services them.
    47  func (server LinuxHostTaoServer) Serve(conn io.ReadWriteCloser) error {
    48  	s := rpc.NewServer()
    49  	err := s.RegisterName("Tao", linuxHostTaoServerStub(server))
    50  	if err != nil {
    51  		return err
    52  	}
    53  	s.ServeCodec(protorpc.NewServerCodec(conn))
    54  	return nil
    55  }
    56  
    57  // GetTaoName is the server stub for Tao.GetTaoName.
    58  func (server linuxHostTaoServerStub) GetTaoName(r *RPCRequest, s *RPCResponse) error {
    59  	s.Data = auth.Marshal(server.lh.GetTaoName(server.child))
    60  	return nil
    61  }
    62  
    63  // ExtendTaoName is the server stub for Tao.ExtendTaoName.
    64  func (server linuxHostTaoServerStub) ExtendTaoName(r *RPCRequest, s *RPCResponse) error {
    65  	ext, err := auth.UnmarshalSubPrin(r.Data)
    66  	if err != nil {
    67  		return err
    68  	}
    69  	return server.lh.ExtendTaoName(server.child, ext)
    70  }
    71  
    72  // GetRandomBytes is the server stub for Tao.GetRandomBytes.
    73  func (server linuxHostTaoServerStub) GetRandomBytes(r *RPCRequest, s *RPCResponse) error {
    74  	if r.Size == nil || *r.Size <= 0 {
    75  		return newError("invalid size")
    76  	}
    77  	data, err := server.lh.GetRandomBytes(server.child, int(*r.Size))
    78  	s.Data = data
    79  	return err
    80  }
    81  
    82  // GetSharedSecret is the server stub for Tao.GetSharedSecret.
    83  func (server linuxHostTaoServerStub) GetSharedSecret(r *RPCRequest, s *RPCResponse) error {
    84  	if r.Size == nil || *r.Size <= 0 {
    85  		return newError("invalid size")
    86  	}
    87  	if r.Policy == nil {
    88  		return newError("missing policy")
    89  	}
    90  	data, err := server.lh.GetSharedSecret(server.child, int(*r.Size), *r.Policy)
    91  	s.Data = data
    92  	return err
    93  }
    94  
    95  // Seal is the server stub for Tao.Seal.
    96  func (server linuxHostTaoServerStub) Seal(r *RPCRequest, s *RPCResponse) error {
    97  	if r.Policy == nil {
    98  		return newError("missing policy")
    99  	}
   100  	data, err := server.lh.Seal(server.child, r.Data, *r.Policy)
   101  	s.Data = data
   102  	return err
   103  }
   104  
   105  // Unseal is the server stub for Tao.Unseal.
   106  func (server linuxHostTaoServerStub) Unseal(r *RPCRequest, s *RPCResponse) error {
   107  	data, policy, err := server.lh.Unseal(server.child, r.Data)
   108  	s.Data = data
   109  	s.Policy = proto.String(policy)
   110  	return err
   111  }
   112  
   113  // Attest is the server stub for Tao.Attest.
   114  func (server linuxHostTaoServerStub) Attest(r *RPCRequest, s *RPCResponse) error {
   115  	stmt, err := auth.UnmarshalForm(r.Data)
   116  	if err != nil {
   117  		return err
   118  	}
   119  	var issuer *auth.Prin
   120  	if r.Issuer != nil {
   121  		p, err := auth.UnmarshalPrin(r.Issuer)
   122  		if err != nil {
   123  			return err
   124  		}
   125  		issuer = &p
   126  	}
   127  	a, err := server.lh.Attest(server.child, issuer, r.Time, r.Expiration, stmt)
   128  	if err != nil {
   129  		return err
   130  	}
   131  	s.Data, err = proto.Marshal(a)
   132  	return err
   133  }
   134  
   135  // InitCounter initializes counter.
   136  func (server linuxHostTaoServerStub) InitCounter(r *RPCRequest, s *RPCResponse) error {
   137  	// fmt.Printf("linuxHostTaoServerStub.InitCounter called %s\n", server.child.ChildSubprin.String()) // REMOVE
   138  	if r.Label == nil || r.Counter == nil {
   139  		return errors.New("Label or counter unspecified")
   140  	}
   141  	err := server.lh.InitCounter(server.child, *r.Label, *r.Counter)
   142  	return err
   143  }
   144  
   145  // GetCounter gets counter
   146  func (server linuxHostTaoServerStub) GetCounter(r *RPCRequest, s *RPCResponse) error {
   147  	if r.Label == nil {
   148  		return errors.New("Label unspecified")
   149  	}
   150  	c, err := server.lh.GetCounter(server.child, *r.Label)
   151  	if err != nil {
   152  		return err
   153  	}
   154  	s.Counter = &c
   155  	return err
   156  }
   157  
   158  // RollbackProtectedSeal does a rollback protected seal
   159  func (server linuxHostTaoServerStub) RollbackProtectedSeal(r *RPCRequest, s *RPCResponse) error {
   160  	if r.Label == nil {
   161  		return errors.New("Label unspecified")
   162  	}
   163  	if r.Policy == nil {
   164  		return errors.New("Policy unspecified")
   165  	}
   166  	sealed, err := server.lh.RollbackProtectedSeal(server.child, *r.Label, r.Data, *r.Policy)
   167  	if err != nil {
   168  		return err
   169  	}
   170  	s.Data = sealed
   171  	return err
   172  }
   173  
   174  // RollbackProtectedUnseal does a rollback protected Unseal
   175  func (server linuxHostTaoServerStub) RollbackProtectedUnseal(r *RPCRequest, s *RPCResponse) error {
   176  	if r.Data == nil {
   177  		return errors.New("Data unspecified")
   178  	}
   179  	data, policy, err := server.lh.RollbackProtectedUnseal(server.child, r.Data)
   180  	if err != nil {
   181  		return err
   182  	}
   183  	s.Data = data
   184  	s.Policy = &policy
   185  	return nil
   186  }