Skip to content

Commit

Permalink
style: meet PEP8
Browse files Browse the repository at this point in the history
  • Loading branch information
shc261392 committed Sep 27, 2021
1 parent 6c99e09 commit 0c43148
Show file tree
Hide file tree
Showing 17 changed files with 158 additions and 114 deletions.
45 changes: 33 additions & 12 deletions c2pa/cai_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import hashlib
import json
import os
import sys

import multibase
import multihash
Expand Down Expand Up @@ -48,7 +47,7 @@ def parse_json(fname):
return output


def convert_to_hex(label, indent = 0, sec_indent = -1):
def convert_to_hex(label, indent=0, sec_indent=-1):
'''Convert label into hexadecimals
'''
if sec_indent == -1:
Expand Down Expand Up @@ -130,7 +129,7 @@ def get_content_lbox(fname):
data = parse_json(fname)

t_box_size = len(convert_to_hex('json'))

payload_size = len(convert_to_hex(data))

total_size = 4 + t_box_size + payload_size
Expand All @@ -144,12 +143,12 @@ def get_uuid_content_box():
'''Generate l_box for size for uuid content
'''
t_box_size = len(convert_to_hex('uuid'))

data_hex = ['63', '61', '73', '67', '00', '11', '00', '10', '80', '00', '00', 'aa', '00', '38', '9b', '71']
data_hex_size = len(data_hex)

