forked from wolfSSL/wolfssljni
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWolfSSLCertificate.java
More file actions
2634 lines (2218 loc) · 90.2 KB
/
WolfSSLCertificate.java
File metadata and controls
2634 lines (2218 loc) · 90.2 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
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* WolfSSLCertificate.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.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
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;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateEncodingException;
/**
* WolfSSLCertificate class, wraps native wolfSSL WOLFSSL_X509 functionality.
*/
public class WolfSSLCertificate implements Serializable {
private static final long serialVersionUID = 1L;
/** Flag if this class is active or not */
private boolean active = false;
/** Internal pointer for native WOLFSSL_X509 */
private long x509Ptr = 0;
/** Does this WolfSSLCertificate own the internal WOLFSSL_X509 pointer?
* If not, don't try to free native memory on free(). */
private boolean weOwnX509Ptr = false;
/** lock around active state */
private transient final Object stateLock = new Object();
/** lock around native WOLFSSL_X509 pointer use */
private transient final Object x509Lock = new Object();
/** Cache alt names once retrieved once */
private ArrayList<List<?>> altNames = null;
/** Cache alt names array once retrieved once */
private WolfSSLAltName[] altNamesArray = null;
/* Public key types used for certificate generation, 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;
static native byte[] X509_get_der(long x509);
static native byte[] X509_get_pem(long x509);
static native byte[] X509_get_tbs(long x509);
static native void X509_free(long x509);
static native int X509_get_serial_number(long x509, byte[] out);
static native String X509_notBefore(long x509);
static native String X509_notAfter(long x509);
static native int X509_version(long x509);
static native byte[] X509_get_signature(long x509);
static native String X509_get_signature_type(long x509);
static native String X509_get_signature_OID(long x509);
static native byte[] X509_print(long x509);
static native int X509_get_isCA(long x509);
static native String X509_get_subject_name(long x509);
static native String X509_get_issuer_name(long x509);
static native long X509_get_issuer_name_ptr(long x509);
static native byte[] X509_get_subject_name_DER(long x509);
static native byte[] X509_get_issuer_name_DER(long x509);
static native byte[] X509_get_pubkey(long x509);
static native String X509_get_pubkey_type(long x509);
static native int X509_get_pathLength(long x509);
static native int X509_verify(long x509, byte[] pubKey, int pubKeySz);
static native boolean[] X509_get_key_usage(long x509);
static native String[] X509_get_extended_key_usage(long x509);
static native String[] X509_get1_ocsp(long x509);
static native int X509_get_aia_overflow(long x509);
static native String[] X509_get1_ca_issuers(long x509);
static native byte[] X509_get_extension(long x509, String oid);
static native int X509_is_extension_set(long x509, String oid);
static native String X509_get_next_altname(long x509);
static native Object[][] X509_get_subject_alt_names_full(long x509);
static native long X509_load_certificate_buffer(byte[] buf, int format);
static native long X509_load_certificate_file(String path, int format);
static native int X509_check_host(long x509, String chk, long flags,
long peerName);
static native long X509_get_ext_d2i_name_constraints(long x509);
/* native functions used for X509v3 certificate generation */
static native long X509_new();
static native int X509_set_subject_name(long x509Ptr, long x509NamePtr);
static native int X509_set_issuer_name(long x509Ptr, long x509NamePtr);
static native int X509_set_issuer_name_from_der(long x509Ptr, byte[] certDer);
static native int X509_set_pubkey_native_open(long x509Ptr, int keyType,
byte[] fileBytes, int format);
static native int X509_add_altname(long x509Ptr, String name, int type);
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);
static native int X509_set_notBefore(long x509Ptr, long timeSecs);
static native int X509_set_notAfter(long x509Ptr, long timeSecs);
static native int X509_set_serialNumber(long x509Ptr, byte[] serialBytes);
static native int X509_set_subject_key_id(long x509Ptr, byte[] skid);
static native int X509_set_subject_key_id_ex(long x509Ptr);
static native int X509_set_authority_key_id(long x509Ptr, byte[] akid);
static native int X509_set_authority_key_id_ex(long x509Ptr,
long issuerPtr);
static native int X509_CRL_set_dist_points(long x509Ptr, byte[] der);
static native int X509_CRL_add_dist_point(long x509Ptr, String uri,
boolean critical);
static native int X509_set_ns_cert_type(long x509Ptr, int nsCertType);
static native int X509_sign(long x509Ptr, int evpKeyType, byte[] keyBytes,
int format, String digestAlg);
/**
* Create new empty WolfSSLCertificate object, for use with X509v3
* certificate generation.
*
* @throws WolfSSLException if native API call fails.
*/
public WolfSSLCertificate() throws WolfSSLException {
x509Ptr = X509_new();
if (x509Ptr == 0) {
throw new WolfSSLException("Failed to create WolfSSLCertificate");
}
/* x509Ptr has been allocated natively, mark as owned */
this.weOwnX509Ptr = true;
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, x509Ptr,
() -> "creating new WolfSSLCertificate");
synchronized (stateLock) {
this.active = true;
}
}
/**
* Create new WolfSSLCertificate from DER-encoded byte array.
*
* @param der ASN.1/DER encoded X.509 certificate
*
* @throws WolfSSLException if input is null, input array length is 0,
* or native API call fails.
*/
public WolfSSLCertificate(byte[] der) throws WolfSSLException {
if (der == null || der.length == 0) {
throw new WolfSSLException(
"Input array must not be null or zero length");
}
x509Ptr = X509_load_certificate_buffer(der, WolfSSL.SSL_FILETYPE_ASN1);
if (x509Ptr == 0) {
throw new WolfSSLException("Failed to create WolfSSLCertificate");
}
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, x509Ptr,
() -> "creating new WolfSSLCertificate(byte[])");
/* x509Ptr has been allocated natively, mark as owned */
this.weOwnX509Ptr = true;
synchronized (stateLock) {
this.active = true;
}
}
/**
* Create WolfSSLCertificate from byte array in specified format.
*
* @param in X.509 certificate byte array in format specified
* @param format format of certificate, either WolfSSL.SSL_FILETYPE_ASN1
* or WolfSSL.SSL_FILETYPE_PEM
*
* @throws WolfSSLException if in array is null, input array length is 0,
* format does not match valid options, or
* native API call fails.
*/
public WolfSSLCertificate(byte[] in, int format) throws WolfSSLException {
if (in == null || in.length == 0) {
throw new WolfSSLException(
"Input array must not be null or zero length");
}
if ((format != WolfSSL.SSL_FILETYPE_ASN1) &&
(format != WolfSSL.SSL_FILETYPE_PEM)) {
throw new WolfSSLException(
"Input format must be WolfSSL.SSL_FILETYPE_ASN1 or " +
"WolfSSL.SSL_FILETYPE_PEM");
}
x509Ptr = X509_load_certificate_buffer(in, format);
if (x509Ptr == 0) {
throw new WolfSSLException("Failed to create WolfSSLCertificate");
}
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, x509Ptr,
() -> "creating new WolfSSLCertificate(byte[], format: " +
format +")");
/* x509Ptr has been allocated natively, mark as owned */
this.weOwnX509Ptr = true;
synchronized (stateLock) {
this.active = true;
}
}
/**
* Create WolfSSLCertificate from specified ASN.1/DER X.509 file.
*
* @param fileName path to X.509 certificate file in ASN.1/DER format
*
* @throws WolfSSLException if fileName is null or native API
* call fails with error.
*/
public WolfSSLCertificate(String fileName) throws WolfSSLException {
if (fileName == null) {
throw new WolfSSLException("Input filename cannot be null");
}
x509Ptr = X509_load_certificate_file(fileName,
WolfSSL.SSL_FILETYPE_ASN1);
if (x509Ptr == 0) {
throw new WolfSSLException("Failed to create WolfSSLCertificate");
}
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, x509Ptr,
() -> "creating new WolfSSLCertificate(" + fileName + ")");
/* x509Ptr has been allocated natively, mark as owned */
this.weOwnX509Ptr = true;
synchronized (stateLock) {
this.active = true;
}
}
/**
* Create WolfSSLCertificate from specified X.509 file in specified
* format.
*
* @param fileName path to X.509 certificate file
* @param format format of certificate file, either
* WolfSSL.SSL_FILETYPE_ASN1 or
* WolfSSL.SSL_FILETYPE_PEM
*
* @throws WolfSSLException if input fileName is null, format is not
* valid, or native API call fails.
*/
public WolfSSLCertificate(String fileName, int format)
throws WolfSSLException {
if (fileName == null) {
throw new WolfSSLException("Input filename cannot be null");
}
if ((format != WolfSSL.SSL_FILETYPE_ASN1) &&
(format != WolfSSL.SSL_FILETYPE_PEM)) {
throw new WolfSSLException(
"Input format must be WolfSSL.SSL_FILETYPE_ASN1 or " +
"WolfSSL.SSL_FILETYPE_PEM");
}
x509Ptr = X509_load_certificate_file(fileName, format);
if (x509Ptr == 0) {
throw new WolfSSLException("Failed to create WolfSSLCertificate");
}
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, x509Ptr,
() -> "creating new WolfSSLCertificate(" + fileName +
", format: " + format + ")");
/* x509Ptr has been allocated natively, mark as owned */
this.weOwnX509Ptr = true;
synchronized (stateLock) {
this.active = true;
}
}
/**
* Create WolfSSLCertificate from pre existing native pointer.
*
* @param x509 pre existing native pointer to WOLFSSL_X509 structure.
* @param doFree should this WOLFSSL_X509 structure be freed when free()
* is called? true to free memory, false to skip free. Free
* should be skipped if caller is controlling memory for this
* WOLFSSL_X509 struct pointer.
*
* @throws WolfSSLException if input pointer is invalid
*/
public WolfSSLCertificate(long x509, boolean doFree)
throws WolfSSLException {
if (x509 == 0) {
throw new WolfSSLException("Input pointer may not be 0/NULL");
}
x509Ptr = x509;
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, x509Ptr,
() -> "creating new WolfSSLCertificate(ptr, doFree: " +
doFree + ")");
if (!doFree) {
/* x509Ptr has NOT been allocated natively, do not mark as owned.
* Original owner is responsible for freeing. */
this.weOwnX509Ptr = false;
}
else {
this.weOwnX509Ptr = true;
}
synchronized (stateLock) {
this.active = true;
}
}
/**
* Verifies that the current WolfSSLCertificate object is active.
*
* @throws IllegalStateException if object has been freed
*/
private void confirmObjectIsActive()
throws IllegalStateException {
synchronized (stateLock) {
if (this.active == false) {
throw new IllegalStateException(
"WolfSSLCertificate object has been freed");
}
}
}
/**
* Protected method to be used by objects of this class to get
* internal WOLFSSL_X509 pointer.
*
* @return internal WOLFSSL_X509 pointer value
* @throws IllegalStateException if object has been freed
*/
protected long getX509Ptr() throws IllegalStateException {
confirmObjectIsActive();
synchronized (x509Lock) {
return this.x509Ptr;
}
}
/**
* Set the Subject Name to be used with this WolfSSLCertificate.
* 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 WolfSSLCertificate for cert generation.
*
* @throws IllegalStateException if WolfSSLCertificate has been freed.
* @throws WolfSSLException if native JNI error occurs.
*/
public void setSubjectName(WolfSSLX509Name name)
throws IllegalStateException, WolfSSLException {
int ret;
confirmObjectIsActive();
synchronized (x509Lock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509Ptr,
() -> "entering getSubjectName(" + name + ")");
/* TODO somehow lock WolfSSLX509Name object while using pointer? */
ret = X509_set_subject_name(this.x509Ptr,
name.getNativeX509NamePtr());
}
if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException(
"Error setting subject name (ret: " + ret + ")");
}
}
/**
* Set the Issuer Name to be used with this WolfSSLCertificate.
* 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
* Issuer Name of WolfSSLCertificate for cert generation.
*
* @throws IllegalStateException if WolfSSLCertificate has been freed.
* @throws WolfSSLException if native JNI error occurs.
*/
public void setIssuerName(WolfSSLX509Name name)
throws IllegalStateException, WolfSSLException {
int ret;
confirmObjectIsActive();
synchronized (x509Lock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509Ptr,
() -> "entering setIssuerName(" + name + ")");
/* TODO somehow lock WolfSSLX509Name object while using pointer? */
ret = X509_set_issuer_name(this.x509Ptr,
name.getNativeX509NamePtr());
}
if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException(
"Error setting issuer name (ret: " + ret + ")");
}
}
/**
* Set the Issuer Name to be used with this WolfSSLCertificate.
* This method copies the issuer name from the existing populated
* WolfSSLCertificate object, which would commonly be initialized
* from a CA certificate file or byte array.
*
* @param cert Initialized and populated WolfSSLCertificate to be set into
* Issuer Name of this WolfSSLCertificate for cert generation.
*
* @throws IllegalStateException if WolfSSLCertificate has been freed.
* @throws WolfSSLException if native JNI error occurs.
*/
public void setIssuerName(WolfSSLCertificate cert)
throws IllegalStateException, WolfSSLException {
int ret;
long x509NamePtr = 0;
confirmObjectIsActive();
synchronized (x509Lock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509Ptr,
() -> "entering setIssuerName(" + cert + ")");
}
x509NamePtr = X509_get_issuer_name_ptr(cert.getX509Ptr());
if (x509NamePtr == 0) {
throw new WolfSSLException(
"Error getting issuer name from WolfSSLCertificate");
}
synchronized (x509Lock) {
/* TODO somehow lock WolfSSLX509Name object while using pointer? */
ret = X509_set_issuer_name(this.x509Ptr, x509NamePtr);
}
if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException(
"Error setting issuer name (ret: " + ret + ")");
}
}
/**
* Set the Issuer Name to be used with this WolfSSLCertificate.
* This method copies the issuer name from the existing populated
* X509Certificate object.
*
* @param cert Initialized and populated X509Certificate to be used to set
* Issuer Name of this WolfSSLCertificate for cert generation.
*
* @throws IllegalStateException if WolfSSLCertificate has been freed.
* @throws WolfSSLException if native JNI error occurs.
* @throws CertificateEncodingException if error occurs while parsing
* X509Certificate
*/
public void setIssuerName(X509Certificate cert)
throws IllegalStateException, WolfSSLException,
CertificateEncodingException {
int ret;
byte[] certDer = null;
confirmObjectIsActive();
synchronized (x509Lock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509Ptr,
() -> "entering setIssuerName(" + cert + ")");
}
/* Get DER encoding of certificate */
certDer = cert.getEncoded();
synchronized (x509Lock) {
ret = X509_set_issuer_name_from_der(this.x509Ptr, certDer);
}
if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException(
"Error setting issuer name (ret: " + ret + ")");
}
}
/**
* Set public key for this WolfSSLCertificate, used when generating
* X509v3 certificates.
*
* @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 WolfSSLCertificate 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 (x509Lock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509Ptr,
() -> "entering setPublicKey(" + filePath + ", keyType: " +
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 WolfSSLCertificate, used when generating
* X509v3 certificates.
*
* @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 WolfSSLCertificate 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 (x509Lock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509Ptr,
() -> "entering setPublicKey(byte[], keyType: " + 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 (x509Lock) {
ret = X509_set_pubkey_native_open(this.x509Ptr, evpKeyType,
key, format);
}
if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException(
"Error setting public key into native WOLFSSL_X509 " +
"(ret: " + ret + ")");
}
}
/**
* Set public key for this WolfSSLCertificate, used when generating
* X509v3 certificates.
*
* @param key PublicKey object containing public key to be used when
* generating X509v3 certificate.
*
* @throws IllegalStateException if WolfSSLCertificate 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 (x509Lock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509Ptr,
() -> "entering 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);
}
/**
* Sets the serial number for this WolfSSLCertificate, used when
* generating X509v3 certificates.
*
* @param serial BigInteger holding serial number for generated cert
*
* @throws IllegalStateException if WolfSSLCertificate has been freed.
* @throws WolfSSLException if invalid arguments or native JNI error occurs.
*/
public void setSerialNumber(BigInteger serial)
throws IllegalStateException, WolfSSLException {
int ret = 0;
byte[] serialBytes = null;
confirmObjectIsActive();
synchronized (x509Lock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509Ptr,
() -> "entering setSerialNumber(" + serial + ")");
}
if (serial == null) {
throw new WolfSSLException("Input BigInteger is null");
}
serialBytes = serial.toByteArray();
if (serialBytes == null || serialBytes.length == 0) {
throw new WolfSSLException(
"BigInteger.toByteArray() is null or 0 length");
}
synchronized (x509Lock) {
ret = X509_set_serialNumber(this.x509Ptr, serialBytes);
}
if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException(
"Error setting serial number into native WOLFSSL_X509 " +
"(ret: " + ret + ")");
}
}
/**
* Sets the Subject Key Identifier extension for this WolfSSLCertificate,
* used when generating X509v3 certificates.
*
* @param skid Byte array containing Subject Key Identifier.
*
* @throws IllegalStateException if WolfSSLCertificate has been freed.
* @throws WolfSSLException if invalid arguments or native JNI error occurs.
*/
public void setSubjectKeyId(byte[] skid)
throws IllegalStateException, WolfSSLException {
int ret = 0;
confirmObjectIsActive();
if (skid == null || skid.length == 0) {
throw new WolfSSLException("Subject Key Identifier is null/empty");
}
synchronized (x509Lock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509Ptr,
() -> "entering setSubjectKeyId(byte[])");
ret = X509_set_subject_key_id(this.x509Ptr, skid);
}
if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException(
"Error setting subject key id into native WOLFSSL_X509 " +
"(ret: " + ret + ")");
}
}
/**
* Sets the Subject Key Identifier extension for this WolfSSLCertificate
* using the public key currently set in the certificate.
*
* @throws IllegalStateException if WolfSSLCertificate has been freed.
* @throws WolfSSLException if native JNI error occurs.
*/
public void setSubjectKeyIdEx()
throws IllegalStateException, WolfSSLException {
int ret = 0;
confirmObjectIsActive();
synchronized (x509Lock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509Ptr,
() -> "entering setSubjectKeyIdEx()");
ret = X509_set_subject_key_id_ex(this.x509Ptr);
}
if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException(
"Error setting subject key id (ex) into native WOLFSSL_X509 " +
"(ret: " + ret + ")");
}
}
/**
* Sets the Authority Key Identifier extension for this WolfSSLCertificate,
* used when generating X509v3 certificates.
*
* @param akid Byte array containing Authority Key Identifier.
*
* @throws IllegalStateException if WolfSSLCertificate has been freed.
* @throws WolfSSLException if invalid arguments or native JNI error occurs.
*/
public void setAuthorityKeyId(byte[] akid)
throws IllegalStateException, WolfSSLException {
int ret = 0;
confirmObjectIsActive();
if (akid == null || akid.length == 0) {
throw new WolfSSLException(
"Authority Key Identifier is null/empty");
}
synchronized (x509Lock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509Ptr,
() -> "entering setAuthorityKeyId(byte[])");
ret = X509_set_authority_key_id(this.x509Ptr, akid);
}
if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException(
"Error setting authority key id into native WOLFSSL_X509 " +
"(ret: " + ret + ")");
}
}
/**
* Sets the Authority Key Identifier extension for this WolfSSLCertificate
* using the issuer certificate.
*
* @param issuer Issuer certificate used to derive the Authority Key ID.
*
* @throws IllegalStateException if WolfSSLCertificate has been freed.
* @throws WolfSSLException if invalid arguments or native JNI error occurs.
*/
public void setAuthorityKeyIdEx(WolfSSLCertificate issuer)
throws IllegalStateException, WolfSSLException {
int ret = 0;
long issuerX509Ptr;
confirmObjectIsActive();
if (issuer == null) {
throw new WolfSSLException("Issuer certificate is null");
}
issuerX509Ptr = issuer.getX509Ptr();
synchronized (x509Lock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509Ptr,
() -> "entering setAuthorityKeyIdEx(issuerPtr=" +
issuerX509Ptr + ")");
ret = X509_set_authority_key_id_ex(this.x509Ptr,
issuerX509Ptr);
}
if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException(
"Error setting authority key id (ex) into native " +
"WOLFSSL_X509 (ret: " + ret + ")");
}
}
/**
* Sets the CRL Distribution Points extension from DER data.
*
* @param der DER-encoded CRL Distribution Points extension.
*
* @throws IllegalStateException if WolfSSLCertificate has been freed.
* @throws WolfSSLException if invalid arguments or native JNI error occurs.
*/
public void setCrlDistPoints(byte[] der)
throws IllegalStateException, WolfSSLException {
int ret = 0;
confirmObjectIsActive();
if (der == null || der.length == 0) {
throw new WolfSSLException("CRL dist points DER is null/empty");
}
synchronized (x509Lock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509Ptr,
() -> "entering setCrlDistPoints(byte[])");
ret = X509_CRL_set_dist_points(this.x509Ptr, der);
}
if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException(
"Error setting CRL dist points into native WOLFSSL_X509 " +
"(ret: " + ret + ")");
}
}
/**
* Adds a CRL Distribution Point URI.
*
* @param uri URI string of the distribution point.
* @param critical Whether to mark the extension critical.
*
* @throws IllegalStateException if WolfSSLCertificate has been freed.
* @throws WolfSSLException if invalid arguments or native JNI error occurs.
*/
public void addCrlDistPoint(String uri, boolean critical)
throws IllegalStateException, WolfSSLException {
int ret = 0;
confirmObjectIsActive();
if (uri == null || uri.isEmpty()) {
throw new WolfSSLException("CRL dist point URI is null/empty");
}
synchronized (x509Lock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509Ptr,
() -> "entering addCrlDistPoint(" + uri + ", critical: " +
critical + ")");
ret = X509_CRL_add_dist_point(this.x509Ptr, uri, critical);
}
if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException(
"Error adding CRL dist point into native WOLFSSL_X509 " +
"(ret: " + ret + ")");
}
}
/**
* Sets the Netscape Certificate Type extension.
*
* @param nsCertType Netscape cert type bitmask.
*
* @throws IllegalStateException if WolfSSLCertificate has been freed.
* @throws WolfSSLException if native JNI error occurs.
*/
public void setNsCertType(int nsCertType)
throws IllegalStateException, WolfSSLException {
int ret = 0;
confirmObjectIsActive();
synchronized (x509Lock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509Ptr,
() -> "entering setNsCertType(" + nsCertType + ")");
ret = X509_set_ns_cert_type(this.x509Ptr, nsCertType);
}
if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException(
"Error setting ns cert type into native WOLFSSL_X509 " +
"(ret: " + ret + ")");
}
}
/**
* Sets the notBefore date for this WolfSSLCertificate, used when
* generating X509v3 certificates.
*
* @param notBefore Date object representing notBefore date/time to set
*
* @throws IllegalStateException if WolfSSLCertificate has been freed.
* @throws WolfSSLException if invalid arguments or native JNI error occurs.
*/
public void setNotBefore(Date notBefore)