• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang util.MessageStream类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/jlmucb/cloudproxy/go/util.MessageStream的典型用法代码示例。如果您正苦于以下问题:Golang MessageStream类的具体用法?Golang MessageStream怎么用?Golang MessageStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了MessageStream类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: RunMessageLoop

// RunMessageLoop handles messages from a client on a given message stream until
// it gets an error trying to read a message.
func (m *ResourceMaster) RunMessageLoop(ms util.MessageStream, programPolicy *ProgramPolicy) error {
	for {
		var msg Message
		if err := ms.ReadMessage(&msg); err != nil {
			return err
		}

		var fop FileOperation
		t := *msg.Type
		if t == MessageType_CREATE || t == MessageType_DELETE || t == MessageType_READ || t == MessageType_WRITE {
			if err := proto.Unmarshal(msg.Data, &fop); err != nil {
				log.Printf("Couldn't unmarshal FileOperation for operation %d\n", t)
				continue
			}

			if err := m.checkFileAuth(&msg, &fop); err != nil {
				log.Printf("The file operation %d didn't pass authorization: %s\n", t, err)
				continue
			}
		}

		switch *msg.Type {
		case MessageType_AUTH_CERT:
			cert, err := m.AuthenticatePrincipal(ms, &msg, programPolicy)
			if err != nil {
				log.Printf("Failed to authenticate a principal: %s\n", err)
				continue
			}

			owner, err := principalNameFromDERCert(cert)
			if err != nil {
				log.Printf("Couldn't get the owner name from the cert: %s\n", err)
				continue
			}
			_, err = m.InsertPrincipal(owner, cert, Authenticated)
			if err != nil {
				log.Printf("Couldn't set the principal as authenticated")
			}
		case MessageType_CREATE:
			if err := m.Create(ms, &fop); err != nil {
				log.Printf("Couldn't create the file %s: %s\n", *fop.Name, err)
			}
		case MessageType_READ:
			if err := m.Read(ms, &fop, programPolicy.SymKeys); err != nil {
				log.Printf("Couldn't create the file %s: %s\n", *fop.Name, err)
			}
		case MessageType_WRITE:
			if err := m.Write(ms, &fop, programPolicy.SymKeys); err != nil {
				log.Printf("Couldn't create the file %s: %s\n", *fop.Name, err)
			}
		default:
			if err := sendResult(ms, false); err != nil {
				log.Printf("Couldn't signal failure for the invalid operation: %s", err)
			}
			log.Printf("Invalid initial message type %d\n", *msg.Type)
		}
	}
}
开发者ID:kevinawalsh,项目名称:cloudproxy,代码行数:60,代码来源:resources.go


示例2: checkResponse

// checkResponse waits for a RollbackResponse and checks to make sure it's not
// an ERROR response from the server.
func checkResponse(ms *util.MessageStream) error {
	var rr RollbackResponse
	if err := ms.ReadMessage(&rr); err != nil {
		return err
	}
	if *rr.Type == RollbackMessageType_ERROR {
		return fmt.Errorf("couldn't set the counter on the remote server")
	}
	return nil
}
开发者ID:William-J-Earl,项目名称:cloudproxy,代码行数:12,代码来源:rollback.go


示例3: sendError

func sendError(err error, ms *util.MessageStream) {
	var errStr = ""
	if err != nil {
		errStr = err.Error()
	}
	resp := &domain_service.DomainServiceResponse{ErrorMessage: &errStr}
	if _, err := ms.WriteMessage(resp); err != nil {
		log.Printf("domain_server: Error sending resp on the channel: %s\n ", err)
	}
}
开发者ID:tmroeder,项目名称:cloudproxy,代码行数:10,代码来源:domain_server.go


示例4: SendMessage

func SendMessage(ms *util.MessageStream, msg *SimpleMessage) error {
	out, err := proto.Marshal(msg)
	if err != nil {
		return errors.New("SendRequest: Can't encode response")
	}
	send := string(out)
	_, err = ms.WriteString(send)
	if err != nil {
		return errors.New("SendResponse: Writestring error")
	}
	return nil
}
开发者ID:tmroeder,项目名称:cloudproxy,代码行数:12,代码来源:taosupport.go


示例5: recvResult

// recvResult waits for a OperationResult on a MessageStream
func recvResult(ms util.MessageStream) (bool, error) {
	var m Message
	if err := ms.ReadMessage(&m); err != nil {
		return false, err
	}
	var res OperationResult
	if err := proto.Unmarshal(m.Data, &res); err != nil {
		return false, err
	}

	return *res.Result, nil
}
开发者ID:kevinawalsh,项目名称:cloudproxy,代码行数:13,代码来源:resources.go


