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
|
Description: Use reflection to access internal X509 API
sun.security.x509 has breaking API changes between Java 17 and Java 21.
Use reflection to access internal API until .M27 release of Apache DS that
switches to Bouncycastle API for certificate manipulation.
Author: Vladimir Petko <vladimir.petko@canonical.com>
Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1052589
Forwarded: not-needed
Last-Update: 2023-11-20
--- a/core/src/main/java/org/apache/directory/server/core/security/CertificateUtil.java
+++ b/core/src/main/java/org/apache/directory/server/core/security/CertificateUtil.java
@@ -63,6 +63,304 @@
import sun.security.x509.X500Name;
import sun.security.x509.X509CertImpl;
import sun.security.x509.X509CertInfo;
+import sun.security.x509.Extension;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+final class X509ImplFactory
+{
+ private static Method NEW_SIGNED = null;
+ private static Method SIGN = null;
+
+ static
+ {
+ try
+ {
+ NEW_SIGNED = X509CertImpl.class.getMethod( "newSigned", X509CertInfo.class, java.security.PrivateKey.class, String.class );
+ }
+ catch ( NoSuchMethodException | SecurityException e )
+ {
+ try
+ {
+ SIGN = X509CertImpl.class.getMethod( "sign", java.security.PrivateKey.class, String.class );
+ }
+ catch ( NoSuchMethodException | SecurityException ex )
+ {
+ throw new RuntimeException( ex );
+ }
+ }
+ }
+
+ private X509ImplFactory( )
+ {
+ }
+
+ public static X509CertImpl create( X509CertInfo info, java.security.PrivateKey pk, String algo )
+ {
+ try
+ {
+ if ( NEW_SIGNED == null )
+ {
+ X509CertImpl impl = X509CertImpl.class.getConstructor( X509CertInfo.class ).newInstance( info );
+ SIGN.invoke( impl, pk, algo );
+ return impl;
+ }
+ return ( X509CertImpl ) NEW_SIGNED.invoke( null, info, pk, algo );
+ }
+ catch ( IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException | NoSuchMethodException | SecurityException e )
+ {
+ throw new RuntimeException( e );
+ }
+ }
+}
+
+class CertificateExtensionsProxy
+{
+ private static Method SET = null;
+ private static Method SET_EXTENSION = null;
+ private CertificateExtensions _ext = null;
+ static
+ {
+ try
+ {
+ SET = CertificateExtensions.class.getMethod( "set", String.class, Object.class );
+ }
+ catch ( NoSuchMethodException | SecurityException e )
+ {
+ try
+ {
+ SET_EXTENSION = CertificateExtensions.class.getMethod( "setExtension", String.class, Extension.class );
+ }
+ catch ( NoSuchMethodException | SecurityException ex )
+ {
+ throw new RuntimeException( ex );
+ }
+ }
+ }
+
+ CertificateExtensionsProxy( CertificateExtensions ext )
+ {
+ _ext = ext;
+ }
+
+ public void set( String name, Extension o )
+ {
+ try
+ {
+ if ( SET == null )
+ {
+ SET_EXTENSION.invoke( _ext, name, o );
+ }
+ else
+ {
+ SET.invoke( _ext, name, o );
+ }
+ }
+ catch ( IllegalAccessException | IllegalArgumentException | InvocationTargetException e )
+ {
+ throw new RuntimeException( e );
+ }
+ }
+}
+
+class X509CertInfoProxy
+{
+ private static Method SET = null;
+ private static Method SET_VERSION = null;
+ private static Method SET_SERIAL_NUMBER = null;
+ private static Method SET_ALGORITHM_ID = null;
+ private static Method SET_ISSUER = null;
+ private static Method SET_VALIDITY = null;
+ private static Method SET_SUBJECT = null;
+ private static Method SET_KEY = null;
+ private static Method SET_EXTENSIONS = null;
+ private X509CertInfo _info = null;
+
+ static
+ {
+ Class clazz = X509CertInfo.class;
+ try
+ {
+ SET = clazz.getMethod( "set", String.class, Object.class );
+ }
+ catch ( NoSuchMethodException | SecurityException e )
+ {
+ try
+ {
+ SET_VERSION = clazz.getMethod( "setVersion", CertificateVersion.class );
+ SET_SERIAL_NUMBER = clazz.getMethod( "setSerialNumber", CertificateSerialNumber.class );
+ SET_ALGORITHM_ID = clazz.getMethod( "setAlgorithmId", CertificateAlgorithmId.class );
+ SET_ISSUER = clazz.getMethod( "setIssuer", X500Name.class );
+ SET_VALIDITY = clazz.getMethod( "setValidity", CertificateValidity.class );
+ SET_SUBJECT = clazz.getMethod( "setSubject", X500Name.class );
+ SET_KEY = clazz.getMethod( "setKey", CertificateX509Key.class );
+ SET_EXTENSIONS = clazz.getMethod( "setExtensions", CertificateExtensions.class );
+ }
+ catch ( NoSuchMethodException | SecurityException ex )
+ {
+ throw new RuntimeException( ex );
+ }
+ }
+ }
+
+ X509CertInfoProxy( X509CertInfo info )
+ {
+ _info = info;
+ }
+
+ public void setVersion( CertificateVersion v )
+ {
+ try
+ {
+ if ( SET != null )
+ {
+ SET.invoke( _info, X509CertInfo.VERSION, v );
+ }
+ else
+ {
+ SET_VERSION.invoke( _info, v );
+ }
+ }
+ catch ( InvocationTargetException | IllegalAccessException e )
+ {
+ throw new RuntimeException( e );
+ }
+ }
+
+ public void setSerialNumber( CertificateSerialNumber sn )
+ {
+ try
+ {
+ if ( SET != null )
+ {
+ SET.invoke( _info, X509CertInfo.SERIAL_NUMBER, sn );
+ }
+ else
+ {
+ SET_SERIAL_NUMBER.invoke( _info, sn );
+ }
+ }
+ catch ( InvocationTargetException | IllegalAccessException e )
+ {
+ throw new RuntimeException( e );
+ }
+ }
+
+ public void setAlgorithmId( CertificateAlgorithmId id )
+ {
+ try
+ {
+ if ( SET != null )
+ {
+ SET.invoke( _info, X509CertInfo.ALGORITHM_ID, id );
+ }
+ else
+ {
+ SET_ALGORITHM_ID.invoke( _info, id );
+ }
+ }
+ catch ( InvocationTargetException | IllegalAccessException e )
+ {
+ throw new RuntimeException( e );
+ }
+ }
+
+ public void setIssuer( X500Name id )
+ {
+ try
+ {
+ if ( SET != null )
+ {
+ SET.invoke( _info, X509CertInfo.ISSUER, id );
+ }
+ else
+ {
+ SET_ISSUER.invoke( _info, id );
+ }
+ }
+ catch ( InvocationTargetException | IllegalAccessException e )
+ {
+ throw new RuntimeException( e );
+ }
+ }
+
+ public void setValidity( CertificateValidity id )
+ {
+ try
+ {
+ if ( SET != null )
+ {
+ SET.invoke( _info, X509CertInfo.VALIDITY, id );
+ }
+ else
+ {
+ SET_VALIDITY.invoke( _info, id );
+ }
+ }
+ catch ( InvocationTargetException | IllegalAccessException e )
+ {
+ throw new RuntimeException( e );
+ }
+ }
+
+ public void setSubject( X500Name id )
+ {
+ try
+ {
+ if ( SET != null )
+ {
+ SET.invoke( _info, X509CertInfo.SUBJECT, id );
+ }
+ else
+ {
+ SET_SUBJECT.invoke( _info, id );
+ }
+ }
+ catch ( InvocationTargetException | IllegalAccessException e )
+ {
+ throw new RuntimeException( e );
+ }
+ }
+
+ public void setKey( CertificateX509Key id )
+ {
+ try
+ {
+ if ( SET != null )
+ {
+ SET.invoke( _info, X509CertInfo.KEY, id );
+ }
+ else
+ {
+ SET_KEY.invoke( _info, id );
+ }
+ }
+ catch ( InvocationTargetException | IllegalAccessException e )
+ {
+ throw new RuntimeException( e );
+ }
+ }
+
+ public void setExtensions( CertificateExtensions id )
+ {
+ try
+ {
+ if ( SET != null )
+ {
+ SET.invoke( _info, X509CertInfo.EXTENSIONS, id );
+ }
+ else
+ {
+ SET_EXTENSIONS.invoke( _info, id );
+ }
+ }
+ catch ( InvocationTargetException | IllegalAccessException e )
+ {
+ throw new RuntimeException( e );
+ }
+ }
+}
/**
* Helper class used to generate self-signed certificates, and load a KeyStore
@@ -90,19 +388,21 @@
Date to = new Date( from.getTime() + days * 86_400_000L );
CertificateValidity interval = new CertificateValidity( from, to );
+ X509CertInfoProxy proxy = new X509CertInfoProxy( info );
+
// Feed the certificate info structure
// version [0] EXPLICIT Version DEFAULT v1
// Version ::= INTEGER { v1(0), v2(1), v3(2) }
- info.set( X509CertInfo.VERSION, new CertificateVersion( CertificateVersion.V3 ) );
+ proxy.setVersion( new CertificateVersion( CertificateVersion.V3 ) );
// serialNumber CertificateSerialNumber
// CertificateSerialNumber ::= INTEGER
BigInteger serialNumber = new BigInteger( 64, new SecureRandom() );
- info.set( X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber( serialNumber ) );
+ proxy.setSerialNumber( new CertificateSerialNumber( serialNumber ) );
// signature AlgorithmIdentifier
AlgorithmId algo = AlgorithmId.get( algoStr );
- info.set( X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId( algo ) );
+ proxy.setAlgorithmId( new CertificateAlgorithmId( algo ) );
// issuer Name
// Name ::= CHOICE {
@@ -115,13 +415,13 @@
// value AttributeValue }
// AttributeType ::= OBJECT IDENTIFIER
// AttributeValue ::= ANY DEFINED BY AttributeType
- info.set( X509CertInfo.ISSUER, issuer );
+ proxy.setIssuer( issuer );
// validity Validity,
// Validity ::= SEQUENCE {
// notBefore Time,
// notAfter Time }
- info.set( X509CertInfo.VALIDITY, interval );
+ proxy.setValidity( interval );
// subject Name
// Name ::= CHOICE {
@@ -134,17 +434,17 @@
// value AttributeValue }
// AttributeType ::= OBJECT IDENTIFIER
// AttributeValue ::= ANY DEFINED BY AttributeType
- info.set( X509CertInfo.SUBJECT, subject );
+ proxy.setSubject( subject );
// subjectPublicKeyInfo SubjectPublicKeyInfo,
// SubjectPublicKeyInfo ::= SEQUENCE {
// algorithm AlgorithmIdentifier,
// subjectPublicKey BIT STRING }
- info.set( X509CertInfo.KEY, new CertificateX509Key( keyPair.getPublic() ) );
+ proxy.setKey( new CertificateX509Key( keyPair.getPublic() ) );
// Extensions. Basically, a subjectAltName and a Basic-Constraint
CertificateExtensions extensions = new CertificateExtensions();
-
+ CertificateExtensionsProxy extProxy = new CertificateExtensionsProxy(extensions);
// SubjectAltName
GeneralNames names = new GeneralNames();
names.add( new GeneralName( new DNSName( InetAddress.getLocalHost().getHostName() ) ) );
@@ -158,17 +458,16 @@
// DerValue.tag_IA5String, "*.apache.org" ) ) ) );
SubjectAlternativeNameExtension subjectAltName = new SubjectAlternativeNameExtension( names );
- extensions.set( subjectAltName.getExtensionId().toString(), subjectAltName );
+ extProxy.set( subjectAltName.getExtensionId().toString(), subjectAltName );
// The Basic-Constraint,
BasicConstraintsExtension basicConstraint = new BasicConstraintsExtension( CRITICAL, isCA, -1 );
- extensions.set( basicConstraint.getExtensionId().toString(), basicConstraint );
+ extProxy.set( basicConstraint.getExtensionId().toString(), basicConstraint );
// Inject the extensions into the cert
- info.set( X509CertInfo.EXTENSIONS, extensions );
+ proxy.setExtensions( extensions );
}
-
-
+
/**
* Create a self signed certificate
*
@@ -194,8 +493,7 @@
setInfo( info, issuer, issuer, keyPair, days, algoStr, SELF_SIGNED );
// Sign the cert to identify the algorithm that's used.
- X509CertImpl certificate = new X509CertImpl( info );
- certificate.sign( keyPair.getPrivate(), algoStr );
+ X509CertImpl certificate = X509ImplFactory.create(info, keyPair.getPrivate(), algoStr);
return certificate;
}
@@ -226,8 +524,7 @@
setInfo( info, subject, issuer, keyPair, days, algoStr, CA_SIGNED );
// Sign the cert to identify the algorithm that's used.
- X509CertImpl certificate = new X509CertImpl( info );
- certificate.sign( keyPair.getPrivate(), algoStr );
+ X509CertImpl certificate = X509ImplFactory.create(info, keyPair.getPrivate(), algoStr);
return certificate;
}
|