summaryrefslogtreecommitdiffstats
path: root/IntelFsp2Pkg/Tools/ConfigEditor/CommonUtility.py
blob: 1229279116fd3623aa9b1123b48d62291938e7ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#!/usr/bin/env python
# @ CommonUtility.py
# Common utility script
#
# Copyright (c) 2016 - 2021, Intel Corporation. All rights reserved.<BR>
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
##

import os
import sys
import shutil
import subprocess
import string
from ctypes import ARRAY, c_char, c_uint16, c_uint32, \
        c_uint8, Structure, sizeof
from importlib.machinery import SourceFileLoader
from SingleSign import single_sign_gen_pub_key


# Key types  defined should match with cryptolib.h
PUB_KEY_TYPE = {
    "RSA": 1,
    "ECC": 2,
    "DSA": 3,
    }

# Signing type schemes  defined should match with cryptolib.h
SIGN_TYPE_SCHEME = {
    "RSA_PKCS1": 1,
    "RSA_PSS": 2,
    "ECC": 3,
    "DSA": 4,
    }

# Hash values defined should match with cryptolib.h
HASH_TYPE_VALUE = {
    "SHA2_256": 1,
    "SHA2_384": 2,
    "SHA2_512": 3,
    "SM3_256": 4,
    }

# Hash values defined should match with cryptolib.h
HASH_VAL_STRING = dict(map(reversed, HASH_TYPE_VALUE.items()))

AUTH_TYPE_HASH_VALUE = {
    "SHA2_256": 1,
    "SHA2_384": 2,
    "SHA2_512": 3,
    "SM3_256": 4,
    "RSA2048SHA256": 1,
    "RSA3072SHA384": 2,
    }

HASH_DIGEST_SIZE = {
    "SHA2_256": 32,
    "SHA2_384": 48,
    "SHA2_512": 64,
    "SM3_256": 32,
    }


class PUB_KEY_HDR (Structure):
    _pack_ = 1
    _fields_ = [
        ('Identifier', ARRAY(c_char, 4)),      # signature ('P', 'U', 'B', 'K')
        ('KeySize',    c_uint16),              # Length of Public Key
        ('KeyType',    c_uint8),               # RSA or ECC
        ('Reserved',   ARRAY(c_uint8, 1)),
        ('KeyData',    ARRAY(c_uint8, 0)),
        ]

    def __init__(self):
        self.Identifier = b'PUBK'


class SIGNATURE_HDR (Structure):
    _pack_ = 1
    _fields_ = [
        ('Identifier', ARRAY(c_char, 4)),
        ('SigSize',    c_uint16),
        ('SigType',    c_uint8),
        ('HashAlg',    c_uint8),
        ('Signature',  ARRAY(c_uint8, 0)),
        ]

    def __init__(self):
        self.Identifier = b'SIGN'


class LZ_HEADER(Structure):
    _pack_ = 1
    _fields_ = [
        ('signature',       ARRAY(c_char, 4)),
        ('compressed_len',  c_uint32),
        ('length',          c_uint32),
        ('version',         c_uint16),
        ('svn',             c_uint8),
        ('attribute',       c_uint8)
    ]
    _compress_alg = {
        b'LZDM': 'Dummy',
        b'LZ4 ': 'Lz4',
        b'LZMA': 'Lzma',
    }


def print_bytes(data, indent=0, offset=0, show_ascii=False):
    bytes_per_line = 16
    printable = ' ' + string.ascii_letters + string.digits + string.punctuation
    str_fmt = '{:s}{:04x}: {:%ds} {:s}' % (bytes_per_line * 3)
    bytes_per_line
    data_array = bytearray(data)
    for idx in range(0, len(data_array), bytes_per_line):
        hex_str = ' '.join(
            '%02X' % val for val in data_array[idx:idx + bytes_per_line])
        asc_str = ''.join('%c' % (val if (chr(val) in printable) else '.')
                          for val in data_array[idx:idx + bytes_per_line])
        print(str_fmt.format(
            indent * ' ',
            offset + idx, hex_str,
            ' ' + asc_str if show_ascii else ''))


