Shawn Willden | 26aaa76 | 2015-02-07 00:31:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "openssl_err.h" |
| 18 | |
Shawn Willden | de4ffa9 | 2015-05-05 07:15:24 -0600 | [diff] [blame] | 19 | #include <openssl/err.h> |
| 20 | #include <openssl/evp.h> |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 21 | |
| 22 | #if defined(OPENSSL_IS_BORINGSSL) |
| 23 | #include <openssl/asn1.h> |
| 24 | #include <openssl/cipher.h> |
Adam Langley | a5fce68 | 2015-02-26 13:33:18 -0800 | [diff] [blame] | 25 | #include <openssl/pkcs8.h> |
| 26 | #include <openssl/x509v3.h> |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 27 | #endif |
Adam Langley | a5fce68 | 2015-02-26 13:33:18 -0800 | [diff] [blame] | 28 | |
Shawn Willden | 26aaa76 | 2015-02-07 00:31:41 -0700 | [diff] [blame] | 29 | #include <hardware/keymaster_defs.h> |
| 30 | #include <keymaster/logger.h> |
| 31 | |
| 32 | namespace keymaster { |
| 33 | |
| 34 | static keymaster_error_t TranslateEvpError(int reason); |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 35 | #if defined(OPENSSL_IS_BORINGSSL) |
Adam Langley | a5fce68 | 2015-02-26 13:33:18 -0800 | [diff] [blame] | 36 | static keymaster_error_t TranslateASN1Error(int reason); |
| 37 | static keymaster_error_t TranslateCipherError(int reason); |
| 38 | static keymaster_error_t TranslatePKCS8Error(int reason); |
| 39 | static keymaster_error_t TranslateX509v3Error(int reason); |
Shawn Willden | 2bf4ad3 | 2015-06-01 07:33:51 -0600 | [diff] [blame] | 40 | static keymaster_error_t TranslateRsaError(int reason); |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 41 | #endif |
Shawn Willden | 26aaa76 | 2015-02-07 00:31:41 -0700 | [diff] [blame] | 42 | |
| 43 | keymaster_error_t TranslateLastOpenSslError(bool log_message) { |
| 44 | unsigned long error = ERR_peek_last_error(); |
| 45 | |
| 46 | if (log_message) { |
| 47 | LOG_D("%s", ERR_error_string(error, NULL)); |
| 48 | } |
| 49 | |
| 50 | int reason = ERR_GET_REASON(error); |
| 51 | switch (ERR_GET_LIB(error)) { |
Shawn Willden | 39ba76d | 2015-10-23 10:11:40 -0600 | [diff] [blame] | 52 | case ERR_LIB_USER: |
| 53 | return static_cast<keymaster_error_t>(reason); |
Shawn Willden | 26aaa76 | 2015-02-07 00:31:41 -0700 | [diff] [blame] | 54 | case ERR_LIB_EVP: |
| 55 | return TranslateEvpError(reason); |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 56 | #if defined(OPENSSL_IS_BORINGSSL) |
Adam Langley | a5fce68 | 2015-02-26 13:33:18 -0800 | [diff] [blame] | 57 | case ERR_LIB_ASN1: |
| 58 | return TranslateASN1Error(reason); |
| 59 | case ERR_LIB_CIPHER: |
| 60 | return TranslateCipherError(reason); |
| 61 | case ERR_LIB_PKCS8: |
| 62 | return TranslatePKCS8Error(reason); |
| 63 | case ERR_LIB_X509V3: |
| 64 | return TranslateX509v3Error(reason); |
Shawn Willden | 2bf4ad3 | 2015-06-01 07:33:51 -0600 | [diff] [blame] | 65 | case ERR_LIB_RSA: |
| 66 | return TranslateRsaError(reason); |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 67 | #else |
| 68 | case ERR_LIB_ASN1: |
| 69 | LOG_E("ASN.1 parsing error %d", reason); |
| 70 | return KM_ERROR_INVALID_ARGUMENT; |
| 71 | #endif |
Shawn Willden | 26aaa76 | 2015-02-07 00:31:41 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Shawn Willden | f01329d | 2015-03-11 21:51:38 -0600 | [diff] [blame] | 74 | LOG_E("Openssl error %d, %d", ERR_GET_LIB(error), reason); |
Shawn Willden | 26aaa76 | 2015-02-07 00:31:41 -0700 | [diff] [blame] | 75 | return KM_ERROR_UNKNOWN_ERROR; |
| 76 | } |
| 77 | |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 78 | #if defined(OPENSSL_IS_BORINGSSL) |
| 79 | |
Adam Langley | a5fce68 | 2015-02-26 13:33:18 -0800 | [diff] [blame] | 80 | keymaster_error_t TranslatePKCS8Error(int reason) { |
| 81 | switch (reason) { |
| 82 | case PKCS8_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM: |
| 83 | case PKCS8_R_UNKNOWN_CIPHER: |
| 84 | return KM_ERROR_UNSUPPORTED_ALGORITHM; |
| 85 | |
| 86 | case PKCS8_R_PRIVATE_KEY_ENCODE_ERROR: |
| 87 | case PKCS8_R_PRIVATE_KEY_DECODE_ERROR: |
| 88 | return KM_ERROR_INVALID_KEY_BLOB; |
| 89 | |
| 90 | case PKCS8_R_ENCODE_ERROR: |
| 91 | return KM_ERROR_INVALID_ARGUMENT; |
| 92 | |
| 93 | default: |
| 94 | return KM_ERROR_UNKNOWN_ERROR; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | keymaster_error_t TranslateCipherError(int reason) { |
| 99 | switch (reason) { |
| 100 | case CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH: |
| 101 | case CIPHER_R_WRONG_FINAL_BLOCK_LENGTH: |
| 102 | return KM_ERROR_INVALID_INPUT_LENGTH; |
| 103 | |
| 104 | case CIPHER_R_UNSUPPORTED_KEY_SIZE: |
| 105 | case CIPHER_R_BAD_KEY_LENGTH: |
| 106 | return KM_ERROR_UNSUPPORTED_KEY_SIZE; |
| 107 | |
| 108 | case CIPHER_R_BAD_DECRYPT: |
| 109 | return KM_ERROR_INVALID_ARGUMENT; |
| 110 | |
| 111 | case CIPHER_R_INVALID_KEY_LENGTH: |
| 112 | return KM_ERROR_INVALID_KEY_BLOB; |
| 113 | |
| 114 | default: |
| 115 | return KM_ERROR_UNKNOWN_ERROR; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | keymaster_error_t TranslateASN1Error(int reason) { |
| 120 | switch (reason) { |
Adam Langley | c332655 | 2015-04-28 13:20:52 -0700 | [diff] [blame] | 121 | #if !defined(OPENSSL_IS_BORINGSSL) |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 122 | case ASN1_R_UNSUPPORTED_CIPHER: |
| 123 | return KM_ERROR_UNSUPPORTED_ALGORITHM; |
| 124 | |
| 125 | case ASN1_R_ERROR_LOADING_SECTION: |
| 126 | return KM_ERROR_INVALID_KEY_BLOB; |
Adam Langley | c332655 | 2015-04-28 13:20:52 -0700 | [diff] [blame] | 127 | #endif |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 128 | |
Adam Langley | a5fce68 | 2015-02-26 13:33:18 -0800 | [diff] [blame] | 129 | case ASN1_R_ENCODE_ERROR: |
| 130 | return KM_ERROR_INVALID_ARGUMENT; |
| 131 | |
| 132 | default: |
| 133 | return KM_ERROR_UNKNOWN_ERROR; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | keymaster_error_t TranslateX509v3Error(int reason) { |
| 138 | switch (reason) { |
| 139 | case X509V3_R_UNKNOWN_OPTION: |
| 140 | return KM_ERROR_UNSUPPORTED_ALGORITHM; |
| 141 | |
| 142 | default: |
| 143 | return KM_ERROR_UNKNOWN_ERROR; |
| 144 | } |
| 145 | } |
| 146 | |
Shawn Willden | 2bf4ad3 | 2015-06-01 07:33:51 -0600 | [diff] [blame] | 147 | keymaster_error_t TranslateRsaError(int reason) { |
| 148 | switch (reason) { |
Shawn Willden | 7d05d88 | 2015-07-10 14:03:14 -0600 | [diff] [blame] | 149 | case RSA_R_KEY_SIZE_TOO_SMALL: |
| 150 | LOG_W("RSA key is too small to use with selected padding/digest", 0); |
| 151 | return KM_ERROR_INCOMPATIBLE_PADDING_MODE; |
Shawn Willden | 2bf4ad3 | 2015-06-01 07:33:51 -0600 | [diff] [blame] | 152 | case RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE: |
| 153 | case RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE: |
| 154 | return KM_ERROR_INVALID_INPUT_LENGTH; |
Shawn Willden | c0a6380 | 2015-07-29 16:43:17 -0600 | [diff] [blame] | 155 | case RSA_R_DATA_TOO_LARGE_FOR_MODULUS: |
| 156 | return KM_ERROR_INVALID_ARGUMENT; |
Shawn Willden | 2bf4ad3 | 2015-06-01 07:33:51 -0600 | [diff] [blame] | 157 | default: |
| 158 | return KM_ERROR_UNKNOWN_ERROR; |
| 159 | }; |
| 160 | } |
| 161 | |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 162 | #endif // OPENSSL_IS_BORINGSSL |
| 163 | |
Shawn Willden | 26aaa76 | 2015-02-07 00:31:41 -0700 | [diff] [blame] | 164 | keymaster_error_t TranslateEvpError(int reason) { |
| 165 | switch (reason) { |
| 166 | |
| 167 | case EVP_R_UNKNOWN_DIGEST: |
| 168 | return KM_ERROR_UNSUPPORTED_DIGEST; |
| 169 | |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 170 | #if !defined(OPENSSL_IS_BORINGSSL) |
| 171 | case EVP_R_UNSUPPORTED_PRF: |
| 172 | case EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM: |
| 173 | case EVP_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION: |
| 174 | case EVP_R_UNSUPPORTED_SALT_TYPE: |
| 175 | case EVP_R_UNKNOWN_PBE_ALGORITHM: |
| 176 | case EVP_R_UNSUPORTED_NUMBER_OF_ROUNDS: |
| 177 | case EVP_R_UNSUPPORTED_CIPHER: |
| 178 | case EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE: |
| 179 | case EVP_R_UNKNOWN_CIPHER: |
| 180 | #endif |
Adam Langley | a5fce68 | 2015-02-26 13:33:18 -0800 | [diff] [blame] | 181 | case EVP_R_UNSUPPORTED_ALGORITHM: |
| 182 | case EVP_R_OPERATON_NOT_INITIALIZED: |
| 183 | case EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE: |
Shawn Willden | 26aaa76 | 2015-02-07 00:31:41 -0700 | [diff] [blame] | 184 | return KM_ERROR_UNSUPPORTED_ALGORITHM; |
| 185 | |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 186 | #if !defined(OPENSSL_IS_BORINGSSL) |
| 187 | case EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH: |
| 188 | case EVP_R_WRONG_FINAL_BLOCK_LENGTH: |
| 189 | return KM_ERROR_INVALID_INPUT_LENGTH; |
| 190 | |
| 191 | case EVP_R_UNSUPPORTED_KEYLENGTH: |
| 192 | case EVP_R_BAD_KEY_LENGTH: |
| 193 | return KM_ERROR_UNSUPPORTED_KEY_SIZE; |
| 194 | #endif |
| 195 | |
| 196 | #if !defined(OPENSSL_IS_BORINGSSL) |
| 197 | case EVP_R_BAD_BLOCK_LENGTH: |
| 198 | case EVP_R_BN_DECODE_ERROR: |
| 199 | case EVP_R_BN_PUBKEY_ERROR: |
| 200 | case EVP_R_CIPHER_PARAMETER_ERROR: |
| 201 | case EVP_R_ERROR_LOADING_SECTION: |
| 202 | case EVP_R_EXPECTING_A_ECDSA_KEY: |
| 203 | case EVP_R_EXPECTING_A_EC_KEY: |
| 204 | case EVP_R_INVALID_DIGEST: |
| 205 | case EVP_R_INVALID_KEY_LENGTH: |
| 206 | case EVP_R_NO_DSA_PARAMETERS: |
| 207 | case EVP_R_PRIVATE_KEY_DECODE_ERROR: |
| 208 | case EVP_R_PRIVATE_KEY_ENCODE_ERROR: |
| 209 | case EVP_R_PUBLIC_KEY_NOT_RSA: |
| 210 | #endif |
Adam Langley | a5fce68 | 2015-02-26 13:33:18 -0800 | [diff] [blame] | 211 | case EVP_R_BUFFER_TOO_SMALL: |
| 212 | case EVP_R_EXPECTING_AN_RSA_KEY: |
| 213 | case EVP_R_EXPECTING_A_DH_KEY: |
| 214 | case EVP_R_EXPECTING_A_DSA_KEY: |
| 215 | case EVP_R_MISSING_PARAMETERS: |
Shawn Willden | 26aaa76 | 2015-02-07 00:31:41 -0700 | [diff] [blame] | 216 | case EVP_R_WRONG_PUBLIC_KEY_TYPE: |
| 217 | return KM_ERROR_INVALID_KEY_BLOB; |
| 218 | |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 219 | #if !defined(OPENSSL_IS_BORINGSSL) |
| 220 | case EVP_R_BAD_DECRYPT: |
| 221 | case EVP_R_ENCODE_ERROR: |
| 222 | #endif |
Shawn Willden | 26aaa76 | 2015-02-07 00:31:41 -0700 | [diff] [blame] | 223 | case EVP_R_DIFFERENT_PARAMETERS: |
| 224 | case EVP_R_DECODE_ERROR: |
Shawn Willden | 26aaa76 | 2015-02-07 00:31:41 -0700 | [diff] [blame] | 225 | return KM_ERROR_INVALID_ARGUMENT; |
| 226 | |
| 227 | case EVP_R_DIFFERENT_KEY_TYPES: |
| 228 | return KM_ERROR_INCOMPATIBLE_ALGORITHM; |
| 229 | } |
| 230 | |
| 231 | return KM_ERROR_UNKNOWN_ERROR; |
| 232 | } |
| 233 | |
| 234 | } // namespace keymaster |