コード例 #1
0
ファイル: client_msg.go プロジェクト: jddixon/upax_go
// msgN, token including DigSig; gets Ack or Error
func (upc *UpaxClient) IntroAndAck() (err error) {
	var (
		name                       string
		id, ckBytes, skBytes, salt []byte
		digSig                     []byte // over name, id, ckBytes, skBytes, salt, in order
	)
	// Send INTRO MSG =====================================
	name = upc.GetName()
	id = upc.GetNodeID().Value()
	ckBytes, err = xc.RSAPubKeyToWire(&upc.ckPriv.PublicKey)
	if err == nil {
		skBytes, err = xc.RSAPubKeyToWire(&upc.skPriv.PublicKey)
		if err == nil {
			rng := xr.NewSystemRNG(0)
			n := uint64(rng.Int63())
			salt = make([]byte, 8)
			binary.LittleEndian.PutUint64(salt, n)
		}
	}
	if err == nil {
		d := sha1.New()
		d.Write([]byte(name))
		d.Write(id)
		d.Write(ckBytes)
		d.Write(skBytes)
		d.Write(salt)
		hash := d.Sum(nil)
		digSig, err = rsa.SignPKCS1v15(
			rand.Reader, upc.skPriv, crypto.SHA1, hash)
	}
	if err == nil {
		token := UpaxClientMsg_Token{
			Name:     &name,
			ID:       id,
			CommsKey: ckBytes,
			SigKey:   skBytes,
			Salt:     salt,
			DigSig:   digSig,
		}
		op := UpaxClientMsg_Intro
		request := &UpaxClientMsg{
			Op:         &op,
			ClientInfo: &token,
		}
		// SHOULD CHECK FOR TIMEOUT
		err = upc.writeMsg(request)
	}
	// Process ACK ========================================
	if err == nil {
		var response *UpaxClientMsg

		// SHOULD CHECK FOR TIMEOUT AND VERIFY THAT IT'S AN ACK
		response, err = upc.readMsg()
		op := response.GetOp()
		if op != UpaxClientMsg_Ack {
			err = ExpectedAck
		}
	}
	return
}
コード例 #2
0
ファイル: client_msg.go プロジェクト: jddixon/upax_go
// msgN, id, salt, sig; gets Ack or Error
func (upc *UpaxClient) ItsMeAndAck() (err error) {
	var (
		id, salt []byte
		digSig   []byte // over id, salt, in order
	)
	// Send ITS_ME MSG ====================================

	id = upc.GetNodeID().Value()
	rng := xr.NewSystemRNG(0)
	n := uint64(rng.Int63())
	salt = make([]byte, 8)
	binary.LittleEndian.PutUint64(salt, n)

	d := sha1.New()
	d.Write(id)
	d.Write(salt)
	hash := d.Sum(nil)
	digSig, err = rsa.SignPKCS1v15(
		rand.Reader, upc.skPriv, crypto.SHA1, hash)

	if err == nil {
		op := UpaxClientMsg_ItsMe
		request := &UpaxClientMsg{
			Op:   &op,
			ID:   id,
			Salt: salt,
			Sig:  digSig,
		}
		// SHOULD CHECK FOR TIMEOUT
		err = upc.writeMsg(request)
	}
	// Process ACK ========================================
	if err == nil {
		var response *UpaxClientMsg

		// SHOULD CHECK FOR TIMEOUT
		response, err = upc.readMsg()
		op := response.GetOp()
		if op != UpaxClientMsg_Ack {
			err = ExpectedAck
		}
	}

	return
}