P2P-based M2M application frameworks have several advantages, such as increased flexibility, efficiency and a lack of single point of failure as compared to centralised approaches. However, there are several security drawbacks which need to be addressed in order to provide the user a secure environment for the provision and usage of M2M applications. This publication presents different security issues inside P2P-based M2M application frameworks and evaluates P2P protocols based on security. In order to avoid different security attacks, the concept of trust and its importance are emphasized. Furthermore, a trust management system with special trust metric parameters is presented which considers the architecture of P2P-based M2M applications. Finally, blockchain principles are integrated for optimising the overall security in the system by improving data storage between peers, avoiding volatility of peers and ensuring correct working M2M applications.