This repository has been archived by the owner on Jun 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CommandExtensions.cs
201 lines (173 loc) · 8.38 KB
/
CommandExtensions.cs
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.Collections.Generic;
using System.Linq;
using ViciNet.Protocol;
using ViciNet.RequestAttribute;
namespace ViciNet
{
public static class CommandExtensions
{
// version
// stats
// reload-settings
/// <param name = "type">child | ike</param>
/// <param name = "name">configuration name</param>
public static void Initiate(this Session session, string type, string name)
{
VoidCommandRequest(session, Command.Initiate, new KeyValueMessage(type, name));
}
/// <param name = "type">child | ike</param>
/// <param name = "name">configuration name</param>
public static Tuple<int, int> Terminate(this Session session, string type, string name)
{
return TypedCommandRequest(session, Command.Terminate, new[] { new KeyValueMessage(type, name) }, messages =>
{
var matches = (messages[1] as KeyValueMessage).Value;
var terminated = (messages[2] as KeyValueMessage).Value;
return new Tuple<int, int>(int.Parse(matches), int.Parse(terminated));
});
}
// rekey
// redirect
// install
// uninstall
public static IEnumerable<SectionMessage> ListSas(this Session session, string ike)
{
var options = new List<KeyValueMessage>();
if (ike != null) options.Add(new KeyValueMessage("ike", ike));
return SectionMessages(session, Command.ListSas, options);
}
public static IEnumerable<SectionMessage> ListPolicies(this Session session, bool drop, bool pass, bool trap, string child, string ike)
{
var options = new List<KeyValueMessage>();
if (drop) options.Add(new KeyValueMessage("drop", "yes"));
if (pass) options.Add(new KeyValueMessage("pass", "yes"));
if (trap) options.Add(new KeyValueMessage("trap", "yes"));
if (child != null) options.Add(new KeyValueMessage("child", ike));
if (ike != null) options.Add(new KeyValueMessage("ike", ike));
return SectionMessages(session, Command.ListSas, options);
}
public static IEnumerable<SectionMessage> ListConns(this Session session, string ike)
{
var options = new List<Message> { new KeyValueMessage("ike", ike) };
return SectionMessages(session, Command.ListSas, options);
}
public static IEnumerable<string> GetConns(this Session session)
{
return GetCommandRequest<IEnumerable<string>>(session, Command.GetConns, messages => (messages[0] as KeyArrayMessage).Values);
}
// type: X509 | X509_AC | X509_CRL | OCSP_RESPONSE | PUBKEY | ANY
// flag: NONE | CA | AA | OCSP | ANY
// subject: set to list only certificates having subject
public static IEnumerable<IEnumerable<KeyValueMessage>> ListCerts(this Session session, string type, string flag, string subject = "")
{
var typeMessage = new KeyValueMessage("type", type);
var flagMessage = new KeyValueMessage("flag", flag);
var subjectMessage = new KeyValueMessage("subject", subject);
return session.StreamedRequest(Command.ListCerts, typeMessage, flagMessage, subjectMessage)
.Select(messages => messages.Select(msg => msg as KeyValueMessage));
}
public static IEnumerable<SectionMessage> ListPolicies(this Session session, string name)
{
var options = new List<Message> { new KeyValueMessage("name", name) };
return SectionMessages(session, Command.ListAuthorities, options);
}
public static void LoadConn(this Session session, Message config)
{
VoidCommandRequest(session, Command.LoadConn, config);
}
public static void UnloadConn(this Session session, string name)
{
VoidCommandRequest(session, Command.UnloadConn, new KeyValueMessage("name", name));
}
/// <param name = "type">X509 | X509_AC | X509_CRL</param>
/// <param name = "flag">NONE | CA | AA | OCSP</param>
/// <param name = "data">PEM or DER encoded key data</param>
public static void LoadCert(this Session session, string type, string flag, string data)
{
VoidCommandRequest(session, Command.LoadCert, new KeyValueMessage("type", type), new KeyValueMessage("flag", flag), new KeyValueMessage("data", data));
}
/// <param name = "type">rsa | ecdsa | bliss | any</param>
/// <param name = "data">PEM or DER encoded key data</param>
public static string LoadKey(this Session session, string type, string data)
{
var typeMessage = new KeyValueMessage("type", type);
var dataMessage = new KeyValueMessage("data", data);
return TypedCommandRequest(session, Command.LoadKey, new[] { typeMessage, dataMessage }, messages => (messages[1] as KeyValueMessage).Value);
}
public static void UnloadKey(this Session session, string id)
{
VoidCommandRequest(session, Command.UnloadKey, new KeyValueMessage("id", id));
}
public static string[] GetKeys(this Session session)
{
return GetCommandRequest(session, Command.GetKeys, messages => (messages[0] as KeyArrayMessage).Values);
}
// load-token
// load-shared
// unload-shared
// get-shared
/// <param name = "type">X509 | X509_AC | X509_CRL | OCSP_RESPONSE | PUBKEY | ANY</param>
public static void FlushCerts(this Session session, string type)
{
VoidCommandRequest(session, Command.FlushCerts, new KeyValueMessage("type", type));
}
// clear-creds
// load-authority
// unload-authority
// load-pool
// unload-pool
// get-pools
// get-algorithms
// get-counters
// reset-counters
private static IEnumerable<SectionMessage> SectionMessages(Session session, Command command, IEnumerable<Message> options)
{
var list = new List<SectionMessage>();
foreach (var messages in session.StreamedRequest(command, options.ToArray()))
{
list.AddRange(messages.Select(msg => msg as SectionMessage));
}
return list;
}
private static void VoidCommandRequest(Session session, Command command, params Message[] messages)
{
var resultMessages = session.Request(command, messages);
switch ((resultMessages[0] as KeyValueMessage).Value)
{
case "yes":
return;
case "no":
throw new System.IO.InvalidDataException((resultMessages[1] as KeyValueMessage).Value);
default:
throw new System.IO.InvalidDataException((resultMessages[1] as KeyValueMessage).Value);
}
}
private static T GetCommandRequest<T>(Session session, Command command, IEnumerable<Message> messages, Func<Message[], T> resultHandler)
{
var resultMessages = session.Request(command, messages.ToArray());
return resultHandler(resultMessages);
}
private static T GetCommandRequest<T>(Session session, Command command, Func<Message[], T> resultHandler)
{
return GetCommandRequest(session, command, new Message[] { }, resultHandler);
}
private static T TypedCommandRequest<T>(Session session, Command command, IEnumerable<Message> messages, Func<Message[], T> resultHandler)
{
var resultMessages = session.Request(command, messages.ToArray());
switch ((resultMessages[0] as KeyValueMessage).Value)
{
case "yes":
return resultHandler(resultMessages);
case "no":
throw new System.IO.InvalidDataException((resultMessages[1] as KeyValueMessage).Value);
default:
throw new System.IO.InvalidDataException((resultMessages[1] as KeyValueMessage).Value);
}
}
private static T TypedCommandRequest<T>(Session session, Command command, Func<Message[], T> resultHandler)
{
return TypedCommandRequest(session, command, new List<Message>(), resultHandler);
}
}
}