forked from wolfSSL/wolfssljni
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWolfSSLX509StoreCtx.java
More file actions
115 lines (96 loc) · 3.5 KB
/
WolfSSLX509StoreCtx.java
File metadata and controls
115 lines (96 loc) · 3.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
/* WolfSSLX509StoreCtxjava
*
* Copyright (C) 2006-2025 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;
/**
* WolfSSLX509StoreCtx class
*
* @author wolfSSL Inc.
*/
public class WolfSSLX509StoreCtx {
private boolean active = false;
private long ctxPtr = 0;
/* lock around active state */
private final Object stateLock = new Object();
/* lock around native WOLFSSL_X509_STORE_CTX pointer use */
private final Object ctxLock = new Object();
static native byte[][] X509_STORE_CTX_getDerCerts(long ctxPtr);
/**
* Create new WolfSSLX509StoreCtx object
*
* @param ctxPtr native pointer to WOLFSSL_X509_STORE structure
*
* @throws WolfSSLException if ctxPtr is 0
*/
public WolfSSLX509StoreCtx(long ctxPtr) throws WolfSSLException {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, ctxPtr,
() -> "creating new WolfSSLX509StoreCtx");
if (ctxPtr == 0) {
throw new WolfSSLException(
"Failed to create WolfSSLX509StoreCtx, input ptr was null");
}
this.active = true;
this.ctxPtr = ctxPtr;
}
/**
* Verifies that the current WolfSSLX509StoreCtx object is active.
*
* @throws IllegalStateException if object has been freed
*/
private synchronized void confirmObjectIsActive()
throws IllegalStateException {
synchronized (stateLock) {
if (this.active == false) {
throw new IllegalStateException(
"WolfSSLX509StoreCtx object has been freed");
}
}
}
/**
* Get certificates in WOLFSSL_X509_STORE_CTX as an array of
* WolfSSLCertificate objects.
*
* The certificate chain is returned in order of peer to root, with peer
* first, then any intermediates, then root last (if present).
*
* @return array of certificates
* @throws WolfSSLException on error
* @throws IllegalStateException if object has been freed
*/
public WolfSSLCertificate[] getCerts()
throws WolfSSLException, IllegalStateException {
WolfSSLCertificate[] certs = null;
confirmObjectIsActive();
synchronized (ctxLock) {
WolfSSLDebug.log(getClass(), WolfSSLDebug.Component.JNI,
WolfSSLDebug.INFO, this.ctxPtr, () -> "entering getCerts()");
byte[][] derCerts = X509_STORE_CTX_getDerCerts(this.ctxPtr);
if (derCerts != null) {
certs = new WolfSSLCertificate[derCerts.length];
for (int i = 0; i < derCerts.length; i++) {
byte[] derCert = derCerts[i];
certs[i] = new WolfSSLCertificate(derCert);
}
}
}
return certs;
}
}