You can distribute the source code for research purposes legally at least here in Germany. That's how AndroidAPS is even possible.