1 /*
   2  * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.rsa;
  27 
  28 import java.io.IOException;
  29 import java.math.BigInteger;
  30 
  31 import java.security.*;
  32 import java.security.interfaces.*;
  33 
  34 import sun.security.util.*;
  35 import sun.security.x509.X509Key;
  36 
  37 /**
  38  * Key implementation for RSA public keys.
  39  *
  40  * Note: RSA keys must be at least 512 bits long
  41  *
  42  * @see RSAPrivateCrtKeyImpl
  43  * @see RSAKeyFactory
  44  *
  45  * @since   1.5
  46  * @author  Andreas Sterbenz
  47  */
  48 public final class RSAPublicKeyImpl extends X509Key implements RSAPublicKey {
  49 
  50     private static final long serialVersionUID = 2644735423591199609L;
  51     private static final BigInteger THREE = BigInteger.valueOf(3);
  52 
  53     private BigInteger n;       // modulus
  54     private BigInteger e;       // public exponent
  55 
  56     /**
  57      * Construct a key from its components. Used by the
  58      * RSAKeyFactory and the RSAKeyPairGenerator.
  59      */
  60     public RSAPublicKeyImpl(BigInteger n, BigInteger e)
  61             throws InvalidKeyException {
  62         this.n = n;
  63         this.e = e;
  64         RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e);
  65         checkExponentRange();
  66         // generate the encoding
  67         algid = RSAPrivateCrtKeyImpl.rsaId;
  68         try {
  69             DerOutputStream out = new DerOutputStream();
  70             out.putInteger(n);
  71             out.putInteger(e);
  72             byte[] keyArray =
  73                 new DerValue(DerValue.tag_Sequence,
  74                              out.toByteArray()).toByteArray();
  75             setKey(new BitArray(keyArray.length*8, keyArray));
  76         } catch (IOException exc) {
  77             // should never occur
  78             throw new InvalidKeyException(exc);
  79         }
  80     }
  81 
  82     /**
  83      * Construct a key from its encoding. Used by RSAKeyFactory.
  84      */
  85     public RSAPublicKeyImpl(byte[] encoded) throws InvalidKeyException {
  86         decode(encoded);
  87         RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e);
  88         checkExponentRange();
  89     }
  90 
  91     private void checkExponentRange() throws InvalidKeyException {
  92         // the exponent should be smaller than the modulus
  93         if (e.compareTo(n) >= 0) {
  94             throw new InvalidKeyException("exponent is larger than modulus");
  95         }
  96 
  97         // the exponent should be at least 3
  98         if (e.compareTo(THREE) < 0) {
  99             throw new InvalidKeyException("exponent is smaller than 3");
 100         }
 101     }
 102 
 103     // see JCA doc
 104     public String getAlgorithm() {
 105         return "RSA";
 106     }
 107 
 108     // see JCA doc
 109     public BigInteger getModulus() {
 110         return n;
 111     }
 112 
 113     // see JCA doc
 114     public BigInteger getPublicExponent() {
 115         return e;
 116     }
 117 
 118     /**
 119      * Parse the key. Called by X509Key.
 120      */
 121     protected void parseKeyBits() throws InvalidKeyException {
 122         try {
 123             DerInputStream in = new DerInputStream(getKey().toByteArray());
 124             DerValue derValue = in.getDerValue();
 125             if (derValue.tag != DerValue.tag_Sequence) {
 126                 throw new IOException("Not a SEQUENCE");
 127             }
 128             DerInputStream data = derValue.data;
 129             n = data.getPositiveBigInteger();
 130             e = data.getPositiveBigInteger();
 131             if (derValue.data.available() != 0) {
 132                 throw new IOException("Extra data available");
 133             }
 134         } catch (IOException e) {
 135             throw new InvalidKeyException("Invalid RSA public key", e);
 136         }
 137     }
 138 
 139     // return a string representation of this key for debugging
 140     public String toString() {
 141         return "Sun RSA public key, " + n.bitLength() + " bits\n  modulus: "
 142                 + n + "\n  public exponent: " + e;
 143     }
 144 
 145     protected Object writeReplace() throws java.io.ObjectStreamException {
 146         return new KeyRep(KeyRep.Type.PUBLIC,
 147                         getAlgorithm(),
 148                         getFormat(),
 149                         getEncoded());
 150     }
 151 }