Can I collaborate outside my supervision on a work and add to my PhD thesis, me being the first author? I wish to work with my Master's supervisor on an idea. The idea is mine and I do the implementation and write the manuscript. My Ph.D. supervisor is not open minded and will surely deny a  collaboration. My question is if it is legal?
Can I add it to my thesis?

It is my work, done during my Ph.D., will get published with me as the first author but no other advisor from my current university.

Please note that I do not work on funding given by my supervisor but survive on a university scholarship. So if I am using resources, it belongs to the university.
I wish to work with my Master's supervisor on an idea. The idea is mine and I do the implementation and write the manuscript. My Ph.D. supervisor is not open minded and will surely deny a collaboration. 


Since you specifically want to know whether this is ethical and legal, I will answer these aspects first.

Legal

Sure, why not? It's your thesis, and an advisor is just an advisor. There is definitely no law or regulation that says that you need to run every contribution you wish to include in your thesis by your advisor.

Ethical

Here things become a little more nuanced, as your advisor usually has, or at least should have, a vested interest in your academic well-being and success in grad school, and you purposefully doing something decidedly unsmart (more on this below) could be considered unethical towards your advisor. That being said, it's easy to infer from your question that you are not on best terms with your advisor anyway, so I guess doing something that may put your advisor into a bad spot may not be that much of a deterrent to you.

The elephant in the room

So sneaking a surprise research contribution behind your advisor's back is legal and not overly unethical (imho, at least), but it's also decidedly not smart. Keep in mind that your advisor will typically also be the most important person in your thesis committee, so it's hard to imagine that a research contribution that he does not like in the first place (otherwise why would he "not be open to a collaboration", as you say?) and that you have also hidden from him will in his mind improve your dissertation. You should also consider the mindset that finding out that her/his student mistrusted her/him enough to hide work will put your advisor in for the final, crucial phase of your thesis.

I understand that as a student who has troubles with their advisor it is frustrating that they are in such a position of power, but trying to ignore this fact and hack around her/him is most certainly not the way to improve one's situation (changing advisor, while awkward, typically is).