# signature 1: placeholder signature data as mockup
# payload_data = ['73', '69', '67', '6e', '61', '74', '75', '72', '65', '20', '70', '6c', '61', '63', '65', '68', '6f', '6c', '64', '65', '72', '3a', '63', '62', '2e', '73', '74', '61', '72', '6c', '69', '6e', '67', '5f', '31', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20']
# payload_data = ['73', '69', '67', '6e', '61', '74', '75', '72', '65', '20', '70', '6c', '61', '63', '65', '68', '6f', '6c', '64', '65', '72', '3a', '63', '62', '2e', '73', '74', '61', '72', '6c', '69', '6e', '67', '5f', '31', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20'] # noqa E501
# signature 2: real example signature
payload_data = ['00', '87', '65', '0C', '94', '6D', 'EE', '53', '05', 'B2', 'D8', '87', '19', 'AD', '30', 'A9',
'9C', 'AB', 'CF', '3D', 'A2', '00', 'C2', '3D', '61', '71', '0B', 'EB', 'E7', '24', 'D0', 'CD',
Expand Down Expand Up @@ -256,7 +255,7 @@ def create_uuid_box(l_box):
'''
t_box = convert_to_hex('uuid')
data_hex = ['63', '61', '73', '67', '00', '11', '00', '10', '80', '00', '00', 'aa', '00', '38', '9b', '71']
# payload_data = ['73', '69', '67', '6e', '61', '74', '75', '72', '65', '20', '70', '6c', '61', '63', '65', '68', '6f', '6c', '64', '65', '72', '3a', '63', '62', '2e', '73', '74', '61', '72', '6c', '69', '6e', '67', '5f', '31', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20']
# payload_data = ['73', '69', '67', '6e', '61', '74', '75', '72', '65', '20', '70', '6c', '61', '63', '65', '68', '6f', '6c', '64', '65', '72', '3a', '63', '62', '2e', '73', '74', '61', '72', '6c', '69', '6e', '67', '5f', '31', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20'] # noqa E501
payload_data = ['00', '87', '65', '0C', '94', '6D', 'EE', '53', '05', 'B2', 'D8', '87', '19', 'AD', '30', 'A9',
'9C', 'AB', 'CF', '3D', 'A2', '00', 'C2', '3D', '61', '71', '0B', 'EB', 'E7', '24', 'D0', 'CD',
'B1', 'CC', 'E5', '0C', '3A', '74', '26', '71', '5A', '86', '04', 'DB', '36', '55', 'C5', '30',
Expand Down Expand Up @@ -302,9 +301,20 @@ def create_injection_block(cai_block, store_block, assertion_store, assertion, c

header = ['FF', 'EB']
c_box = convert_to_hex('JP')
box_remain = ['00', '01', '00', '00', '00','01']

block = header + l_box + c_box + box_remain + cai_block + store_block + assertion_store + assertion + claim + signature
box_remain = ['00', '01', '00', '00', '00', '01']

block = (
header +
l_box +
c_box +
box_remain +
cai_block +
store_block +
assertion_store +
assertion +
claim +
signature
)

return block

Expand Down Expand Up @@ -421,9 +431,20 @@ def create_complete(cai_l_box, cai_block, store_block, assertion_block, assertio

header = ['FF', 'EB']
c_box = convert_to_hex('JP')
box_remain = ['00', '01', '00', '00', '00','01']

final_cai_block = header + l_box + c_box + box_remain + cai_block + store_block + assertion_block + assertions + claim_block + signature_block
box_remain = ['00', '01', '00', '00', '00', '01']

final_cai_block = (
header +
l_box +
c_box +
box_remain +
cai_block +
store_block +
assertion_block +
assertions +
claim_block +
signature_block
)

return final_cai_block

Expand Down
12 changes: 6 additions & 6 deletions c2pa/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5

from cai.jumbf import Box
from cai.jumbf import ContentBox
from cai.jumbf import DescriptionBox
from cai.jumbf import SuperBox

from cai.jumbf import create_json_superbox
from cai.jumbf import json_to_bytes

from endesive import plain
Expand All @@ -40,10 +38,10 @@
'''

Cai_content_types = {
'claim_block' : '6361636200110010800000aa00389b71',
'store' : '6361737400110010800000aa00389b71',
'claim_block' : '6361636200110010800000aa00389b71', # noqa E203
'store' : '6361737400110010800000aa00389b71', # noqa E203
'assertion_store': '6361617300110010800000aa00389b71',
'claim' : '6361636c00110010800000aa00389b71',
'claim' : '6361636c00110010800000aa00389b71', # noqa E203
'claim_signature': '6361736700110010800000aa00389b71',
}

Expand Down Expand Up @@ -152,6 +150,7 @@ def create_claim(self, assertion_store,
claim['parent_claim'] = parent_claim
return claim


class CaiClaimEndesiveSignature(SuperBox):
def __init__(self, claim, key):
super(CaiClaimEndesiveSignature, self).__init__()
Expand All @@ -169,6 +168,7 @@ def create_endesive_signature(self, claim, key):
payload = bytes.fromhex(uuid) + signature
return payload


class CaiClaimCMSSignature(SuperBox):
def __init__(self, claim, key):
super(CaiClaimCMSSignature, self).__init__()
Expand Down Expand Up @@ -256,4 +256,4 @@ def __init__(self):
super(CaiClaimBlock, self).__init__()
self.description_box = DescriptionBox(
content_type=Cai_content_types['claim_block'],
label='cai')
label='cai')
38 changes: 19 additions & 19 deletions c2pa/jumbf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
# Spec B.1
Jumbf_content_types = {
'codestream': '6579d6fbdba2446bb2ac1b82feeb89d1',
'xml' : '786d6c2000110010800000aa00389b71',
'json' : '6a736f6e00110010800000aa00389b71',
'uuid' : '7575696400110010800000aa00389b71',
'xml' : '786d6c2000110010800000aa00389b71', # noqa E203
'json' : '6a736f6e00110010800000aa00389b71', # noqa E203
'uuid' : '7575696400110010800000aa00389b71', # noqa E203
}


Expand All @@ -45,7 +45,7 @@ def get_size(self):
# Calculate box size dynamically.
# 8 is from l_box (4) + t_box (4)
return 8 + len(self.payload)

def set_payload(self):
pass

Expand Down Expand Up @@ -173,9 +173,9 @@ def convert_bytes(self):


def create_single_content_superbox(content=b'',
t_box_type='',
content_type='',
label=''):
t_box_type='',
content_type='',
label=''):
c_box = ContentBox(t_box_type=t_box_type)
c_box.payload = content
d_box = DescriptionBox(content_type=content_type, label=label)
Expand All @@ -202,7 +202,7 @@ def create_codestream_superbox(content=b'', label=''):


def json_to_bytes(json_object):
return json.dumps(json_object, separators=(',',':')).encode('utf-8')
return json.dumps(json_object, separators=(',', ':')).encode('utf-8')


