In this paper, we investigate the physical layer security in downlink of Power Domain Non-Orthogonal Multiple Access (PD-NOMA)-based heterogeneous cellular network (HetNet). In this paper, we assume two categories of users are available: 1) Trusted users and 2) untrusted users (eavesdroppers) at which transparency of users is not clear for the BSs, i.e., they are potential eavesdroppers. Our aim is to maximize the sum secrecy rate of the network. To this end, we formulate joint subcarrier and power allocation optimization problems to increase sum secrecy rate. Moreover, we propose a novel scheme at which the eavesdroppers are prevented from doing Successive Interference Cancellation (SIC), while legitimate users are able to do it. In practical systems, perfectly availability of all eavesdroppers' Channel State Information (CSI) at legitimate transmitters are impractical. Also CSIs of legitimate users may be also imperfect due to the error of channel estimation. Hence, we study two cases of CSI availability: 1) Perfect CSI of nodes (legitimate users and eavesdroppers) are available at the BSs and 2) imperfect CSI of nodes are available at the BSs. Since the proposed optimization problems are non-convex, we adopt the well-known iterative algorithm called Alternative Search Method (ASM). In this algorithm, the optimization problems are converted to two subproblems, power allocation and subcarrier allocation. We solve the power allocation problem by the Successive Convex Approximation approach and solve the subcarrier allocation subproblem, by exploiting the Mesh Adaptive Direct Search algorithm (MADS). Moreover, in order to study the optimality gap of the proposed solution method, we apply the monotonic optimization method. Moreover, we evaluate the proposed scheme for secure massive connectivity in Heterogeneous Ultra Dense Networks (HUDNs). Furthermore, we investigate multiple antennas base stations scenario in this literature. Finally, we numerically compare the proposed scheme with the conventional case at which the eavesdroppers are able to apply SIC. Numerical results highlight that the proposed scheme significantly improves the sum secrecy rate compared with the conventional case.