001/*
002 * Copyright 2017-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2017-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) 2017-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.util.ssl.cert;
037
038
039
040import java.math.BigInteger;
041
042import com.unboundid.asn1.ASN1BigInteger;
043import com.unboundid.asn1.ASN1BitString;
044import com.unboundid.asn1.ASN1Element;
045import com.unboundid.asn1.ASN1Sequence;
046import com.unboundid.util.Debug;
047import com.unboundid.util.NotMutable;
048import com.unboundid.util.NotNull;
049import com.unboundid.util.StaticUtils;
050import com.unboundid.util.ThreadSafety;
051import com.unboundid.util.ThreadSafetyLevel;
052
053import static com.unboundid.util.ssl.cert.CertMessages.*;
054
055
056
057/**
058 * This class provides a data structure for representing the information
059 * contained in an RSA public key in an X.509 certificate.  As per
060 * <A HREF="https://www.ietf.org/rfc/rfc8017.txt">RFC 8017</A> section A.1.1,
061 * an RSA public key is identified by OID 1.2.840.113549.1.1.1 and the value is
062 * encoded as follows:
063 * <PRE>
064 *   RSAPublicKey ::= SEQUENCE {
065 *      modulus            INTEGER,    -- n
066 *      publicExponent     INTEGER  }  -- e
067 * </PRE>
068 */
069@NotMutable()
070@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
071public final class RSAPublicKey
072       extends DecodedPublicKey
073{
074  /**
075   * The serial version UID for this serializable class.
076   */
077  private static final long serialVersionUID = 1837190736740174338L;
078
079
080
081  // The modulus for the RSA public key.
082  @NotNull private final BigInteger modulus;
083
084  // The public exponent for the RSA public key.
085  @NotNull private final BigInteger publicExponent;
086
087
088
089  /**
090   * Creates a new RSA public key with the provided information.
091   *
092   * @param  modulus         The modulus for this RSA public key.  It must not
093   *                         be {@code null}.
094   * @param  publicExponent  The public exponent for this RSA public key.  It
095   *                         must not be {@code null}.
096   */
097  RSAPublicKey(@NotNull final BigInteger modulus,
098               @NotNull final BigInteger publicExponent)
099  {
100    this.modulus = modulus;
101    this.publicExponent = publicExponent;
102  }
103
104
105
106  /**
107   * Creates a new RSA decoded public key from the provided bit string.
108   *
109   * @param  subjectPublicKey  The bit string containing the encoded public key.
110   *
111   * @throws  CertException  If the provided public key cannot be decoded as an
112   *                         RSA public key.
113   */
114  RSAPublicKey(@NotNull final ASN1BitString subjectPublicKey)
115       throws CertException
116  {
117    try
118    {
119      final byte[] keyBytes = subjectPublicKey.getBytes();
120      final ASN1Element[] keyElements =
121           ASN1Sequence.decodeAsSequence(keyBytes).elements();
122      modulus = keyElements[0].decodeAsBigInteger().getBigIntegerValue();
123      publicExponent = keyElements[1].decodeAsBigInteger().getBigIntegerValue();
124    }
125    catch (final Exception e)
126    {
127      Debug.debugException(e);
128      throw new CertException(
129           ERR_RSA_PUBLIC_KEY_CANNOT_DECODE.get(
130                StaticUtils.getExceptionMessage(e)),
131           e);
132    }
133  }
134
135
136
137  /**
138   * Encodes this RSA public key.
139   *
140   * @return  The encoded representation of this RSA public key.
141   */
142  @NotNull()
143  ASN1BitString encode()
144  {
145    final ASN1Sequence publicKeySequence = new ASN1Sequence(
146         new ASN1BigInteger(modulus),
147         new ASN1BigInteger(publicExponent));
148    final boolean[] bits =
149         ASN1BitString.getBitsForBytes(publicKeySequence.encode());
150    return new ASN1BitString(bits);
151  }
152
153
154
155  /**
156   * Retrieves the modulus (n) for the RSA public key.
157   *
158   * @return  The modulus for the RSA public key.
159   */
160  @NotNull()
161  public BigInteger getModulus()
162  {
163    return modulus;
164  }
165
166
167
168  /**
169   * Retrieves the public exponent (e) for the RSA public key.
170   *
171   * @return  The public exponent for the RSA public key.
172   */
173  @NotNull()
174  public BigInteger getPublicExponent()
175  {
176    return publicExponent;
177  }
178
179
180
181  /**
182   * {@inheritDoc}
183   */
184  @Override()
185  public void toString(@NotNull final StringBuilder buffer)
186  {
187    buffer.append("RSAPublicKey(modulus=");
188    StaticUtils.toHex(modulus.toByteArray(), ":", buffer);
189    buffer.append(", publicExponent=");
190    StaticUtils.toHex(publicExponent.toByteArray(), ":", buffer);
191    buffer.append(')');
192  }
193}