示例6: GetMessage

func GetMessage(ms *util.MessageStream) (*SimpleMessage,
	error) {
	resp, err := ms.ReadString()
	if err != nil {
		return nil, err
	}
	msg := new(SimpleMessage)
	err = proto.Unmarshal([]byte(resp), msg)
	if err != nil {
		return nil, errors.New("GetResponse: Can't unmarshal message")
	}
	return msg, nil
}
开发者ID:tmroeder,项目名称:cloudproxy,代码行数:13,代码来源:taosupport.go


示例7: SetCounter

// SetCounter sets the remote counter for this program.
func SetCounter(ms *util.MessageStream, counter uint64) error {
	rm := &RollbackMessage{
		Type: RollbackMessageType_SET_COUNTER.Enum(),
		Data: EncodeCounter(counter),
	}
	if _, err := ms.WriteMessage(rm); err != nil {
		return err
	}

	// TODO(tmroeder): we currently ignore the value of the counter returned
	// by the server.
	return checkResponse(ms)
}
开发者ID:William-J-Earl,项目名称:cloudproxy,代码行数:14,代码来源:rollback.go


示例8: readResult

// readResult reads an OperationResult and returns its value or an error.
func readResult(ms util.MessageStream) (bool, error) {
	// Read the response wrapper message.
	var arm Message
	if err := ms.ReadMessage(&arm); err != nil {
		return false, err
	}
	if *arm.Type != MessageType_OP_RES {
		return false, fmt.Errorf("didn't receive OP_RES from the server")
	}

	var opr OperationResult
	if err := proto.Unmarshal(arm.Data, &opr); err != nil {
		return false, err
	}
	return *opr.Result, nil
}
开发者ID:kevinawalsh,项目名称:cloudproxy,代码行数:17,代码来源:resources.go


示例9: sendResult

// sendResult sends an OperationResult with the given value on the given stream.
func sendResult(ms util.MessageStream, result bool) error {
	res := &OperationResult{Result: proto.Bool(result)}
	ar := &Message{
		Type: MessageType_OP_RES.Enum(),
	}
	var err error
	if ar.Data, err = proto.Marshal(res); err != nil {
		return err
	}

	if _, err := ms.WriteMessage(ar); err != nil {
		return err
	}

	return nil
}
开发者ID:kevinawalsh,项目名称:cloudproxy,代码行数:17,代码来源:resources.go


示例10: sendCredentials

func (conn *Conn) sendCredentials(ms util.MessageStream) {
	if conn.Authenticate && conn.delegation != nil {
		ms.WriteString("delegation")
		ms.WriteMessage(conn.delegation)
	} else if conn.Authenticate {
		ms.WriteString("key")
	} else {
		ms.WriteString("anonymous")
	}
}
开发者ID:kevinawalsh,项目名称:cloudproxy,代码行数:10,代码来源:listener.go


示例11: RunMessageLoop

// RunMessageLoop handles incoming messages for the RollbackMaster and passes
// them to the appropriate functions.
func (r *RollbackMaster) RunMessageLoop(ms *util.MessageStream, programPolicy *ProgramPolicy, name string) error {
	for {
		var msg RollbackMessage
		if err := ms.ReadMessage(&msg); err != nil {
			return err
		}

		switch *msg.Type {
		case RollbackMessageType_SET_COUNTER:
			i, err := decodeCounter(msg.Data)
			if err != nil {
				log.Printf("failed to decode counter for SET_COUNTER: %s", err)
				continue
			}

			if err = r.SetCounter(ms, name, i); err != nil {
				log.Printf("failed to set the counter on the RollbackMaster: %s", err)
				continue
			}
		case RollbackMessageType_GET_COUNTER:
			if err := r.GetCounter(ms, name); err != nil {
				log.Printf("failed to get the counter for program %s", name)
				continue
			}
		case RollbackMessageType_SET_HASH:
			var rh RollbackHash
			if err := proto.Unmarshal(msg.Data, &rh); err != nil {
				log.Printf("failed to unmarshal the parameters for SET_HASH: %s", err)
				continue
			}
			if err := r.SetHash(ms, name, *rh.Item, rh.Hash); err != nil {
				log.Printf("failed to set the hash for item %s on program %s: %s", *rh.Item, name, err)
				continue
			}
		case RollbackMessageType_GET_HASHED_VERIFIER:
			if err := r.GetHashedVerifier(ms, name, string(msg.Data)); err != nil {
				log.Printf("failed to get the hashed verifier for program %s: %s", name, err)
				continue
			}
		default:
			log.Printf("unknown rollback message %d", *msg.Type)
		}
	}
}
开发者ID:William-J-Earl,项目名称:cloudproxy,代码行数:46,代码来源:rollback.go


