(CNN) In his 23 years with the Bureau of Alcohol, Tobacco, Firearms and Explosives, Dan O'Kelly was one of the agency's top gun experts.

He served for five years as the lead firearms technology instructor at the ATF National Academy, where he co-wrote the curriculum for incoming agents.

These days, however, O'Kelly is using his formidable firearms expertise and institutional knowledge of the ATF to take aim at his former employer.

He's at the center of a brewing legal dispute that federal prosecutors say has the potential to upend the 1968 Gun Control Act and "seriously undermine the ATF's ability to trace and regulate firearms nationwide."

As O'Kelly sees it, the ATF has been deliberately misinterpreting a key gun control regulation for decades because officials fear that following the letter of the law would allow criminals to build AR-15s and other firearms piece by piece with unregulated parts.

He said he voiced his concerns to an ATF official two decades ago, but was rebuffed.

Now, however, his view is gaining traction in courtrooms around the country.

In December, a federal judge in Ohio dismissed weapons-related charges against two men after O'Kelly testified that the AR-15 part at issue in their case was not subject to federal law or regulation.

US District Court Judge James G. Carr for the Northern District of Ohio called the ATF's long-standing interpretation of the regulation "unreasonable and legally unacceptable."

ATF said in a statement to CNN that it was reviewing that case and others involving the issue and would have no further comment until that review is complete. The agency declined to discuss O'Kelly's testimony as an expert witness.

A brief history lesson in gun control legislation is required to understand the unfolding controversy and O'Kelly's role in it.

In the early days of gun control, every single part of a gun was subject to regulation under the Federal Firearms Act of 1938

Three decades later, with the passage of the 1968 Gun Control Act , Congress sought to streamline what was considered an overly burdensome regulation by choosing a single part of a weapon as its key component for regulatory purposes.

Under the new law, manufacturers were required to stamp that part with a serial number for tracing purposes, and it would be subject to all the same laws as a completed firearm itself. Since 1993, that includes a provision that licensed dealers conduct criminal background checks on would-be buyers of the part, just as they would prospective purchasers of a fully intact firearm.

This key part, according to the Gun Control Act, was referred to as "the frame or receiver," which is, generally speaking, the body of a firearm in the area surrounding the trigger.

An accompanying federal regulation provided a precise, highly technical definition:

"That part of a firearm which provides housing for the hammer, bolt or breechblock, and firing mechanism, and which is usually threaded at its forward portion to receive the barrel."

The problem -- and this is where O'Kelly comes in -- is that he says roughly 60% of the guns in America do not have a single part that falls under that definition. The AR-15, for example, has a split receiver -- one upper and one lower. Neither meets the requirement on its own.

"For 50 years, ATF has been making this square peg fit in the round hole," O'Kelly told CNN, "when, in fact, it doesn't."

At 64, with more than half his life devoted to law enforcement, first as a cop in Indiana, then as a federal agent, the last thing O'Kelly wants, he said, is to be seen as at odds with the ATF's mission of keeping guns from criminals.

He's not on a crusade, he insisted. He isn't seeking the spotlight.

Put simply, he said, "when I'm asked to testify in court, I take an oath to tell the truth, and I have."

Since leaving the ATF in 2011, and forming his own firearms training and consulting firm, O'Kelly has been a paid expert witness in at least four cases in which he has detailed his view that the AR-15 and many other guns don't have a single part that is subject to federal law and regulation.

The first time was in 2014 after he got a call from a San Diego area lawyer asking his opinion about a case involving the seizure of approximately 6,000 polymer gun parts the ATF had determined were "receivers" under the law. The devices were taken as part of a criminal investigation into a Southern California-based company Ares Armor , which was suspected of dealing firearms without a license.

The company's owner at the time, Dimitri Karras , was never charged with a crime. His company later filed a civil lawsuit against the ATF seeking the return of his property. O'Kelly submitted a declaration in support of Karras' lawsuit, stating that the lower receivers "had only two of the four features listed by the ATF in their own definition of a 'receiver.'"