def get_bits_from_bytes(bytes, start, length):
    if length == 0:
        return 0
    byte_start = (start) // 8
    byte_end = (start + length - 1) // 8
    bit_start = start & 7
    mask = (1 << length) - 1
    val = bytes_to_value(bytes[byte_start:byte_end + 1])
    val = (val >> bit_start) & mask
    return val


def set_bits_to_bytes(bytes, start, length, bvalue):
    if length == 0:
        return
    byte_start = (start) // 8
    byte_end = (start + length - 1) // 8
    bit_start = start & 7
    mask = (1 << length) - 1
    val = bytes_to_value(bytes[byte_start:byte_end + 1])
    val &= ~(mask << bit_start)
    val |= ((bvalue & mask) << bit_start)
    bytes[byte_start:byte_end+1] = value_to_bytearray(
        val,
        byte_end + 1 - byte_start)


def value_to_bytes(value, length):
    return value.to_bytes(length, 'little')


def bytes_to_value(bytes):
    return int.from_bytes(bytes, 'little')


def value_to_bytearray(value, length):
    return bytearray(value_to_bytes(value, length))

# def value_to_bytearray (value, length):
    return bytearray(value_to_bytes(value, length))


def get_aligned_value(value, alignment=4):
    if alignment != (1 << (alignment.bit_length() - 1)):
        raise Exception(
            'Alignment (0x%x) should to be power of 2 !' % alignment)
    value = (value + (alignment - 1)) & ~(alignment - 1)
    return value


def get_padding_length(data_len, alignment=4):
    new_data_len = get_aligned_value(data_len, alignment)
    return new_data_len - data_len


def get_file_data(file, mode='rb'):
    return open(file, mode).read()


def gen_file_from_object(file, object):
    open(file, 'wb').write(object)


def gen_file_with_size(file, size):
    open(file, 'wb').write(b'\xFF' * size)


def check_files_exist(base_name_list, dir='', ext=''):
    for each in base_name_list:
        if not os.path.exists(os.path.join(dir, each + ext)):
            return False
    return True


def load_source(name, filepath):
    mod = SourceFileLoader(name, filepath).load_module()
    return mod


def get_openssl_path():
    if os.name == 'nt':
        if 'OPENSSL_PATH' not in os.environ:
            openssl_dir = "C:\\Openssl\\bin\\"
            if os.path.exists(openssl_dir):
                os.environ['OPENSSL_PATH'] = openssl_dir
            else:
                os.environ['OPENSSL_PATH'] = "C:\\Openssl\\"
                if 'OPENSSL_CONF' not in os.environ:
                    openssl_cfg = "C:\\Openssl\\openssl.cfg"
                    if os.path.exists(openssl_cfg):
                        os.environ['OPENSSL_CONF'] = openssl_cfg
        openssl = os.path.join(
            os.environ.get('OPENSSL_PATH', ''),
            'openssl.exe')
    else:
        # Get openssl path for Linux cases
        openssl = shutil.which('openssl')

    return openssl


def run_process(arg_list, print_cmd=False, capture_out=False):
    sys.stdout.flush()
    if os.name == 'nt' and os.path.splitext(arg_list[0])[1] == '' and \
       os.path.exists(arg_list[0] + '.exe'):
        arg_list[0] += '.exe'
    if print_cmd:
        print(' '.join(arg_list))

    exc = None
    result = 0
    output = ''
    try:
        if capture_out:
            output = subprocess.check_output(arg_list).decode()
        else:
            result = subprocess.call(arg_list)
    except Exception as ex:
        result = 1
        exc = ex

    if result:
        if not print_cmd:
            print('Error in running process:\n  %s' % ' '.join(arg_list))
        if exc is None:
            sys.exit(1)
        else:
            raise exc

    return output


# Adjust hash type algorithm based on Public key file
def adjust_hash_type(pub_key_file):
    key_type = get_key_type(pub_key_file)
    if key_type == 'RSA2048':
        hash_type = 'SHA2_256'
    elif key_type == 'RSA3072':
        hash_type = 'SHA2_384'
    else:
        hash_type = None

    return hash_type


