Guilherme Rodrigues
Guilherme Rodrigues

Reputation: 389

Sign string using python encryption - Error: Unable to deserialize core data

I have this code in Python:

import base64
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives import serialization

private_key_pem = """
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDCpA0jK8IgLtV+
b/X3t+cmt9r3owRyD
Ywbm6Kbsvj/pXNYaI47toIk=
-----END PRIVATE KEY-----
"""

data = b"""GET
/v1/contas/aa/aa
agencia=3995&conta=75557

eyJhbGciOiJSUzUxMiJ9.ew0KIsDQoiYXVkIjogImh0dHBzOi8vb3BlbmFwaS1heHdheS5hcGkuc
1728392567000
2024-10-08T10:02:00-00:00
SHA256"""

private_key = serialization.load_pem_private_key(
    private_key_pem.encode('utf-8'),
    password=None, 
    backend=default_backend()
)

signature = private_key.sign(
    data,
    padding.PKCS1v15(),
    hashes.SHA256()
)

signature_b64 = base64.b64encode(signature).decode('utf-8')

signature_b64 = signature_b64.rstrip('=\n').replace('+', '-').replace('/', '_')
print(signature_b64)

    

This function above makes a signature using a private key, and it runs smoothly using Python. But I need to pass this function to my psotgresql database, using plpython3u, so I replicated it like this:

DROP FUNCTION IF EXISTS public.teste;
CREATE OR REPLACE FUNCTION public.teste()
RETURNS TEXT AS $$
import base64
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives import serialization

private_key_pem = '''-----BEGIN PRIVATE KEY-----
mt9r3owRyDOej0p9qDP0kO0v3eQsKSKmMnV1/WDo0r6Wo/cjsMs4mFC9O
nqvy26/1xE1+mHBoC/+rvhoEn59kzL
Ywbm6Kbsvj/pXNYaI47toIk=
-----END PRIVATE KEY-----'''

data = """GET
/v1/contas/aa/aa
agencia=aa&conta=aa

eyJhbGciOiJSUzUxMiJ9.ew0KInZlciI6ICIxLjAiLA0KImlzcyI6Ih5LmFwaS5wcmViYW5jby5jb20uYnIvYXV0aC9zZXJ2ZXIvdjEuMS90b2tlbiIsDQKGn2JWQ
1728392567000
2024-10-08T10:02:00-00:00
SHA256"""

private_key = serialization.load_pem_private_key(
    private_key_pem.encode('utf-8'),
    password=None, 
    backend=default_backend()
)

signature = private_key.sign(
    data,
    padding.PKCS1v15(),
    hashes.SHA256()
)

signature_b64 = base64.b64encode(signature).decode('utf-8')

signature_b64 = signature_b64.rstrip('=\n').replace('+', '-').replace('/', '_')
plpy.notice(signature_b64)
$$ LANGUAGE plpython3u;

the functions are exactly the same, but when running the function in the database I receive the error:

Erro SQL [38000]: ERROR: ValueError: ('Could not deserialize key data. The data may be in an incorrect format, the provided password may be incorrect, it may be encrypted with an unsupported algorithm, or it may be an unsupported key type (e.g. EC curves with explicit parameters).', [<OpenSSLError(code=503841036, lib=60, reason=524556, reason_text=unsupported)>]) Onde: Traceback (most recent call last): PL/Python function "teste", line 44, in private_key = serialization.load_pem_private_key( PL/Python function "teste"

I can't understand why the error happens since the data and functions are exactly the same

Upvotes: 0

Views: 22

Answers (0)

Related Questions