@@ -19,8 +19,76 @@ KEY="${P2C_CERT/.pem/.key}"
1919# Get the directory where the key is located
2020KEY_DIR=$( dirname " $KEY " )
2121
22+ # PKCS#11 sends signature input for RSA in ASN.1 encoding
23+ if (openssl asn1parse -inform DER -in " $P2C_DATA " 2> /dev/null) | grep -q ' SEQUENCE' ; then
24+ echo " $P2C_DATA ist ASN.1-coded extract raw data from DigestInfo"
25+
26+ openssl asn1parse -inform DER -in " $P2C_DATA "
27+
28+ hash_hex=$( openssl asn1parse -inform DER -in " $P2C_DATA " | awk ' /OCTET STRING/ {sub(/^.*\[HEX DUMP\]:/, ""); print $0; exit}' )
29+ if [ ! -n " $hash_hex " ]; then
30+ echo " Extraction failed"
31+ openssl asn1parse -inform DER -in " $P2C_DATA "
32+ exit 1
33+ fi
34+
35+ echo " $hash_hex " | xxd -r -p > " ${P2C_DATA} .raw"
36+
37+ if [ ! -s " ${P2C_DATA} .raw" ]; then
38+ echo " Extraction failed"
39+ openssl asn1parse -inform DER -in " $P2C_DATA "
40+ exit 1
41+ fi
42+
43+ export P2C_DATA=" ${P2C_DATA} .raw"
44+ fi
45+
46+ ls -l " $P2C_DATA "
47+
2248# sign hashed data
2349openssl pkeyutl -sign -inkey " $KEY " -in " $P2C_DATA " -out " $P2C_SIG "
2450
2551# Verify plain the signature
2652openssl pkeyutl -verify -certin -inkey $P2C_CERT -in $P2C_DATA -sigfile $P2C_SIG
53+
54+ # The signer returns the signature in ASN.1 format for ECDSA/EDDSA, and in DER format for RSA.
55+ # The PKCS#11 expects the signature in DER format for RSA and in raw R|S for ECDSA/EDDSA.
56+ if [ " $P2C_MECHANISM " == " CKM_ECDSA" ]; then
57+ echo " Converting ECDSA signature from asn.1 to DER format"
58+
59+ # Determine curve from certificate if available
60+ curve_bytes=32 # Default for P-256
61+
62+ if [ -f " ${P2C_CERT} " ]; then
63+ curve_info=$( openssl x509 -in " ${P2C_CERT} " -text -noout | sed -n ' /Public Key Algorithm/,/Signature Algorithm/p' | grep -E " (ASN1 OID|NIST CURVE|brainpool|secp|prime)" )
64+
65+ if echo " $curve_info " | grep -q " brainpoolP512r1\|secp521r1" ; then
66+ curve_bytes=66 # P-521
67+ echo " Detected P-521 curve"
68+ elif echo " $curve_info " | grep -q " brainpoolP384r1\|secp384r1" ; then
69+ curve_bytes=48 # P-384
70+ echo " Detected P-384 curve"
71+ elif echo " $curve_info " | grep -q " brainpoolP256r1\|secp256r1\|prime256v1" ; then
72+ curve_bytes=32 # P-256
73+ echo " Detected P-256 curve"
74+ else
75+ echo " Unknown curve, using default P-256"
76+ fi
77+
78+ echo " Detected curve requiring $curve_bytes bytes per component"
79+ fi
80+ hex_chars=$(( curve_bytes * 2 ))
81+
82+ # openssl asn1parse -inform DER -in $P2C_SIG
83+
84+ r=$( openssl asn1parse -inform DER -in $P2C_SIG | awk ' /INTEGER/ {sub(/^.*:/,"",$0); print $0}' | head -1)
85+ s=$( openssl asn1parse -inform DER -in $P2C_SIG | awk ' /INTEGER/ {sub(/^.*:/,"",$0); print $0}' | tail -1)
86+
87+ # Use detected curve length for padding
88+ printf " %0*s" " $hex_chars " " $r " | tr ' ' 0 | xxd -r -p > r.bin
89+ printf " %0*s" " $hex_chars " " $s " | tr ' ' 0 | xxd -r -p > s.bin
90+
91+ cat r.bin s.bin > $P2C_SIG
92+
93+ rm r.bin s.bin
94+ fi
0 commit comments