aboutsummaryrefslogtreecommitdiffstats
path: root/main.py
diff options
context:
space:
mode:
authorKonstantin Ryabitsev <konstantin@linuxfoundation.org>2021-04-29 17:30:53 -0400
committerKonstantin Ryabitsev <konstantin@linuxfoundation.org>2021-04-29 17:30:53 -0400
commit50850b1cab29eabb46a1ff48a469088fc3457313 (patch)
treeeb37d8b482048fd5e514014ca7c935827b057281 /main.py
parent7d34f11b9143fad202ab3cf0e49c9637fe30369a (diff)
downloadpatch-attestation-poc-master.tar.gz
Use email.header for making a folder headerHEADmaster
Instead of implementing our own header folding, use Python's bundled method. Reduces our own code and is likely to be more robust. Signed-off-by: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
Diffstat (limited to 'main.py')
-rwxr-xr-xmain.py29
1 files changed, 5 insertions, 24 deletions
diff --git a/main.py b/main.py
index 2e732e3..8d7e5a0 100755
--- a/main.py
+++ b/main.py
@@ -5,6 +5,7 @@ import sys
import os
import base64
import email.utils
+import email.header
import re
import subprocess
import hashlib
@@ -157,28 +158,6 @@ def splitter(longstr: bytes, limit: int = 78) -> bytes:
return b' '.join(splitstr)
-def folder(longhdr: bytes, limit: int = 78) -> bytes:
- lines = list()
- line = b''
- for chunk in longhdr.split(b' '):
- if len(line + chunk) > limit:
- lines.append(line)
- line = b''
- if len(chunk) > limit:
- # the chunk itself is longer than limit, so append it as-is
- lines.append(b' ' + chunk)
- continue
- if not len(lines) and not len(line):
- # We're at the very start, so no need to prepend with ' '
- line += chunk
- else:
- line += b' ' + chunk
- if len(line):
- lines.append(line)
-
- return b'\r\n'.join(lines)
-
-
def get_git_toplevel(gitdir: str = None) -> str:
cmdargs = ['git']
if gitdir:
@@ -400,7 +379,8 @@ def cmd_sign_ed25519(cmdargs) -> None:
sys.exit(1)
bdata = sk.sign(digest, encoder=Base64Encoder)
- dshdr = folder(dshdr + splitter(bdata))
+ hhdr = email.header.make_header([(dshdr + splitter(bdata), 'us-ascii')], maxlinelen=78)
+ dshdr = hhdr.encode().encode()
headers.append(dshdr)
signed = b'\r\n'.join(headers) + b'\r\n\r\n' + payload
logger.info('--- SIGNED MESSAGE STARTS ---')
@@ -430,7 +410,8 @@ def cmd_sign_pgp(cmdargs) -> None:
logger.critical(err.decode())
sys.exit(ecode)
bdata = base64.b64encode(out)
- dshdr = folder(dshdr + splitter(bdata))
+ hhdr = email.header.make_header([(dshdr + splitter(bdata), 'us-ascii')], maxlinelen=78)
+ dshdr = hhdr.encode().encode()
headers.append(dshdr)
signed = b'\r\n'.join(headers) + b'\r\n\r\n' + payload
logger.info('--- SIGNED MESSAGE STARTS ---')