def get_app11_marker_segment_headers(data_bytes):
Expand All @@ -211,29 +211,29 @@ def get_app11_marker_segment_headers(data_bytes):
headers = {}
for offset in offsets:
try:
ci = data_bytes[offset + 4 : offset + 6].decode('utf-8')
ci = data_bytes[offset + 4: offset + 6].decode('utf-8')
except Exception as e:
print('Find App11 marker, and fail to get CI')
print(f'Find App11 marker, and fail to get CI. Exception: {e}')
ci = None
try:
tbox = data_bytes[offset + 16 : offset + 20].decode('utf-8')
tbox = data_bytes[offset + 16: offset + 20].decode('utf-8')
except Exception as e:
print('Find App11 marker, and fail to get TBox')
print(f'Find App11 marker, and fail to get TBox. Exception: {e}')
tbox = None

if ci == 'JP' and tbox == 'jumb':
header = {}
header['le'] = int.from_bytes(data_bytes[offset + 2 : offset + 4], byteorder='big')
header['ci'] = data_bytes[offset + 4 : offset + 6].decode('utf-8')
header['en'] = int.from_bytes(data_bytes[offset + 6 : offset + 8], byteorder='big')
header['z'] = int.from_bytes(data_bytes[offset + 8 : offset + 12], byteorder='big')
header['lbox'] = int.from_bytes(data_bytes[offset + 12 : offset + 16], byteorder='big')
header['tbox'] = data_bytes[offset + 16 : offset + 20].decode('utf-8')
header['le'] = int.from_bytes(data_bytes[offset + 2: offset + 4], byteorder='big')
header['ci'] = data_bytes[offset + 4: offset + 6].decode('utf-8')
header['en'] = int.from_bytes(data_bytes[offset + 6: offset + 8], byteorder='big')
header['z'] = int.from_bytes(data_bytes[offset + 8: offset + 12], byteorder='big')
header['lbox'] = int.from_bytes(data_bytes[offset + 12: offset + 16], byteorder='big')
header['tbox'] = data_bytes[offset + 16: offset + 20].decode('utf-8')
header['offset'] = offset

# passive protection to skip illegal or empty segment
if header['le'] > 10:
headers[header['z']] = header
else:
print('Unknown CI ({0}) or TBox ({1}) of offset {2}'.format(ci, tbox, hex(offset)))
return headers
return headers
57 changes: 31 additions & 26 deletions c2pa/starling.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import os

from c2pa.core import CaiClaimBlock
from c2pa.core import CaiClaimCMSSignature
from c2pa.core import CaiStore
from c2pa.jumbf import App11Box

Expand All @@ -36,6 +35,7 @@
'''Starling CLI tool to generate CAI metadata.
'''


