Esempio n. 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
	}

}
Esempio n. 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
	}

}
Esempio n. 3
0
func contact_S(debug utils.Debug, ip string, port string) (token nssktokens.AToken, e error) {
	e = nil

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

	nonce := make([]byte, 16)
	_, _ = rand.Read(nonce)
	debug.PrintBuffer(nonce, "Nonce N = ")

	// connect to the server

	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)

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

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

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

	// Read S's Response

	ibuff := utils.MakeTcpIEncoding(conn)

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

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

	if token.B != b {
		fmt.Printf("Invalid B in token\n")
		return
	}
	return
}
Esempio n. 4
0
File: ora.go Progetto: optimuse/spig
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)

}