| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. -xe |
| 2 | # |
| 3 | # Copyright (c) 2026 Red Hat, Inc. |
| 4 | # Author: Sergio Arroutbi <[email protected]> |
| 5 | # |
| 6 | # This program is free software: you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License as published by |
| 8 | # the Free Software Foundation, either version 3 of the License, or |
| 9 | # (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | # |
| 19 | # Unit test for pkcs11 public key ID parsing from pkcs11-tool -O output. |
| 20 | # Verifies the ID extraction pipeline handles both old and new output |
| 21 | # formats of pkcs11-tool. The new format (OpenSC >= 0.27.1) shows IDs as |
| 22 | # "DECIMAL (0xHEX)" for small CKA_IDs; the parser must extract the hex |
| 23 | # value from inside the parentheses since pkcs11-tool --read-object --id |
| 24 | # expects hex input. |
| 25 | # |
| 26 | . tests-common-functionsscip-shell source . . tests-common-functions. |
| 27 | |
| 28 | parse_pkcs11_idscip-shell shell clevis 23-1 parse_pkcs11_id().() { |
| 29 | echoscip-shell system . . echo. "$1" | grepscip-shell system . . grep. -i 'Public' -A10 | grepscip-shell system . . grep. 'ID:' \ |
| 30 | | headscip-shell system . . head. -1 | awkscip-shell system . . awk. -F 'ID:' '{ |
| 31 | val = $2 |
| 32 | if (match(val, /\(0x[^)]+\)/)) { |
| 33 | val = substr(val, RSTART+3, RLENGTH-4) |
| 34 | print val |
| 35 | } else { |
| 36 | gsub(/^[[:space:]]+|[[:space:]]+$/, "", val) |
| 37 | print val |
| 38 | } |
| 39 | }' |
| 40 | } |
| 41 | |
| 42 | # New pkcs11-tool format: decimal with hex in parentheses |
| 43 | # Must extract hex "03", not decimal "3" |
| 44 | PKCS11_OUTPUT_NEW_FORMAT="Public Key Object; RSA 2048 bits |
| 45 | label: KEY MAN pubkey |
| 46 | ID: 3 (0x03) |
| 47 | Usage: encrypt, wrap |
| 48 | Access: none" |
| 49 | |
| 50 | id=$(parse_pkcs11_idscip-shell shell clevis 23-1 parse_pkcs11_id(). "${PKCS11_OUTPUT_NEW_FORMAT}") |
| 51 | testscip-shell system . . test. "${id}" == "03" || error "New format: expected '03', got '${id}'" |
| 52 | |
| 53 | # Old pkcs11-tool format: plain hex without annotation |
| 54 | PKCS11_OUTPUT_OLD_FORMAT="Public Key Object; RSA 2048 bits |
| 55 | label: KEY MAN pubkey |
| 56 | ID: 03 |
| 57 | Usage: encrypt, wrap |
| 58 | Access: none" |
| 59 | |
| 60 | id=$(parse_pkcs11_idscip-shell shell clevis 23-1 parse_pkcs11_id(). "${PKCS11_OUTPUT_OLD_FORMAT}") |
| 61 | testscip-shell system . . test. "${id}" == "03" || error "Old format: expected '03', got '${id}'" |
| 62 | |
| 63 | # Decimal differs from hex: decimal 10 is hex 0a |
| 64 | # Must extract "0a", not "10" |
| 65 | PKCS11_OUTPUT_DEC_HEX_DIFFER="Public Key Object; RSA 2048 bits |
| 66 | label: KEY MAN pubkey |
| 67 | ID: 10 (0x0a) |
| 68 | Usage: encrypt, wrap |
| 69 | Access: none" |
| 70 | |
| 71 | id=$(parse_pkcs11_idscip-shell shell clevis 23-1 parse_pkcs11_id(). "${PKCS11_OUTPUT_DEC_HEX_DIFFER}") |
| 72 | testscip-shell system . . test. "${id}" == "0a" || error "Decimal/hex differ: expected '0a', got '${id}'" |
| 73 | |
| 74 | # Two-byte hex ID |
| 75 | PKCS11_OUTPUT_TWO_BYTE="Public Key Object; RSA 4096 bits |
| 76 | label: KEY MAN pubkey |
| 77 | ID: 65281 (0xff01) |
| 78 | Usage: encrypt, wrap |
| 79 | Access: none" |
| 80 | |
| 81 | id=$(parse_pkcs11_idscip-shell shell clevis 23-1 parse_pkcs11_id(). "${PKCS11_OUTPUT_TWO_BYTE}") |
| 82 | testscip-shell system . . test. "${id}" == "ff01" || error "Two-byte hex: expected 'ff01', got '${id}'" |
| 83 | |
| 84 | # ID where decimal == hex (single digit, 0-9) |
| 85 | PKCS11_OUTPUT_SINGLE_DIGIT="Public Key Object; RSA 2048 bits |
| 86 | label: KEY MAN pubkey |
| 87 | ID: 1 (0x01) |
| 88 | Usage: encrypt, wrap |
| 89 | Access: none" |
| 90 | |
| 91 | id=$(parse_pkcs11_idscip-shell shell clevis 23-1 parse_pkcs11_id(). "${PKCS11_OUTPUT_SINGLE_DIGIT}") |
| 92 | testscip-shell system . . test. "${id}" == "01" || error "Single digit: expected '01', got '${id}'" |
| 93 | |
| 94 | # Multiple objects: should pick the first Public Key ID hex value |
| 95 | PKCS11_OUTPUT_MULTIPLE="Certificate Object; type = X.509 cert |
| 96 | label: Certificate for Key Management |
| 97 | ID: 99 |
| 98 | Public Key Object; RSA 2048 bits |
| 99 | label: KEY MAN pubkey |
| 100 | ID: 42 (0x2a) |
| 101 | Usage: encrypt, wrap |
| 102 | Access: none" |
| 103 | |
| 104 | id=$(parse_pkcs11_idscip-shell shell clevis 23-1 parse_pkcs11_id(). "${PKCS11_OUTPUT_MULTIPLE}") |
| 105 | testscip-shell system . . test. "${id}" == "2a" || error "Multiple objects: expected '2a', got '${id}'" |
| 106 | |
| 107 | # Old format with multi-byte hex (no parentheses) |
| 108 | PKCS11_OUTPUT_OLD_MULTI="Public Key Object; RSA 2048 bits |
| 109 | label: KEY MAN pubkey |
| 110 | ID: ff01 |
| 111 | Usage: encrypt, wrap |
| 112 | Access: none" |
| 113 | |
| 114 | id=$(parse_pkcs11_idscip-shell shell clevis 23-1 parse_pkcs11_id(). "${PKCS11_OUTPUT_OLD_MULTI}") |
| 115 | testscip-shell system . . test. "${id}" == "ff01" || error "Old multi-byte: expected 'ff01', got '${id}'" |