HSM SECURITY Securing PKCS#11 Interfaces
Total Page:16
File Type:pdf, Size:1020Kb
HSM SECURITY Securing PKCS#11 Interfaces Whitepaper v1.9 August 2019 Cryptosense | HSM Security Whitepaper 2 Contents 1. Introduction 3 2. Brief History of PKCS#11 3 3. Overview of PKCS#11 Design 3 4. Threat Scenario and Security Properties for PKCS#11 4 5. Attacking PKCS#11 HSM Security 5 5.1. Memory corruption attacks 5 5.2. Non-compliant PKCS#11 Implementations 6 5.3. Errors in Crypto Implementation 7 5.4. Attacks on Compliant PKCS#11 Implementations 7 6. Errors in Using the HSM 9 6.1. Insecure Crypto Mechanisms 10 6.2. Misuse of Crypto 10 6.3. Key Management Failures 10 7. Summary 10 8. About Cryptosense 11 9. Bibliography 11 This document is protected by copyright. No part of the document may be reproduced or redistributed in any form by any means without the prior written authorization of Cryptosense. This document is provided “as is" without any warranty of any kind. Cryptosense SA cannot be held responsible for any misconduct or malicious use of this document by a third party or damage caused by any information this document contains. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. Cryptosense SA, 231 Rue Saint-Honoré, 75001 Paris France cryptosense.com © Cryptosense 2019 Cryptosense | HSM Security Whitepaper 3 1. Introduction Modern applications that use cryptography usually access that functionality via an application program interface (API) to a software or hardware cryptographic provider. Security-critical applications often make use of Hardware Security Modules (HSMs): special purpose computers that provide high-speed cryptographic services whilst keeping key material inside a tamper- sensitive enclosure. Together with smart cards or similar chip-based tokens, they form the backbone of many modern cryptographic applications in diverse sectors from banking to automotive. HSMs and smart cards typically undergo a security certification procedure such as FIPS 140 or Common Criteria that examines tamper resistance and susceptibility to side channel attacks amongst other things. However, neither examines the security of the interface of the device. In fact, it would be hard to categorically state that the interface is secure, since each user will typically choose different configuration options that may affect security, and use of the interface by applications must also be taken into account. The most commonly used API standard is RSA PKCS#11 [3], also known as Cryptoki. Following convention we will refer to PKCS#11 to mean the API itself rather than the document that defines it. While PKCS#11 has important benefits for interoperability since it is so widely supported, it is a large and open standard supporting a wide range of use cases. This makes it extremely tricky to implement correctly, and difficult to configure and use in a secure way. In this white paper, we discuss attacks on systems using the PKCS#11 API. We consider what it means for an interface to be secure, and we discuss how to audit applications’ use of the API. There will be plenty of concrete examples of attacks on real devices, and we will explain how to detect these issues using Cryptosense Analyzer Platform. 2. Brief History of PKCS#11 The Public Key Cryptography Standards (PKCS) were proposed by RSA Inc, the company set up to commercialise the patent on the RSA encryption and signature algorithms. The first standard in the series, PKCS#1, describes the required mathematical properties of RSA keys, how to perform RSA encryption, decryption, signing, and verification using a standard padding format etc. Further standards described password-based cryptography, various aspects of Public Key Infrastructure, Diffie-Hellman Key Agreement. Many of the PKCS have been abandoned or fallen out of use, but PKCS#11, the standard that defines the Cryptoki interface for cryptographic hardware, was handed over to the OASIS PKCS#11 Technical Committee in 2012, who continue to revise and develop it. The first version (v1.0) of PKCS#11 was published in 1995. The current version is 2.40, released in 2015. Version 3.0 is in preparation. 3. Overview of PKCS#11 Design RSA PKCS#11 describes its API in just under 400 pages [3]. Here we give just a brief description to enable understanding of the various families of vulnerabilities. © Cryptosense 2019 Cryptosense | HSM Security Whitepaper 4 The API is stateful, maintaining in particular session state, operation state for e.g. encryption of large chunks of data, and information about cryptographic objects. In a PKCS#11-based API, applications initiate a session with the cryptographic token, by supplying a PIN. Once a session is initiated, the application may access the objects stored on the token, such as keys and certificates. Objects are referenced in the API via handles, which can be thought of as pointers to or names for the objects. Objects have attributes, which may be e.g. the value of a key, or metadata giving properties of the object, e.g. whether the key may be used for encryption, or for encrypting other keys. New objects can be created by calling a key generation command, or by ‘unwrapping’ an encrypted key packet. In both cases a fresh handle is returned. When a function in the token’s API is called with a reference to a particular object, the token first checks that the attributes of the object allow it to be used for that function. For example, if the encrypt function is called with the handle for a particular key, that key must have the CKA_ ENCRYPT encrypt attribute set to true. To protect a key from being revealed, the attribute CKA_SENSITIVE can be set to true. This means that requests to view the object’s key value via the API will result in an error message. Further protection can be given to a key by setting the CKA_EXTRACTABLE attribute to false. In this case, even a request to wrap the key, i.e. export it from the device encrypted, will be refused. 4. Threat Scenario and Security Properties for PKCS#11 The API between applications and cryptographic services is a natural trust boundary. We would expect a well-designed interface to maintain some security properties even if the application becomes compromised. This is indeed what PKCS#11 aims to do, as stated (in a rather roundabout way) in the Security Considerations section of the standard [9]: As an interface to cryptographic devices, Cryptoki provides a basis for security in a computer or communications system. Two of the particular features of the interface that facilitate such security are the following: 1. “Access to private objects on the token, and possibly to cryptographic functions and/or certificates on the token as well, requires a PIN. Thus, possessing the cryptographic device that implements the token may not be sufficient to use it; the PIN may also be needed. 2. Additional protection can be given to private keys and secret keys by marking them as “sensitive” or “unextractable”. Sensitive keys cannot be revealed in plaintext off the token, and unextractable keys cannot be revealed off the token even when encrypted (though they can still be used as keys).” In PKCS#11, the PIN required to open a session on the cryptographic device is passed directly from the application to the device, hence if the application is compromised, the PIN will typically be compromised as well, as the standard notes: “While the token itself may be secure, one must also consider the security of the operating system by which the application interfaces to it, especially since the PIN may be passed through the operating system. This can make it easy for a rogue application on the operating system to obtain the PIN; it is also possible that other devices monitoring communication lines to the © Cryptosense 2019 Cryptosense | HSM Security Whitepaper 5 cryptographic device can obtain the PIN. Rogue applications and devices may also change the commands sent to the cryptographic device to obtain services other than what the application requested. [..] “We note that none of the attacks just described can compromise keys marked “sensitive”, since a key that is sensitive will always remain sensitive. Similarly, a key that is unextractable cannot be modified to be extractable.” Real-world instances of attacks where the intruder has obtained direct access to the HSM API include the RBS Worldpay compromise [1] and DigiNotar breach [2]. To summarise, the aim of the PKCS#11 API is to protect SENSITIVE and UNEXTRACTABLE keys on the device no matter whether the PIN has been intercepted and the application compromised so that it can send any sequence of PKCS#11 calls to the interface. In the rest of this whitepaper, we will concentrate on attacks on precisely these properties. 5. Attacking PKCS#11 HSM Security We have established a threat scenario where an attacker has full access to the PKCS#11 API using legitimate user credentials, and an objective to obtain the values of the “sensitive” and “unextractable” keys inside. Below we discuss the various ways this can be achieved. 5.1. Memory corruption attacks The first thing to note is that PKCS#11 describes a large low-level C API, with buffers, pointers, Type-Length-Value lists, etc., all of which must be carefully managed to avoid buffer overruns or other memory corruption based attacks. Like any large and complex system of this nature, bugs are quite common in practice. Sometimes, partial input sanitisation takes place in the PKCS#11 driver on the client machine rather than in the HSM itself, meaning that an attacker can bypass the checks to force invalid calls into the HSM and trigger a memory management error.