When historical facts are sketchy, how much freedom has a writer to "fill-in" the gaps?

Take a historical figure who probably existed, but his identity is not known as a fact, who might have influenced events of his time. Let us accept he existed and his influence was real. In such a case, true history did not turn out as he would have wished. Unless, that is, he was intending the opposite result from the one he appeared to want. Or he was duped by others.

Is it legitimate to make assumptions as to his motives, and to attribute acts and, indeed, machinations, to him and to other real historical figures to "explain" why events unfolded as they did?