Device-to-Device communication (D2D), combined with edge caching and distinct domains, is a promising approach for offloading data from wireless mobile networks. However, user security is still an open issue in D2D communication. Security vulnerabilities remain possible as a side effect of enabling straightforward, direct, and spontaneous interactions between untrustworthy users. To address this issue, this work involves designing TrustMD (Trust Multiple Domain), a multi-layer framework combining diverse technologies inspired by blockchain and trust management to develop a secure and scalable framework for multi-domain, edge, and D2D caching layers. Specifically, TrustMD combines edge trust storage with blockchain for distributed storage management in a multi-layer architecture designed to store trust control data in edge efficiently and D2D networks across different domains. Our experiments with TrustMD showed a significant improvement in data goodput, reaching as high as 95% of the total network throughput. In contrast, state-of-the-art approaches without trust control dissemination achieved at most 80%. Even though we observed a 7% increase in D2D overhead, TrustMD can effectively control latency levels. TrustMD managed security effectively without compromising network performance, reducing false negative rates up to 31% in the best-case scenario. TrustMD offers a scalable and effective security solution that boosts network performance and ensures robust protection.