He also noted that the definition failed to cover a long list of semi-automatic rifles and handguns, including the very popular Ruger .22 caliber pistol and Uzi-style rifles. The regulation, O'Kelly wrote, pertained mainly to a part found in manually operated weapons such as revolvers and pump action shotguns.

He said the ATF's attempt to classify the items seized from Ares Armor as receivers, as well as similar rulings in other cases, displayed "a lack of logic and consistency, which has created a climate in the firearm industry wherein it is nearly impossible for the reasonable man to be able to operate in good faith within the law."

The ATF returned 5,786 lower receivers to Karras two months after O'Kelly's declaration was filed. The civil case was later dismissed.

A year later, O'Kelly was called upon again -- this time by lawyers in Oakland, California on behalf of a man who had purchased a lower receiver for an AR-15 style machine gun from an undercover ATF agent in the parking garage of a shopping mall.

Prosecutors charged Alejandro Jimenez , a convicted felon, with illegally possessing and failing to register a machine gun based on the notion that the lower receiver he bought was a machine gun all by itself under the law.

But O'Kelly said prosecutors were wrong to designate the item Jimenez purchased as a receiver.

"It is simply a piece of cast metal that must be equipped with numerous parts in order to become a weapon," he wrote.

The judge, citing the disconnect between the regulation and the item Jimenez possessed, subsequently dismissed the indictment, and prosecutors abandoned the case.

In wake of the dismissal, then-US Attorney General Loretta Lynch wrote a letter to Congress outlining the issues. She advised lawmakers that the judge's order was not suitable for appeal and that if ATF officials believed the definition of a receiver should be changed, they should pursue regulatory or administrative action.

The ATF did neither. And it would not be long before the dispute over what constitutes a receiver would arise again.

This time the stakes were much higher. The case involved a man accused of running an AR-15 style factory South of Los Angeles. Prosecutors alleged that Joseph Roh was illegally manufacturing the weapons for $1,000 a piece and had at least 19 convicted felons among his clients.

Roh opted for a bench trial, placing his fate in the hands of a judge alone. A central issue of the case was whether the lower receivers he stocked in his factory could fairly be considered firearms under the federal law.

Roh's lawyer, Gregory Nicolaysen, said O'Kelly came across as a "very neutral, very technical" witness in stating that they could not.

But he said there was also a "certain vehemence" in his testimony that stemmed from his view that his former agency had been ignoring the problem for decades.

"O'Kelly brings an intrinsic understanding of the agency, the agency's arrogance, and its willingness to be deceptive," Nicolaysen said. "He was a turning point in the litigation."

Though prosecutors had argued that adopting O'Kelly's view, "would sweep aside more than 50 years of the ATF's regulation of AR-15s and other semi-automatic firearms," the judge issued a tentative order doing just that.

Faced with the order, prosecutors cut a no-prison-time deal with Roh in order to prevent the tentative order from becoming final, drawing publicity and creating case law that could hamper ATF enforcement efforts.

A CNN story on the case ricocheted around the gun world and in federal courthouses. A week after it was published, Carr, the judge in Ohio, directed both the prosecution and defense in the case he was overseeing to address the tentative order in the Roh case and Lynch's letter.

Federal prosecutors in Ohio had charged two men, Richard Rowold and Steven Robison , with conspiring to illegally purchase more than four dozen AR-15 lower receivers. Rowold, a convicted felon, was charged with illegally possessing the devices. Robison was accused of illegally purchasing them on Rowold's behalf, though authorities belatedly discovered that Robison, too, had a criminal record that should have prohibited him from buying them as well.

When agents searched Rowold's apartment in March of 2018 they found a box with an attached receipt showing he had purchased the lower receivers for $1,700 and that they were to be delivered to Robison, according to a search warrant affidavit. Only 15 of the 50 that were shipped remained in the box.

Defense attorneys for both men argued that their cases should be thrown out because the lower receivers are not firearms, as the ATF contends, and are therefore not subject to any regulation or laws.

Following dueling motions on the issue, Carr called for an evidentiary hearing in September, 2019. It was focused on an increasingly familiar question: Whether lower receivers qualified as a firearm under the 1968 Gun Control Act and the related federal regulation.

