blob: b9c61538c5498534b7dba363f321fe1869e2eb34 [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) {
Shawn Willdend79791b2015-05-09 12:48:36 +0000117 case ASN1_R_UNSUPPORTED_CIPHER:
118 return KM_ERROR_UNSUPPORTED_ALGORITHM;
119
120 case ASN1_R_ERROR_LOADING_SECTION:
121 return KM_ERROR_INVALID_KEY_BLOB;
122
Adam Langleya5fce682015-02-26 13:33:18 -0800123 case ASN1_R_ENCODE_ERROR:
124 return KM_ERROR_INVALID_ARGUMENT;
125
126 default:
127 return KM_ERROR_UNKNOWN_ERROR;
128 }
129}
130
131keymaster_error_t TranslateX509v3Error(int reason) {
132 switch (reason) {
133 case X509V3_R_UNKNOWN_OPTION:
134 return KM_ERROR_UNSUPPORTED_ALGORITHM;
135
136 default:
137 return KM_ERROR_UNKNOWN_ERROR;
138 }
139}
140
Shawn Willdend79791b2015-05-09 12:48:36 +0000141#endif // OPENSSL_IS_BORINGSSL
142
Shawn Willden26aaa762015-02-07 00:31:41 -0700143keymaster_error_t TranslateEvpError(int reason) {
144 switch (reason) {
145
146 case EVP_R_UNKNOWN_DIGEST:
147 return KM_ERROR_UNSUPPORTED_DIGEST;
148
Shawn Willdend79791b2015-05-09 12:48:36 +0000149#if !defined(OPENSSL_IS_BORINGSSL)
150 case EVP_R_UNSUPPORTED_PRF:
151 case EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM:
152 case EVP_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION:
153 case EVP_R_UNSUPPORTED_SALT_TYPE:
154 case EVP_R_UNKNOWN_PBE_ALGORITHM:
155 case EVP_R_UNSUPORTED_NUMBER_OF_ROUNDS:
156 case EVP_R_UNSUPPORTED_CIPHER:
157 case EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE:
158 case EVP_R_UNKNOWN_CIPHER:
159#endif
Adam Langleya5fce682015-02-26 13:33:18 -0800160 case EVP_R_UNSUPPORTED_ALGORITHM:
161 case EVP_R_OPERATON_NOT_INITIALIZED:
162 case EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE:
Shawn Willden26aaa762015-02-07 00:31:41 -0700163 return KM_ERROR_UNSUPPORTED_ALGORITHM;
164
Shawn Willdend79791b2015-05-09 12:48:36 +0000165#if !defined(OPENSSL_IS_BORINGSSL)
166 case EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH:
167 case EVP_R_WRONG_FINAL_BLOCK_LENGTH:
168 return KM_ERROR_INVALID_INPUT_LENGTH;
169
170 case EVP_R_UNSUPPORTED_KEYLENGTH:
171 case EVP_R_BAD_KEY_LENGTH:
172 return KM_ERROR_UNSUPPORTED_KEY_SIZE;
173#endif
174
175#if !defined(OPENSSL_IS_BORINGSSL)
176 case EVP_R_BAD_BLOCK_LENGTH:
177 case EVP_R_BN_DECODE_ERROR:
178 case EVP_R_BN_PUBKEY_ERROR:
179 case EVP_R_CIPHER_PARAMETER_ERROR:
180 case EVP_R_ERROR_LOADING_SECTION:
181 case EVP_R_EXPECTING_A_ECDSA_KEY:
182 case EVP_R_EXPECTING_A_EC_KEY:
183 case EVP_R_INVALID_DIGEST:
184 case EVP_R_INVALID_KEY_LENGTH:
185 case EVP_R_NO_DSA_PARAMETERS:
186 case EVP_R_PRIVATE_KEY_DECODE_ERROR:
187 case EVP_R_PRIVATE_KEY_ENCODE_ERROR:
188 case EVP_R_PUBLIC_KEY_NOT_RSA:
189#endif
Adam Langleya5fce682015-02-26 13:33:18 -0800190 case EVP_R_BUFFER_TOO_SMALL:
191 case EVP_R_EXPECTING_AN_RSA_KEY:
192 case EVP_R_EXPECTING_A_DH_KEY:
193 case EVP_R_EXPECTING_A_DSA_KEY:
194 case EVP_R_MISSING_PARAMETERS:
Shawn Willden26aaa762015-02-07 00:31:41 -0700195 case EVP_R_WRONG_PUBLIC_KEY_TYPE:
196 return KM_ERROR_INVALID_KEY_BLOB;
197
Shawn Willdend79791b2015-05-09 12:48:36 +0000198#if !defined(OPENSSL_IS_BORINGSSL)
199 case EVP_R_BAD_DECRYPT:
200 case EVP_R_ENCODE_ERROR:
201#endif
Shawn Willden26aaa762015-02-07 00:31:41 -0700202 case EVP_R_DIFFERENT_PARAMETERS:
203 case EVP_R_DECODE_ERROR:
Shawn Willden26aaa762015-02-07 00:31:41 -0700204 return KM_ERROR_INVALID_ARGUMENT;
205
206 case EVP_R_DIFFERENT_KEY_TYPES:
207 return KM_ERROR_INCOMPATIBLE_ALGORITHM;
208 }
209
210 return KM_ERROR_UNKNOWN_ERROR;
211}
212
213} // namespace keymaster