def rsa_sign_file(
      priv_key, pub_key, hash_type, sign_scheme,
      in_file, out_file, inc_dat=False, inc_key=False):

    bins = bytearray()
    if inc_dat:
        bins.extend(get_file_data(in_file))


# def single_sign_file(priv_key, hash_type, sign_scheme, in_file, out_file):

    out_data = get_file_data(out_file)

    sign = SIGNATURE_HDR()
    sign.SigSize = len(out_data)
    sign.SigType = SIGN_TYPE_SCHEME[sign_scheme]
    sign.HashAlg = HASH_TYPE_VALUE[hash_type]

    bins.extend(bytearray(sign) + out_data)
    if inc_key:
        key = gen_pub_key(priv_key, pub_key)
        bins.extend(key)

    if len(bins) != len(out_data):
        gen_file_from_object(out_file, bins)


def get_key_type(in_key):

    # Check in_key is file or key Id
    if not os.path.exists(in_key):
        key = bytearray(gen_pub_key(in_key))
    else:
        # Check for public key in binary format.
        key = bytearray(get_file_data(in_key))

    pub_key_hdr = PUB_KEY_HDR.from_buffer(key)
    if pub_key_hdr.Identifier != b'PUBK':
        pub_key = gen_pub_key(in_key)
        pub_key_hdr = PUB_KEY_HDR.from_buffer(pub_key)

    key_type = next(
        (key for key,
            value in PUB_KEY_TYPE.items() if value == pub_key_hdr.KeyType))
    return '%s%d' % (key_type, (pub_key_hdr.KeySize - 4) * 8)


def get_auth_hash_type(key_type, sign_scheme):
    if key_type == "RSA2048" and sign_scheme == "RSA_PKCS1":
        hash_type = 'SHA2_256'
        auth_type = 'RSA2048_PKCS1_SHA2_256'
    elif key_type == "RSA3072" and sign_scheme == "RSA_PKCS1":
        hash_type = 'SHA2_384'
        auth_type = 'RSA3072_PKCS1_SHA2_384'
    elif key_type == "RSA2048" and sign_scheme == "RSA_PSS":
        hash_type = 'SHA2_256'
        auth_type = 'RSA2048_PSS_SHA2_256'
    elif key_type == "RSA3072" and sign_scheme == "RSA_PSS":
        hash_type = 'SHA2_384'
        auth_type = 'RSA3072_PSS_SHA2_384'
    else:
        hash_type = ''
        auth_type = ''
    return auth_type, hash_type


# def single_sign_gen_pub_key(in_key, pub_key_file=None):


def gen_pub_key(in_key, pub_key=None):

    keydata = single_sign_gen_pub_key(in_key, pub_key)

    publickey = PUB_KEY_HDR()
    publickey.KeySize = len(keydata)
    publickey.KeyType = PUB_KEY_TYPE['RSA']

    key = bytearray(publickey) + keydata

    if pub_key:
        gen_file_from_object(pub_key, key)

    return key


