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

Valery Kharseko
11 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
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
/*
 * 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.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.security.spec.MGF1ParameterSpec;
import java.security.spec.PSSParameterSpec;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
 
import javax.net.ssl.KeyManagerFactory;
 
/** Checks whether a staged keystore can supply the default administration TLS key. */
public final class AdminSecretValidator
{
  private static final String ADMIN_ALIAS = "admin-cert";
  private static final Set<String> TLS_SERVER_USAGES = Set.of(
      "1.3.6.1.5.5.7.3.1", "2.5.29.37.0", // serverAuth and anyExtendedKeyUsage
      "1.3.6.1.4.1.311.10.3.3", "2.16.840.1.113730.4.1"); // Legacy server-gated cryptography, accepted by JSSE.
  // JSSE's TLS 1.2 and 1.3 RSA-PSS schemes use a salt exactly as long as the digest.
  private static final List<PSSParameterSpec> TLS_PSS_PARAMETERS = List.of(
      new PSSParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, 32, PSSParameterSpec.TRAILER_FIELD_BC),
      new PSSParameterSpec("SHA-384", "MGF1", MGF1ParameterSpec.SHA384, 48, PSSParameterSpec.TRAILER_FIELD_BC),
      new PSSParameterSpec("SHA-512", "MGF1", MGF1ParameterSpec.SHA512, 64, PSSParameterSpec.TRAILER_FIELD_BC));
 
  public static void main(String[] args) throws Exception
  {
    final char[] pin;
    try (BufferedReader reader = new BufferedReader(new FileReader(args[1])))
    {
      final String firstLine = reader.readLine();
      if (firstLine == null)
      {
        throw new IllegalArgumentException("The administration PIN file has no first line");
      }
      pin = firstLine.toCharArray();
    }
 
    try
    {
      final KeyStore store = KeyStore.getInstance("JKS");
      try (InputStream input = Files.newInputStream(Path.of(args[0])))
      {
        store.load(input, pin);
      }
 
      if (!store.entryInstanceOf(ADMIN_ALIAS, KeyStore.PrivateKeyEntry.class))
      {
        throw new IllegalArgumentException("The administration keystore has no admin-cert private key");
      }
 
      final KeyManagerFactory factory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
      factory.init(store, pin);
      factory.getKeyManagers();
 
      // The key manager loads every key with the same PIN. Only the configured alias
      // needs to sign, since it is the one selected for administration TLS.
      final PrivateKey key = (PrivateKey) store.getKey(ADMIN_ALIAS, pin);
      verifyAdminCertificate(key, (X509Certificate) store.getCertificate(ADMIN_ALIAS));
    }
    finally
    {
      Arrays.fill(pin, '\0');
    }
  }
 
  private static void verifyAdminCertificate(PrivateKey key, X509Certificate certificate)
      throws GeneralSecurityException
  {
    final PublicKey certificateKey = certificate.getPublicKey();
    // JSSE requires both keys to match the negotiated authentication type.
    if (!key.getAlgorithm().equals(certificateKey.getAlgorithm()))
    {
      throw new GeneralSecurityException("The admin-cert private key and certificate use different algorithms");
    }
    final boolean[] keyUsage = certificate.getKeyUsage();
    if (keyUsage != null && (keyUsage.length == 0 || !keyUsage[0])) // digitalSignature is the first bit.
    {
      throw new CertificateException("The admin-cert certificate does not allow digital signatures");
    }
    final List<String> extendedKeyUsage = certificate.getExtendedKeyUsage();
    if (extendedKeyUsage != null && Collections.disjoint(extendedKeyUsage, TLS_SERVER_USAGES))
    {
      throw new CertificateException("The admin-cert certificate does not allow TLS server authentication");
    }
 
    if ("RSASSA-PSS".equals(key.getAlgorithm()))
    {
      if (!matchesTlsPssSignature(key, certificateKey))
      {
        throw new GeneralSecurityException("The admin-cert key and certificate do not match a TLS RSA-PSS scheme");
      }
    }
    else if (!matchesSignature(signatureFor(key), key, certificateKey))
    {
      throw new GeneralSecurityException("The admin-cert private key does not match its certificate");
    }
  }
 
  private static boolean matchesTlsPssSignature(PrivateKey key, PublicKey certificateKey)
      throws GeneralSecurityException
  {
    for (PSSParameterSpec parameters : TLS_PSS_PARAMETERS)
    {
      final Signature signature = Signature.getInstance("RSASSA-PSS");
      signature.setParameter(parameters);
      try
      {
        if (matchesSignature(signature, key, certificateKey))
        {
          return true;
        }
      }
      catch (InvalidKeyException e)
      {
        // Either key can restrict the digest or the minimum salt length. Try the next TLS scheme.
      }
    }
    return false;
  }
 
  private static boolean matchesSignature(Signature signature, PrivateKey key, PublicKey certificateKey)
      throws GeneralSecurityException
  {
    signature.initSign(key);
    signature.update((byte) 0);
    final byte[] signed = signature.sign();
 
    signature.initVerify(certificateKey);
    signature.update((byte) 0);
    return signature.verify(signed);
  }
 
  private static Signature signatureFor(PrivateKey key) throws GeneralSecurityException
  {
    switch (key.getAlgorithm())
    {
    case "RSA":
      return Signature.getInstance("SHA256withRSA");
    case "EC":
      return Signature.getInstance("SHA256withECDSA");
    case "DSA":
      return Signature.getInstance("SHA256withDSA");
    case "EdDSA":
    case "Ed25519":
    case "Ed448":
      return Signature.getInstance(key.getAlgorithm());
    default:
      throw new GeneralSecurityException("The admin-cert key does not support signing: " + key.getAlgorithm());
    }
  }
}