-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle
401 lines (311 loc) · 9.72 KB
/
build.gradle
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import org.apache.tools.ant.DefaultLogger
import java.nio.file.Paths
// udercouch.download used to get FDSM and JavaAnt direct from Github
plugins {
id 'java'
id "de.undercouch.download" version "3.4.3"
id "idea"
}
group 'net.echogy.templates'
version '1.0'
// load user-configurable settings
apply from: 'config.gradle'
// JavaCard on Fidesmo doesn't support anything after Java 8
sourceCompatibility = 1.8
// JavaCard version and location set in config.gradle
String jcVersion = project.properties.build.jcVersion
String jcDir = "${locations.javacard[jcVersion]}"
// Controls the mapping between Ant logging (for building CAP files) and Gradle logging.
// Set to VERBOSE or DEBUG for more logs, set to WARN for fewer
ant.lifecycleLogLevel = "INFO"
repositories {
mavenCentral()
jcenter()
}
configurations {
fdsm
}
// recreate defaults for visibility. Also fixes a weird issue with IntelliJ
sourceSets {
applet {
java {
srcDirs = ['src/applet/java']
}
resources {
srcDirs = ['src/applet/resources']
}
}
client {
java {
srcDirs = ['src/client/java']
}
resources {
srcDirs = ['src/client/resources']
}
}
test {
java {
srcDirs = ['src/test/java']
}
resources {
srcDirs = ['src/test/resources']
}
}
}
dependencies {
implementation group: 'com.github.martinpaljak', name: 'ant-javacard', version: '18.07.12'
fdsm requireFdsm()
implementation configurations.fdsm.dependencies
implementation requireAntJavacard()
appletImplementation fileTree("$jcDir/lib") { include '*.jar'}
clientImplementation sourceSets.applet.output
clientImplementation (group: 'com.klinec', name: 'jcardsim', version: '3.0.4') {transitive = false }
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.4.0'
testImplementation group: 'org.hamcrest', name: 'hamcrest-core', version: '2.1'
testImplementation (group: 'com.klinec', name: 'jcardsim', version: '3.0.4') {transitive = false }
testImplementation sourceSets.applet.output
testRuntime group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.4.0'
}
def requireFdsm() {
def bin = locations.bin
mkdir bin
download {
src 'https://github.com/fidesmo/fdsm/releases/download/19.01.31/fdsm.jar'
dest bin
onlyIfModified true
}
return files("$bin/fdsm.jar")
}
def requireAntJavacard() {
def lib = locations.lib
mkdir lib
download {
src 'https://github.com/martinpaljak/ant-javacard/releases/download/18.09.25/ant-javacard.jar'
dest lib
onlyIfModified true
}
return files("$lib/ant-javacard.jar")
}
// TASKS
clean {
doFirst {
delete file(locations.lib)
delete file(locations.bin)
delete file(locations.out)
// deletes Intellij's output directory
delete file("$rootDir/out")
}
}
task setup {
group = "Build Setup"
description = "Setup for the build by creating folders and performing other initialization actions"
doLast {
mkdir locations.lib
mkdir locations.bin
mkdir locations.out
}
}
test {
useJUnitPlatform()
testLogging {
events = ["passed", "skipped", "failed", 'standardOut', 'standardError']
showStandardStreams = true
}
//uncomment to force all tests (even those that passed) to run every time
outputs.upToDateWhen { false }
}
task setupAntJavacard {
group = "Build Setup"
description = "Create the Ant taskdef for compiling JavaCard to a cap file"
dependsOn setup
doLast {
ant.taskdef(
name: "javacard",
classname: "pro.javacard.ant.JavaCard",
classpath: "${locations.lib}/ant-javacard.jar"
)
}
}
task javaVersion {
group = 'Version Check'
description = 'Returns the JDK version being used by project'
String javaHome = project.properties['org.gradle.java.home']
doLast {
exec {
commandLine("$javaHome/bin/javac", "-version")
}
}
}
task collectDependencies() {
group = "Misc"
description = "Copies all dependencies into the {lib} folder"
doLast {
copy {
from configurations.compileClasspath
into locations.lib
}
copy {
from configurations.appletCompileClasspath
into locations.lib
}
copy {
from configurations.clientCompileClasspath
into locations.lib
}
copy {
from configurations.testCompileClasspath
into locations.lib
}
copy {
from configurations.testRuntimeClasspath
into locations.lib
}
}
}
task runClient(type: JavaExec) {
group = "Execution"
description = 'Run the JavaCard client. Use: runClient --args="<app_id_or_aid> [reset]"'
dependsOn compileClientJava
main = client.mainClass
classpath = sourceSets.client.runtimeClasspath
}
task cap {
group = "Build"
description = "Compile applet into a .cap file using Fidesmo's Javacard utility"
dependsOn setupAntJavacard
doFirst {
mkdir applet.classDir
mkdir applet.capDir
}
doLast {
def buffer = new ByteArrayOutputStream()
def captureStream = new PrintStream(buffer, true, "UTF-8")
def listener = new DefaultLogger(
errorPrintStream: captureStream,
outputPrintStream: captureStream,
messageOutputLevel: org.apache.tools.ant.Project.MSG_INFO
)
ant.project.addBuildListener(listener)
ant.javacard(jckit: locations.javacard.get('305')) {
cap(sources: applet.srcDir,
classes: applet.classDir,
targetsdk: jcDir,
fidesmoappid: applet.appId,
output: Paths.get((String) applet.capDir, (String) applet.capFile),
verify: applet.verify, ints: applet.intSupport, debug: applet.debug
)
{
applet(class: "${applet.package}.${applet.class}")
}
}
String result = buffer.toString("UTF-8")
def filenameRegEx = ~'CAP saved to (.+$)'
def matches = (result =~ filenameRegEx)
cap.ext.file = (matches.hasGroup()) ? matches[0][1] : null
ant.project.removeBuildListener(listener)
}
}
task upload {
group = "FDSM"
description = "Upload the generated cap file to Fidesmo"
dependsOn cap
doLast {
println "Executing: java -jar $configurations.fdsm.asPath --upload $cap.file --app-id $applet.appId --app-key $applet.appKey"
exec {
commandLine("java", "-jar", configurations.fdsm.asPath,
"--upload", cap.file,
"--app-id", applet.appId, "--app-key", applet.appKey)
}
}
}
task install {
group = "FDSM"
description = "Installs the generated cap file to a card. Requires that cap file was previously uploaded"
dependsOn cap
doLast {
exec {
commandLine("java", "-jar", configurations.fdsm.asPath,
"--install", cap.file,
"--app-id", applet.appId, "--app-key", applet.appKey)
}
}
}
task installUpload {
group = "FDSM"
description = "Uploads the cap file to Fidesmo then installs it to a card"
dependsOn cap
doLast {
exec {
commandLine("java", "-jar", configurations.fdsm.asPath,
"--install", cap.file,
"--upload",
"--app-id", applet.appId, "--app-key", applet.appKey)
}
}
}
task installTrace {
group = "FDSM"
description = "Install cap file to a card but print out APDU and API traces"
dependsOn cap
doLast {
exec {
commandLine("java", "-jar", configurations.fdsm.asPath,
"--install", cap.file,
"--verbose", "--trace-apdu", "--trace-api",
"--app-id", applet.appId, "--app-key", applet.appKey)
}
}
}
task uninstall {
group = "FDSM"
description = "Uninstalls an applet from a card. Based only on app-id and app-key, but still needs a cap file"
dependsOn cap
doLast {
exec {
commandLine("java", "-jar", configurations.fdsm.asPath,
"--uninstall", cap.file,
"--app-id", applet.appId, "--app-key", applet.appKey)
}
}
}
task cardinfo {
group = "FDSM"
description = "Get information about the first detected card along with any installed applets"
doLast {
exec {
commandLine("java", "-jar", configurations.fdsm.asPath,
"--card-info")
}
exec {
commandLine("java", "-jar", configurations.fdsm.asPath,
"--card-apps")
}
}
}
task installVivokeyOtp {
group = "FDSM"
description = "Installs the VivoKey OTP applet. Requires a VivoKey device."
doLast {
exec {
commandLine("java", "-jar", configurations.fdsm.asPath,
"--run", "61fc54d5/invivo")
}
}
}
task uninstallVivokeyOtp {
group = "FDSM"
description = "Uninstalls the VivoKey OTP applet. Requires a VivoKey device."
doLast {
exec {
commandLine("java", "-jar", configurations.fdsm.asPath,
"--run", "61fc54d5/destroy")
}
}
}
task foo {
group = "Misc"
description = "Print a famous quote by Amal"
doLast {
println "You are a giant bag of salty water"
}
}