示例12: SetHash

// SetHash sets the value of a hash for a given item for this program.
func SetHash(ms *util.MessageStream, item string, hash []byte) error {
	rh := &RollbackHash{
		Item: proto.String(item),
		Hash: hash,
	}
	rhb, err := proto.Marshal(rh)
	if err != nil {
		return err
	}
	rm := &RollbackMessage{
		Type: RollbackMessageType_SET_HASH.Enum(),
		Data: rhb,
	}
	if _, err := ms.WriteMessage(rm); err != nil {
		return err
	}

	return checkResponse(ms)
}
开发者ID:William-J-Earl,项目名称:cloudproxy,代码行数:20,代码来源:rollback.go


示例13: GetCounter

// GetCounter implements RollbackMessageType_GET_COUNTER and returns the current
// value of a counter to the requestor.
func (r *RollbackMaster) GetCounter(ms *util.MessageStream, name string) error {
	var emptyData []byte
	rr := &RollbackResponse{
		Type: RollbackMessageType_ERROR.Enum(),
		Data: emptyData,
	}

	p := r.FindRollbackProgram(name)
	if p == nil {
		if _, err := ms.WriteMessage(rr); err != nil {
			return err
		}
		return fmt.Errorf("couldn't find a rollback program with name %s", name)
	}

	rr.Type = RollbackMessageType_GET_COUNTER.Enum()
	rr.Data = EncodeCounter(p.MonotonicCounter)
	_, err := ms.WriteMessage(rr)
	return err
}
开发者ID:William-J-Earl,项目名称:cloudproxy,代码行数:22,代码来源:rollback.go


示例14: GetHashedVerifier

// GetHashedVerifier gets the hash of the counter and the item hash for a given
// item.
func GetHashedVerifier(ms *util.MessageStream, item string) ([]byte, error) {
	rm := &RollbackMessage{
		Type: RollbackMessageType_GET_HASHED_VERIFIER.Enum(),
		Data: []byte(item),
	}
	if _, err := ms.WriteMessage(rm); err != nil {
		return nil, err
	}

	// We can't use checkResponse here since we need to get the value out of
	// the response to read the hash.
	var rr RollbackResponse
	if err := ms.ReadMessage(&rr); err != nil {
		return nil, err
	}
	if *rr.Type == RollbackMessageType_ERROR {
		return nil, fmt.Errorf("couldn't set the counter on the remote server")
	}

	return rr.Data, nil
}
开发者ID:William-J-Earl,项目名称:cloudproxy,代码行数:23,代码来源:rollback.go


示例15: sendOperation

// sendOperation is a helper method that sets up the data structures needed for
// a FileOperation message like CREATE, WRITE, or READ, and sends this message
// on the MessageStream.
func sendOperation(ms util.MessageStream, mt MessageType, cert []byte, name string) error {
	fop := &FileOperation{
		Subject: cert,
		Name:    proto.String(name),
	}

	fopb, err := proto.Marshal(fop)
	if err != nil {
		return err
	}
	m := &Message{
		Type: mt.Enum(),
		Data: fopb,
	}

	if _, err := ms.WriteMessage(m); err != nil {
		return err
	}

	return nil
}
开发者ID:kevinawalsh,项目名称:cloudproxy,代码行数:24,代码来源:resources.go


示例16: AuthenticatePrincipal

// AuthenticatePrincipal is a client method used to send a request to a
// ResourceMaster to authenticate a principal with a given certificate and a
// given set of keys.
func AuthenticatePrincipal(ms util.MessageStream, key *tao.Keys, derCert []byte) error {
	// Send the authentication request, which supposes that a server is
	// waiting to receive this request.
	c := &Message{
		Type: MessageType_AUTH_CERT.Enum(),
		Data: derCert,
	}
	if _, err := ms.WriteMessage(c); err != nil {
		return err
	}

	// Receive a challenge nonce from the server.
	var nc Message
	if err := ms.ReadMessage(&nc); err != nil {
		return err
	}
	if *nc.Type != MessageType_NONCE_CHALL {
		return fmt.Errorf("didn't receive NONCE_CHALL from the server")
	}

	// Sign the nonce.
	sn := &Message{
		Type: MessageType_SIGNED_NONCE.Enum(),
	}
	var err error
	if sn.Data, err = key.SigningKey.Sign(nc.Data, ChallengeContext); err != nil {
		return err
	}
	if _, err := ms.WriteMessage(sn); err != nil {
		return err
	}

	// Get the result from the server after verificaton.
	res, err := readResult(ms)
	if err != nil {
		return err
	}

	if !res {
		return fmt.Errorf("the signed nonce failed verification")
	}
	return nil
}
开发者ID:kevinawalsh,项目名称:cloudproxy,代码行数:46,代码来源:resources.go


