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