@@ -127,7 +127,7 @@ static native int X509_set_pubkey_native_open(long x509Ptr, int keyType,
127127 static native int X509_add_ext_via_nconf_nid (long x509Ptr , int nid ,
128128 String extValue , boolean isCritical );
129129 static native int X509_add_ext_via_set_object_boolean (long x509Ptr ,
130- int nid , boolean extValue , boolean isCritical );
130+ int nid , boolean extValue , boolean isCritical , int pathLen );
131131 static native int X509_set_notBefore (long x509Ptr , long timeSecs );
132132 static native int X509_set_notAfter (long x509Ptr , long timeSecs );
133133 static native int X509_set_serialNumber (long x509Ptr , byte [] serialBytes );
@@ -1195,6 +1195,36 @@ public void addExtension(int nid, String value, boolean isCritical)
11951195 public void addExtension (int nid , boolean value , boolean isCritical )
11961196 throws IllegalStateException , WolfSSLException {
11971197
1198+ addExtension (nid , value , -1 , isCritical );
1199+ }
1200+
1201+ /**
1202+ * Add Basic Constraints extension with CA flag and path length
1203+ * constraint to a WOLFSSL_X509.
1204+ *
1205+ * This method allows setting the Basic Constraints extension with both the
1206+ * CA boolean and an optional path length constraint. The path length limits
1207+ * the number of intermediate CA certificates that may follow this
1208+ * certificate in a valid certification path.
1209+ *
1210+ * To set Basic Constraints without a path length constraint, use
1211+ * {@link #addExtension(int, boolean, boolean)} with
1212+ * {@code WolfSSL.NID_basic_constraints} instead.
1213+ *
1214+ * @param nid NID of extension to add. Must be:
1215+ * WolfSSL.NID_basic_constraints
1216+ * @param value Boolean value of CA flag (true for CA, false for end entity)
1217+ * @param pathLen Maximum number of intermediate CA certificates allowed
1218+ * below this CA. Must be >= 0, or -1 to not set a path length
1219+ * constraint. Only meaningful when value is true.
1220+ * @param isCritical Boolean flag indicating if this extension is critical
1221+ *
1222+ * @throws IllegalStateException if WolfSSLCertificate has been freed
1223+ * @throws WolfSSLException if invalid arguments or on native JNI error.
1224+ */
1225+ public void addExtension (int nid , boolean value , int pathLen ,
1226+ boolean isCritical ) throws IllegalStateException , WolfSSLException {
1227+
11981228 int ret = 0 ;
11991229
12001230 confirmObjectIsActive ();
@@ -1203,20 +1233,33 @@ public void addExtension(int nid, boolean value, boolean isCritical)
12031233 WolfSSLDebug .log (getClass (), WolfSSLDebug .Component .JNI ,
12041234 WolfSSLDebug .INFO , this .x509Ptr ,
12051235 () -> "entering addExtension(nid: " + nid + ", value: " +
1206- value + ", isCritical: " + isCritical + ")" );
1236+ value + ", pathLen: " + pathLen + ", isCritical: " +
1237+ isCritical + ")" );
12071238 }
12081239
12091240 if (nid != WolfSSL .NID_basic_constraints ) {
12101241 throw new WolfSSLException (
12111242 "Unsupported X509v3 extension NID: " + nid );
12121243 }
12131244
1245+ if (pathLen < -1 ) {
1246+ throw new WolfSSLException (
1247+ "Path length must be >= 0 or -1, got: " + pathLen );
1248+ }
1249+
12141250 synchronized (x509Lock ) {
1215- ret = X509_add_ext_via_set_object_boolean (
1216- this . x509Ptr , nid , value , isCritical );
1251+ ret = X509_add_ext_via_set_object_boolean (this . x509Ptr , nid , value ,
1252+ isCritical , pathLen );
12171253 }
12181254
1219- if (ret != WolfSSL .SSL_SUCCESS ) {
1255+ if (ret == WolfSSL .NOT_COMPILED_IN ) {
1256+ throw new WolfSSLException (
1257+ "Basic Constraints with pathLen NOT_COMPILED_IN, " +
1258+ "requires wolfSSL > 5.8.4 or wolfSSL PR 9940 " +
1259+ "patch with WOLFSSL_PR9940_PATCH_APPLIED " +
1260+ "defined (ret: " + ret + ")" );
1261+ }
1262+ else if (ret != WolfSSL .SSL_SUCCESS ) {
12201263 throw new WolfSSLException (
12211264 "Error setting extension into native WOLFSSL_X509 " +
12221265 "(ret: " + ret + ")" );
0 commit comments