示例17: SendFile

// SendFile reads a file from disk and streams it to a receiver across a
// MessageStream. If there are sufficient bytes in the keys (at least
// hmacKeySize+aesKeySize), then it will attempt to check the integrity of the
// file with HMAC-SHA256 and decrypt it with AES-CTR-128.
func SendFile(ms util.MessageStream, dir string, filename string, keys []byte) error {
	fullpath := path.Join(dir, filename)
	fileInfo, err := os.Stat(fullpath)
	if err != nil {
		return fmt.Errorf("in SendFile: no file '%s' found: %s", fullpath, err)
	}
	file, err := os.Open(fullpath)
	if err != nil {
		return fmt.Errorf("in SendFile: can't open file '%s': %s", fullpath, err)
	}
	defer file.Close()

	// This encryption scheme uses AES-CTR with HMAC-SHA256 for integrity
	// protection.
	var hm hash.Hash
	var ctr cipher.Stream
	iv := make([]byte, ivSize)
	hasKeys := len(keys) >= minKeySize

	// The variable "left" gives the total number of bytes left to read from
	// the (maybe encrypted) file.
	left := fileInfo.Size()
	buf := make([]byte, bufferSize)
	if hasKeys {
		dec, err := aes.NewCipher(keys[:aesKeySize])
		if err != nil || dec == nil {
			return fmt.Errorf("can't create AES cipher in SendFile: %s", err)
		}
		if _, err := file.Read(iv); err != nil {
			return err
		}
		// Remove the IV from the number of remaining bytes to decrypt.
		left = left - ivSize

		// Take all the remaining key bytes for the HMAC key.
		hm = hmac.New(sha256.New, keys[aesKeySize:])
		hmacSize := hm.Size()

		// The HMAC input starts with the IV.
		hm.Write(iv)

		ctr = cipher.NewCTR(dec, iv)
		if ctr == nil {
			return fmt.Errorf("can't create AES-CTR encryption")
		}

		// Remove the HMAC-SHA256 output from the bytes to check.
		left = left - int64(hmacSize)

		// Secure decryption in this case requires reading the file
		// twice: once to check the MAC, and once to decrypt the bytes.
		// The MAC must be checked before *any* decryption occurs and
		// before *any* decrypted bytes are sent to the receiver.
		for {
			// Figure out how many bytes to read on this iteration.
			readSize := int64(bufferSize)
			final := false
			if left <= bufferSize {
				readSize = left
				final = true
			}

			// Read the (maybe encrypted) bytes from the file.
			n, err := file.Read(buf[:readSize])
			if err != nil {
				return err
			}
			left = left - int64(n)
			hm.Write(buf[:n])
			if final {
				break
			}
		}
		computed := hm.Sum(nil)
		original := buf[:hmacSize]

		// Read the file's version of the HMAC and check it securely
		// against the computed version.
		if _, err := file.Read(original); err != nil {
			return err
		}
		if !hmac.Equal(computed, original) {
			return fmt.Errorf("invalid file HMAC on decryption for file '%s'", fullpath)
		}

		// Go back to the beginning of the file (minus the IV) for
		// decryption.
		if _, err := file.Seek(ivSize, 0); err != nil {
			return fmt.Errorf("couldn't seek back to the beginning of file '%s': %s", fullpath, err)
		}

		// Reset the number of bytes so it only includes the encrypted
		// bytes.
		left = fileInfo.Size() - int64(ivSize+hmacSize)
	}

//.........这里部分代码省略.........
开发者ID:kevinawalsh,项目名称:cloudproxy,代码行数:101,代码来源:fileproxy.go


示例18: GetFile

// GetFile receives bytes from a sender and optionally encrypts them and adds
// integrity protection, and writes them to disk.
func GetFile(ms util.MessageStream, dir string, filename string, keys []byte) error {
	fullpath := path.Join(dir, filename)
	file, err := os.Create(fullpath)
	if err != nil {
		return fmt.Errorf("can't create file '%s' in GetFile", fullpath)
	}
	defer file.Close()

	var ctr cipher.Stream
	var hm hash.Hash
	iv := make([]byte, ivSize)

	hasKeys := len(keys) >= minKeySize
	if hasKeys {
		enc, err := aes.NewCipher(keys[:aesKeySize])
		if err != nil || enc == nil {
			return fmt.Errorf("couldn't create an AES cipher: %s", err)
		}

		// Use the remaining bytes of the key slice for the HMAC key.
		hm = hmac.New(sha256.New, keys[aesKeySize:])
		if _, err := rand.Read(iv); err != nil {
			return fmt.Errorf("couldn't read random bytes for a fresh IV: %s", err)
		}

		// The first bytes of the HMAC input are the IV.
		hm.Write(iv)
		ctr = cipher.NewCTR(enc, iv)
		if ctr == nil {
			return fmt.Errorf("couldn't create a new instance of AES-CTR-128")
		}
		if _, err = file.Write(iv); err != nil {
			return err
		}
	}

	// temp holds temporary encrypted ciphertext before it's written to
	// disk.
	temp := make([]byte, bufferSize)
	for {
		var m Message
		if err := ms.ReadMessage(&m); err != nil {
			return nil
		}

		// Sanity check: this must be FILE_LAST or FILE_NEXT.
		t := *m.Type
		if !(t == MessageType_FILE_LAST || t == MessageType_FILE_NEXT) {
			return fmt.Errorf("received invalid message type %d during file streaming in GetFile", t)
		}

		if hasKeys {
			l := len(m.Data)
			ctr.XORKeyStream(temp, m.Data)
			hm.Write(temp[:l])
			if _, err = file.Write(temp[:l]); err != nil {
				return err
			}
		} else {
			if _, err = file.Write(m.Data); err != nil {
				return err
			}
		}

		// FILE_LAST corresponds to receiving the final bytes of the
		// file.
		if *m.Type == MessageType_FILE_LAST {
			break
		}
	}

	// Write the MAC at the end of the file.
	if hasKeys {
		hmacBytes := hm.Sum(nil)
		if _, err = file.Write(hmacBytes[:]); err != nil {
			return err
		}
	}

	return nil
}
开发者ID:kevinawalsh,项目名称:cloudproxy,代码行数:83,代码来源:fileproxy.go


示例19: recvCredentials

func (conn *Conn) recvCredentials(ms util.MessageStream) {
	m, err := ms.ReadString()
	if err != nil {
		return
	}
	if m == "delegation" {
		var a Attestation
		if err = ms.ReadMessage(&a); err != nil {
			return
		}
		// Validate the peer certificate
		peerCert := conn.ConnectionState().PeerCertificates[0]
		p, err := ValidatePeerAttestation(&a, peerCert)
		if err != nil {
			ms.SetErr(err)
			return
		}
		if conn.guard != nil {
			if conn.verifier != nil {
				if err = AddEndorsements(conn.guard, &a, conn.verifier); err != nil {
					ms.SetErr(err)
					return
				}
			}
			if !conn.guard.IsAuthorized(p, "Connect", nil) {
				ms.SetErr(errors.New("principal delegator in client attestation is not authorized to connect"))
				return
			}
		}
		conn.peer = &p
	} else if m == "key" {
		peerCert := conn.ConnectionState().PeerCertificates[0]
		v, err := FromX509(peerCert)
		if err != nil {
			ms.SetErr(errors.New("can't decode key from peer certificate"))
			return
		}
		p := v.ToPrincipal()
		conn.peer = &p
	} else if m == "anonymous" {
		if conn.guard != nil {
			err = errors.New("peer did not provide tao delegation")
			ms.SetErr(err)
			return
		}
	} else {
		err = errors.New("unrecognized authentication handshake: " + m)
		ms.SetErr(err)
		return
	}
}
开发者ID:kevinawalsh,项目名称:cloudproxy,代码行数:51,代码来源:listener.go


示例20: sendResponse

func sendResponse(ms util.MessageStream, resp *taoca.Response) {
	_, err := ms.WriteMessage(resp)
	if err != nil {
		fmt.Printf("error writing response: %s\n", err)
	}
}
开发者ID:kevinawalsh,项目名称:taoca,代码行数:6,代码来源:server.go



注:本文中的github.com/jlmucb/cloudproxy/go/util.MessageStream类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang options.FailIf函数代码示例发布时间:2022-05-23
下一篇:
Golang util.NewMessageStream函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap