Exemple #1
0
func bworker(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.

	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("AES IV error: %v\n", e)
		return
	}
	debug.PrintBuffer(ivB, "B's IV = ")

	//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
	}

	debug.Printf("Reading A's Token")
	tokenA, e := ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(tokenA, "Token Ciphertext = ")

	// Send output to the server

	laddr := "127.0.0.1:8005"

	addr, e := net.ResolveTCPAddr("tcp", laddr)
	if e != nil {
		fmt.Printf("Cannot resolve address %s\n", laddr)
		return
	}
	sconn, e := net.DialTCP("tcp", nil, addr)
	if e != nil {
		fmt.Printf("Dialed failed on address %s\n", laddr)
		return
	}

	defer func() {
		sconn.Close()
	}()

	sobuff := utils.MakeTcpOEncoding(sconn)

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

	e = sobuff.WriteString(a)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

	e = sobuff.WriteString(b)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

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

	// Set up & send B's Token

	var tokenB ortokens.UserToken

	usernonce := make([]byte, 16)
	_, _ = rand.Read(usernonce)

	tokenB.UserNonce = usernonce[0:]
	tokenB.Nonce = nonce[0:]
	tokenB.A = a
	tokenB.B = b

	e = ortokens.WriteUserToken(ivB, keyB, &tokenB, sobuff)

	// Read Server Response

	sibuff := utils.MakeTcpIEncoding(sconn)

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

	if !bytes.Equal(rnonce, nonce) {
		fmt.Printf("Invalid nonce\n")
		return
	}

	debug.Printf("Reading A's Key Token")
	keytokenA, e := sibuff.ReadBinary()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(keytokenA, "Key Token Ciphertext = ")

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

	if !bytes.Equal(keytokenB.UserNonce, tokenB.UserNonce) {
		fmt.Printf("Invalid nonce\n")
		return
	}

	// Reply to A

	obuff := utils.MakeTcpOEncoding(conn)

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

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

	// Get cipphertext message

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

	debug.Printf("Reading protocol message ciphertext")
	ciphertext, e := ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}
	debug.PrintBuffer(ciphertext, "Ciphertext = ")

	debug.Printf("Decrypting ciphertext")
	t, e := utils.Decrypt(ortokens.AMP, iv, keytokenB.Key[0:], ciphertext)
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}
	debug.PrintBuffer(t, "Plaintext = ")

	sbuff := utils.MakeByteIEncoding(t)

	debug.Printf("Reading message")
	msg, e := sbuff.ReadString()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}
	debug.Printf("Message = %s", msg)

	// Send response

	pbuff := utils.MakeByteOEncoding(2048)

	//      e = pbuff.WriteString(strconv.Itoa(len(msg)))
	e = pbuff.WriteInteger(strconv.Itoa(len(msg)))
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}

	plaintext, e := pbuff.GetBuffer()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}

	ciphertext, e = utils.Encrypt(ortokens.AMP, iv, keytokenB.Key[0:], plaintext)
	if e != nil {
		fmt.Printf("Encryption error: %v\n", e)
		return
	}

	e = obuff.WriteBinary(ciphertext)
	if e != nil {
		fmt.Printf("Error: %s\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
	}

}
Exemple #3
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 #4
0
func bworker(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.

	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 = ")

	//Get input from TCP stream

	ibuff := utils.MakeTcpIEncoding(conn)
	obuff := utils.MakeTcpOEncoding(conn)

	token, e := nssktokens.ReadBToken(debug, ivB, keyB, ibuff)
	if e != nil {
		fmt.Printf("Error: %v\n", e)
		return
	}

	// Respond to A

	nonce := uint64(time.Now().Unix())
	debug.Printf("Nonce NB = %v", nonce)

	tbuff := utils.MakeByteOEncoding(2048)

	e = tbuff.WriteUint64(nonce)
	if e != nil {
		return
	}

	plaintext, e := tbuff.GetBuffer()
	if e != nil {
		return
	}

	iv, e := aeskey.Iv()
	if e != nil {
		fmt.Printf("AES IV error: %v\n", e)
		return
	}
	debug.PrintBuffer(iv, "Session IV = ")

	ciphertext, e := utils.Encrypt(nssktokens.AMP, iv, token.Key[0:], plaintext)
	if e != nil {
		return
	}

	e = obuff.WriteBinary(ciphertext)
	if e != nil {
		return
	}

	// Check A's response

	debug.Printf("Reading protocol message ciphertext")
	ciphertext, e = ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}
	debug.PrintBuffer(ciphertext, "Ciphertext = ")

	debug.Printf("Decrypting ciphertext")
	t, e := utils.Decrypt(nssktokens.AMP, iv, token.Key[0:], ciphertext)
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}
	debug.PrintBuffer(t, "Plaintext = ")

	sbuff := utils.MakeByteIEncoding(t)

	debug.Printf("Reading nonce-1")
	n, e := sbuff.ReadUint64()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}
	debug.Printf("NB-1 = %v", n)

	if n != nonce-1 {
		fmt.Printf("Ivalid nonce\n")
		return
	}

	// Get cipphertext message

	debug.Printf("Reading protocol message ciphertext")
	ciphertext, e = ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}
	debug.PrintBuffer(ciphertext, "Ciphertext = ")

	debug.Printf("Decrypting ciphertext")
	t, e = utils.Decrypt(nssktokens.AMP, iv, token.Key[0:], ciphertext)
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}
	debug.PrintBuffer(t, "Plaintext = ")

	sbuff = utils.MakeByteIEncoding(t)

	debug.Printf("Reading message")
	msg, e := sbuff.ReadString()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}
	debug.Printf("Message = %s", msg)

	// Send response

	//msg = strings.ToUpper(strings.Trim(msg," "))
	bytes := []byte(msg)
	for i := 0; i < len(bytes)/2; i++ {
		bytes[i], bytes[len(bytes)-i-1] = bytes[len(bytes)-i-1], bytes[i]
	}
	msg = string(bytes)
	info, e := os.Lstat("./.msg")
	if e == nil && info.Mode().IsRegular() {
		msg = "This is a fixed message to prevent cheating"
	}
	pbuff := utils.MakeByteOEncoding(2048)

	e = pbuff.WriteString(msg)
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}

	plaintext, e = pbuff.GetBuffer()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}

	ciphertext, e = utils.Encrypt(nssktokens.AMP, iv, token.Key[0:], plaintext)
	if e != nil {
		fmt.Printf("Encryption error: %v\n", e)
		return
	}

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

}