libcli:util: Add gnutls_error
[bbaumbach/samba-autobuild/.git] / libcli / util / gnutls_error.c
1 /*
2  * Copyright (c) 2019      Andreas Schneider <asn@samba.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "includes.h"
19 #include "gnutls_error.h"
20
21 #include <gnutls/gnutls.h>
22
23 NTSTATUS _gnutls_error_to_ntstatus(int gnutls_rc,
24                                    NTSTATUS blocked_status,
25                                    const char *function,
26                                    const char *location)
27 {
28         NTSTATUS status;
29
30         if (gnutls_rc == GNUTLS_E_SUCCESS) {
31                 return NT_STATUS_OK;
32         }
33
34         switch (gnutls_rc) {
35         case GNUTLS_E_UNWANTED_ALGORITHM:
36                 status = blocked_status;
37                 break;
38         case GNUTLS_E_MEMORY_ERROR:
39                 status = NT_STATUS_NO_MEMORY;
40                 break;
41         case GNUTLS_E_INVALID_REQUEST:
42                 status = NT_STATUS_INVALID_VARIANT;
43                 break;
44         case GNUTLS_E_DECRYPTION_FAILED:
45                 status = NT_STATUS_DECRYPTION_FAILED;
46                 break;
47         case GNUTLS_E_ENCRYPTION_FAILED:
48                 status = NT_STATUS_ENCRYPTION_FAILED;
49                 break;
50         case GNUTLS_E_SHORT_MEMORY_BUFFER:
51                 status = NT_STATUS_INVALID_PARAMETER;
52                 break;
53         case GNUTLS_E_BASE64_DECODING_ERROR:
54         case GNUTLS_E_HASH_FAILED:
55         case GNUTLS_E_LIB_IN_ERROR_STATE:
56         case GNUTLS_E_INTERNAL_ERROR:
57         default:
58                 status = NT_STATUS_INTERNAL_ERROR;
59                 break;
60         }
61
62         D_WARNING("%s: GNUTLS ERROR: %s, NTSTATUS: %s at %s\n",
63                   function,
64                   gnutls_strerror_name(gnutls_rc),
65                   nt_errstr(status),
66                   location);
67
68         return status;
69 }