This is completely legal, provided it is done in a manner in which it is still possible to safely challenge for the ball or there are no opponents nearby.
FIFA Laws of the Game, 2013/14, p. 121:
Playing in a dangerous manner is defined as any action that, while
trying to play the ball, threatens injury to someone (including the player
himself). It is committed with an opponent nearby and prevents the opponent
from playing the ball for fear of injury.
If the referee decides that the action by the player makes it dangerous for opponents to challenge for the ball, an indirect free kick is awarded to the opposing team.
If this goes further, and playing the ball moves from being unsafe to an impossibility (for an unreasonable amount of time), a player is cautioned for unsporting behaviour. Note that there is no requirement here for opponents to be nearby.
This is no longer contained in the present text of the Laws, but was last seen in the 2006 FIFA Q&A to the Laws of The Game, p.30:
A player intentionally lies on the ball for an unreasonable length of
time. What action does the referee take?
He stops play, cautions the player for unsporting behaviour and restarts
play with an indirect free kick.
In the current (2013/14) laws, this roughly corresponds to the following on p. 123:
There are different circumstances when a player must be cautioned for
unsporting behaviour, e.g. if a player:
...
- acts in a manner which shows a lack of respect for the game
...
The restart is once again an indirect free kick.
Thanks to posdef, here is a clip of Cuauhtémoc Blanco using this technique.