Exemple #1
0
func sworker(name string, conn *net.TCPConn) {
	debug := utils.NewDebug(utils.USER, name)
	defer func() {
		debug.Printf("... %s worker finished.", name)
		conn.Close()
	}()

	debug.Printf("%s worker connected to remote address %s", name, conn.RemoteAddr())

	// Obtain keys etc.

	keyA, e := aeskey.KeyA()
	if e != nil {
		fmt.Printf("%s AES key error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(keyA, "A's Key = ")

	ivA, e := aeskey.IvA()
	if e != nil {
		fmt.Printf("AES IV error: %v\n", e)
		return
	}
	debug.PrintBuffer(ivA, "A's IV = ")

	keyB, e := aeskey.KeyB()
	if e != nil {
		fmt.Printf("%s AES key error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(keyB, "B's Key = ")

	ivB, e := aeskey.IvB()
	if e != nil {
		fmt.Printf("AES IV error: %v\n", e)
		return
	}
	debug.PrintBuffer(ivB, "B's IV = ")

	sessionKey, e := aeskey.SessionKey()
	if e != nil {
		fmt.Printf("%s AES key error: %v\n", name, e)
		return
	}

	//Get input from TCP stream

	ibuff := utils.MakeTcpIEncoding(conn)

	debug.Printf("Reading A")
	a, e := ibuff.ReadString()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.Printf("A = %v", a)

	if a != "student" {
		fmt.Printf("Incorrect name for A\n")
		return
	}

	debug.Printf("Reading B")
	b, e := ibuff.ReadString()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.Printf("B = %v", b)

	if b != "lecturer" {
		fmt.Printf("Incorrect name for B\n")
		return
	}

	debug.Printf("Reading nonce N")
	nonce, e := ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(nonce, "Nonce N = ")

	// Send output to TCP stream

	obuff := utils.MakeTcpOEncoding(conn)

	// Set up & send B's Key Token

	var token_B nssktokens.BToken

	token_B.A = a
	token_B.Key = sessionKey[0:]

	ciphertext, e := nssktokens.WriteBToken(debug, ivB, keyB, &token_B)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

	// Set up & send A's Key Token

	var token_A nssktokens.AToken

	token_A.Nonce = nonce[0:]
	token_A.B = b
	token_A.Key = sessionKey[0:]
	token_A.CipherText = ciphertext
	e = nssktokens.WriteAToken(debug, ivA, keyA, &token_A, obuff)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

}
Exemple #2
0
func sworker(name string, conn *net.TCPConn) {
	debug := utils.NewDebug(utils.USER, name)
	defer func() {
		debug.Printf("... %s worker finished.", name)
		conn.Close()
	}()

	debug.Printf("%s worker connected to remote address %s", name, conn.RemoteAddr())

	// Obtain keys etc.

	keyA, e := aeskey.KeyA()
	if e != nil {
		fmt.Printf("%s AES key error: %v\n", name, e)
		return
	}

	ivA, e := aeskey.IvA()
	if e != nil {
		fmt.Printf("%s AES IV error: %v\n", name, e)
		return
	}

	keyB, e := aeskey.KeyB()
	if e != nil {
		fmt.Printf("%s AES key error: %v\n", name, e)
		return
	}

	ivB, e := aeskey.IvB()
	if e != nil {
		fmt.Printf("%s AES IV error: %v\n", name, e)
		return
	}

	sessionKey, e := aeskey.SessionKey()
	if e != nil {
		fmt.Printf("%s AES key error: %v\n", name, e)
		return
	}

	//Get input from TCP stream

	ibuff := utils.MakeTcpIEncoding(conn)

	debug.Printf("Reading nonce N")
	nonce, e := ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(nonce, "Nonce N = ")

	debug.Printf("Reading A")
	a, e := ibuff.ReadString()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.Printf("A = %v", a)

	if a != "student" {
		fmt.Printf("Incorrect name for A\n")
		return
	}

	debug.Printf("Reading B")
	b, e := ibuff.ReadString()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.Printf("B = %v", b)

	if b != "lecturer" {
		fmt.Printf("Incorrect name for B\n")
		return
	}

	tokenA, e := ortokens.ReadUserToken(debug, "A", ivA, keyA, ibuff)
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}

	if tokenA.A != a || tokenA.B != b || !bytes.Equal(tokenA.Nonce, nonce) {
		fmt.Printf("Invalid token for A\n")
		return
	}

	tokenB, e := ortokens.ReadUserToken(debug, "B", ivB, keyB, ibuff)
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}

	if tokenB.A != a || tokenB.B != b || !bytes.Equal(tokenB.Nonce, nonce) {
		fmt.Printf("Invalid token for B\n")
		return
	}

	// Send output to TCP stream

	obuff := utils.MakeTcpOEncoding(conn)

	e = obuff.WriteBinary(nonce)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

	// Set up & send A's Key Token

	var keytokenA ortokens.KeyToken

	keytokenA.UserNonce = tokenA.UserNonce[0:]
	keytokenA.Key = sessionKey[0:]

	e = ortokens.WriteKeyToken(ivA, keyA, &keytokenA, obuff)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

	// Set up & send B's Key Token

	var keytokenB ortokens.KeyToken

	keytokenB.UserNonce = tokenB.UserNonce[0:]
	keytokenB.Key = sessionKey[0:]

	e = ortokens.WriteKeyToken(ivB, keyB, &keytokenB, obuff)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

}