-
Notifications
You must be signed in to change notification settings - Fork 0
/
Crypto.fsx
197 lines (193 loc) · 6.8 KB
/
Crypto.fsx
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
open System
open System.Diagnostics
open System.Security.Cryptography
let [<Literal>] sshkeygen = @"C:\Program Files\Git\usr\bin\ssh-keygen.exe"
let [<Literal>] openssl = @"C:\Program Files\Git\mingw64\bin\openssl.exe"
let procinfo app args=
new ProcessStartInfo(
app,
args|>String.concat " ",
UseShellExecute=false,
RedirectStandardOutput=true,
RedirectStandardInput=true,
RedirectStandardError=true,
CreateNoWindow =true,
WorkingDirectory= __SOURCE_DIRECTORY__
)
let generateKeys (name:string) (pass:string) (c:string)=
async {
if IO.File.Exists(name) && IO.File.Exists(name+".pub") then
return 1,"you have files"
else
//IO.File.Delete(name)
//IO.File.Delete(name+".pub")
let pi=
procinfo sshkeygen [
"-f"
name
"-t"
//"ed25519"
"rsa"
"-b"
"4096"
"-C"
c
"-N"
pass
]
let p=new Process(StartInfo=pi)
let s=
if p.Start() then
let s = p.StandardOutput.ReadToEnd()+p.StandardError.ReadToEnd()
p.WaitForExit()
s
else
p.StandardError.ReadToEnd()
return (p.ExitCode,s)
}
let genSessionKeys name c = generateKeys name String.Empty c
let keygenerator issuer=generateKeys (issuer+"/key") "1234567890" "[email protected]"|>Async.RunSynchronously
let filekeygenerator issuer file=generateKeys (issuer+"/"+file+".key") "1234567890" "[email protected]"|>Async.RunSynchronously
//openssl rsautl -encrypt -oaep -pubin -inkey <(ssh-keygen -e -f recipients-key.pub -m PKCS8) -in secret.key -out secret.key.enc
let generatePK atype (pubfile:string)=
async {
let pi=
procinfo sshkeygen [
"-e"
"-f"
pubfile
"-m"
atype
]
let p=new Process(StartInfo=pi)
let s=
if p.Start() then
let s = p.StandardOutput.ReadToEnd()//+p.StandardError.ReadToEnd()
p.WaitForExit()
s
else
p.StandardError.ReadToEnd()
return (p.ExitCode,s)
}
let generatePKCS8 = generatePK "PKCS8"
let generatePEM = generatePK "PEM"
let encrypt name pubfile=
async {
let! rc,key=generatePKCS8 pubfile
if rc<>0 then
return 1,"cant get PKCS8"
else
System.IO.File.WriteAllText(pubfile+".pkcs8",key)
let pi=
procinfo openssl [
"rsautl"
"-encrypt"
"-oaep"
"-pubin"
"-inkey"
pubfile+".pkcs8"
"-in"
name
"-out"
name+".enc"
]
let p=new Process(StartInfo=pi)
let s=
if p.Start() then
let s = p.StandardOutput.ReadToEnd()+p.StandardError.ReadToEnd()
p.WaitForExit()
s
else
p.StandardError.ReadToEnd()
return (p.ExitCode,s)
}
let decrypt name prifile (pripass:string)=
//openssl rsautl -decrypt -oaep -inkey ~/.ssh/id_rsa -in secret.key.enc -out secret.key
async {
let pi=
procinfo openssl
[
"rsautl"
"-decrypt"
"-oaep"
"-inkey"
prifile
"-in"
name
"-out"
name+".dec"
"-passin"
"pass:"+pripass
]
let p=new Process(StartInfo=pi)
let s=
if p.Start() then
let s = p.StandardOutput.ReadToEnd()+p.StandardError.ReadToEnd()
p.WaitForExit()
s
else
p.StandardError.ReadToEnd()
return (p.ExitCode,s)
}
let sign prifile pripass name =
//openssl pkeyutl -sign -inkey ~/.ssh/id_rsa -in some-file
async {
let pi=
procinfo openssl [
"pkeyutl"
"-sign"
"-inkey"
prifile
"-in"
name
"-out"
name+".sig"
"-passin"
"pass:"+pripass
]
let p=new Process(StartInfo=pi)
let stdout,stderr=
if p.Start() then
let s = (p.StandardOutput.ReadToEnd()|>System.Text.Encoding.ASCII.GetBytes,p.StandardError.ReadToEnd())
p.WaitForExit()
s
else
Array.empty,p.StandardError.ReadToEnd()
return (p.ExitCode,stdout,stderr)
}
let verify pubfile sigfile name=
//openssl pkeyutl -verify -inkey $PUBLIC_KEY_FILE -sigfile signature-file -in some-file
async {
let! rc,key=generatePKCS8 pubfile
if rc<>0 then
return 1,String.Empty,"cant get pkcs8"
else
System.IO.File.WriteAllText(pubfile+".pkcs8",key)
let pi=
procinfo openssl [
"pkeyutl"
"-verify"
"-pubin"
"-inkey"
pubfile+".pkcs8"
"-sigfile"
sigfile
"-in"
name
]
let p=new Process(StartInfo=pi)
let stdout,stderr=
if p.Start() then
let s = (p.StandardOutput.ReadToEnd(),p.StandardError.ReadToEnd())
p.WaitForExit()
s
else
String.Empty,p.StandardError.ReadToEnd()
return (p.ExitCode,stdout,stderr)
}
let verificationResult = function |(0, _, _)->true|_->false
let testSign() = sign "Alice/key" "1234567890" "Alice/MySecret.doc"|>Async.RunSynchronously
let testVerify()=verify "Alice/key.pub" "Alice/MySecret.doc.sig" "Alice/MySecret.doc"|>Async.RunSynchronously
let testVerifyFalse()=verify "Alice/MySecret.doc.key.pub" "Alice/MySecret.doc.sig" "Alice/MySecret.doc"|>Async.RunSynchronously
//testVerify()|>verificationResult
//testVerifyFalse()|>verificationResult