diff --git a/include/libssh/pki.h b/include/libssh/pki.h index 9b104349..fa0a81e6 100644 --- a/include/libssh/pki.h +++ b/include/libssh/pki.h @@ -162,4 +162,9 @@ ssh_public_key ssh_pki_convert_key_to_publickey(const ssh_key key); ssh_private_key ssh_pki_convert_key_to_privatekey(const ssh_key key); int ssh_key_algorithm_allowed(ssh_session session, const char *type); + +/* PKCS11 URI function to check if filename is a path or a PKCS11 URI */ +bool ssh_pki_is_uri(const char *filename); +char *ssh_pki_export_pub_uri_from_priv_uri(const char *priv_uri); + #endif /* PKI_H_ */ diff --git a/src/pki.c b/src/pki.c index 6dcb1204..24ffd25d 100644 --- a/src/pki.c +++ b/src/pki.c @@ -64,6 +64,8 @@ #include "libssh/misc.h" #include "libssh/agent.h" +#define PKCS11_URI "pkcs11:" + enum ssh_keytypes_e pki_privatekey_type_from_string(const char *privkey) { char *start = NULL; @@ -1568,6 +1570,47 @@ fail: return SSH_ERROR; } +/** + *@brief Detect if the pathname in cmp is a PKCS #11 URI. + * + * @param[in] cmp The path to the public/private key + * or a private/public PKCS #11 URI. + * + * @returns true if filename is a URI starting with "pkcs11:" + * false otherwise. + */ +bool ssh_pki_is_uri(const char *cmp) +{ + int rc; + + rc = strncmp(cmp, PKCS11_URI, strlen(PKCS11_URI)); + if (rc == 0) { + return true; + } + + return false; +} + +/** + *@brief export a Public PKCS #11 URI from a Private PKCS #11 URI + * by replacing "type=private" to "type=public". + * TODO: Improve the parser + * + * @param[in] priv_uri Private PKCS #11 URI. + * + * @returns pointer to the public PKCS #11 URI + */ +char *ssh_pki_export_pub_uri_from_priv_uri(const char *priv_uri) +{ + char *pub_uri_temp = strdup(priv_uri); + + pub_uri_temp = ssh_strreplace(pub_uri_temp, + "type=private", + "type=public"); + + return pub_uri_temp; +} + /** * @brief Import a public key from the given filename. *