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 | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 40 | #endif |
Shawn Willden | 26aaa76 | 2015-02-07 00:31:41 -0700 | [diff] [blame] | 41 | |
| 42 | keymaster_error_t TranslateLastOpenSslError(bool log_message) { |
| 43 | unsigned long error = ERR_peek_last_error(); |
| 44 | |
| 45 | if (log_message) { |
| 46 | LOG_D("%s", ERR_error_string(error, NULL)); |
| 47 | } |
| 48 | |
| 49 | int reason = ERR_GET_REASON(error); |
| 50 | switch (ERR_GET_LIB(error)) { |
| 51 | |
| 52 | case ERR_LIB_EVP: |
| 53 | return TranslateEvpError(reason); |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 54 | #if defined(OPENSSL_IS_BORINGSSL) |
Adam Langley | a5fce68 | 2015-02-26 13:33:18 -0800 | [diff] [blame] | 55 | case ERR_LIB_ASN1: |
| 56 | return TranslateASN1Error(reason); |
| 57 | case ERR_LIB_CIPHER: |
| 58 | return TranslateCipherError(reason); |
| 59 | case ERR_LIB_PKCS8: |
| 60 | return TranslatePKCS8Error(reason); |
| 61 | case ERR_LIB_X509V3: |
| 62 | return TranslateX509v3Error(reason); |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 63 | #else |
| 64 | case ERR_LIB_ASN1: |
| 65 | LOG_E("ASN.1 parsing error %d", reason); |
| 66 | return KM_ERROR_INVALID_ARGUMENT; |
| 67 | #endif |
Shawn Willden | 26aaa76 | 2015-02-07 00:31:41 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Shawn Willden | f01329d | 2015-03-11 21:51:38 -0600 | [diff] [blame] | 70 | LOG_E("Openssl error %d, %d", ERR_GET_LIB(error), reason); |
Shawn Willden | 26aaa76 | 2015-02-07 00:31:41 -0700 | [diff] [blame] | 71 | return KM_ERROR_UNKNOWN_ERROR; |
| 72 | } |
| 73 | |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 74 | #if defined(OPENSSL_IS_BORINGSSL) |
| 75 | |
Adam Langley | a5fce68 | 2015-02-26 13:33:18 -0800 | [diff] [blame] | 76 | keymaster_error_t TranslatePKCS8Error(int reason) { |
| 77 | switch (reason) { |
| 78 | case PKCS8_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM: |
| 79 | case PKCS8_R_UNKNOWN_CIPHER: |
| 80 | return KM_ERROR_UNSUPPORTED_ALGORITHM; |
| 81 | |
| 82 | case PKCS8_R_PRIVATE_KEY_ENCODE_ERROR: |
| 83 | case PKCS8_R_PRIVATE_KEY_DECODE_ERROR: |
| 84 | return KM_ERROR_INVALID_KEY_BLOB; |
| 85 | |
| 86 | case PKCS8_R_ENCODE_ERROR: |
| 87 | return KM_ERROR_INVALID_ARGUMENT; |
| 88 | |
| 89 | default: |
| 90 | return KM_ERROR_UNKNOWN_ERROR; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | keymaster_error_t TranslateCipherError(int reason) { |
| 95 | switch (reason) { |
| 96 | case CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH: |
| 97 | case CIPHER_R_WRONG_FINAL_BLOCK_LENGTH: |
| 98 | return KM_ERROR_INVALID_INPUT_LENGTH; |
| 99 | |
| 100 | case CIPHER_R_UNSUPPORTED_KEY_SIZE: |
| 101 | case CIPHER_R_BAD_KEY_LENGTH: |
| 102 | return KM_ERROR_UNSUPPORTED_KEY_SIZE; |
| 103 | |
| 104 | case CIPHER_R_BAD_DECRYPT: |
| 105 | return KM_ERROR_INVALID_ARGUMENT; |
| 106 | |
| 107 | case CIPHER_R_INVALID_KEY_LENGTH: |
| 108 | return KM_ERROR_INVALID_KEY_BLOB; |
| 109 | |
| 110 | default: |
| 111 | return KM_ERROR_UNKNOWN_ERROR; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | keymaster_error_t TranslateASN1Error(int reason) { |
| 116 | switch (reason) { |
Adam Langley | c332655 | 2015-04-28 13:20:52 -0700 | [diff] [blame^] | 117 | #if !defined(OPENSSL_IS_BORINGSSL) |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 118 | case ASN1_R_UNSUPPORTED_CIPHER: |
| 119 | return KM_ERROR_UNSUPPORTED_ALGORITHM; |
| 120 | |
| 121 | case ASN1_R_ERROR_LOADING_SECTION: |
| 122 | return KM_ERROR_INVALID_KEY_BLOB; |
Adam Langley | c332655 | 2015-04-28 13:20:52 -0700 | [diff] [blame^] | 123 | #endif |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 124 | |
Adam Langley | a5fce68 | 2015-02-26 13:33:18 -0800 | [diff] [blame] | 125 | case ASN1_R_ENCODE_ERROR: |
| 126 | return KM_ERROR_INVALID_ARGUMENT; |
| 127 | |
| 128 | default: |
| 129 | return KM_ERROR_UNKNOWN_ERROR; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | keymaster_error_t TranslateX509v3Error(int reason) { |
| 134 | switch (reason) { |
| 135 | case X509V3_R_UNKNOWN_OPTION: |
| 136 | return KM_ERROR_UNSUPPORTED_ALGORITHM; |
| 137 | |
| 138 | default: |
| 139 | return KM_ERROR_UNKNOWN_ERROR; |
| 140 | } |
| 141 | } |
| 142 | |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 143 | #endif // OPENSSL_IS_BORINGSSL |
| 144 | |
Shawn Willden | 26aaa76 | 2015-02-07 00:31:41 -0700 | [diff] [blame] | 145 | keymaster_error_t TranslateEvpError(int reason) { |
| 146 | switch (reason) { |
| 147 | |
| 148 | case EVP_R_UNKNOWN_DIGEST: |
| 149 | return KM_ERROR_UNSUPPORTED_DIGEST; |
| 150 | |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 151 | #if !defined(OPENSSL_IS_BORINGSSL) |
| 152 | case EVP_R_UNSUPPORTED_PRF: |
| 153 | case EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM: |
| 154 | case EVP_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION: |
| 155 | case EVP_R_UNSUPPORTED_SALT_TYPE: |
| 156 | case EVP_R_UNKNOWN_PBE_ALGORITHM: |
| 157 | case EVP_R_UNSUPORTED_NUMBER_OF_ROUNDS: |
| 158 | case EVP_R_UNSUPPORTED_CIPHER: |
| 159 | case EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE: |
| 160 | case EVP_R_UNKNOWN_CIPHER: |
| 161 | #endif |
Adam Langley | a5fce68 | 2015-02-26 13:33:18 -0800 | [diff] [blame] | 162 | case EVP_R_UNSUPPORTED_ALGORITHM: |
| 163 | case EVP_R_OPERATON_NOT_INITIALIZED: |
| 164 | case EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE: |
Shawn Willden | 26aaa76 | 2015-02-07 00:31:41 -0700 | [diff] [blame] | 165 | return KM_ERROR_UNSUPPORTED_ALGORITHM; |
| 166 | |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 167 | #if !defined(OPENSSL_IS_BORINGSSL) |
| 168 | case EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH: |
| 169 | case EVP_R_WRONG_FINAL_BLOCK_LENGTH: |
| 170 | return KM_ERROR_INVALID_INPUT_LENGTH; |
| 171 | |
| 172 | case EVP_R_UNSUPPORTED_KEYLENGTH: |
| 173 | case EVP_R_BAD_KEY_LENGTH: |
| 174 | return KM_ERROR_UNSUPPORTED_KEY_SIZE; |
| 175 | #endif |
| 176 | |
| 177 | #if !defined(OPENSSL_IS_BORINGSSL) |
| 178 | case EVP_R_BAD_BLOCK_LENGTH: |
| 179 | case EVP_R_BN_DECODE_ERROR: |
| 180 | case EVP_R_BN_PUBKEY_ERROR: |
| 181 | case EVP_R_CIPHER_PARAMETER_ERROR: |
| 182 | case EVP_R_ERROR_LOADING_SECTION: |
| 183 | case EVP_R_EXPECTING_A_ECDSA_KEY: |
| 184 | case EVP_R_EXPECTING_A_EC_KEY: |
| 185 | case EVP_R_INVALID_DIGEST: |
| 186 | case EVP_R_INVALID_KEY_LENGTH: |
| 187 | case EVP_R_NO_DSA_PARAMETERS: |
| 188 | case EVP_R_PRIVATE_KEY_DECODE_ERROR: |
| 189 | case EVP_R_PRIVATE_KEY_ENCODE_ERROR: |
| 190 | case EVP_R_PUBLIC_KEY_NOT_RSA: |
| 191 | #endif |
Adam Langley | a5fce68 | 2015-02-26 13:33:18 -0800 | [diff] [blame] | 192 | case EVP_R_BUFFER_TOO_SMALL: |
| 193 | case EVP_R_EXPECTING_AN_RSA_KEY: |
| 194 | case EVP_R_EXPECTING_A_DH_KEY: |
| 195 | case EVP_R_EXPECTING_A_DSA_KEY: |
| 196 | case EVP_R_MISSING_PARAMETERS: |
Shawn Willden | 26aaa76 | 2015-02-07 00:31:41 -0700 | [diff] [blame] | 197 | case EVP_R_WRONG_PUBLIC_KEY_TYPE: |
| 198 | return KM_ERROR_INVALID_KEY_BLOB; |
| 199 | |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 200 | #if !defined(OPENSSL_IS_BORINGSSL) |
| 201 | case EVP_R_BAD_DECRYPT: |
| 202 | case EVP_R_ENCODE_ERROR: |
| 203 | #endif |
Shawn Willden | 26aaa76 | 2015-02-07 00:31:41 -0700 | [diff] [blame] | 204 | case EVP_R_DIFFERENT_PARAMETERS: |
| 205 | case EVP_R_DECODE_ERROR: |
Shawn Willden | 26aaa76 | 2015-02-07 00:31:41 -0700 | [diff] [blame] | 206 | return KM_ERROR_INVALID_ARGUMENT; |
| 207 | |
| 208 | case EVP_R_DIFFERENT_KEY_TYPES: |
| 209 | return KM_ERROR_INCOMPATIBLE_ALGORITHM; |
| 210 | } |
| 211 | |
| 212 | return KM_ERROR_UNKNOWN_ERROR; |
| 213 | } |
| 214 | |
| 215 | } // namespace keymaster |