#!/usr/bin/python3 """ * File : pcp2-check-pubkey * [Part of the Pure Crypto Project https://senderek.ie/pcp2] * * Version : 2.0 * Date : 6 Jan 2025 * License : BSD * * Copyright (c) 2003 - 2025 * Ralf Senderek, Ireland. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Ralf Senderek. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * """ import sys, os # default intallation path for pcp2 modules sys.path.append("/usr/share/pcp2") import pure ERR_CORRUPT = 4 #-----------------------------------------------------------# def key_valid(K): # returns K if valid, else None. if len(K) == 6 : StoredSecHash = pure.toLong(K[5][15:]) print(StoredSecHash) pure.Modulus = pure.toLong(K[0]) pure.Encryption = pure.toLong(K[1]) pure.HashModulus = pure.toLong(K[2]) pure.Generator = pure.toLong(K[3]) pure.UserID = pure.Line(K[4]) # check the integrity of the stored securityhash pure.print_securityhash(False) print(pure.Securityhash) if StoredSecHash != pure.Securityhash : print ("\nWarning: The key material", end="") print (" is inconsistent with the stored Security-Hash.") print ("Your key has been tampered with !.\n") sys.exit(ERR_CORRUPT) else: print ("The integrity for [", KeyfileName ,"] is checked successfully!") return K else : print ("The public key is corrupted.") return None #-----------------------------------------------------------# if len(sys.argv) == 2: print() try: FILE = open(sys.argv[1], "rb") Content = FILE.readlines() FILE.close() KeyfileName = sys.argv[1] Result = key_valid (Content) except IOError : print ("\nCannot read the public key", sys.argv[1]) sys.exit(1) else: print("usage: pcp2-check-pubkey publickeyfile") sys.exit(0) #-----------------------------------------------------------# # Copyright 2003 - 2025, Ralf Senderek, Ireland # #-----------------------------------------------------------#