def decompress(in_file, out_file, tool_dir=''):
    if not os.path.isfile(in_file):
        raise Exception("Invalid input file '%s' !" % in_file)

    # Remove the Lz Header
    fi = open(in_file, 'rb')
    di = bytearray(fi.read())
    fi.close()

    lz_hdr = LZ_HEADER.from_buffer(di)
    offset = sizeof(lz_hdr)
    if lz_hdr.signature == b"LZDM" or lz_hdr.compressed_len == 0:
        fo = open(out_file, 'wb')
        fo.write(di[offset:offset + lz_hdr.compressed_len])
        fo.close()
        return

    temp = os.path.splitext(out_file)[0] + '.tmp'
    if lz_hdr.signature == b"LZMA":
        alg = "Lzma"
    elif lz_hdr.signature == b"LZ4 ":
        alg = "Lz4"
    else:
        raise Exception("Unsupported compression '%s' !" % lz_hdr.signature)

    fo = open(temp, 'wb')
    fo.write(di[offset:offset + lz_hdr.compressed_len])
    fo.close()

    compress_tool = "%sCompress" % alg
    if alg == "Lz4":
        try:
            cmdline = [
                os.path.join(tool_dir, compress_tool),
                "-d",
                "-o", out_file,
                temp]
            run_process(cmdline, False, True)
        except Exception:
            msg_string = "Could not find/use CompressLz4 tool, " \
                        "trying with python lz4..."
            print(msg_string)
            try:
                import lz4.block
                if lz4.VERSION != '3.1.1':
                    msg_string = "Recommended lz4 module version " \
                                "is '3.1.1'," + lz4.VERSION \
                                + " is currently installed."
                    print(msg_string)
            except ImportError:
                msg_string = "Could not import lz4, use " \
                            "'python -m pip install lz4==3.1.1' " \
                            "to install it."
                print(msg_string)
                exit(1)
            decompress_data = lz4.block.decompress(get_file_data(temp))
            with open(out_file, "wb") as lz4bin:
                lz4bin.write(decompress_data)
    else:
        cmdline = [
            os.path.join(tool_dir, compress_tool),
            "-d",
            "-o", out_file,
            temp]
        run_process(cmdline, False, True)
    os.remove(temp)


def compress(in_file, alg, svn=0, out_path='', tool_dir=''):
    if not os.path.isfile(in_file):
        raise Exception("Invalid input file '%s' !" % in_file)

    basename, ext = os.path.splitext(os.path.basename(in_file))
    if out_path:
        if os.path.isdir(out_path):
            out_file = os.path.join(out_path, basename + '.lz')
        else:
            out_file = os.path.join(out_path)
    else:
        out_file = os.path.splitext(in_file)[0] + '.lz'

    if alg == "Lzma":
        sig = "LZMA"
    elif alg == "Tiano":
        sig = "LZUF"
    elif alg == "Lz4":
        sig = "LZ4 "
    elif alg == "Dummy":
        sig = "LZDM"
    else:
        raise Exception("Unsupported compression '%s' !" % alg)

    in_len = os.path.getsize(in_file)
    if in_len > 0:
        compress_tool = "%sCompress" % alg
        if sig == "LZDM":
            shutil.copy(in_file, out_file)
            compress_data = get_file_data(out_file)
        elif sig == "LZ4 ":
            try:
                cmdline = [
                    os.path.join(tool_dir, compress_tool),
                    "-e",
                    "-o", out_file,
                    in_file]
                run_process(cmdline, False, True)
                compress_data = get_file_data(out_file)
            except Exception:
                msg_string = "Could not find/use CompressLz4 tool, " \
                            "trying with python lz4..."
                print(msg_string)
                try:
                    import lz4.block
                    if lz4.VERSION != '3.1.1':
                        msg_string = "Recommended lz4 module version " \
                                    "is '3.1.1', " + lz4.VERSION \
                                    + " is currently installed."
                        print(msg_string)
                except ImportError:
                    msg_string = "Could not import lz4, use " \
                                "'python -m pip install lz4==3.1.1' " \
                                "to install it."
                    print(msg_string)
                    exit(1)
                compress_data = lz4.block.compress(
                    get_file_data(in_file),
                    mode='high_compression')
        elif sig == "LZMA":
            cmdline = [
                os.path.join(tool_dir, compress_tool),
                "-e",
                "-o", out_file,
                in_file]
            run_process(cmdline, False, True)
            compress_data = get_file_data(out_file)
    else:
        compress_data = bytearray()

    lz_hdr = LZ_HEADER()
    lz_hdr.signature = sig.encode()
    lz_hdr.svn = svn
    lz_hdr.compressed_len = len(compress_data)
    lz_hdr.length = os.path.getsize(in_file)
    data = bytearray()
    data.extend(lz_hdr)
    data.extend(compress_data)
    gen_file_from_object(out_file, data)

    return out_file