123 lines
2.6 KiB
Python
123 lines
2.6 KiB
Python
#
|
|
# Purpose: Write 2 new JPG image files => PicSeal JPG Files
|
|
#
|
|
# Data: public key, private key, image hash, image signature
|
|
# Has: JPG fingerprint
|
|
#
|
|
# Big-endian
|
|
# Pub: [app15:2|size:2|'picseal':7|type:1|sig:512|pubkey:550]
|
|
# Pvt: [app15:2|size:2|'picseal':7|type:1|sig:512|pvtkey:2347]
|
|
#
|
|
import struct
|
|
import logging
|
|
from Crypto.PublicKey import RSA
|
|
from libs.crypto_sig import Signature
|
|
|
|
|
|
class JpgPicSeal:
|
|
|
|
app15_marker = b'\xff\xef'
|
|
# ascii code for "picseal"
|
|
picseal_marker = b'\x70\x69\x63\x73\x65\x61\x6C'
|
|
pub_marker = b'\x01'
|
|
pvt_marker = b'\x02'
|
|
|
|
|
|
def __init__(self, sig=None):
|
|
if (sig):
|
|
self.sig = sig
|
|
else:
|
|
self.sig = Signature()
|
|
self.sig_data = None
|
|
|
|
|
|
#
|
|
# READ
|
|
#
|
|
|
|
def isPicSeal(self, buf):
|
|
if (buf == JpgPicSeal.picseal_marker):
|
|
return True
|
|
return False
|
|
|
|
#
|
|
# [size:2|picseal:7|type:1|sig:512|key:550] == 1072
|
|
#
|
|
def deserialize(self, buf):
|
|
retval = False
|
|
|
|
# read type 0x01 is public key, 0x02 private key
|
|
try:
|
|
if (buf[9] == ord(JpgPicSeal.pub_marker)):
|
|
self.sig.importPubKey(buf[522:])
|
|
print("*** *** ***")
|
|
print("*** Public Key Import Sucessful")
|
|
print("*** *** ***")
|
|
else:
|
|
self.sig.importPvtKey(buf[522:])
|
|
print("*** *** ***")
|
|
print("*** Private Key Import Sucessful")
|
|
print("*** *** ***")
|
|
retval = True
|
|
except Exception as ex:
|
|
logging.debug(ex)
|
|
|
|
return retval
|
|
|
|
|
|
#
|
|
def readPub(self):
|
|
pass
|
|
|
|
def readPvt(self):
|
|
pass
|
|
|
|
|
|
|
|
#
|
|
# WRITE
|
|
#
|
|
|
|
#
|
|
def serilize(self, fname):
|
|
pass
|
|
#
|
|
# input is the Crypto Sig class
|
|
#
|
|
def writePub(self, fhw):
|
|
self.__writeData(fhw, self.sig.sig_data, self.sig.getPubKeyDER(), JpgPicSeal.pub_marker)
|
|
|
|
#
|
|
# input is the Crypto Sig class
|
|
#
|
|
def writePvt(self, fhw):
|
|
self.__writeData(fhw, self.sig.sig_data, self.sig.getPvtKeyDER(), JpgPicSeal.pvt_marker)
|
|
|
|
|
|
#
|
|
def __writeData(self, fhw, sig_data, keyder, pubpvt):
|
|
# must include 2 bytes for length too, plus 1 for the key type
|
|
size = 3
|
|
size += len(JpgPicSeal.picseal_marker)
|
|
size += len(sig_data)
|
|
size += len(keyder)
|
|
|
|
logging.debug("3+{}+{}+{}=={} | picseal, sig, key".format(str(len(JpgPicSeal.picseal_marker)), str(len(sig_data)), str(len(keyder)), str(size) ))
|
|
#
|
|
# write header
|
|
# write app marker
|
|
fhw.write(JpgPicSeal.app15_marker)
|
|
# write the size of picseal record
|
|
fhw.write(size.to_bytes(2, byteorder='big'))
|
|
|
|
#
|
|
# write picseal data
|
|
# write picseal marker
|
|
fhw.write(JpgPicSeal.picseal_marker)
|
|
fhw.write(pubpvt)
|
|
# write signature of image
|
|
fhw.write(sig_data)
|
|
# write the key data
|
|
fhw.write(keyder)
|
|
|