error_codes.h revision ee93b5e81e62fdc0053c74ad565957354e540a99
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#ifndef TRUNKS_ERROR_CODES_H_
18#define TRUNKS_ERROR_CODES_H_
19
20#include <string>
21
22#include "trunks/tpm_generated.h"  // For TPM_RC.
23#include "trunks/trunks_export.h"
24
25namespace trunks {
26
27// Use the TPM_RC type but with different layer bits (12 - 15). Choose the layer
28// value arbitrarily. Currently TSS2 uses 9 for TCTI and 8 for SAPI.
29// TCTI and SAPI error codes taken from
30// http://www.trustedcomputinggroup.org/resources/
31// tss_system_level_api_and_tpm_command_transmission_interface_specification
32const TPM_RC kTrunksErrorBase = (7 << 12);
33const TPM_RC kTctiErrorBase = (8 << 12);
34const TPM_RC kSapiErrorBase = (9 << 12);
35const TPM_RC kResourceManagerTpmErrorBase = (11 << 12);
36const TPM_RC kResourceManagerErrorBase = (12 << 12);
37
38const TPM_RC TRUNKS_RC_AUTHORIZATION_FAILED = kTrunksErrorBase + 1;
39const TPM_RC TRUNKS_RC_ENCRYPTION_FAILED = kTrunksErrorBase + 2;
40const TPM_RC TRUNKS_RC_READ_ERROR = kTrunksErrorBase + 3;
41const TPM_RC TRUNKS_RC_WRITE_ERROR = kTrunksErrorBase + 4;
42const TPM_RC TRUNKS_RC_IPC_ERROR = kTrunksErrorBase + 5;
43const TPM_RC TRUNKS_RC_SESSION_SETUP_ERROR = kTrunksErrorBase + 6;
44
45const TPM_RC TCTI_RC_TRY_AGAIN = kTctiErrorBase + 1;
46const TPM_RC TCTI_RC_GENERAL_FAILURE = kTctiErrorBase + 2;
47const TPM_RC TCTI_RC_BAD_CONTEXT = kTctiErrorBase + 3;
48const TPM_RC TCTI_RC_WRONG_ABI_VERSION = kTctiErrorBase + 4;
49const TPM_RC TCTI_RC_NOT_IMPLEMENTED = kTctiErrorBase + 5;
50const TPM_RC TCTI_RC_BAD_PARAMETER = kTctiErrorBase + 6;
51const TPM_RC TCTI_RC_INSUFFICIENT_BUFFER = kTctiErrorBase + 7;
52const TPM_RC TCTI_RC_NO_CONNECTION = kTctiErrorBase + 8;
53const TPM_RC TCTI_RC_DRIVER_NOT_FOUND = kTctiErrorBase + 9;
54const TPM_RC TCTI_RC_DRIVERINFO_NOT_FOUND = kTctiErrorBase + 10;
55const TPM_RC TCTI_RC_NO_RESPONSE = kTctiErrorBase + 11;
56const TPM_RC TCTI_RC_BAD_VALUE = kTctiErrorBase + 12;
57
58const TPM_RC SAPI_RC_INVALID_SESSIONS = kSapiErrorBase + 1;
59const TPM_RC SAPI_RC_ABI_MISMATCH = kSapiErrorBase + 2;
60const TPM_RC SAPI_RC_INSUFFICIENT_BUFFER = kSapiErrorBase + 3;
61const TPM_RC SAPI_RC_BAD_PARAMETER = kSapiErrorBase + 4;
62const TPM_RC SAPI_RC_BAD_SEQUENCE = kSapiErrorBase + 5;
63const TPM_RC SAPI_RC_NO_DECRYPT_PARAM = kSapiErrorBase + 6;
64const TPM_RC SAPI_RC_NO_ENCRYPT_PARAM = kSapiErrorBase + 7;
65const TPM_RC SAPI_RC_NO_RESPONSE_RECEIVED = kSapiErrorBase + 8;
66const TPM_RC SAPI_RC_BAD_SIZE = kSapiErrorBase + 9;
67const TPM_RC SAPI_RC_CORRUPTED_DATA = kSapiErrorBase + 10;
68const TPM_RC SAPI_RC_INSUFFICIENT_CONTEXT = kSapiErrorBase + 11;
69const TPM_RC SAPI_RC_INSUFFICIENT_RESPONSE = kSapiErrorBase + 12;
70const TPM_RC SAPI_RC_INCOMPATIBLE_TCTI = kSapiErrorBase + 13;
71const TPM_RC SAPI_RC_MALFORMED_RESPONSE = kSapiErrorBase + 14;
72const TPM_RC SAPI_RC_BAD_TCTI_STRUCTURE = kSapiErrorBase + 15;
73
74// Returns a description of |error|.
75TRUNKS_EXPORT std::string GetErrorString(TPM_RC error);
76
77// Strips the P and N bits from a 'format one' error. If the given error code
78// is not a format one error, it is returned as is. The error that is returned
79// can be compared to TPM_RC_* constant values. See TPM 2.0 Part 2 Section 6.6
80// for details on format one errors.
81TRUNKS_EXPORT TPM_RC GetFormatOneError(TPM_RC error);
82
83// Creates a well-formed response with the given |error_code|.
84TRUNKS_EXPORT std::string CreateErrorResponse(TPM_RC error_code);
85
86}  // namespace trunks
87
88#endif  // TRUNKS_ERROR_CODES_H_
89