예제 #1
0
// Simple check keys.
func KeysSimpleCheck(data string, option *options.SysOption) {
	if privKey, err := option.GetPrivKey(); err == nil {
		if pubKey, err := option.GetPubKey(); err == nil {
			rand := helpers.NewSystemRandom()
			mainPart := fmt.Sprint(
				"%s.%s",
				base64.StdEncoding.EncodeToString([]byte(rand.CreatePassword(64))),
				base64.StdEncoding.EncodeToString([]byte(rand.CreatePassword(96))))

			sig, err := jwt.SigningMethodRS256.Sign(mainPart, privKey)
			if err == nil {
				err := jwt.SigningMethodRS256.Verify(mainPart, sig, pubKey)
				if err == nil {
					log.Printf("Keys from '%s' is correct\n", option.KeyDir)
				}
			} else {
				log.Fatalf("Can't ctrate signature: %s\n", err)
			}
		} else {
			log.Fatalf("Can't open public key! Error: %s\n", err)
		}
	} else {
		log.Fatalf("Can't open private key! Error: %s\n", err)
	}
}
예제 #2
0
func sendCallMethod(
	msg *CoreMsg,
	serverBusyAccounting *helpers.ServerBusyAccounting,
	serverMethods *helpers.ServerMethods) ([]*ServerCmd, error) {
	//
	var err error
	var cmd, servCmd *ServerCmd
	cid := (*msg).Cid
	method := (*msg).Method
	if serverMethods.IsPublic(method) {
		if freeCid, exists := serverMethods.SearchFree(method, serverBusyAccounting); exists {
			rand := helpers.NewSystemRandom()
			task := rand.CreateTaskId()
			taskInfo := fmt.Sprintf("{\"task\": \"%s\"}", task)
			cmd = NewServerDataCmd(CmdOk, cid, &taskInfo)
			execDataInst := execData{
				Task:   task,
				Data:   string((*msg).Data),
				Method: method}
			if data, dumpErr := dumps(execDataInst, false); dumpErr != nil {
				err = dumpErr
			} else {
				servCmd = NewServerDataCmd(CmdCallMethod, freeCid, &data)
				rllogger.Outputf(rllogger.LogDebug, "method '%s' -> %s", method, freeCid)
			}
		} else {
			cmd = NewServerCmd(CmdWaitFree, cid)
		}
	} else {
		cmd = NewServerCmd(CmdProblem, cid)
		err = errors.New(fmt.Sprintf("Method '%s' not found", method))
	}
	return []*ServerCmd{cmd, servCmd}, err
}
예제 #3
0
func NewConnectionDataManager(options options.SysOption) *ConnectionDataManager {
	rand := helpers.NewSystemRandom()
	result := ConnectionDataManager{
		rand:    *rand,
		options: options,
		// all pointer set reserved now
		storage:         make([]*ConnectionDataStorageCell, GroupCount),
		AsyncSafeObject: *(helpers.NewAsyncSafeObject())}
	return &result
}
예제 #4
0
func sendAuthRequest(
	cmd *ClientCmd,
	context *helpers.ConnectionContext,
	option HandlerParamsReader) (*ServerCmd, error) {
	// Send server random key for auth
	rand := helpers.NewSystemRandom()
	key := rand.CreatePassword(option.GetDefaultKeySize())
	contextData := ContextNewData{auth: false, data: key}
	answer := ServerCmd{
		contextUpdater: &contextData,
		baseCmd:        baseCmd{Data: key, Target: CmdAuthRequest}}
	return &answer, nil
}
예제 #5
0
func sendAuthResult(
	cmd *ClientCmd,
	context *helpers.ConnectionContext,
	option HandlerParamsReader) (*ServerCmd, error) {
	// Check client hash
	serverKey := context.GetTmpData()
	var err error
	var result *ServerCmd
	if len(serverKey) == option.GetDefaultKeySize() {
		requestData := (*cmd).Data
		if len(requestData) > 0 {
			if clientParts := strings.Split(requestData, ":"); len(clientParts) == 2 {
				// clientParts[0] - hash from client clientParts[1] - client "salt"
				line := fmt.Sprintf(
					"%s%s%s",
					// main key
					option.GetSecretKey(),
					// client key
					clientParts[1],
					// server key
					serverKey)

				if hashMethod(line, option) == clientParts[0] {
					rand := helpers.NewSystemRandom()
					keySize, node := option.GetCidConstructorData()
					contextData := ContextNewData{auth: true}
					// offer new cid
					answer := ServerCmd{
						contextUpdater: &contextData,
						baseCmd:        baseCmd{Cid: rand.CreateCid(keySize, node), Target: CmdClientData}}
					result = &answer

				} else {
					err = errors.New("Auth failed!")
				}
			} else {
				err = errors.New("Client data format error.")
			}
		} else {
			err = errors.New("Client data not found.")
		}
	} else {
		// incorrect
		err = errors.New("Connection without auth request?")
	}
	return result, err
}