webauthn::WebAuthn method cose_public_key_pem (protected)

 <instance of webauthn::WebAuthn[i]> cose_public_key_pem \
    [ -cose cose ]

Defined in packages/webauthn/tcl/webauthn-procs.tcl

Convert a COSE_Key into a PEM-encoded public key. Supports the WebAuthn-relevant COSE key types: - EC2 (kty=2): Elliptic Curve keys (e.g., ES256 / P-256) - RSA (kty=3): RSA keys (e.g., RS256) - OKP (kty=1): Octet Key Pair keys, including: * Ed25519 / Ed448 (signature) * X25519 / X448 (key agreement) Note: This method performs key *conversion only*. It does not enforce algorithm or usage constraints. Callers (e.g., assertion verification) must ensure that the selected key type and curve are appropriate for the intended operation (e.g., signature vs. key agreement).

Switches:
-cose (optional)
Parsed COSE_Key dict.
Returns:
PEM string of public key

Testcases:
No testcase defined.
Source code:
if {![dict exists $cose 1]} {
    throw {validation key-invalid} "COSE key missing kty"
}

set kty [dict get $cose 1]

switch -- $kty {
    1 {
        #
        # OKP
        #
        if {![dict exists $cose -1] || ![dict exists $cose -2]} {
            throw {validation key-invalid} "OKP COSE key missing crv/x"
        }

        set crv [dict get $cose -1]
        set crvMap {
            6 Ed25519
            7 Ed448
            4 X25519
            5 X448
        }

        if {![dict exists $crvMap $crv]} {
            throw {validation curve-unsupported}  "unsupported COSE OKP curve \"$crv\""
        }

        set name [dict get $crvMap $crv]
        set x [dict get $cose -2]

        return [ns_crypto::key import  -name OKP  -params [dict create crv $name x $x]]
    }
    2 {
        #
        # EC2
        #
        if {![dict exists $cose -1]} {
            throw {validation key-invalid} "EC COSE key missing curve"
        }
        if {![dict exists $cose -2] || ![dict exists $cose -3]} {
            throw {validation key-invalid} "EC COSE key missing x/y coordinates"
        }

        set crv [dict get $cose -1]
        set curveMap {
            1 prime256v1
            2 secp384r1
            3 secp521r1
            8 secp256k1
        }
        if {![dict exists $curveMap $crv]} {
            throw {validation curve-unsupported}  "unsupported COSE EC curve \"$crv\""
        }

        set group [dict get $curveMap $crv]
        set x [dict get $cose -2]
        set y [dict get $cose -3]

        return [ns_crypto::key import  -name EC  -params [dict create group $group x $x y $y]]
    }

    3 {
        #
        # RSA
        #
        if {![dict exists $cose -1] || ![dict exists $cose -2]} {
            throw {validation key-invalid}  "RSA COSE key missing modulus/exponent"
        }

        set n [dict get $cose -1]
        set e [dict get $cose -2]

        return [ns_crypto::key import  -name RSA  -params [dict create n $n e $e]]
    }

    default {
        throw {validation keytype-unsupported}  "unsupported COSE kty \"$kty\""
    }
}
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: