summaryrefslogtreecommitdiffhomepage
path: root/client/backend.h
blob: 1eece81b2bb538a59f001bdf0ca44698f9254af6 (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

/*

  Copyright (c) 2010-2011 Samuel Lidén Borell <samuel@kodafritt.se>
 
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, token to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

#ifndef BACKEND_H
#define BACKEND_H

#include <stdbool.h>
#include "../common/bidtypes.h"

typedef struct Token Token;

typedef enum {
    TokenChange_Added,
    TokenChange_Changed,
    TokenChange_Removed,
} TokenChange;

/**
 * A backend notifier object monitors all supported backends for tokens that
 * are used to identify a certain person.
 */
typedef struct BackendNotifier BackendNotifier;

typedef void (*BackendNotifyFunction)(Token *token, TokenChange change);

typedef enum {
    // The token needs...
    TokenStatus_NeedPassword = 1, // a password on the keyboard
    TokenStatus_NeedCard,         // an inserted smart card
    TokenStatus_NeedPIN,          // a pin code to be entered on a device
    TokenStatus_NeedConfirm,      // a confirm button to be pressed on a device
} TokenStatus;

typedef enum  {
    TokenError_Success =      0,
    TokenError_Unknown =      1,
    TokenError_NotImplemented,
    TokenError_MessageTooLong,
    TokenError_SignatureFailure,
    TokenError_HostnameMismatch,
    TokenError_NoCertsMatched,
    TokenError_FailedToStoreCerts,
    // File errors
    TokenError_FileNotReadable,
    TokenError_CantCreateFile,
    TokenError_CantWriteToFile,
    TokenError_BadFile,
    TokenError_BadPassword,
    // Smart card errors
    TokenError_BadPin,
    // Key generation errors
    TokenError_NoRandomState,
} TokenError;

/* Notification methods */
BackendNotifier *backend_createNotifier(const char *subjectFilter,
                                        KeyUsage keyUsage,
                                        BackendNotifyFunction notifyFunction);
void backend_freeNotifier(BackendNotifier *notifier);

void backend_scanTokens(BackendNotifier *notifier);

/* Function to manually add files */
TokenError backend_addFile(BackendNotifier *notifier,
                           const char *file, size_t length, void *tag);

/* Enrollment */
TokenError backend_createRequest(const RegutilInfo *info,
                                 const char *hostname,
                                 const char *password,
                                 char **request, size_t *reqlen);
char *backend_getSubjectDisplayName(const char *dn);
TokenError backend_storeCertificates(const char *p7data, size_t length,
                                     const char *hostname);

/* Token methods */
TokenStatus token_getStatus(const Token *token);
char *token_getDisplayName(const Token *token);
void *token_getTag(const Token *token);
// The password must not be free'd until the signature has been generated
void token_usePassword(Token *token, const char *password);
bool token_getBase64Chain(Token *token, char ***certs, size_t *count);
bool token_sign(Token *token, const char *message, size_t messagelen,
                char **signature, size_t *siglen);
bool token_remove(Token *token);
void token_free(Token *token);
TokenError token_getLastError(const Token *token);

#endif