Of course, one answer is trivial--that if you don't make anything illegal, nothing that is moral is illegal. You can always make the laws lax enough to make sure that all moral actions are legal.
But let's assume you don't want the trivial answer; you want a legal system that makes immoral things illegal as well as moral things legal. There is no way to do this.
Let's consider speeding: speed limits are designed for safety, assuming certain traffic conditions, road conditions, atmospheric conditions, driver experience, and vehicle stability. Usually the speed limit is based on conditions that are somewhere between average and worst-case. But suppose the Super Bowl is on so there is no traffic, the road was just repaved, it's clear and sunny, you are young enough to have quick reactions but old enough to be a very experienced driver, and you are driving a new sports car. Then there is no way that going ten miles over the speed limit is creating any hazard for anyone, so doing so is not immoral, but it is still illegal.
You might argue that in this case, let's change the legal system to say that there is an implicit addition to the speed limit based on conditions. But this puts the judgment about speed back in the hands of the driver, and the whole point of the speed limit was that drivers cannot be trusted to make that judgement. The only alternative is to create a complex set of laws about the various kinds of conditions and how much each condition affects the posted speed limit.
Now, I don't think it would be possible to come up with a set of laws that accurately reflects reality as to how fast it is safe to go in all conditions, not least of all because "safe" is an imprecise word. But let's say you could, then surely this set of laws would be so complicated that most drivers could never remember them. Surely it is not just to demand that drivers obey laws that are too complex to remember.
But let's say that someone was able to come up with a miracle formula that is easy to remember, easy to calculate, and everyone can apply it. Now suppose you have a child in your car who is bleeding to death from an injury. Do you have to follow this formula, or does the very real and immediate danger to the child outweigh the speculative and statistical danger of speeding? Presumably you agree with me that it is moral to speed in this case (within reason). So now we need to add an entirely new dimension to our speeding laws to cover the level of urgency that the driver is facing. Is it only life and death situations? What about fear of losing a limb? What about fear of losing a finger? What about fear of losing a job? The possibilities are just too complex to ever encode into written law.
An objection you might make to this whole argument is that speeding is not really a law; it is a regulation, and the law can be written in such a way as to make it legal to violate regulations, so long as you can justify doing so. But then let's talk about stealing. When is it immoral to take something that doesn't belong to you? Can you reach into someone's car and grab a box of tissue when your child gets an injury and you need to stop the bleeding? If the child's life is in danger and you notice the keys are in the car, and you have no other way to get to an emergency room, is it OK to take the car? Is it OK to pick up a pretty rock that you find on someone else's property and keep it? Is it OK to take someone's cat when they have been neglecting it and abusing it for months?
The point is that morality--what is right and wrong--is just too complex an issue to ever be encoded into written laws.