Prosecutors, as they had in earlier cases, acknowledged that AR-15 lower receivers did not perfectly fit the definition in the relevant federal code.

But they argued that the ATF's interpretation that they were covered nonetheless was reasonable and consistent with the intent of the act.

They noted that there was a preamble to the "definitions" section of the code that provided the caveat "unless manifestly incompatible with the act."

To rule that an AR-15 lower receiver was not a receiver would be "manifestly incompatible" with Congress' intent, they argued.

O'Kelly once again took the stand as an expert witness for the defense.

He told the judge that, legally, lower receivers were no more subject to ATF regulation than "a lamp shade, for example," but that the agency had insisted otherwise for decades.

He was cross-examined by James Vann, assistant chief counsel for the ATF. Vann, assigned to ATF headquarters in Washington, DC, had traveled to Ohio to represent the government in the case.

Vann asked O'Kelly about his tenure at the ATF National Academy and whether he taught new agents that the AR-15 did not have a receiver.

"No, I did not," O'Kelly responded. "That would have been seriously frowned upon by the agency."

When Vann pressed him on why he didn't include that information in his curriculum, O'Kelly replied, "It wasn't allowed."

At that point the judge intervened, asking the same question.

O'Kelly told Carr he voiced his concerns to an official named Richard Turner at ATF headquarters, but was told "we're not going to teach that."

O'Kelly added, "I could only teach what was approved by headquarters."

Turner declined comment through an ATF spokeswoman.

Following his testimony, O'Kelly submitted a declaration in the case saying it was his professional opinion that roughly 60 percent of the firearms in America do not have a single part that qualifies as a receiver under the current regulation.

In siding with the defense, Carr wrote that the ATF's long-standing interpretation was "plainly erroneous and inconsistent with the regulation."

He added, "misapplying the law for a long time provides no immunity from scrutiny."

The Associated Press was first to report the outcome of the case and Carr's comments.

Adam Winkler, a constitutional law professor at UCLA and an expert on the second amendment, said the ruling exposed "another major loophole in federal gun laws" and predicted it would lead to a surge in so-called "ghost guns," homemade weapons assembled from individual parts, none of which bear serial numbers, if the problem wasn't fixed.

"This ruling will speed the way toward more and more unregulated firearms," Winkler told CNN.

Jay Jacobson, a California-based gunmaker who has hired O'Kelly as a consultant and considers him a friend, said the former agent would take no joy in seeing his testimony benefit bad guys.

"Dan doesn't want to see criminals getting loose," Jacobson said. "But if there is not a crime, he's going to testify in your defense. If you don't like that, change the law."

The morning after Carr's ruling was made public, O'Kelly met with a CNN reporter at a gun shop a short drive from his seasonal home in central Florida.

He did some show and tell with an AR-15, talked about his years as an ATF agent, and shot the breeze with Carey Baker, the shop owner, who also happens to be a former Florida state representative and senator.

Baker, whose shop was established in 1886, said he did not want any ambiguity in gun laws and regulations.

"As a dealer, with everything I own at risk, I need clear language," Baker said. "I want to comply. I don't want to jeopardize 130 years' worth of business."

He said he knew an AR-15 lower receiver didn't meet the definition in the regulation, but that he treated it as a firearm anyway.

"It shouldn't be that way, but that's how it is," Baker said. "They need to better define what they're attempting to regulate."

O'Kelly, now eight years removed from the ATF, said he believes it is "mere arrogance" that's behind the problematic language lasting for as long as it has.

"We're just going to say it applies -- so tough," is how he characterized the institutional mindset.

He said he has deep respect for, and remains friendly with, many of his former colleagues.

He's heard from some of them that his opinions -- or his willingness to express them -- have not gone over well within the agency. One former co-worker has stopped returning his calls, he said.

"Every time this comes up, it's like 'kill the messenger'," O'Kelly said. "I'm the bad guy."

"It stings," he acknowledged.

But he said he was not responsible for writing the law or regulation; He has merely testified truthfully when asked about them in court.

"If you have a problem with the truth," O'Kelly said, "who's the bad guy?"