{"action":"created","issue":{"url":"https://api.github.com/repos/OpenSC/OpenSC/issues/395","labels_url":"https://api.github.com/repos/OpenSC/OpenSC/issues/395/labels{/name}","comments_url":"https://api.github.com/repos/OpenSC/OpenSC/issues/395/comments","events_url":"https://api.github.com/repos/OpenSC/OpenSC/issues/395/events","html_url":"https://github.com/OpenSC/OpenSC/issues/395","id":62679074,"number":395,"title":"Broken Secure channel in dnie driver","user":{"login":"miguel-cv","id":2589754,"avatar_url":"https://avatars.githubusercontent.com/u/2589754?v=3","gravatar_id":"","url":"https://api.github.com/users/miguel-cv","html_url":"https://github.com/miguel-cv","followers_url":"https://api.github.com/users/miguel-cv/followers","following_url":"https://api.github.com/users/miguel-cv/following{/other_user}","gists_url":"https://api.github.com/users/miguel-cv/gists{/gist_id}","starred_url":"https://api.github.com/users/miguel-cv/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/miguel-cv/subscriptions","organizations_url":"https://api.github.com/users/miguel-cv/orgs","repos_url":"https://api.github.com/users/miguel-cv/repos","events_url":"https://api.github.com/users/miguel-cv/events{/privacy}","received_events_url":"https://api.github.com/users/miguel-cv/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":10,"created_at":"2015-03-18T12:13:36Z","updated_at":"2015-05-06T15:23:01Z","closed_at":"2015-05-06T13:51:51Z","body":"Hi:\r\n\r\nTesting HEAD code, i can't establish a secure session with card. Failure log:\r\n<pre> <code>\r\n0x7f604a43f740 13:48:04.787 [opensc-pkcs11] reader-pcsc.c:182:pcsc_internal_transmit: called\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] apdu.c:185:sc_apdu_log:\r\nIncoming APDU data [    2 bytes] =====================================\r\n90 00 ..\r\n======================================================================\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] apdu.c:397:sc_single_transmit: returning with: 0 (Success)\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] apdu.c:550:sc_transmit: returning with: 0 (Success)\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] card.c:386:sc_unlock: called\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] cwa-dnie.c:787:dnie_transmit_apdu_internal: returning with: 0 (Success)\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] card-dnie.c:1542:dnie_check_sw: called\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] card-dnie.c:1554:dnie_check_sw: returning with: 0 (Success)\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] cwa-dnie.c:870:dnie_wrap_apdu: returning with: 0 (Success)\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] card-dnie.c:1542:dnie_check_sw: called\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] card-dnie.c:1554:dnie_check_sw: returning with: 0 (Success)\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] cwa14890.c:740:cwa_external_auth: returning with: 0 (Success)\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] cwa14890.c:1369:cwa_create_secure_channel: Step 8.4.2: Compute Session Keys\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] cwa14890.c:769:cwa_compute_session_keys: called\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] cwa14890.c:821:cwa_compute_session_keys: Kenc: 2485E4A3A1DA93B228CEB8E8C9155C9C\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] cwa14890.c:822:cwa_compute_session_keys: Kmac: 88B310BBA2DCFC8B7B80398CC7805ED7\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] cwa14890.c:823:cwa_compute_session_keys: SSC:  C472134684730F9E\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] cwa14890.c:825:cwa_compute_session_keys: returning with: 0 (Success)\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] cwa14890.c:1377:cwa_create_secure_channel: CreateSecureChannel post-operations\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] cwa14890.c:1400:cwa_create_secure_channel: returning with: 0 (Success)\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] cwa14890.c:1444:cwa_encode_apdu: called\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] cwa14890.c:105:cwa_trace_apdu:\r\nAPDU before encode: ==================================================\r\nCLA: 00 INS: 20 P1: 00 P2: 00 Lc: 09 Le: 00 DATA: [    9 bytes]\r\n00 00 00 00 00 00 00 00 00                     xxxxxxxxx                  ( REDACTED )\r\n======================================================================\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] cwa14890.c:200:cwa_compose_tlv: called\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] cwa14890.c:229:cwa_compose_tlv: returning with: 0 (Success)\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] cwa14890.c:139:cwa_increase_ssc: called\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] cwa14890.c:141:cwa_increase_ssc: Curr SSC: 'C472134684730F9E'\r\n0x7f604a43f740 13:48:05.472 [opensc-pkcs11] cwa14890.c:147:cwa_increase_ssc: Next SSC: 'C472134684730F9F'\r\n0x7f604a43f740 13:48:05.473 [opensc-pkcs11] cwa14890.c:148:cwa_increase_ssc: returning with: 0 (Success)\r\n0x7f604a43f740 13:48:05.473 [opensc-pkcs11] cwa14890.c:200:cwa_compose_tlv: called\r\n0x7f604a43f740 13:48:05.473 [opensc-pkcs11] cwa14890.c:229:cwa_compose_tlv: returning with: 0 (Success)\r\n0x7f604a43f740 13:48:05.473 [opensc-pkcs11] cwa14890.c:1605:cwa_encode_apdu: returning with: 0 (Success)\r\n0x7f604a43f740 13:48:05.473 [opensc-pkcs11] cwa-dnie.c:698:dnie_transmit_apdu_internal: called\r\n0x7f604a43f740 13:48:05.473 [opensc-pkcs11] cwa-dnie.c:706:dnie_transmit_apdu_internal: envelope tx is not required\r\n0x7f604a43f740 13:48:05.473 [opensc-pkcs11] apdu.c:561:sc_transmit_apdu: called\r\n0x7f604a43f740 13:48:05.473 [opensc-pkcs11] card.c:346:sc_lock: called\r\n0x7f604a43f740 13:48:05.473 [opensc-pkcs11] apdu.c:528:sc_transmit: called\r\n0x7f604a43f740 13:48:05.473 [opensc-pkcs11] apdu.c:382:sc_single_transmit: called\r\n0x7f604a43f740 13:48:05.473 [opensc-pkcs11] apdu.c:387:sc_single_transmit: CLA:C, INS:20, P1:0, P2:0, data(25) 0x1f3a710\r\n0x7f604a43f740 13:48:05.473 [opensc-pkcs11] reader-pcsc.c:249:pcsc_transmit: reader 'Hewlett Packard HP USB Smartcard CCID Keyboard [HP Smartcard Keyboard] (14092300001220) 00 00'\r\n0x7f604a43f740 13:48:05.473 [opensc-pkcs11] apdu.c:185:sc_apdu_log:\r\nOutgoing APDU data [   30 bytes] =====================================\r\n0C 20 00 00 19 98 AA 6A 49 60 7F 00 00 98 AA 6A . .....jI`.....j\r\n49 60 7F 00 00 A9 17 29 8E 04 45 EA 48 40       I`.....)..E.H@\r\n======================================================================\r\n0x7f604a43f740 13:48:05.473 [opensc-pkcs11] reader-pcsc.c:182:pcsc_internal_transmit: called\r\n0x7f604a43f740 13:48:05.493 [opensc-pkcs11] apdu.c:185:sc_apdu_log:\r\nIncoming APDU data [    2 bytes] =====================================\r\n66 88 f.\r\n======================================================================\r\n0x7f604a43f740 13:48:05.493 [opensc-pkcs11] apdu.c:397:sc_single_transmit: returning with: 0 (Success)\r\n0x7f604a43f740 13:48:05.493 [opensc-pkcs11] apdu.c:550:sc_transmit: returning with: 0 (Success)\r\n0x7f604a43f740 13:48:05.493 [opensc-pkcs11] card.c:386:sc_unlock: called\r\n0x7f604a43f740 13:48:05.493 [opensc-pkcs11] cwa-dnie.c:787:dnie_transmit_apdu_internal: returning with: 0 (Success)\r\n0x7f604a43f740 13:48:05.493 [opensc-pkcs11] card-dnie.c:1542:dnie_check_sw: called\r\n0x7f604a43f740 13:48:05.493 [opensc-pkcs11] card-dnie.c:1547:dnie_check_sw: Cryptographic checksum invalid\r\n0x7f604a43f740 13:48:05.493 [opensc-pkcs11] cwa-dnie.c:845:dnie_wrap_apdu: Detected SM error/collision. Try 1\r\n0x7f604a43f740 13:48:05.493 [opensc-pkcs11] cwa14890.c:1067:cwa_create_secure_channel: called\r\n0x7f604a43f740 13:48:05.493 [opensc-pkcs11] cwa14890.c:1082:cwa_create_secure_channel: CWA SM initialization requested\r\n0x7f604a43f740 13:48:05.493 [opensc-pkcs11] cwa14890.c:1092:cwa_create_secure_channel: Resseting card\r\n0x7f604a43f740 13:48:05.493 [opensc-pkcs11] reader-pcsc.c:413:pcsc_reconnect: Reconnecting to the card...\r\n</code></pre>\r\n\r\nDoing a git bisect run points to this commit:  \r\nhttps://github.com/OpenSC/OpenSC/commit/3a92bf7af5aaa85fdbc0434d0a55393e68e0f98d\r\n\r\nIt seems to work removing ( in file  src/libopensc/cwa14890.c )\r\n\r\nfree(buffer);  (in function cwa_parse_tlv )\r\n\r\nand removing : \r\n\r\nfree(apdubuf); (in function cwa_encode_apdu) \r\n\r\n...but i don't know if this causes memory leaks, and i know this is not the \"correct\" solution, but my C knowledge is very limited to tell you more.\r\n\r\nThank you for looking at it...\r\n\r\nEDIT: The HEAD code inside valgrind works well..."},"comment":{"url":"https://api.github.com/repos/OpenSC/OpenSC/issues/comments/99512915","html_url":"https://github.com/OpenSC/OpenSC/issues/395#issuecomment-99512915","issue_url":"https://api.github.com/repos/OpenSC/OpenSC/issues/395","id":99512915,"user":{"login":"miguel-cv","id":2589754,"avatar_url":"https://avatars.githubusercontent.com/u/2589754?v=3","gravatar_id":"","url":"https://api.github.com/users/miguel-cv","html_url":"https://github.com/miguel-cv","followers_url":"https://api.github.com/users/miguel-cv/followers","following_url":"https://api.github.com/users/miguel-cv/following{/other_user}","gists_url":"https://api.github.com/users/miguel-cv/gists{/gist_id}","starred_url":"https://api.github.com/users/miguel-cv/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/miguel-cv/subscriptions","organizations_url":"https://api.github.com/users/miguel-cv/orgs","repos_url":"https://api.github.com/users/miguel-cv/repos","events_url":"https://api.github.com/users/miguel-cv/events{/privacy}","received_events_url":"https://api.github.com/users/miguel-cv/received_events","type":"User","site_admin":false},"created_at":"2015-05-06T15:23:01Z","updated_at":"2015-05-06T15:23:01Z","body":"Problem solved. With latest version ( that includes @germanblanco patches) the secure channel works well. Tested also with next patch from @hsanjuan and @dengert ..."}}