{"action":"created","issue":{"url":"https://api.github.com/repos/OpenSC/libp11/issues/266","repository_url":"https://api.github.com/repos/OpenSC/libp11","labels_url":"https://api.github.com/repos/OpenSC/libp11/issues/266/labels{/name}","comments_url":"https://api.github.com/repos/OpenSC/libp11/issues/266/comments","events_url":"https://api.github.com/repos/OpenSC/libp11/issues/266/events","html_url":"https://github.com/OpenSC/libp11/issues/266","id":385015639,"node_id":"MDU6SXNzdWUzODUwMTU2Mzk=","number":266,"title":"RSA-OAEP padding rejected???","user":{"login":"mouse07410","id":5923577,"node_id":"MDQ6VXNlcjU5MjM1Nzc=","avatar_url":"https://avatars1.githubusercontent.com/u/5923577?v=4","gravatar_id":"","url":"https://api.github.com/users/mouse07410","html_url":"https://github.com/mouse07410","followers_url":"https://api.github.com/users/mouse07410/followers","following_url":"https://api.github.com/users/mouse07410/following{/other_user}","gists_url":"https://api.github.com/users/mouse07410/gists{/gist_id}","starred_url":"https://api.github.com/users/mouse07410/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mouse07410/subscriptions","organizations_url":"https://api.github.com/users/mouse07410/orgs","repos_url":"https://api.github.com/users/mouse07410/repos","events_url":"https://api.github.com/users/mouse07410/events{/privacy}","received_events_url":"https://api.github.com/users/mouse07410/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":14,"created_at":"2018-11-27T22:30:00Z","updated_at":"2018-12-25T00:34:10Z","closed_at":"2018-12-10T17:34:47Z","author_association":"CONTRIBUTOR","body":"I upgraded the YubiHSM2 SDK, and was running an old(er) test-script. To my surprise, it failed on OAEP decryption:\r\n\r\n```\r\nDecrypting the symmetric key on the token...\r\n~/openssl-1.1/bin/openssl rasutl -engine pkcs11 -keyform engine -decrypt -inkey \"pkcs11:token=YubiHSM;id=%04%02;type=private\" -oaep -in /tmp/derive.1201.key.enc | xxd -p -c 200\r\nengine \"pkcs11\" set.\r\nEnter PKCS#11 token PIN for YubiHSM:\r\nRSA operation error\r\n140735608378240:error:8106F402:libp11:pkcs11_mechanism:Unsupported padding type:p11_rsa.c:69:\r\n```\r\n\r\nPuzzled, I changed `openssl rsautl` to `openssl pkeyutl`, and it worked:\r\n```\r\n$ yhsm2-rsa-encr-demo2 \r\nGenerate a random data file (1000 bytes), Base64-encoded...\r\nopenssl rand -base64 -out /tmp/derive.2804.text 1000\r\n\r\nGenerated\r\n  key: e60b9b067025c9bb597743cd3dda65e634d12336f07ed7ff284268dc70ed1ee8\r\n  iv:  17f8e4e29ea88cec0aadf9823eaec2c7\r\n\r\nEncrypt file /tmp/derive.2804.text with this key and IV...\r\nopenssl enc -aes-256-cfb -a -e -in /tmp/derive.2804.text -out /tmp/derive.2804.text.enc -K e60b9b067025c9bb597743cd3dda65e634d12336f07ed7ff284268dc70ed1ee8 -iv 17f8e4e29ea88cec0aadf9823eaec2c7\r\n\r\nEncrypting this random symmetric key to token RSA KEY MAN key...\r\necho e60b9b067025c9bb597743cd3dda65e634d12336f07ed7ff284268dc70ed1ee8 | xxd -r -p -c 200 | ~/openssl-1.1/bin/openssl rsautl -engine pkcs11 -keyform engine -encrypt -pubin -inkey \"pkcs11:token=YubiHSM;id=%04%02;type=public\" -oaep -out /tmp/derive.2804.key.enc\r\nengine \"pkcs11\" set.\r\nEnter PKCS#11 token PIN for YubiHSM:\r\n\r\nDecrypting the symmetric key on the token...\r\nKEY2=\"~/openssl-1.1/bin/openssl pkeyutl -engine pkcs11 -keyform engine -decrypt -inkey \"pkcs11:token=YubiHSM;id=%04%02;type=private\" -pkeyopt rsa_padding_mode:oaep -in /tmp/derive.2804.key.enc | xxd -p -c 200\"\r\nengine \"pkcs11\" set.\r\nEnter PKCS#11 token PIN for YubiHSM:\r\n\r\n. . . . .\r\n\r\nKEY1=\"e60b9b067025c9bb597743cd3dda65e634d12336f07ed7ff284268dc70ed1ee8\"\r\nKEY2=\"e60b9b067025c9bb597743cd3dda65e634d12336f07ed7ff284268dc70ed1ee8\"\r\nOriginal and decrypted keys  match\r\nOriginal and decrypted files match\r\n```\r\n\r\n\r\nBut looking at the code in `p11_rsa.c`, I noticed that it doesn't seem to accept OAEP paddings (all the support for it is in `p11_pkey.c`):\r\n```\r\nstatic int pkcs11_mechanism(CK_MECHANISM *mechanism, const int padding)\r\n{\r\n\tmemset(mechanism, 0, sizeof(CK_MECHANISM));\r\n\tswitch (padding) {\r\n\tcase RSA_PKCS1_PADDING:\r\n\t\t mechanism->mechanism = CKM_RSA_PKCS;\r\n\t\t break;\r\n\tcase RSA_NO_PADDING:\r\n\t\tmechanism->mechanism = CKM_RSA_X_509;\r\n\t\tbreak;\r\n\tcase RSA_X931_PADDING:\r\n\t\tmechanism->mechanism = CKM_RSA_X9_31;\r\n\t\tbreak;\r\n\tdefault:\r\n\t\tP11err(P11_F_PKCS11_MECHANISM, P11_R_UNSUPPORTED_PADDING_TYPE);\r\n\t\treturn -1;\r\n\t}\r\n\treturn 0;\r\n}\r\n```\r\n\r\nShould anything be done about it? I know that `rsautl` used to work a few months ago... Since then both `libp11` and `OpenSC` evolved somewhat, and there were some additions for PSS and OAEP support in OpenSC..."},"comment":{"url":"https://api.github.com/repos/OpenSC/libp11/issues/comments/449780815","html_url":"https://github.com/OpenSC/libp11/issues/266#issuecomment-449780815","issue_url":"https://api.github.com/repos/OpenSC/libp11/issues/266","id":449780815,"node_id":"MDEyOklzc3VlQ29tbWVudDQ0OTc4MDgxNQ==","user":{"login":"mtrojnar","id":13538198,"node_id":"MDQ6VXNlcjEzNTM4MTk4","avatar_url":"https://avatars3.githubusercontent.com/u/13538198?v=4","gravatar_id":"","url":"https://api.github.com/users/mtrojnar","html_url":"https://github.com/mtrojnar","followers_url":"https://api.github.com/users/mtrojnar/followers","following_url":"https://api.github.com/users/mtrojnar/following{/other_user}","gists_url":"https://api.github.com/users/mtrojnar/gists{/gist_id}","starred_url":"https://api.github.com/users/mtrojnar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mtrojnar/subscriptions","organizations_url":"https://api.github.com/users/mtrojnar/orgs","repos_url":"https://api.github.com/users/mtrojnar/repos","events_url":"https://api.github.com/users/mtrojnar/events{/privacy}","received_events_url":"https://api.github.com/users/mtrojnar/received_events","type":"User","site_admin":false},"created_at":"2018-12-25T00:34:10Z","updated_at":"2018-12-25T00:34:10Z","author_association":"CONTRIBUTOR","body":"This is the failed PSS request:\r\n```\r\n 29: C_SignInit 2018-12-23 08:36:28.533 [in] hSession = 0x7fb932512630 pMechanism->type=CKM_RSA_PKCS_PSS pMechanism->pParameter->hashAlg=CKM_SHA384 pMechanism->pParameter->mgf=CKG_MGF1_SHA384 pMechanism->pParameter->sLen=206 [in] hKey = 0x7fb932431b80 Returned: 112 CKR_MECHANISM_INVALID\r\n```\r\nThis is the failed raw RSA request:\r\n```\r\n 30: C_SignInit 2018-12-23 08:36:28.533 [in] hSession = 0x7fb932512630 pMechanism->type=CKM_RSA_X_509 [in] hKey = 0x7fb932431b80 Returned: 112 CKR_MECHANISM_INVALID \r\n```\r\nIf your question is \"Why my module does not support raw RSA?\" then I really don't know, and I don't think this is the right forum to ask this questions.  The libp11 project doesn't develop PKCS#11 modules."}}
{"actor":{"display_login":"mtrojnar"}}