Beispiel #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
	}

}
Beispiel #2
0
func main() {
	var help = flag.Bool("h", false, "help")
	var ip = flag.String("i", "127.0.0.1", "ip address")
	var port = flag.String("p", "8006", "port")
	flag.Parse()
	if *help || flag.NArg() != 1 {
		fmt.Printf("USAGE: ora <string>\n")
		flag.PrintDefaults()
		return
	}

	utils.Version()

	debug := utils.NewDebug(utils.USER, "OR A")

	keyA, e := aeskey.KeyA()
	if e != nil {
		fmt.Printf("AES key error: %v\n", 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 = ")

	laddr := "" + *ip + ":" + *port

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

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

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

	fmt.Printf("Connected to remote address %s\n", conn.RemoteAddr())
	fmt.Printf("Connected from local address %s\n", conn.LocalAddr())

	obuff := utils.MakeTcpOEncoding(conn)

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

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

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

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

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

	// Set up & send A's Token

	var tokenA ortokens.UserToken

	tokenA.UserNonce = usernonce[0:]
	tokenA.Nonce = nonce[0:]
	tokenA.A = "student"
	tokenA.B = "lecturer"

	e = ortokens.WriteUserToken(ivA, keyA, &tokenA, obuff)

	// Read B's Response

	ibuff := utils.MakeTcpIEncoding(conn)

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

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

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

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

	// Send ciphertext

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

	pbuff := utils.MakeByteOEncoding(2048)

	e = pbuff.WriteString(flag.Arg(0))
	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, keytokenA.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
	}

	// Get cipphertext 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(ortokens.AMP, iv, keytokenA.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.ReadInteger()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}

	fmt.Printf("%s\n", msg)

}