mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Valery Kharseko
7 hours ago 0db6a25e6bba295ce7ed840c2da08e8012d72384
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
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions copyright [year] [name of copyright owner]".
 *
 * Copyright 2026 3A Systems, LLC.
 */
 
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.KeyFactory;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.interfaces.RSAPrivateCrtKey;
import java.security.spec.MGF1ParameterSpec;
import java.security.spec.PSSParameterSpec;
import java.security.spec.RSAPrivateCrtKeySpec;
 
/** Re-encodes the fixture's RSA private key as RSA-PSS while retaining its certificate. */
public final class ReencodeAdminSecretKey
{
  public static void main(String[] args) throws Exception
  {
    final String alias = "admin-cert";
    final Path storePath = Path.of(args[0]);
    final char[] pin = Files.readString(Path.of(args[1])).stripTrailing().toCharArray();
    final KeyStore store = KeyStore.getInstance("JKS");
    try (InputStream input = Files.newInputStream(storePath))
    {
      store.load(input, pin);
    }
 
    final PSSParameterSpec params;
    if (args.length > 2)
    {
      final String digest = args[2];
      params = new PSSParameterSpec(digest, "MGF1", new MGF1ParameterSpec(digest),
          Integer.parseInt(args[3]), PSSParameterSpec.TRAILER_FIELD_BC);
    }
    else
    {
      params = null;
    }
 
    final RSAPrivateCrtKey rsa = (RSAPrivateCrtKey) store.getKey(alias, pin);
    final RSAPrivateCrtKeySpec spec = new RSAPrivateCrtKeySpec(rsa.getModulus(), rsa.getPublicExponent(),
        rsa.getPrivateExponent(), rsa.getPrimeP(), rsa.getPrimeQ(), rsa.getPrimeExponentP(),
        rsa.getPrimeExponentQ(), rsa.getCrtCoefficient(), params);
    final PrivateKey pss = KeyFactory.getInstance("RSASSA-PSS").generatePrivate(spec);
    store.setKeyEntry(alias, pss, pin, store.getCertificateChain(alias));
    try (OutputStream output = Files.newOutputStream(storePath))
    {
      store.store(output, pin);
    }
  }
}