Bug 48672 - AttributeError: 'PKey' object has no attribute '_x509' при проверке подписи
Summary: AttributeError: 'PKey' object has no attribute '_x509' при проверке подписи
Status: NEW
Alias: None
Product: Sisyphus
Classification: Development
Component: python3-module-openssl (show other bugs)
Version: unstable
Hardware: x86_64 Linux
: P5 normal
Assignee: Vitaly Lipatov
QA Contact: qa-sisyphus
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-12-04 10:41 MSK by Монастырский Илья
Modified: 2023-12-04 11:04 MSK (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Монастырский Илья 2023-12-04 10:41:57 MSK
Версии пакетов:
python3-module-openssl-23.2.0-alt1
python3-module-cryptography-41.0.7-alt1

Стенды с ошибкой (обновлены до Sisyphus):
Alt Workstation K 10.2.1 x86-64
Alt Education KDE/XFCE 10.2 x86-64
Alt Workstation 10.1 x86-64
Alt Server 10.1 x86-64

Шаги воспроизведения: 
1) Сгенерировать пару ключей: 
$ openssl genrsa -out private_key.pem 2048
$ openssl rsa -in private_key.pem -pubout -out public_key.pem

2) Собрать скрипт:
$ cat > test.py << EOF
from OpenSSL.crypto import load_privatekey, FILETYPE_PEM

with open('private_key.pem') as f:
    private_key = load_privatekey(FILETYPE_PEM, f.read())

from OpenSSL.crypto import sign

message = b'This is a message to be signed'
signature = sign(private_key, message, 'sha256')

with open('signature.bin', 'wb') as f:
    f.write(signature)

from OpenSSL.crypto import load_publickey

with open('public_key.pem') as f:
    public_key = load_publickey(FILETYPE_PEM, f.read())

with open('signature.bin', 'rb') as f:
    signature = f.read()

from OpenSSL.crypto import verify

verified = verify(public_key, signature, message, 'sha256')
if verified:
    print('Signature is valid')
else:
    print('Signature is not valid')
EOF

3) Выполнить: 
python3 test.py


Результат:
Traceback (most recent call last):
  File "/home/test/test2/test.py", line 24, in <module>
    verified = verify(public_key, signature, message, 'sha256')
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/site-packages/OpenSSL/crypto.py", line 3158, in verify
    pkey = _lib.X509_get_pubkey(cert._x509)
                                ^^^^^^^^^^
AttributeError: 'PKey' object has no attribute '_x509'

Ожидаемый результат:
Успешная проверка подписи. Вывод: Signature is valid

================================================================================
Проверялось в p10 - ошибка воспроизводится
Ошибка так же воспроизводится при использовании python-module-OpenSSL и python-module-cryptography в python2
Comment 1 Монастырский Илья 2023-12-04 11:01:03 MSK
Ссылка на код: 
https://pythonhint.com/post/1239035426773414/how-to-implement-openssl-functionality-in-python