class Starling(object):
def __init__(self,
media_bytes,
Expand Down Expand Up @@ -161,13 +161,13 @@ def multiple_claims_injection(self):

# re-construct Claim Block payload
claim_block_payload = bytearray()
## App11 Length of Marker Segment (the Le parameter) value is 8 ~ 65535.
## App11 Packet Sequence number (the Z parameter) value is 1 ~ 2^32-1.
## The Claim Block maximum size will be ~= 2^48 B ~= 280 TB.
# App11 Length of Marker Segment (the Le parameter) value is 8 ~ 65535.
# App11 Packet Sequence number (the Z parameter) value is 1 ~ 2^32-1.
# The Claim Block maximum size will be ~= 2^48 B ~= 280 TB.
for i in range(1, header_number + 1):
payload_start = self.app11_headers[i]['offset'] + 20
payload_end = payload_start + (self.app11_headers[i]['le'] - 18)
payload = self.raw_bytes[payload_start : payload_end]
payload = self.raw_bytes[payload_start: payload_end]
claim_block_payload += payload
store_bytes = cai_store.convert_bytes()

Expand All @@ -178,27 +178,31 @@ def multiple_claims_injection(self):
updated_app11_segment = App11Box(en=last_en)
updated_app11_segment.payload = updated_claim_block_bytes

## Assuming that current CAI data consists of 3 App11 segments.
##
## +-- starting point
## v
## +-------+----+----+------+-----+------+------+-------------------------------+
## | APP11 | Le | CI | En=1 | Z=1 | LBox | TBox | Payload (Claim Block, part 1) |
## +-------+----+----+------+-----+------+------+-------------------------------+
## | APP11 | Le | CI | En=1 | Z=2 | LBox | TBox | Payload (Claim Block, part 2) |
## +-------+----+----+------+-----+------+------+-------------------------------+
## | APP11 | Le | CI | En=1 | Z=3 | LBox | TBox | Payload (Claim Block, part 3) |
## +-------+----+----+------+-----+------+------+-------------------------------+
## ^
## ending point --+
##
## starting point of current CAI data
# Assuming that current CAI data consists of 3 App11 segments.
#
# +-- starting point
# v
# +-------+----+----+------+-----+------+------+-------------------------------+
# | APP11 | Le | CI | En=1 | Z=1 | LBox | TBox | Payload (Claim Block, part 1) |
# +-------+----+----+------+-----+------+------+-------------------------------+
# | APP11 | Le | CI | En=1 | Z=2 | LBox | TBox | Payload (Claim Block, part 2) |
# +-------+----+----+------+-----+------+------+-------------------------------+
# | APP11 | Le | CI | En=1 | Z=3 | LBox | TBox | Payload (Claim Block, part 3) |
# +-------+----+----+------+-----+------+------+-------------------------------+
# ^
# ending point --+
#
# starting point of current CAI data
update_range_s = self.app11_headers[1]['offset']
## ending point of current CAI data
# ending point of current CAI data
update_range_e = self.app11_headers[header_number]['offset'] + self.app11_headers[header_number]['le'] + 2

# save CAI-injected media
data_bytes = self.raw_bytes[:update_range_s] + updated_app11_segment.convert_bytes() + self.raw_bytes[update_range_e:]
data_bytes = (
self.raw_bytes[:update_range_s] +
updated_app11_segment.convert_bytes() +
self.raw_bytes[update_range_e:]
)
cai_data_bytes = insert_xmp_key(data_bytes, store_label=self.store_label)
return cai_data_bytes

Expand Down Expand Up @@ -283,10 +287,11 @@ def main():
# private key for signature
if key_filepath != '':
with open(key_filepath, 'rb') as f:
if type_sig=='cms':
if type_sig == 'cms':
key = f.read()
elif type_sig=='endesive':
# load_key_and_certificates second parameter is password to decrypt the data. Can be set to None of PKCS12 is not encrypted
elif type_sig == 'endesive':
# load_key_and_certificates second parameter is password to decrypt the data.
# Can be set to None of PKCS12 is not encrypted
# https://cryptography.io/en/latest/hazmat/primitives/asymmetric/serialization.html
key = pkcs12.load_key_and_certificates(f.read(), b'', backends.default_backend())
else:
Expand Down Expand Up @@ -314,4 +319,4 @@ def main():


if __name__ == "__main__":
main()
main()
5 changes: 3 additions & 2 deletions data/Keys/endesive_sig.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ def main():
with open('demo2_user1.p12', 'rb') as fp:
p12 = pkcs12.load_key_and_certificates(fp.read(), b'1234', backends.default_backend())
unsigned = open('plain-unsigned.txt', 'rb').read()
signed = plain.sign(unsigned,
signed = plain.sign(
unsigned,
p12[0], p12[1], p12[2],
'sha256',
attrs=True
)
open('plain-signed-attr.txt', 'wb').write(signed)


main()
main()
4 changes: 3 additions & 1 deletion data/Keys/verify_sig.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from endesive import plain


def main():
trusted_cert_pems = (open('demo2_ca.crt.pem', 'rt').read(),)
unsigned = open('plain-unsigned.txt', 'rb').read()
Expand All @@ -12,5 +13,6 @@ def main():
print('signature ok?', signatureok)
print('hash ok?', hashok)


if __name__ == "__main__":
main()
main()
2 changes: 0 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import subprocess

from setuptools import setup
from setuptools import find_packages

Expand Down
Loading

0 comments on commit 0c43148

Please sign in to comment.