Question: I have a [YubiKey NEO](https://www.yubico.com/products/yubikey-hardware/yubikey-neo/) which has a lot of amazing capabilities such as OTP, U2F, and PGP smart card for PGP/GPG and even SSH keys. One of the applications I've discovered recently for the device is [a PIV applet](https://developers.yubico.com/yubico-piv-tool/) which you can use to securely store a SSL certificate's private RSA key.

I find this pretty fascinating, as it makes it much more difficult without physical access to steal a SSL certificate.

Is it possible to use a smart card like this for a SSL server's private key? I've never seen configuration in Apache or nginx which would seem to indicate support for anything other than file-based SSL private keys. 

Also, [the demo given for the PIV applet](https://developers.yubico.com/yubico-piv-tool/Certificate_Authority_with_NEO.html) shows how to create a local file-based private key and then send it to the smart card; is there a way to create the key securely on the card, so that it is never stored anywhere? I know I could just store it in a RAM disk/filesystem so that it's never written to disk, but is there a way to generate it on-device as is possible using OpenPGP for PGP keys?

Answer: 
> 
> I've never seen configuration in Apache or nginx which would seem to
>  indicate support for anything other than file-based SSL private keys.
> 
> 
> 

You are using the Yubikey as a Hardware Security Module (HSM). They key is stored on and never leaves the HSM and all encryption and decryption is done on the HSM. The Yubikey seems to fulfill such the requirements. 

You might need to write your own PKCS#11 driver since there isn't one out there. For the server side, you would have to use `mod_nss` instead of `mod_ssl`. `mod_nss` allows you to use a custom PKCS#11 driver whereas `mod_ssl` uses the default openssl engine.

Compared with other purpose built HSMs, I am unsure of the stability of using the Yubikey as a HSM. This is because the main use case of a yubikey is for applications which require one-time access, e.g. authentication, encrypting an email and not for applications which require continuous access. A server under load might overwhelm the Yubikey and result in sluggish performance.
Apache supports specifying the crypto device. The directive is called [SSLCryptoDevice](https://httpd.apache.org/docs/2.4/mod/mod_ssl.html#sslcryptodevice). You can specify 

Pkcs11 as the device. Update: according to [this answer](https://serverfault.com/a/711611/81387) it's not supported with mod\_ssl. That answer suggests mod\_nss, which supports it. According to <https://mod.gnutls.org/> mod\_gnutls also supports pkcs11.