-
Notifications
You must be signed in to change notification settings - Fork 72
/
class_Crypt (2).ahk
161 lines (147 loc) · 4.44 KB
/
class_Crypt (2).ahk
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
class Crypt
{
; Provider Types
static PROV_RSA_FULL := 1
, PROV_RSA_SIG := 2
, PROV_DSS := 3
, PROV_FORTEZZA := 4
, PROV_MS_EXCHANGE := 5
, PROV_SSL := 6
, PROV_STT_MER := 7 ; <= XP
, PROV_STT_ACQ := 8 ; <= XP
, PROV_STT_BRND := 9 ; <= XP
, PROV_STT_ROOT := 10 ; <= XP
, PROV_STT_ISS := 11 ; <= XP
, PROV_RSA_SCHANNEL := 12
, PROV_DSS_DH := 13
, PROV_EC_ECDSA_SIG := 14
, PROV_EC_ECNRA_SIG := 15
, PROV_EC_ECDSA_FULL := 16
, PROV_EC_ECNRA_FULL := 17
, PROV_DH_SCHANNEL := 18
, PROV_SPYRUS_LYNKS := 20
, PROV_RNG := 21
, PROV_INTEL_SEC := 22
, PROV_REPLACE_OWF := 23 ; >= XP
, PROV_RSA_AES := 24 ; >= XP
; CryptAcquireContext - dwFlags
; http://msdn.microsoft.com/en-us/library/aa379886
static VERIFYCONTEXT := 0xF0000000
, NEWKEYSET := 0x00000008
, DELETEKEYSET := 0x00000010
, MACHINE_KEYSET := 0x00000020
, SILENT := 0x00000040
, CRYPT_DEFAULT_CONTAINER_OPTIONAL := 0x00000080
; CryptGenKey - dwFlag
; http://msdn.microsoft.com/en-us/library/aa379941
static EXPORTABLE := 0x00000001
, USER_PROTECTED := 0x00000002
, CREATE_SALT := 0x00000004
, UPDATE_KEY := 0x00000008
, NO_SALT := 0x00000010
, PREGEN := 0x00000040
, ARCHIVABLE := 0x00004000
, FORCE_KEY_PROTECTION_HIGH := 0x00008000
; Key Types
static AT_KEYEXCHANGE := 1
, AT_SIGNATURE := 2
;
; METHODS
;
AcquireContext(Container, Provider, dwProvType, dwFlags)
{
if DllCall("Advapi32\CryptAcquireContext"
, "ptr*", hProv
, "ptr", Container ? &Container : 0
, "ptr", Provider ? &Provider : 0
, "uint", dwProvType
, "uint", dwFlags)
{
if (dwFlags & this.DELETEKEYSET)
; Success, but hProv is invalid in this case.
return 1
; Wrap it up so it'll be released at some point.
return new this.Context(hProv)
}
return 0
}
IsSigned(FilePath)
{
return DllCall("Crypt32\CryptQueryObject"
, "uint", CERT_QUERY_OBJECT_FILE := 1
, "wstr", FilePath
, "uint", CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED := 1<<10
, "uint", CERT_QUERY_FORMAT_FLAG_BINARY := 2
, "uint", 0
, "uint*", dwEncoding
, "uint*", dwContentType
, "uint*", dwFormatType
, "ptr", 0
, "ptr", 0
, "ptr", 0)
}
;
; Error Detection
;
__Get(name)
{
ListLines
MsgBox 16,, Attempt to access invalid property Crypt.%name%.
Pause
}
;
; CLASSES
;
class _Handle
{
__New(handle)
{
this.h := handle
}
__Delete()
{
this.Dispose()
}
}
class Context extends Crypt._Handle
{
GenerateKey(KeyType, KeyBitLength, dwFlags)
{
if DllCall("Advapi32\CryptGenKey"
, "ptr", this.h
, "uint", KeyType
, "uint", (KeyBitLength << 16) | dwFlags
, "ptr*", hKey)
{
global Crypt
return new Crypt.Key(hKey)
}
return 0
}
CreateSelfSignCertificate(NameObject, StartTime, EndTime)
{
ctx := DllCall("Crypt32\CertCreateSelfSignCertificate"
, "ptr", this.h
, "ptr", IsObject(NameObject) ? NameObject.p : NameObject
, "uint", 0, "ptr", 0, "ptr", 0
, "ptr", IsObject(StartTime) ? StartTime.p : StartTime
, "ptr", IsObject(EndTime) ? EndTime.p : EndTime
, "ptr", 0, "ptr")
global Cert
return ctx ? new Cert.Context(ctx) : 0
}
Dispose()
{
if this.h && DllCall("Advapi32\CryptReleaseContext", "ptr", this.h, "uint", 0)
this.h := 0
}
}
class Key extends Crypt._Handle
{
Dispose()
{
if this.h && DllCall("Advapi32\CryptDestroyKey", "ptr", this.h)
this.h := 0
}
}
}