-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathWolfSSLX509Name.java
More file actions
637 lines (515 loc) · 19.3 KB
/
WolfSSLX509Name.java
File metadata and controls
637 lines (515 loc) · 19.3 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
/* WolfSSLX509Name.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.nio.charset.StandardCharsets;
/**
* WolfSSLX509Name class, wraps native WOLFSSL_X509_NAME functionality.
*/
public class WolfSSLX509Name {
private boolean active = false;
private long x509NamePtr = 0;
/* Lock around active state */
private final Object stateLock = new Object();
/* Lock around x509NamePtr pointer access */
private final Object x509NameLock = new Object();
/* Cache name elements in Java before pushing through JNI, for easier
* retrieval from getXXX() methods */
private String countryName = null;
private String stateOrProvinceName = null;
private String streetAddress = null;
private String localityName = null;
private String surname = null;
private String commonName = null;
private String emailAddress = null;
private String organizationName = null;
private String organizationalUnitName = null;
private String postalCode = null;
private String userId = null;
/* Encoding types, matched to native define values */
private static final int MBSTRING_UTF8 = 0x100;
/* Native JNI methods */
static native long X509_NAME_new();
static native void X509_NAME_free(long x509Name);
static native int X509_NAME_add_entry_by_txt(long x509Name, String field,
int type, byte[] entry, int len, int loc, int set);
/**
* Create new empty WolfSSLX509Name object.
*
* @throws WolfSSLException if native API call fails.
*/
public WolfSSLX509Name() throws WolfSSLException {
x509NamePtr = X509_NAME_new();
if (x509NamePtr == 0) {
throw new WolfSSLException("Failed to create WolfSSLX509Name");
}
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, x509NamePtr,
() -> "creating new WolfSSLX509Name");
synchronized (stateLock) {
this.active = true;
}
}
/**
* Verifies that the current WolfSSLX509Name object is active.
*
* @throws IllegalStateException if object has been freed
*/
private void confirmObjectIsActive()
throws IllegalStateException {
synchronized (stateLock) {
if (this.active == false) {
throw new IllegalStateException(
"WolfSSLX509Name object has been freed");
}
}
}
/**
* For package use only, return native WOLFSSL_X509_NAME pointer.
*
* @return native WOLFSSL_X509_POINTER value
* @throws IllegalStateException if WolfSSLX509Name has been freed.
*/
protected long getNativeX509NamePtr() throws IllegalStateException {
confirmObjectIsActive();
/* TODO lock around x509NamePtr for caller use */
synchronized (x509NameLock) {
return this.x509NamePtr;
}
}
/**
* Private helper function to call native JNI function
* X509_NAME_add_entry_by_txt().
*
* @param field String containing field name to set, for example
* "countryName"
* @param entry String value to store into field
*
* @throws WolfSSLException if arguments are invalid or error occurs
* with native JNI call.
*/
private synchronized void addEntryByTxt(String field, String entry)
throws WolfSSLException {
int ret = 0;
byte[] entryBytes = null;
if (field == null || entry == null) {
throw new WolfSSLException(
"field or entry is null in addEntryByTxt()");
}
synchronized (x509NameLock) {
entryBytes = entry.getBytes(StandardCharsets.UTF_8);
ret = X509_NAME_add_entry_by_txt(this.x509NamePtr, field,
MBSTRING_UTF8, entryBytes, entryBytes.length, -1, 0);
}
if (ret != WolfSSL.SSL_SUCCESS) {
throw new WolfSSLException("Error setting " + field +
" into WolfSSLX509Name (error: " + ret + ")");
}
}
/**
* Set country name for this name object.
*
* @param countryName String containing country name to be set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
* @throws WolfSSLException if native JNI error has occurred, or input
* argument is invalid.
*/
public synchronized void setCountryName(String countryName)
throws IllegalStateException, WolfSSLException {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered setCountryName(" + countryName + ")");
addEntryByTxt("countryName", countryName);
this.countryName = countryName;
}
/**
* Set state or province name for this name object.
*
* @param name String containing state or province name to be set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
* @throws WolfSSLException if native JNI error has occurred, or input
* argument is invalid.
*/
public synchronized void setStateOrProvinceName(String name)
throws IllegalStateException, WolfSSLException {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered setStateOrProvinceName(" + name + ")");
addEntryByTxt("stateOrProvinceName", name);
this.stateOrProvinceName = name;
}
/**
* Set street address for this name object.
*
* @param address String containing street address to be set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
* @throws WolfSSLException if native JNI error has occurred, or input
* argument is invalid.
*/
public synchronized void setStreetAddress(String address)
throws IllegalStateException, WolfSSLException {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered setStreetAddress(" + address + ")");
addEntryByTxt("streetAddress", address);
this.streetAddress = address;
}
/**
* Set locality name / city for this name object.
*
* @param name String containing locality name to be set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
* @throws WolfSSLException if native JNI error has occurred, or input
* argument is invalid.
*/
public synchronized void setLocalityName(String name)
throws IllegalStateException, WolfSSLException {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered setLocalityName(" + name + ")");
addEntryByTxt("localityName", name);
this.localityName = name;
}
/**
* Set surname for this name object.
*
* @param name String containing surname to be set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
* @throws WolfSSLException if native JNI error has occurred, or input
* argument is invalid.
*/
public synchronized void setSurname(String name)
throws IllegalStateException, WolfSSLException {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered setSurname(" + name + ")");
addEntryByTxt("surname", name);
this.surname = name;
}
/**
* Set common name for this name object.
*
* @param name String containing common name to be set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
* @throws WolfSSLException if native JNI error has occurred, or input
* argument is invalid.
*/
public synchronized void setCommonName(String name)
throws IllegalStateException, WolfSSLException {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered setCommonName(" + name + ")");
addEntryByTxt("commonName", name);
this.commonName = name;
}
/**
* Set email address for this name object.
*
* @param email String containing email address to be set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
* @throws WolfSSLException if native JNI error has occurred, or input
* argument is invalid.
*/
public synchronized void setEmailAddress(String email)
throws IllegalStateException, WolfSSLException {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered setEmailAddress(" + email + ")");
addEntryByTxt("emailAddress", email);
this.emailAddress = email;
}
/**
* Set organization name for this name object.
*
* @param name String containing organization name to be set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
* @throws WolfSSLException if native JNI error has occurred, or input
* argument is invalid.
*/
public synchronized void setOrganizationName(String name)
throws IllegalStateException, WolfSSLException {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered setOrganizationName(" + name + ")");
addEntryByTxt("organizationName", name);
this.organizationName = name;
}
/**
* Set organizational unit name for this name object.
*
* @param name String containing organizational unit name to be set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
* @throws WolfSSLException if native JNI error has occurred, or input
* argument is invalid.
*/
public synchronized void setOrganizationalUnitName(String name)
throws IllegalStateException, WolfSSLException {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered setOrganizationalUnitName(" + name + ")");
addEntryByTxt("organizationalUnitName", name);
this.organizationalUnitName = name;
}
/**
* Set postal code for this name object.
*
* @param code String containing postal code to be set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
* @throws WolfSSLException if native JNI error has occurred, or input
* argument is invalid.
*/
public synchronized void setPostalCode(String code)
throws IllegalStateException, WolfSSLException {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered setPostalCode(" + code + ")");
addEntryByTxt("postalCode", code);
this.postalCode = code;
}
/**
* Set user ID for this name object.
*
* @param id String containing user ID to be set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
* @throws WolfSSLException if native JNI error has occurred, or input
* argument is invalid.
*/
public synchronized void setUserId(String id)
throws IllegalStateException, WolfSSLException {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered setUserId(" + id + ")");
addEntryByTxt("userId", id);
this.userId = id;
}
/**
* Get country name set in this object.
*
* @return country name string, or null if not yet set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
*/
public synchronized String getCountryName() {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered getCountryName()");
return this.countryName;
}
/**
* Get state or province name set in this object.
*
* @return state or province name string, or null if not yet set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
*/
public synchronized String getStateOrProvinceName() {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered getStateOrProvinceName()");
return this.stateOrProvinceName;
}
/**
* Get street address set in this object.
*
* @return street address string, or null if not yet set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
*/
public synchronized String getStreetAddress() {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered getStreetAddress()");
return this.streetAddress;
}
/**
* Get locality name set in this object.
*
* @return locality name string, or null if not yet set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
*/
public synchronized String getLocalityName() {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered getLocalityName()");
return this.localityName;
}
/**
* Get surname set in this object.
*
* @return surname string, or null if not yet set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
*/
public synchronized String getSurname() {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered getSurname()");
return this.surname;
}
/**
* Get common name set in this object.
*
* @return common name string, or null if not yet set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
*/
public synchronized String getCommonName() {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered getCommonName()");
return this.commonName;
}
/**
* Get email address set in this object.
*
* @return email address string, or null if not yet set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
*/
public synchronized String getEmailAddress() {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered getEmailAddress()");
return this.emailAddress;
}
/**
* Get organization name set in this object.
*
* @return organization name string, or null if not yet set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
*/
public synchronized String getOrganizationName() {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered getOrganizationName()");
return this.organizationName;
}
/**
* Get organizational unit name set in this object.
*
* @return organizational unit name string, or null if not yet set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
*/
public synchronized String getOrganizationalUnitName() {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered getOrganizationalUnitName()");
return this.organizationalUnitName;
}
/**
* Get postal code set in this object.
*
* @return postal code string, or null if not yet set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
*/
public synchronized String getPostalCode() {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered getPostalCode()");
return this.postalCode;
}
/**
* Get user ID set in this object.
*
* @return user ID string, or null if not yet set
*
* @throws IllegalStateException if WolfSSLX509Name has been freed.
*/
public synchronized String getUserId() {
confirmObjectIsActive();
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered getUserId()");
return this.userId;
}
@Override
public String toString() {
synchronized (stateLock) {
if (this.active == false) {
return "";
}
}
/* TODO: wrap wolfSSL_X509_NAME_oneline() */
return "";
}
/**
* Free native resources of WolfSSLX509Name.
*/
public synchronized void free() {
synchronized (stateLock) {
if (this.active == false) {
/* already freed, just return */
return;
}
synchronized (x509NameLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.x509NamePtr,
() -> "entered free()");
/* free native resources */
X509_NAME_free(this.x509NamePtr);
this.active = false;
this.x509NamePtr = 0;
}
}
}
@SuppressWarnings({"deprecation", "removal"})
@Override
protected void finalize() throws Throwable
{
this.free();
super.finalize();
}
}