Repository navigation
Expand file tree
/
Copy pathSignatureOperation.java
More file actions
346 lines (315 loc) · 13.5 KB
/
Copy pathSignatureOperation.java
File metadata and controls
346 lines (315 loc) · 13.5 KB
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
package com.example.crypto.algorithms;
// import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.*;
import java.security.spec.ECGenParameterSpec;
import java.util.Base64;
import java.util.Properties;
/**
* Demonstrates various digital signature operations:
*
* 1) RSA-PSS (modern, safer) - CBOM/SAST: Classified as a Modern Digital
* Signature scheme using RSA-PSS. RSA-PSS with SHA-256 is recommended.
*
* 2) ECDSA with secp256r1 - CBOM/SAST: Classified as an Elliptic Curve Digital
* Signature Algorithm. Secure when used with a strong curve and proper
* randomness.
*
* 3) Ed25519 (RFC 8032) - CBOM/SAST: Classified as a modern, high-performance
* signature scheme.
*
* 4) SHA1withRSA (deprecated/unsafe example) - CBOM/SAST: Classified as a
* legacy digital signature scheme. SHA-1 and 1024-bit RSA are deprecated.
*
* Additional nuanced examples:
*
* - Signing and verifying an empty message. - Signing data with non-ASCII
* characters. - Demonstrating signature tampering and its detection. - A
* dynamic (runtime-selected) signature algorithm scenario ("known unknown").
*
* Requirements: - BouncyCastle for ECDSA, Ed25519, and RSA-PSS (if needed). -
* Java 11+ for native Ed25519 support or using BC for older versions.
*/
public class SignatureOperation {
// static {
// // Register the BouncyCastle provider.
// Security.addProvider(new BouncyCastleProvider());
// }
///////////////////////////////////////
// 1. RSA-PSS (Recommended)
///////////////////////////////////////
/**
* Generate an RSA key pair for RSA-PSS.
* Uses a 2048-bit key.
*
* CBOM/SAST Notes:
* - Parent: Modern Digital Signature (RSA-PSS).
*/
public KeyPair generateRSAPSSKeyPair() throws Exception {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
return kpg.generateKeyPair();
}
/**
* Sign data using RSA-PSS with SHA-256.
*
* CBOM/SAST Notes: - Parent: Modern Digital Signature (RSA-PSS).
*/
public byte[] signRSAPSS(PrivateKey privateKey, byte[] data) throws Exception {
Signature signature = Signature.getInstance("SHA256withRSAandMGF1");
signature.initSign(privateKey);
signature.update(data);
return signature.sign();
}
/**
* Verify data using RSA-PSS with SHA-256.
*
* CBOM/SAST Notes: - Parent: Modern Digital Signature (RSA-PSS).
*/
public boolean verifyRSAPSS(PublicKey publicKey, byte[] data, byte[] sigBytes) throws Exception {
Signature signature = Signature.getInstance("SHA256withRSAandMGF1");
signature.initVerify(publicKey);
signature.update(data);
return signature.verify(sigBytes);
}
///////////////////////////////////////
// 2. ECDSA (secp256r1)
///////////////////////////////////////
/**
* Generate an ECDSA key pair on secp256r1.
*
* CBOM/SAST Notes:
* - Parent: Elliptic Curve Digital Signature.
*/
public KeyPair generateECDSAKeyPair() throws Exception {
KeyPairGenerator ecKpg = KeyPairGenerator.getInstance("EC", "BC");
ecKpg.initialize(new ECGenParameterSpec("secp256r1"), new SecureRandom());
return ecKpg.generateKeyPair();
}
/**
* Sign data using ECDSA with SHA-256.
*
* CBOM/SAST Notes: - Parent: Elliptic Curve Digital Signature.
*/
public byte[] signECDSA(PrivateKey privateKey, byte[] data) throws Exception {
Signature signature = Signature.getInstance("SHA256withECDSA", "BC");
signature.initSign(privateKey);
signature.update(data);
return signature.sign();
}
/**
* Verify data using ECDSA with SHA-256.
*
* CBOM/SAST Notes: - Parent: Elliptic Curve Digital Signature.
*/
public boolean verifyECDSA(PublicKey publicKey, byte[] data, byte[] sigBytes) throws Exception {
Signature signature = Signature.getInstance("SHA256withECDSA", "BC");
signature.initVerify(publicKey);
signature.update(data);
return signature.verify(sigBytes);
}
///////////////////////////////////////
// 3. Ed25519 (RFC 8032)
///////////////////////////////////////
/**
* Generate an Ed25519 key pair.
*
* CBOM/SAST Notes:
* - Parent: Modern Digital Signature (EdDSA).
*/
public KeyPair generateEd25519KeyPair() throws Exception {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("Ed25519", "BC");
return kpg.generateKeyPair();
}
/**
* Sign data using Ed25519.
*
* CBOM/SAST Notes: - Parent: Modern Digital Signature (EdDSA).
*/
public byte[] signEd25519(PrivateKey privateKey, byte[] data) throws Exception {
Signature signature = Signature.getInstance("Ed25519", "BC");
signature.initSign(privateKey);
signature.update(data);
return signature.sign();
}
/**
* Verify data using Ed25519.
*
* CBOM/SAST Notes: - Parent: Modern Digital Signature (EdDSA).
*/
public boolean verifyEd25519(PublicKey publicKey, byte[] data, byte[] sigBytes) throws Exception {
Signature signature = Signature.getInstance("Ed25519", "BC");
signature.initVerify(publicKey);
signature.update(data);
return signature.verify(sigBytes);
}
///////////////////////////////////////
// 4. SHA1withRSA (Deprecated/Unsafe)
///////////////////////////////////////
/**
* Generate an RSA key pair for the deprecated/unsafe example.
* Uses a 1024-bit key.
*
* CBOM/SAST Notes:
* - Parent: Legacy Digital Signature.
* - RSA with SHA-1 and 1024-bit keys is deprecated and should be avoided.
*/
public KeyPair generateRSAUnsafeKeyPair() throws Exception {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024);
return kpg.generateKeyPair();
}
/**
* Sign data using SHA1withRSA.
*
* CBOM/SAST Notes: - Parent: Legacy Digital Signature. - SHA-1 is
* deprecated and RSA with 1024 bits is considered weak.
*/
public byte[] signSHA1withRSA(PrivateKey privateKey, byte[] data) throws Exception {
Signature signature = Signature.getInstance("SHA1withRSA");
signature.initSign(privateKey);
signature.update(data);
return signature.sign();
}
/**
* Verify data using SHA1withRSA.
*
* CBOM/SAST Notes: - Parent: Legacy Digital Signature. - Verification of
* SHA1withRSA is insecure.
*/
public boolean verifySHA1withRSA(PublicKey publicKey, byte[] data, byte[] sigBytes) throws Exception {
Signature signature = Signature.getInstance("SHA1withRSA");
signature.initVerify(publicKey);
signature.update(data);
return signature.verify(sigBytes);
}
///////////////////////////////////////
// Nuanced Edge-Case Examples
///////////////////////////////////////
/**
* Demonstrates signing and verifying an empty message.
*
* CBOM/SAST Notes:
* - Edge Case: Signing empty input should be handled correctly but might be
* unexpected.
*/
public void signAndVerifyEmptyMessage() throws Exception {
byte[] emptyMessage = new byte[0];
KeyPair kp = generateRSAPSSKeyPair();
byte[] sig = signRSAPSS(kp.getPrivate(), emptyMessage);
boolean verified = verifyRSAPSS(kp.getPublic(), emptyMessage, sig);
System.out.println("Empty message signature verified? " + verified);
}
/**
* Demonstrates that even a slight tampering with the signature will cause
* verification to fail.
*
* CBOM/SAST Notes: - Edge Case: Signature integrity is critical. Any
* change-even a single byte-should invalidate the signature.
*/
public void tamperSignatureEdgeCase() throws Exception {
byte[] message = "Important Message".getBytes();
KeyPair kp = generateECDSAKeyPair();
byte[] originalSig = signECDSA(kp.getPrivate(), message);
// Tamper with the signature by flipping one bit.
byte[] tamperedSig = originalSig.clone();
tamperedSig[0] ^= 0x01;
boolean verifiedOriginal = verifyECDSA(kp.getPublic(), message, originalSig);
boolean verifiedTampered = verifyECDSA(kp.getPublic(), message, tamperedSig);
System.out.println("Original ECDSA signature verified? " + verifiedOriginal);
System.out.println("Tampered ECDSA signature verified? " + verifiedTampered);
}
/**
* Demonstrates dynamic signature algorithm selection. This is a "known
* unknown" scenario where the algorithm is chosen at runtime based on
* configuration. If the configuration is compromised or misconfigured, an
* insecure algorithm might be selected.
*
* CBOM/SAST Notes: - Known Unknown: Dynamic configuration introduces
* ambiguity and risk. - Ensure that fallback defaults are secure.
*/
public void dynamicSignatureSelectionDemo() throws Exception {
// Simulate loading a configuration.
Properties config = new Properties();
// For demonstration, let's assume the config might specify an algorithm.
// Possible values: "SHA256withRSAandMGF1", "SHA256withECDSA", "Ed25519",
// "SHA1withRSA"
// Here we simulate an unknown or insecure algorithm being selected.
config.setProperty("signature.algorithm", "SHA1withRSA"); // Insecure choice!
String algorithm = config.getProperty("signature.algorithm", "SHA256withRSAandMGF1");
KeyPair kp;
Signature signature;
if ("SHA256withRSAandMGF1".equalsIgnoreCase(algorithm)) {
kp = generateRSAPSSKeyPair();
signature = Signature.getInstance("SHA256withRSAandMGF1");
} else if ("SHA256withECDSA".equalsIgnoreCase(algorithm)) {
kp = generateECDSAKeyPair();
signature = Signature.getInstance("SHA256withECDSA", "BC");
} else if ("Ed25519".equalsIgnoreCase(algorithm)) {
kp = generateEd25519KeyPair();
signature = Signature.getInstance("Ed25519", "BC");
} else if ("SHA1withRSA".equalsIgnoreCase(algorithm)) {
kp = generateRSAUnsafeKeyPair();
signature = Signature.getInstance("SHA1withRSA");
} else {
// Fallback to a secure default.
kp = generateRSAPSSKeyPair();
signature = Signature.getInstance("SHA256withRSAandMGF1");
}
byte[] message = "Dynamic Signature Demo".getBytes();
signature.initSign(kp.getPrivate());
signature.update(message);
byte[] sigBytes = signature.sign();
// Verify using the same algorithm.
signature.initVerify(kp.getPublic());
signature.update(message);
boolean verified = signature.verify(sigBytes);
System.out.println("Dynamic algorithm (" + algorithm + ") signature verified? " + verified);
}
///////////////////////////////////////
// Demo Method: runSignatureDemos
///////////////////////////////////////
/**
* Demonstrates digital signature operations for various algorithms.
* It generates key pairs, signs a message, and verifies the signature for:
* - RSA-PSS
* - ECDSA (secp256r1)
* - Ed25519
* - SHA1withRSA (deprecated/unsafe)
* Additionally, it runs several edge-case demos.
*
* CBOM/SAST Classification:
* - Shows both modern, secure signature schemes and a deprecated example.
* - Also demonstrates handling of edge cases and dynamic selection risks.
*/
public void runSignatureDemos() throws Exception {
byte[] message = "Hello Signature World!".getBytes();
// ============ RSA-PSS ============
KeyPair rsaPssKP = generateRSAPSSKeyPair();
byte[] rsaPssSig = signRSAPSS(rsaPssKP.getPrivate(), message);
System.out.println("RSA-PSS Signature: " + Base64.getEncoder().encodeToString(rsaPssSig));
boolean rsaPssVerified = verifyRSAPSS(rsaPssKP.getPublic(), message, rsaPssSig);
System.out.println("RSA-PSS Verified? " + rsaPssVerified);
// ============ ECDSA (secp256r1) ============
KeyPair ecdsaKP = generateECDSAKeyPair();
byte[] ecdsaSig = signECDSA(ecdsaKP.getPrivate(), message);
System.out.println("ECDSA Signature: " + Base64.getEncoder().encodeToString(ecdsaSig));
boolean ecdsaVerified = verifyECDSA(ecdsaKP.getPublic(), message, ecdsaSig);
System.out.println("ECDSA Verified? " + ecdsaVerified);
// ============ Ed25519 ============
KeyPair ed25519KP = generateEd25519KeyPair();
byte[] ed25519Sig = signEd25519(ed25519KP.getPrivate(), message);
System.out.println("Ed25519 Signature: " + Base64.getEncoder().encodeToString(ed25519Sig));
boolean ed25519Verified = verifyEd25519(ed25519KP.getPublic(), message, ed25519Sig);
System.out.println("Ed25519 Verified? " + ed25519Verified);
// ============ SHA1withRSA (Deprecated/Unsafe) ============
KeyPair rsaUnsafeKP = generateRSAUnsafeKeyPair();
byte[] rsaUnsafeSig = signSHA1withRSA(rsaUnsafeKP.getPrivate(), message);
System.out.println("SHA1withRSA Signature (Insecure): " + Base64.getEncoder().encodeToString(rsaUnsafeSig));
boolean rsaUnsafeVerified = verifySHA1withRSA(rsaUnsafeKP.getPublic(), message, rsaUnsafeSig);
System.out.println("SHA1withRSA Verified? " + rsaUnsafeVerified);
// ============ Edge Cases ============
signAndVerifyEmptyMessage();
tamperSignatureEdgeCase();
dynamicSignatureSelectionDemo();
}
}