File: error.c

package info (click to toggle)
yubihsm-shell 2.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,972 kB
  • sloc: ansic: 41,773; sh: 1,679; cpp: 528; makefile: 18
file content (80 lines) | stat: -rw-r--r-- 3,078 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright 2015-2018 Yubico AB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <yubihsm.h>

#include <stddef.h>

#define ERR(name, desc)                                                        \
  { name, desc }

typedef struct {
  yh_rc rc;
  const char *description;
} err_t;

static const err_t errors[] = {
  ERR(YHR_SUCCESS, "Success"),
  ERR(YHR_MEMORY_ERROR, "Unable to allocate memory"),
  ERR(YHR_INIT_ERROR, "Unable to initialize libyubihsm"),
  ERR(YHR_CONNECTION_ERROR, "Connection error"),
  ERR(YHR_CONNECTOR_NOT_FOUND, "Unable to find a suitable connector"),
  ERR(YHR_INVALID_PARAMETERS, "Invalid argument to a function"),
  ERR(YHR_WRONG_LENGTH, "Mismatch between expected and received length"),
  ERR(YHR_BUFFER_TOO_SMALL, "Not enough space to store data"),
  ERR(YHR_CRYPTOGRAM_MISMATCH, "Unable to verify cryptogram"),
  ERR(YHR_SESSION_AUTHENTICATION_FAILED, "Unable to authenticate session"),
  ERR(YHR_MAC_MISMATCH, "Unable to verify MAC"),
  ERR(YHR_DEVICE_OK, "No error"),
  ERR(YHR_DEVICE_INVALID_COMMAND, "Unrecognized command"),
  ERR(YHR_DEVICE_INVALID_DATA, "Malformed command / invalid data"),
  ERR(YHR_DEVICE_INVALID_SESSION, "Invalid session"),
  ERR(YHR_DEVICE_AUTHENTICATION_FAILED,
      "Message encryption / verification failed"),
  ERR(YHR_DEVICE_SESSIONS_FULL, "All sessions are allocated"),
  ERR(YHR_DEVICE_SESSION_FAILED, "Session creation failed"),
  ERR(YHR_DEVICE_STORAGE_FAILED, "Storage failure"),
  ERR(YHR_DEVICE_WRONG_LENGTH, "Wrong length"),
  ERR(YHR_DEVICE_INSUFFICIENT_PERMISSIONS, "Wrong permissions for operation"),
  ERR(YHR_DEVICE_LOG_FULL, "Log buffer is full and forced audit is set"),
  ERR(YHR_DEVICE_OBJECT_NOT_FOUND, "Object not found"),
  ERR(YHR_DEVICE_INVALID_ID, "Invalid ID used"),
  ERR(YHR_DEVICE_INVALID_OTP, "Invalid OTP"),
  ERR(YHR_DEVICE_DEMO_MODE, "Demo mode, power cycle device"),
  ERR(YHR_DEVICE_COMMAND_UNEXECUTED,
      "The command execution has not terminated"),
  ERR(YHR_GENERIC_ERROR, "Generic error"),
  ERR(YHR_DEVICE_OBJECT_EXISTS, "An Object with that ID already exists"),
  ERR(YHR_CONNECTOR_ERROR, "Connector operation failed"),
  ERR(YHR_DEVICE_SSH_CA_CONSTRAINT_VIOLATION, "SSH CA constraint violation"),
  ERR(YHR_DEVICE_ALGORITHM_DISABLED, "Algorithm disabled"),
};

const char *yh_strerror(yh_rc err) {
  static const char *unknown = "Unknown yubihsm error";
  const char *p;

  if (-err < 0 || -err >= (int) (sizeof(errors) / sizeof(errors[0]))) {
    return unknown;
  }

  p = errors[-err].description;
  if (!p) {
    p = unknown;
  }

  return p;
}