Beispiel #1
0
func (handler *PairSetup) ServeHTTP(response http.ResponseWriter, request *http.Request) {
	log.Printf("[VERB] %v POST /pair-setup", request.RemoteAddr)
	response.Header().Set("Content-Type", netio.HTTPContentTypePairingTLV8)

	key := handler.context.GetConnectionKey(request)
	session := handler.context.Get(key).(netio.Session)
	controller := session.PairSetupHandler()
	if controller == nil {
		log.Println("[VERB] Create new pair setup controller")
		var err error
		controller, err = pair.NewSetupServerController(handler.device, handler.database)
		if err != nil {
			log.Println(err)
		}

		session.SetPairSetupHandler(controller)
	}

	res, err := pair.HandleReaderForHandler(request.Body, controller)

	if err != nil {
		log.Println("[ERRO]", err)
		response.WriteHeader(http.StatusInternalServerError)
	} else {
		io.Copy(response, res)
	}
}
Beispiel #2
0
func (handler *PairVerify) ServeHTTP(response http.ResponseWriter, request *http.Request) {
	log.Printf("[VERB] %v POST /pair-verify", request.RemoteAddr)
	response.Header().Set("Content-Type", netio.HTTPContentTypePairingTLV8)

	key := handler.context.GetConnectionKey(request)
	session := handler.context.Get(key).(netio.Session)
	controller := session.PairVerifyHandler()
	if controller == nil {
		log.Println("[VERB] Create new pair verify controller")
		controller = pair.NewVerifyServerController(handler.database, handler.context)
		session.SetPairVerifyHandler(controller)
	}

	res, err := pair.HandleReaderForHandler(request.Body, controller)

	if err != nil {
		log.Println(err)
		response.WriteHeader(http.StatusInternalServerError)
	} else {
		io.Copy(response, res)
		// Setup secure session
		if controller.KeyVerified() == true {
			// Verification is done
			// Switch to secure session
			secureSession, err := crypto.NewSecureSessionFromSharedKey(controller.SharedKey())
			if err != nil {
				log.Println("[ERRO] Could not setup secure session.", err)
			} else {
				log.Println("[VERB] Setup secure session")
			}
			session.SetCryptographer(secureSession)
		}
	}
}
Beispiel #3
0
func (handler *Pairing) ServeHTTP(response http.ResponseWriter, request *http.Request) {
	log.Printf("[VERB] %v POST /pairings", request.RemoteAddr)
	response.Header().Set("Content-Type", netio.HTTPContentTypePairingTLV8)

	res, err := pair.HandleReaderForHandler(request.Body, handler.controller)

	if err != nil {
		log.Println(err)
		response.WriteHeader(http.StatusInternalServerError)
	} else {
		io.Copy(response, res)
	}
}
Beispiel #4
0
func main() {
	database, _ := db.NewDatabase("./data")
	c, _ := netio.NewDevice("Golang Client", database)
	client := pair.NewSetupClientController("740-51-881", c, database)
	pairStartRequest := client.InitialPairingRequest()

	pairStartResponse, err := pairSetup(pairStartRequest)
	if err != nil {
		log.Fatal(err)
	}

	// 2) S -> C
	pairVerifyRequest, err := pair.HandleReaderForHandler(pairStartResponse, client)
	if err != nil {
		log.Fatal(err)
	}

	// 3) C -> S
	pairVerifyResponse, err := pairSetup(pairVerifyRequest)
	if err != nil {
		log.Fatal(err)
	}

	// 4) S -> C
	pairKeyRequest, err := pair.HandleReaderForHandler(pairVerifyResponse, client)
	if err != nil {
		log.Fatal(err)
	}

	// 5) C -> S
	pairKeyRespond, err := pairSetup(pairKeyRequest)
	if err != nil {
		log.Fatal(err)
	}

	// 6) S -> C
	request, err := pair.HandleReaderForHandler(pairKeyRespond, client)
	if err != nil {
		log.Fatal(err)
	}

	if request != nil {
		log.Println(request)
	}

	log.Println("*** Pairing done ***")

	verify := pair.NewVerifyClientController(c, database)

	verifyStartRequest := verify.InitialKeyVerifyRequest()
	// 1) C -> S
	verifyStartResponse, err := pairVerify(verifyStartRequest)
	if err != nil {
		log.Fatal(err)
	}

	// 2) S -> C
	verifyFinishRequest, err := pair.HandleReaderForHandler(verifyStartResponse, verify)
	if err != nil {
		log.Fatal(err)
	}

	// 3) C -> S
	verifyFinishResponse, err := pairVerify(verifyFinishRequest)
	if err != nil {
		log.Fatal(err)
	}

	// 4) S -> C
	last_request, err := pair.HandleReaderForHandler(verifyFinishResponse, verify)
	if err != nil {
		log.Fatal(err)
	}

	if last_request != nil {
		log.Println(last_request)
	}

	log.Println("*** Key Verification done ***")
}