blob: 04ab6f64d63f1e38829e6c14cc4383ba9c81dfb9 [file] [log] [blame]
Shawn Willden26aaa762015-02-07 00:31:41 -07001/*
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 Willdende4ffa92015-05-05 07:15:24 -060019#include <openssl/err.h>
20#include <openssl/evp.h>
Shawn Willdend79791b2015-05-09 12:48:36 +000021
22#if defined(OPENSSL_IS_BORINGSSL)
23#include <openssl/asn1.h>
24#include <openssl/cipher.h>
Adam Langleya5fce682015-02-26 13:33:18 -080025#include <openssl/pkcs8.h>
26#include <openssl/x509v3.h>
Shawn Willdend79791b2015-05-09 12:48:36 +000027#endif
Adam Langleya5fce682015-02-26 13:33:18 -080028
Shawn Willden26aaa762015-02-07 00:31:41 -070029#include <hardware/keymaster_defs.h>
30#include <keymaster/logger.h>
31
32namespace keymaster {
33
34static keymaster_error_t TranslateEvpError(int reason);
Shawn Willdend79791b2015-05-09 12:48:36 +000035#if defined(OPENSSL_IS_BORINGSSL)
Adam Langleya5fce682015-02-26 13:33:18 -080036static keymaster_error_t TranslateASN1Error(int reason);
37static keymaster_error_t TranslateCipherError(int reason);
38static keymaster_error_t TranslatePKCS8Error(int reason);
39static keymaster_error_t TranslateX509v3Error(int reason);
Shawn Willdend79791b2015-05-09 12:48:36 +000040#endif
Shawn Willden26aaa762015-02-07 00:31:41 -070041
42keymaster_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 Willdend79791b2015-05-09 12:48:36 +000054#if defined(OPENSSL_IS_BORINGSSL)
Adam Langleya5fce682015-02-26 13:33:18 -080055 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 Willdend79791b2015-05-09 12:48:36 +000063#else
64 case ERR_LIB_ASN1:
65 LOG_E("ASN.1 parsing error %d", reason);
66 return KM_ERROR_INVALID_ARGUMENT;
67#endif
Shawn Willden26aaa762015-02-07 00:31:41 -070068 }
69
Shawn Willdenf01329d2015-03-11 21:51:38 -060070 LOG_E("Openssl error %d, %d", ERR_GET_LIB(error), reason);
Shawn Willden26aaa762015-02-07 00:31:41 -070071 return KM_ERROR_UNKNOWN_ERROR;
72}
73
Shawn Willdend79791b2015-05-09 12:48:36 +000074#if defined(OPENSSL_IS_BORINGSSL)
75
Adam Langleya5fce682015-02-26 13:33:18 -080076keymaster_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
94keymaster_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
115keymaster_error_t TranslateASN1Error(int reason) {
116 switch (reason) {
Adam Langleyc3326552015-04-28 13:20:52 -0700117#if !defined(OPENSSL_IS_BORINGSSL)
Shawn Willdend79791b2015-05-09 12:48:36 +0000118 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 Langleyc3326552015-04-28 13:20:52 -0700123#endif
Shawn Willdend79791b2015-05-09 12:48:36 +0000124
Adam Langleya5fce682015-02-26 13:33:18 -0800125 case ASN1_R_ENCODE_ERROR:
126 return KM_ERROR_INVALID_ARGUMENT;
127
128 default:
129 return KM_ERROR_UNKNOWN_ERROR;
130 }
131}
132
133keymaster_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 Willdend79791b2015-05-09 12:48:36 +0000143#endif // OPENSSL_IS_BORINGSSL
144
Shawn Willden26aaa762015-02-07 00:31:41 -0700145keymaster_error_t TranslateEvpError(int reason) {
146 switch (reason) {
147
148 case EVP_R_UNKNOWN_DIGEST:
149 return KM_ERROR_UNSUPPORTED_DIGEST;
150
Shawn Willdend79791b2015-05-09 12:48:36 +0000151#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 Langleya5fce682015-02-26 13:33:18 -0800162 case EVP_R_UNSUPPORTED_ALGORITHM:
163 case EVP_R_OPERATON_NOT_INITIALIZED:
164 case EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE:
Shawn Willden26aaa762015-02-07 00:31:41 -0700165 return KM_ERROR_UNSUPPORTED_ALGORITHM;
166
Shawn Willdend79791b2015-05-09 12:48:36 +0000167#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 Langleya5fce682015-02-26 13:33:18 -0800192 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 Willden26aaa762015-02-07 00:31:41 -0700197 case EVP_R_WRONG_PUBLIC_KEY_TYPE:
198 return KM_ERROR_INVALID_KEY_BLOB;
199
Shawn Willdend79791b2015-05-09 12:48:36 +0000200#if !defined(OPENSSL_IS_BORINGSSL)
201 case EVP_R_BAD_DECRYPT:
202 case EVP_R_ENCODE_ERROR:
203#endif
Shawn Willden26aaa762015-02-07 00:31:41 -0700204 case EVP_R_DIFFERENT_PARAMETERS:
205 case EVP_R_DECODE_ERROR:
Shawn Willden26aaa762015-02-07 00:31:41 -0700206 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