-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdsign-check-code.pb
238 lines (214 loc) · 10.7 KB
/
cdsign-check-code.pb
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
; Contract or any document e-sign check
; Copyright (C) 2021 Alexander Vankov
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
; You should have received a copy of the GNU General Public License
; along with this program. If not, see <https://www.gnu.org/licenses/>.
; Some global variables: name of the GPG executable, temporary and settings directory
CompilerIf #PB_Compiler_OS = #PB_OS_Linux
Global prog$ = "gpg"
CompilerElse
Global prog$ = "gpgbin"+#PS$+"gpg"
CompilerEndIf
Global prefdir$ = ".edm"
Global tempdir$ = GetTemporaryDirectory() + "cdsign"
IncludeFile "cdsign-check-form.pbf"
IncludeFile "cdsign-check-procedures.pb"
LoadSettings()
OpenMain_Window()
DisableGadget(View_CD, #True) ; disable show signed document button
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
; Select document file
Case File_CD
If EventType() = #PB_EventType_LeftClick
filetk$ = OpenFileRequester("Select a document file", DefaultFile$, "All files|*.*|Documents (Word, PDF, txt)|*.doc;*.docx;*.odt;*.pdf;*.txt", 0)
SetGadgetText(String_Contract, filetk$)
If filetk$ = ""
DisableGadget(View_CD, #True) ; disable show signed document button
EndIf
EndIf
; Select DS file
Case File_DS
If EventType() = #PB_EventType_LeftClick
fileds$ = OpenFileRequester("Select a digital signature file", DefaultFile$, "All files|*.*|DS files (asc, sig)|*.asc;*.sig", 0)
SetGadgetText(String_Signature, fileds$)
EndIf
; Select key file
Case Key_Import
If EventType() = #PB_EventType_LeftClick
filepubkey$ = OpenFileRequester("Select a key file", DefaultFile$, "All files|*.*|Key files (key)|*.key", 0)
SetGadgetText(String_Key, filepubkey$)
EndIf
; Check DS
Case Check_DS
If EventType() = #PB_EventType_LeftClick
Output$ = ""
GPGKey$ = ""
curdir$ = GetCurrentDirectory()
; Key import code
If filepubkey$
If ReadFile(0, filepubkey$)
While Eof(0) = 0
GPGKey$ = GPGKey$ + ReadString(0) + #LF$
Wend
CloseFile(0)
EndIf
; Run GPG
params$ = "--charset utf8 --import --batch"
exegpg = RunProgram(prog$, params$, curdir$, #PB_Program_Hide | #PB_Program_Open | #PB_Program_Read | #PB_Program_Write | #PB_Program_Error | #PB_Program_UTF8)
If exegpg
WriteProgramString(exegpg, GPGKey$, #PB_UTF8)
WriteProgramData(exegpg, #PB_Program_Eof, 1024)
While ProgramRunning(exegpg)
If AvailableProgramOutput(exegpg)
Output$ = Output$ + ReadProgramString(exegpg, #PB_UTF8) + #CRLF$
EndIf
Wend
; GPG outputs to stderr, read from stderr
t$ = ReadProgramError(exegpg, #PB_UTF8)
While t$
Output$ + t$ + #CRLF$
t$ = ReadProgramError(exegpg, #PB_UTF8)
Wend
CloseProgram(exegpg)
SetGadgetText(Result_Text, Output$)
EndIf
EndIf
; End of key import code
; Check DS code
; Create temp subdirectory in temp directory
; Temp subdirectory required because of encoding problems in some OSes (e.g. Windows)
If FileSize(tempdir$) = -2 : DeleteDirectory(tempdir$, "", #PB_FileSystem_Recursive | #PB_FileSystem_Force) : EndIf
If CreateDirectory(tempdir$)
tempfiletk$ = tempdir$ + #PS$ + "esign-check-tk"
tempfileds$ = tempdir$ + #PS$ + "esign-check-ds"
If CopyFile(filetk$, tempfiletk$) And CopyFile(fileds$, tempfileds$)
Output$ = Output$ + "Ready to DS check" + #CRLF$
Else
tempfiletk$ = filetk$
tempfileds$ = fileds$
Output$ = Output$ + "Not all required files selected or ready" + #CRLF$
EndIf
Else
Output$ = Output$ + "Error writing to temporary directory" + #CRLF$
EndIf
; Run GPG
params$ = "--charset utf8 --verify " + #DQUOTE$ + tempfileds$ + #DQUOTE$ + " " + #DQUOTE$ + tempfiletk$ + #DQUOTE$
exegpg = RunProgram(prog$, params$, curdir$, #PB_Program_Hide | #PB_Program_Open | #PB_Program_Read | #PB_Program_Write | #PB_Program_Error | #PB_Program_UTF8)
If exegpg
While ProgramRunning(exegpg)
If AvailableProgramOutput(exegpg)
Output$ = Output$ + ReadProgramString(exegpg, #PB_UTF8) + #CRLF$
EndIf
Wend
; GPG outputs to stderr, read from stderr
t$ = ReadProgramError(exegpg, #PB_UTF8)
While t$
Output$ + t$ + #CRLF$
t$ = ReadProgramError(exegpg, #PB_UTF8)
Wend
CloseProgram(exegpg)
SetGadgetText(Result_Text, Output$)
goodocur = CountString(Output$, "Good signature") + CountString(Output$, "Действительная подпись") + CountString(Output$, "Хорошая подпись")
badocur = CountString(Output$, "BAD signature") + CountString(Output$, "ПЛОХАЯ подпись")
nokey = CountString(Output$, "No public key") + CountString(Output$, "Нет открытого ключа")
; nosign = CountString(Output$, "the signature could not be verified") + CountString(Output$, "Не могу проверить подпись")
If goodocur > 0
DisableGadget(View_CD, #False) ; enable show signed document button
Else
DisableGadget(View_CD, #True) ; disable show signed document button
EndIf
; Find key creation date and expiration term in key packets data
If goodocur > 0 Or badocur > 0
keyid$ = GetSigID(Output$)
params$ = "--charset utf8 -a --export --batch " + keyid$
exegpg = RunProgram(prog$, params$, curdir$, #PB_Program_Hide | #PB_Program_Open | #PB_Program_Read | #PB_Program_Write | #PB_Program_Error | #PB_Program_UTF8)
If exegpg
While ProgramRunning(exegpg)
If AvailableProgramOutput(exegpg)
keytolistpackets$ = keytolistpackets$ + ReadProgramString(exegpg, #PB_UTF8) + #LF$
EndIf
Wend
EndIf
params$ = "--charset utf8 --list-packets --batch"
exegpg = RunProgram(prog$, params$, curdir$, #PB_Program_Hide | #PB_Program_Open | #PB_Program_Read | #PB_Program_Write | #PB_Program_Error | #PB_Program_UTF8)
If exegpg
WriteProgramString(exegpg, keytolistpackets$, #PB_UTF8)
WriteProgramData(exegpg, #PB_Program_Eof, 1024)
While ProgramRunning(exegpg)
If AvailableProgramOutput(exegpg)
keypackets$ = keypackets$ + ReadProgramString(exegpg, #PB_UTF8) + #CRLF$
EndIf
Wend
t$ = ReadProgramError(exegpg, #PB_UTF8)
While t$
Output$ + t$ + #CRLF$
t$ = ReadProgramError(exegpg, #PB_UTF8)
Wend
EndIf
keydates$ = GetKeyDates(keypackets$)
Output$ = Output$ + keydates$ + #CRLF$
SetGadgetText(Result_Text, Output$)
Output$ = Output$ + "Run finished"
EndIf
; Change result fields
If goodocur > 0
SetGadgetText(Result_Bool, "SIGNATURE VALID")
SetGadgetColor(Result_Bool, #PB_Gadget_FrontColor, RGB(0, 0, 0))
SetGadgetColor(Result_Bool, #PB_Gadget_BackColor, RGB(0, 255, 0))
ElseIf badocur > 0
SetGadgetText(Result_Bool, "SIGNATURE INVALID")
SetGadgetColor(Result_Bool, #PB_Gadget_FrontColor, RGB(0, 0, 0))
SetGadgetColor(Result_Bool, #PB_Gadget_BackColor, RGB(255, 0, 0))
ElseIf nokey > 0
SetGadgetText(Result_Bool, "NO PUBLIC KEY")
SetGadgetColor(Result_Bool, #PB_Gadget_FrontColor, RGB(0, 0, 0))
SetGadgetColor(Result_Bool, #PB_Gadget_BackColor, RGB(255, 0, 0))
Else
SetGadgetText(Result_Bool, "CANNOT VERIFY SIGNATURE")
SetGadgetColor(Result_Bool, #PB_Gadget_FrontColor, RGB(0, 0, 0))
SetGadgetColor(Result_Bool, #PB_Gadget_BackColor, RGB(255, 0, 0))
EndIf
EndIf
; Delete temporary files and subdirectory
If DeleteDirectory(tempdir$, "", #PB_FileSystem_Recursive | #PB_FileSystem_Force)
Output$ = Output$ + #CRLF$ + "Temp directory removed"
Else
Output$ = Output$ + #CRLF$ + "Error writing to temporary directory" + #CRLF$
SetGadgetText(Result_Text, Output$)
EndIf
; Check DS code end
EndIf
; Additional buttons
; Open a file to sign
Case View_CD
If filetk$ And EventType() = #PB_EventType_LeftClick
ShellOpen(filetk$)
EndIf
; Open help
Case Button_Help
If EventType() = #PB_EventType_LeftClick
ShellOpen("docs"+#PS$+"help-check.html")
EndIf
; Open README
Case Button_About
If EventType() = #PB_EventType_LeftClick
ShellOpen("docs"+#PS$+"README-check.txt")
EndIf
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
; IDE Options = PureBasic 5.70 LTS (Windows - x64)
; CursorPosition = 61
; Folding = -
; EnableXP