Weryfikacja sygnatury
Rozdział zawiera opis algorytmu weryfikacji sygnatury wraz z opisem metod służących do pobrania publicznych kluczy podpisujących.
Lista metod
GET
/v1/izi/signing-keys/public- https://dokumentacja-inpost.atlassian.net/wiki/spaces/PL/pages/204505089GET
/v1/izi/signing-keys/public/{version}- https://dokumentacja-inpost.atlassian.net/wiki/spaces/PL/pages/204570625
Na tej stronie:
Algorytm weryfikacji sygnatury
Signature verification algorithm:
Recipient receives signed request with headers:
x-signature- signaturex-signature-timestamp- ISO8601 datetime string in UTC timezone with time of signature generation ex.2023-05-11T15:02:23.429Zx-public-key-ver- version of keys used to generate signaturex-public-key-hash-SHA-256hash of public key used to generate signature
Recipient checks if already have cached public key with given version
key present in cache:
recipient checks if public key hash matches calculated as:
SHA-256hash frompublic_key_base64field. if verification is:positive - continue
negative - reject request with http: 401
{ "error_code": "INVALID_SIGNATURE", "error_message": "error description" }
key not present in cache:
obtain public key from
/v1/izi/signing-keys/public/{keyVersion}or/api/v1/izi/signing-keys/publicand verify hash as above
Prepare base64 string that consists of
DIGEST,external-merchant-id,x-public-key-ver,x-signature-timestamp. Values are separated with commasx-public-key-ver,x-signature-timestampfrom headers, use empty value if header is missing.merchant_external_idvalue comes from endpoints/v1/izi/signing-keys/public/{keyVersion}and/v1/izi/signing-keys/publicasmerchant_external_idfieldDIGEST: base64 form ofSHA-256hash (Message Digest) generated from request body. Use empty byte array as request body if body is missing.
Decode base64 signature and verify it with the
SHA256withRSAalgorithm for the given public key and signature string. If verification:positive - continue
negative - reject request with http: 401
{ "error_code": "INVALID_SIGNATURE", "error_message": "error description" }
Recipient compares
x-signature-timestampvalue to current time. If difference is:less or equal to 240s - continue
bigger than 240s - reject request with http: 401
{ "error_code": "INVALID_SIGNATURE", "error_message": "error description" }
Manual signature verification:
curl --location 'http://{basket-app-host}/basket-app/api/v1/izi/signing-keys/public/{keyVersion}'replace {Unknown macro: { {keyVersion}}}with value from header:
x-public-key-verecho "$PUBLIC_KEY_BASE64" | openssl base64 -d -A | openssl rsa -pubin -inform DER -outform PEM -out pubkey.pemreplace$PUBLIC_KEY_BASE64with value from field:public_key_base64DIGEST=$(echo -n "$(<message_body)" | openssl dgst -sha256 -binary | openssl enc -base64 -A)wheremessage_bodyis a file with EXACT request body (without any additions, whitespaces etc.)echo -n "$DIGEST,$EXTERNAL_MERCHANT_ID,$KEY_VERSION,$SIGNATURE_TIMESTAMP" | openssl enc -base64 -A -out signature_stringwhere$EXTERNAL_MERCHANT_IDvalue is from public key endpoint - fieldmerchant_external_id,$KEY_VERSIONvalue is from headerx-public-key-verand$SIGNATURE_TIMESTAMPvalue is fromx-signature-timestampopenssl enc -base64 -d -A -in request_signature -out signature.binwhererequest_signatureis a file with value from headerx-signatureopenssl dgst -sha256 -verify pubkey.pem -signature signature.bin signature_stringshould printVerified OK