-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathWolfSSLCertRequest.java
More file actions
864 lines (737 loc) · 30.7 KB
/
WolfSSLCertRequest.java
File metadata and controls
864 lines (737 loc) · 30.7 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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
/* WolfSSLCertRequest.java
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL 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 2 of the License, or
* (at your option) any later version.
*
* wolfSSL 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
package com.wolfssl;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.security.PublicKey;
import java.security.PrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.interfaces.ECPrivateKey;
/**
* WolfSSLCertRequest class, wraps native X509_REQ functionality.
*/
public class WolfSSLCertRequest {
private boolean active = false;
/* native X509_REQ pointer */
private long x509ReqPtr = 0;
/* lock around active state */
private final Object stateLock = new Object();
/* lock around native X509_REQ pointer use */
private final Object x509ReqLock = new Object();
/* Public key types used for CSR, mirrored from
* native enum in wolfssl/openssl/evp.h */
private static final int EVP_PKEY_RSA = 16;
private static final int EVP_PKEY_EC = 18;
/* Define from <wolfssl/openssl/asn1.h> */
private static final int MBSTRING_ASC = 0x1001;
/* Native JNI methods */
static native long X509_REQ_new();
static native void X509_REQ_free(long x509ReqPtr);
static native int X509_REQ_set_subject_name(long x509ReqPtr,
long x509NamePtr);
static native int X509_REQ_add1_attr_by_NID(long x509ReqPtr, int nid,
int type, byte[] bytes);
static native int X509_REQ_set_version(long x509ReqPtr, long ver);
static native byte[] X509_REQ_print(long x509ReqPtr);
static native int X509_REQ_sign(long x509ReqPtr, int evpKeyType,
byte[] keyBytes, int format, String digestAlg);
static native int X509_REQ_set_pubkey_native_open(long x509ReqPtr,
int keyType, byte[] fileBytes, int format);
static native byte[] X509_REQ_get_der(long x509);
static native byte[] X509_REQ_get_pem(long x509);
static native int X509_add_ext_via_nconf_nid(long x509Ptr, int nid,
String extValue, boolean isCritical);
static native int X509_add_ext_via_set_object_boolean(long x509Ptr,
int nid, boolean extValue, boolean isCritical, int pathLen);
/**
* Create new empty WolfSSLCertRequest object, for use with CSR generation
*
* @throws WolfSSLException if native API call fails.
*/
public WolfSSLCertRequest() throws WolfSSLException {
x509ReqPtr = X509_REQ_new();
if (x509ReqPtr == 0) {
throw new WolfSSLException("Failed to create WolfSSLCertRequest");
}
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, x509ReqPtr,
() -> "creating new WolfSSLCertRequest");
synchronized (stateLock) {
this.active = true;
}
}
/**
* Verifies that the current WolfSSLCertRequest object is active.
*
* @throws IllegalStateException if object has been freed
*/
private void confirmObjectIsActive()
throws IllegalStateException {
synchronized (stateLock) {
if (this.active == false) {
throw new IllegalStateException(
"WolfSSLCertRequest object has been freed");
}
}
}
/**
* Set the Subject Name to be used with this WolfSSLCertRequest.
* Note that the WolfSSLX509Name object should be completely set up
* before calling this method. This method copies/duplicates the contents
* of the WOLFSSL_X509_NAME (WolfSSLX509Name) into the native
* WOLFSSL_X509 structure.
*
* @param name Initialized and populated WolfSSLX509 name to be set into
* Subject Name of WolfSSLCertRequest for cert generation.
*
* @throws IllegalStateException if WolfSSLCertRequest has been freed.
* @throws WolfSSLException if native JNI error occurs.
*/
public void setSubjectName(WolfSSLX509Name name)
throws IllegalStateException, WolfSSLException {
int ret;
confirmObjectIsActive();
synchronized (x509ReqLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509ReqPtr,
() -> "entered setSubjectName(" + name + ")");
/* TODO somehow lock WolfSSLX509Name object while using pointer? */
ret = X509_REQ_set_subject_name(this.x509ReqPtr,
name.getNativeX509NamePtr());
}
if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException(
"Error setting subject name (ret: " + ret + ")");
}
}
/**
* Add a CSR attribute to this WolfSSLCertRequest
*
* @param nid NID of an attribute to add. Must be one of:
* WolfSSL.NID_pkcs9_challengePassword
* WolfSSL.NID_serialNumber
* WolfSSL.NID_pkcs9_unstructuredName
* WolfSSL.NID_pkcs9_contentType
* WolfSSL.NID_surname
* WolfSSL.NID_initials
* WolfSSL.NID_givenName
* WolfSSL.NID_dnQualifier
* @param value value of attribute to set, if passing in String, use
* String.getBytes()
*
* @throws IllegalStateException if WolfSSLCertRequest has been freed.
* @throws WolfSSLException if native JNI error occurs.
*/
public void addAttribute(int nid, byte[] value)
throws IllegalStateException, WolfSSLException {
int ret;
confirmObjectIsActive();
synchronized (x509ReqLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509ReqPtr,
() -> "entered addAttribute(nid: " + nid + ", byte[])");
}
if (nid != WolfSSL.NID_pkcs9_challengePassword &&
nid != WolfSSL.NID_serialNumber &&
nid != WolfSSL.NID_pkcs9_unstructuredName &&
nid != WolfSSL.NID_pkcs9_contentType &&
nid != WolfSSL.NID_surname &&
nid != WolfSSL.NID_initials &&
nid != WolfSSL.NID_givenName &&
nid != WolfSSL.NID_dnQualifier) {
throw new WolfSSLException(
"Unsupported CSR attribute NID: " + nid);
}
if (value == null || value.length == 0) {
throw new WolfSSLException(
"CSR attribute value may not be null or zero length");
}
synchronized (x509ReqLock) {
ret = X509_REQ_add1_attr_by_NID(this.x509ReqPtr, nid,
MBSTRING_ASC, value);
}
if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException(
"Error setting CSR attribute (ret: " + ret + ")");
}
}
/**
* Set CSR version for this WolfSSLCertRequest object.
*
* Calling this method is optional when generating a CSR. By default,
* a value of 0 (zero) is used for the CSR version. This is currently
* the version used by all CSR RFCs/specs.
*
* @param version version to set for CSR
*
* @throws IllegalStateException if WolfSSLCertRequest has been freed.
* @throws WolfSSLException if native JNI error occurs.
*/
public void setVersion(long version)
throws IllegalStateException, WolfSSLException {
int ret;
confirmObjectIsActive();
synchronized (x509ReqLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509ReqPtr,
() -> "entered setVersion(" + version + ")");
ret = X509_REQ_set_version(this.x509ReqPtr, version);
}
if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException(
"Error setting CSR version (ret: " + ret + ")");
}
}
/**
* Set public key for this WolfSSLCertRequest, used when generating
* Certificate Signing Requests
*
* @param filePath Path to public key file
* @param keyType Type of public key algorithm, options are:
* WolfSSL.RSAk
* WolfSSL.ECDSAk
* @param format Format of public key file, options are:
* WolfSSL.SSL_FILETYPE_ASN1 (DER formatted)
* WolfSSL.SSL_FILETYPE_PEM (PEM formatted)
*
* @throws IllegalStateException if WolfSSLCertRequest has been freed.
* @throws IOException on error opening input file
* @throws WolfSSLException if invalid arguments or native JNI error occurs.
*/
public void setPublicKey(String filePath, int keyType, int format)
throws IllegalStateException, IOException, WolfSSLException {
File keyFile = null;
byte[] fileBytes = null;
confirmObjectIsActive();
synchronized (x509ReqLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509ReqPtr,
() -> "entered setPublicKey(" + filePath + ", type: " +
keyType + ", format: " + format + ")");
}
if (filePath == null || filePath.isEmpty()) {
throw new WolfSSLException("File path is null or empty");
}
keyFile = new File(filePath);
if (!keyFile.exists()) {
throw new WolfSSLException(
"Input file does not exist: " + filePath);
}
fileBytes = WolfSSL.fileToBytes(keyFile);
if (fileBytes == null) {
throw new WolfSSLException(
"Failed to read bytes from file: " + filePath);
}
setPublicKey(fileBytes, keyType, format);
}
/**
* Set public key for this WolfSSLCertRequest, used when generating
* Certificate Signing Requests
*
* @param key Byte array containing public key
* @param keyType Type of public key algorithm, options are:
* WolfSSL.RSAk
* WolfSSL.ECDSAk
* @param format Format of public key file, options are:
* WolfSSL.SSL_FILETYPE_ASN1 (DER formatted)
* WolfSSL.SSL_FILETYPE_PEM (PEM formatted)
*
* @throws IllegalStateException if WolfSSLCertRequest has been freed.
* @throws IOException on error opening input file
* @throws WolfSSLException if invalid arguments or native JNI error occurs.
*/
public void setPublicKey(byte[] key, int keyType, int format)
throws IllegalStateException, IOException, WolfSSLException {
int ret = 0;
int evpKeyType;
confirmObjectIsActive();
synchronized (x509ReqLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509ReqPtr,
() -> "entered setPublicKey(byte[], type: " + keyType +
", format: " + format + ")");
}
if (key == null || key.length == 0) {
throw new WolfSSLException("Key array is null or empty");
}
if (format != WolfSSL.SSL_FILETYPE_ASN1 &&
format != WolfSSL.SSL_FILETYPE_PEM) {
throw new WolfSSLException(
"Invalid key format, must be PEM or DER");
}
if (keyType == WolfSSL.RSAk) {
evpKeyType = EVP_PKEY_RSA;
} else if (keyType == WolfSSL.ECDSAk) {
evpKeyType = EVP_PKEY_EC;
} else {
throw new WolfSSLException("Unsupported public key type");
}
synchronized (x509ReqLock) {
ret = X509_REQ_set_pubkey_native_open(this.x509ReqPtr, evpKeyType,
key, format);
}
if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException(
"Error setting public key into native WOLFSSL_X509_REQ " +
"(ret: " + ret + ")");
}
}
/**
* Set public key for this WolfSSLCertRequest, used when generating
* Certificate Signing Requests
*
* @param key PublicKey object containing public key to be used when
* generating CSR.
*
* @throws IllegalStateException if WolfSSLCertRequest has been freed.
* @throws WolfSSLException if invalid arguments or native JNI error occurs.
* @throws IOException on error opening/reading public key
*/
public void setPublicKey(PublicKey key)
throws IllegalStateException, IOException, WolfSSLException {
int keyType;
byte[] encodedKey = null;
confirmObjectIsActive();
synchronized (x509ReqLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509ReqPtr,
() -> "entered setPublicKey(" + key + ")");
}
if (key instanceof RSAPublicKey) {
keyType = WolfSSL.RSAk;
}
else if (key instanceof ECPublicKey) {
keyType = WolfSSL.ECDSAk;
}
else {
throw new WolfSSLException(
"PublicKey must be of type RSAPublicKey or ECPublicKey");
}
/* Get DER encoded key */
encodedKey = key.getEncoded();
if (encodedKey == null) {
throw new WolfSSLException(
"Error getting encoded (DER) format of PublicKey");
}
setPublicKey(encodedKey, keyType, WolfSSL.SSL_FILETYPE_ASN1);
}
/**
* Add an extension to a WolfSSLCertRequest given the NID and extension
* value String.
*
* This method supports the following extensions:
* - Key Usage (WolfSSL.NID_key_usage)
* - Extended Key Usage (WolfSSL.NID_ext_key_usage)
* - Subject Alt Name (WolfSSL.NED_subject_alt_name)
*
* @param nid NID of extension to add. Must be one of:
* WolfSSL.NID_key_usage
* WolfSSL.NID_ext_key_usage
* WolfSSL.NID_subject_alt_name
* @param value String value of extension to set. For keyUsage and
* extKeyUsage this should be a comma-delimited list.
* For subjectAltName, this is a single value. Possible
* values for keyUsage and extKeyUsage are:
*
* NID_key_usage:
* digitalSignature
* nonRepudiation
* contentCommitment
* keyEncipherment
* dataEncipherment
* keyAgreement
* keyCertSign
* cRLSign
* encipherOnly
* decipherOnly
*
* NID_ext_key_usage:
* serverAuth
* clientAuth
* codeSigning
* emailProtection
* timeStamping
* OCSPSigning
*
* @param isCritical Boolean flag indicating if this extension is
* critical
*
* @throws IllegalStateException if WolfSSLCertRequest has been freed.
* @throws WolfSSLException if invalid arguments or native JNI error occurs.
*/
public void addExtension(int nid, String value, boolean isCritical)
throws IllegalStateException, WolfSSLException {
int ret = 0;
confirmObjectIsActive();
synchronized (x509ReqLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509ReqPtr,
() -> "entered addExtension(nid: " + nid + ", value: " + value +
", isCritical: " + isCritical + ")");
}
if (nid != WolfSSL.NID_key_usage &&
nid != WolfSSL.NID_subject_alt_name &&
nid != WolfSSL.NID_ext_key_usage) {
throw new WolfSSLException(
"Unsupported X509v3 extension NID: " + nid);
}
synchronized (x509ReqLock) {
ret = X509_add_ext_via_nconf_nid(this.x509ReqPtr, nid, value,
isCritical);
}
if (ret != WolfSSL.SSL_SUCCESS) {
if ((WolfSSL.getLibVersionHex() <= 0x05006003) &&
(nid == WolfSSL.NID_key_usage ||
nid == WolfSSL.NID_ext_key_usage)) {
/* wolfSSL versions 5.6.3 and earlier did not include code
* fixes to native wolfSSL allowing this extension support to
* work. Use a version > 5.6.3 or apply patch from wolfSSL
* PR 6585 for correct support */
throw new WolfSSLException(
"Error setting extension into native WOLFSSL_X509 " +
"(ret: " + ret + ").\nNeed to use wolfSSL version " +
"greater than 5.6.3 for extension support (PR 6585).");
}
throw new WolfSSLException(
"Error setting extension into native WOLFSSL_X509 " +
"(ret: " + ret + ")");
}
}
/**
* Add an extension to a WolfSSLCertRequest given the NID and extension
* value true/false value.
*
* This method supports the following extensions:
* - Basic Constraints (WolfSSL.NID_basic_constraints)
*
* @param nid NID of extension to add. Must be one of:
* WolfSSL.NID_basic_constraints
* @param value Boolean value of extension (true/false)
* @param isCritical Boolean flag indicating if this extension is
* critical
*
* @throws IllegalStateException if WolfSSLCertRequest has been freed.
* @throws WolfSSLException if invalid arguments or native JNI error occurs.
*/
public void addExtension(int nid, boolean value, boolean isCritical)
throws IllegalStateException, WolfSSLException {
addExtension(nid, value, -1, isCritical);
}
/**
* Add Basic Constraints extension with CA flag and path length constraint
* to a WolfSSLCertRequest.
*
* This method allows setting the Basic Constraints extension with both the
* CA boolean and an optional path length constraint. The path length limits
* the number of intermediate CA certificates that may follow this
* certificate in a valid certification path.
*
* To set Basic Constraints without a path length constraint, use
* {@link #addExtension(int, boolean, boolean)} with
* {@code WolfSSL.NID_basic_constraints} instead.
*
* @param nid NID of extension to add. Must be:
* WolfSSL.NID_basic_constraints
* @param value Boolean value of CA flag (true for CA, false for end entity)
* @param pathLen Maximum number of intermediate CA certificates allowed
* below this CA. Must be >= 0, or -1 to not set a path length
* constraint. Only meaningful when value is true.
* @param isCritical Boolean flag indicating if this extension is critical
*
* @throws IllegalStateException if WolfSSLCertRequest has been freed
* @throws WolfSSLException if invalid arguments or on native JNI error
*/
public void addExtension(int nid, boolean value, int pathLen,
boolean isCritical) throws IllegalStateException, WolfSSLException {
int ret = 0;
confirmObjectIsActive();
synchronized (x509ReqLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509ReqPtr,
() -> "entered addExtension(nid: " + nid + ", value: " + value +
", pathLen: " + pathLen + ", isCritical: " + isCritical + ")");
}
if (nid != WolfSSL.NID_basic_constraints) {
throw new WolfSSLException(
"Unsupported X509v3 extension NID: " + nid);
}
if (pathLen < -1) {
throw new WolfSSLException(
"Path length must be >= 0 or -1, got: " + pathLen);
}
if (!value && pathLen >= 0) {
throw new WolfSSLException(
"pathLen must not be set when isCA is FALSE (RFC 5280), " +
"got pathLen: " + pathLen);
}
synchronized (x509ReqLock) {
ret = X509_add_ext_via_set_object_boolean(this.x509ReqPtr, nid,
value, isCritical, pathLen);
}
if (ret == WolfSSL.NOT_COMPILED_IN) {
throw new WolfSSLException(
"addExtension NOT_COMPILED_IN, pathLen " +
"support requires wolfSSL > 5.8.4 or " +
"PR 9940 patch (ret: " + ret + ")");
}
else if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException(
"Error setting extension into native WOLFSSL_X509 " +
"(ret: " + ret + ")");
}
}
/**
* Sign certificate request with private key from file.
*
* @param filePath Path to private key file
* @param keyType Type of public key algorithm, options are:
* WolfSSL.RSAk
* WolfSSL.ECDSAk
* @param format Format of private key file, options are:
* WolfSSL.SSL_FILETYPE_ASN1 (DER formatted)
* WolfSSL.SSL_FILETYPE_PEM (PEM formatted)
* @param digestAlg Message digest algorithm to use for signature
* generation. Options include the following, but native algorithm
* must be compiled into wolfSSL to be available:
* "MD4", "MD5", "SHA1", "SHA224", "SHA256", "SHA384",
* "SHA512", "SHA3_224", "SHA3_256", "SHA3_384", "SHA3_512"
*
* @throws IllegalStateException if WolfSSLCertRequest has been freed.
* @throws IOException on error opening input file
* @throws WolfSSLException if invalid arguments or native JNI error occurs.
*/
public void signRequest(String filePath, int keyType, int format,
String digestAlg) throws IllegalStateException, IOException,
WolfSSLException {
File keyFile = null;
byte[] fileBytes = null;
confirmObjectIsActive();
synchronized (x509ReqLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509ReqPtr,
() -> "entered signRequest(" + filePath + ", keyType: " +
keyType + ", format: " + format + ", digestAlg: " +
digestAlg + ")");
}
if (filePath == null || filePath.isEmpty()) {
throw new WolfSSLException("File path is null or empty");
}
keyFile = new File(filePath);
if (!keyFile.exists()) {
throw new WolfSSLException(
"Input file does not exist: " + filePath);
}
fileBytes = WolfSSL.fileToBytes(keyFile);
if (fileBytes == null) {
throw new WolfSSLException(
"Failed to read bytes from file: " + filePath);
}
signRequest(fileBytes, keyType, format, digestAlg);
}
/**
* Sign certificate request with private key from buffer.
*
* @param key Byte array containing private key
* @param keyType Type of public key algorithm, options are:
* WolfSSL.RSAk
* WolfSSL.ECDSAk
* @param format Format of private key file, options are:
* WolfSSL.SSL_FILETYPE_ASN1 (DER formatted)
* WolfSSL.SSL_FILETYPE_PEM (PEM formatted)
* @param digestAlg Message digest algorithm to use for signature
* generation. Options include the following, but native algorithm
* must be compiled into wolfSSL to be available:
* "MD4", "MD5", "SHA1", "SHA224", "SHA256", "SHA384",
* "SHA512", "SHA3_224", "SHA3_256", "SHA3_384", "SHA3_512"
*
* @throws IllegalStateException if WolfSSLCertRequest has been freed.
* @throws WolfSSLException if invalid arguments or native JNI error occurs.
*/
public void signRequest(byte[] key, int keyType, int format,
String digestAlg) throws IllegalStateException, WolfSSLException {
int ret = 0;
int evpKeyType;
confirmObjectIsActive();
synchronized (x509ReqLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509ReqPtr,
() -> "entered signRequest(byte[], keyType: " + keyType +
", format: " + format + ", digestAlg: " + digestAlg + ")");
}
if (key == null || key.length == 0) {
throw new WolfSSLException("Key array is null or empty");
}
if (format != WolfSSL.SSL_FILETYPE_ASN1 &&
format != WolfSSL.SSL_FILETYPE_PEM) {
throw new WolfSSLException(
"Invalid key format, must be PEM or DER");
}
if (keyType == WolfSSL.RSAk) {
evpKeyType = EVP_PKEY_RSA;
} else if (keyType == WolfSSL.ECDSAk) {
evpKeyType = EVP_PKEY_EC;
} else {
throw new WolfSSLException("Unsupported private key type");
}
synchronized (x509ReqLock) {
ret = X509_REQ_sign(this.x509ReqPtr, evpKeyType, key, format,
digestAlg);
}
if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException(
"Error signing native X509_REQ (ret: " + ret + ")");
}
}
/**
* Sign certificate request with private key from PrivateKey object.
*
* @param key java.security.PrivateKey object containing private key,
* must be of type RSAPrivateKey or ECPrivateKey
* @param digestAlg Message digest algorithm to use for signature
* generation. Options include the following, but native algorithm
* must be compiled into wolfSSL to be available:
* "MD4", "MD5", "SHA1", "SHA224", "SHA256", "SHA384",
* "SHA512", "SHA3_224", "SHA3_256", "SHA3_384", "SHA3_512"
*
* @throws IllegalStateException if WolfSSLCertRequest has been freed.
* @throws WolfSSLException if invalid arguments or native JNI error occurs.
*/
public void signRequest(PrivateKey key, String digestAlg)
throws IllegalStateException, WolfSSLException {
int ret = 0;
int evpKeyType;
byte[] encodedKey = null;
confirmObjectIsActive();
synchronized (x509ReqLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509ReqPtr,
() -> "entered signRequest(key: " + key + ", digestAlg: " +
digestAlg + ")");
}
if (key == null) {
throw new WolfSSLException("Key object is null");
}
if (key instanceof RSAPrivateKey) {
evpKeyType = EVP_PKEY_RSA;
}
else if (key instanceof ECPrivateKey) {
evpKeyType = EVP_PKEY_EC;
}
else {
throw new WolfSSLException(
"PrivateKey must be of type RSAPrivateKey or ECPrivateKey");
}
/* Get DER encoded key */
encodedKey = key.getEncoded();
if (encodedKey == null) {
throw new WolfSSLException("PrivateKey does not support encoding");
}
synchronized (x509ReqLock) {
ret = X509_REQ_sign(this.x509ReqPtr, evpKeyType, encodedKey,
WolfSSL.SSL_FILETYPE_ASN1, digestAlg);
}
if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException(
"Error signing native X509_REQ (ret: " + ret + ")");
}
}
/**
* Get ASN.1/DER encoding of this CSR, after signRequest() has been called.
*
* @return DER encoded array of CSR or null if not available.
*
* @throws IllegalStateException if WolfSSLCertRequest has been freed.
* @throws WolfSSLJNIException if native JNI operation fails
*/
public byte[] getDer() throws IllegalStateException, WolfSSLJNIException {
confirmObjectIsActive();
synchronized (x509ReqLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509ReqPtr, () -> "entered getDer()");
return X509_REQ_get_der(this.x509ReqPtr);
}
}
/**
* Get PEM encoding of this CSR, after signRequest() has been called.
*
* @return PEM encoded array of CSR or null if not available.
*
* @throws IllegalStateException if WolfSSLCertRequest has been freed.
* @throws WolfSSLJNIException if native JNI operation fails
*/
public byte[] getPem() throws IllegalStateException, WolfSSLJNIException {
confirmObjectIsActive();
synchronized (x509ReqLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509ReqPtr, () -> "entered getPem()");
return X509_REQ_get_pem(this.x509ReqPtr);
}
}
@Override
public String toString() {
byte[] x509ReqText = null;
synchronized (stateLock) {
if (this.active == false) {
return super.toString();
}
synchronized (x509ReqLock) {
x509ReqText = X509_REQ_print(this.x509ReqPtr);
}
if (x509ReqText != null) {
/* let Java do the modified UTF-8 conversion */
return new String(x509ReqText, Charset.forName("UTF-8"));
} else {
System.out.println("toString: x509ReqTest == null");
}
}
return super.toString();
}
/**
* Frees WolfSSLCertRequest native resources.
*/
public synchronized void free() {
synchronized (stateLock) {
if (this.active == false) {
/* already freed, just return */
return;
}
synchronized (x509ReqLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509ReqPtr, () -> "entered free()");
/* free native resources */
X509_REQ_free(this.x509ReqPtr);
/* free Java resources */
this.active = false;
this.x509ReqPtr = 0;
}
}
}
@SuppressWarnings({"deprecation", "removal"})
@Override
protected void finalize() throws Throwable
{
this.free();
super.finalize();
}
}