-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmyproxy.c
107 lines (90 loc) · 2.85 KB
/
myproxy.c
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
#include "gpkcs11_locl.h"
CK_RV
get_myproxy_creds(char *server, char *username, char *password, char **creds)
{
myproxy_socket_attrs_t *socket_attrs = NULL;
myproxy_request_t *client_request = NULL;
myproxy_response_t *server_response = NULL;
char *request_buffer = NULL;
char creds_file[MAXPATHLEN];
int ret, requestlen;
verror_clear();
socket_attrs = malloc(sizeof(*socket_attrs));
if (socket_attrs == NULL) {
ret = CKR_DEVICE_MEMORY;
goto end;
}
memset(socket_attrs, 0, sizeof(*socket_attrs));
client_request = malloc(sizeof(*client_request));
if (client_request == NULL) {
ret = CKR_DEVICE_MEMORY;
goto end;
}
memset(client_request, 0, sizeof(*client_request));
server_response = malloc(sizeof(*server_response));
if (server_response == NULL) {
ret = CKR_DEVICE_MEMORY;
goto end;
}
memset(server_response, 0, sizeof(*server_response));
socket_attrs->psport = MYPROXY_SERVER_PORT;
socket_attrs->pshost = strdup(server);
if (socket_attrs->pshost == NULL) {
ret = CKR_DEVICE_MEMORY;
goto end;
}
ret = myproxy_init_client(socket_attrs);
if (ret < 0) {
gpkcs11_log("Error contacting MyProxy server %s: %s\n", socket_attrs->pshost, verror_get_string());
ret = CKR_GENERAL_ERROR;
goto end;
}
GSI_SOCKET_allow_anonymous(socket_attrs->gsi_socket, 1);
ret = myproxy_authenticate_init(socket_attrs, NULL);
if (ret < 0) {
gpkcs11_log("Error authenticating MyProxy server %s: %s\n", socket_attrs->pshost, verror_get_string());
ret = CKR_GENERAL_ERROR;
goto end;
}
client_request->version = strdup(MYPROXY_VERSION);
client_request->command_type = MYPROXY_RETRIEVE_CERT;
strncpy(client_request->passphrase, password, sizeof(client_request->passphrase));
client_request->username = strdup(username);
requestlen = myproxy_serialize_request_ex(client_request, &request_buffer);
if (requestlen < 0) {
gpkcs11_log("Error preparing MyProxy request: %s\n", verror_get_string());
ret = CKR_GENERAL_ERROR;
goto end;
}
ret = myproxy_send(socket_attrs, request_buffer, requestlen);
free(request_buffer);
if (ret < 0) {
gpkcs11_log("Error sending MyProxy request: %s\n", verror_get_string());
ret = CKR_GENERAL_ERROR;
goto end;
}
ret = myproxy_recv_response_ex(socket_attrs, server_response, client_request);
if (ret != 0) {
gpkcs11_log("Error receiving MyProxy response: %s\n", verror_get_string());
ret = CKR_GENERAL_ERROR;
goto end;
}
ret = myproxy_accept_credentials(socket_attrs, creds_file, sizeof(creds_file));
if (ret < 0) {
gpkcs11_log("Error receiving credentials: %s\n", verror_get_string());
ret = CKR_GENERAL_ERROR;
goto end;
}
*creds = strdup(creds_file);
if (*creds == NULL) {
ret = CKR_DEVICE_MEMORY;
goto end;
}
ret = 0;
end:
if (socket_attrs && socket_attrs->socket_fd)
close(socket_attrs->socket_fd);
myproxy_free(socket_attrs, client_request, server_response);
verror_clear();
return ret;
}