001/*
002 * Copyright 2022-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2022-2024 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2022-2024 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.ldap.sdk.unboundidds.logs.v2.json;
037
038
039
040import java.util.ArrayList;
041import java.util.Collections;
042import java.util.List;
043
044import com.unboundid.ldap.sdk.unboundidds.logs.AccessLogMessageType;
045import com.unboundid.ldap.sdk.unboundidds.logs.LogException;
046import com.unboundid.ldap.sdk.unboundidds.logs.v2.
047            ClientCertificateAccessLogMessage;
048import com.unboundid.util.NotMutable;
049import com.unboundid.util.NotNull;
050import com.unboundid.util.Nullable;
051import com.unboundid.util.ThreadSafety;
052import com.unboundid.util.ThreadSafetyLevel;
053import com.unboundid.util.json.JSONObject;
054import com.unboundid.util.json.JSONValue;
055
056
057
058/**
059 * This class provides a data structure that holds information about a
060 * JSON-formatted client certificate access log message.
061 * <BR>
062 * <BLOCKQUOTE>
063 *   <B>NOTE:</B>  This class, and other classes within the
064 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
065 *   supported for use against Ping Identity, UnboundID, and
066 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
067 *   for proprietary functionality or for external specifications that are not
068 *   considered stable or mature enough to be guaranteed to work in an
069 *   interoperable way with other types of LDAP servers.
070 * </BLOCKQUOTE>
071 */
072@NotMutable()
073@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
074public final class JSONClientCertificateAccessLogMessage
075       extends JSONAccessLogMessage
076       implements ClientCertificateAccessLogMessage
077{
078  /**
079   * The serial version UID for this serializable class.
080   */
081  private static final long serialVersionUID = -7815846275883142789L;
082
083
084
085  // The peer certificate chain for this log message.
086  @NotNull private final List<JSONCertificate> peerCertificateChain;
087
088  // The auto-authenticated as DN for this log message.
089  @Nullable private final String autoAuthenticatedAsDN;
090
091
092
093  /**
094   * Creates a new JSON client certificate access log message from the provided
095   * JSON object.
096   *
097   * @param  jsonObject  The JSON object that contains an encoded representation
098   *                     of this log message.  It must not be {@code null}.
099   *
100   * @throws  LogException  If the provided JSON object cannot be parsed as a
101   *                        valid log message.
102   */
103  public JSONClientCertificateAccessLogMessage(
104              @NotNull final JSONObject jsonObject)
105         throws LogException
106  {
107    super(jsonObject);
108
109    autoAuthenticatedAsDN =
110         getString(JSONFormattedAccessLogFields.AUTO_AUTHENTICATED_AS);
111
112    final List<JSONValue> certValues = jsonObject.getFieldAsArray(
113         JSONFormattedAccessLogFields.PEER_CERTIFICATE_CHAIN.getFieldName());
114    if (certValues == null)
115    {
116      peerCertificateChain = Collections.emptyList();
117    }
118    else
119    {
120      final List<JSONCertificate> certList = new ArrayList<>(certValues.size());
121      for (final JSONValue v : certValues)
122      {
123        if (v instanceof JSONObject)
124        {
125          certList.add(new JSONCertificate((JSONObject) v));
126        }
127        else
128        {
129          certList.clear();
130          break;
131        }
132      }
133
134      peerCertificateChain = Collections.unmodifiableList(certList);
135    }
136  }
137
138
139
140  /**
141   * {@inheritDoc}
142   */
143  @Override()
144  @NotNull()
145  public AccessLogMessageType getMessageType()
146  {
147    return AccessLogMessageType.CLIENT_CERTIFICATE;
148  }
149
150
151
152  /**
153   * Retrieves the peer certificate chain for this log message.
154   *
155   * @return  The peer certificate chain for this log message, or {@code null}
156   *          if it is not included in the log message.
157   */
158  @NotNull()
159  public List<JSONCertificate> getPeerCertificateChain()
160  {
161    return peerCertificateChain;
162  }
163
164
165
166  /**
167   * {@inheritDoc}
168   */
169  @Override()
170  @Nullable()
171  public String getPeerSubjectDN()
172  {
173    if (peerCertificateChain.isEmpty())
174    {
175      return null;
176    }
177    else
178    {
179      return peerCertificateChain.get(0).getSubjectDN();
180    }
181  }
182
183
184
185  /**
186   * {@inheritDoc}
187   */
188  @Override()
189  @NotNull()
190  public List<String> getIssuerSubjectDNs()
191  {
192    final List<String> issuerSubjectDNs = new ArrayList<>();
193    for (final JSONCertificate c : peerCertificateChain)
194    {
195      final String issuerSubjectDN = c.getIssuerSubjectDN();
196      if (issuerSubjectDN == null)
197      {
198        issuerSubjectDNs.clear();
199        break;
200      }
201
202      if (! issuerSubjectDNs.contains(issuerSubjectDN))
203      {
204        issuerSubjectDNs.add(issuerSubjectDN);
205      }
206    }
207
208    return Collections.unmodifiableList(issuerSubjectDNs);
209  }
210
211
212
213  /**
214   * {@inheritDoc}
215   */
216  @Override()
217  @Nullable()
218  public String getAutoAuthenticatedAsDN()
219  {
220    return autoAuthenticatedAsDN;
221  }
222}