Showing the changes to java/pkg/sun/security/rsa/RSAPublicKeyImpl.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,135 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2003, 2008, 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 | |||
| 52 | private BigInteger n; // modulus | ||
| 53 | private BigInteger e; // public exponent | ||
| 54 | |||
| 55 | /** | ||
| 56 | * Construct a key from its components. Used by the | ||
| 57 | * RSAKeyFactory and the RSAKeyPairGenerator. | ||
| 58 | */ | ||
| 59 | public RSAPublicKeyImpl(BigInteger n, BigInteger e) | ||
| 60 | throws InvalidKeyException { | ||
| 61 | this.n = n; | ||
| 62 | this.e = e; | ||
| 63 | RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e); | ||
| 64 | // generate the encoding | ||
| 65 | algid = RSAPrivateCrtKeyImpl.rsaId; | ||
| 66 | try { | ||
| 67 | DerOutputStream out = new DerOutputStream(); | ||
| 68 | out.putInteger(n); | ||
| 69 | out.putInteger(e); | ||
| 70 | DerValue val = | ||
| 71 | new DerValue(DerValue.tag_Sequence, out.toByteArray()); | ||
| 72 | key = val.toByteArray(); | ||
| 73 | } catch (IOException exc) { | ||
| 74 | // should never occur | ||
| 75 | throw new InvalidKeyException(exc); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | /** | ||
| 80 | * Construct a key from its encoding. Used by RSAKeyFactory. | ||
| 81 | */ | ||
| 82 | public RSAPublicKeyImpl(byte[] encoded) throws InvalidKeyException { | ||
| 83 | decode(encoded); | ||
| 84 | RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e); | ||
| 85 | } | ||
| 86 | |||
| 87 | // see JCA doc | ||
| 88 | public String getAlgorithm() { | ||
| 89 | return "RSA"; | ||
| 90 | } | ||
| 91 | |||
| 92 | // see JCA doc | ||
| 93 | public BigInteger getModulus() { | ||
| 94 | return n; | ||
| 95 | } | ||
| 96 | |||
| 97 | // see JCA doc | ||
| 98 | public BigInteger getPublicExponent() { | ||
| 99 | return e; | ||
| 100 | } | ||
| 101 | |||
| 102 | /** | ||
| 103 | * Parse the key. Called by X509Key. | ||
| 104 | */ | ||
| 105 | protected void parseKeyBits() throws InvalidKeyException { | ||
| 106 | try { | ||
| 107 | DerInputStream in = new DerInputStream(key); | ||
| 108 | DerValue derValue = in.getDerValue(); | ||
| 109 | if (derValue.tag != DerValue.tag_Sequence) { | ||
| 110 | throw new IOException("Not a SEQUENCE"); | ||
| 111 | } | ||
| 112 | DerInputStream data = derValue.data; | ||
| 113 | n = RSAPrivateCrtKeyImpl.getBigInteger(data); | ||
| 114 | e = RSAPrivateCrtKeyImpl.getBigInteger(data); | ||
| 115 | if (derValue.data.available() != 0) { | ||
| 116 | throw new IOException("Extra data available"); | ||
| 117 | } | ||
| 118 | } catch (IOException e) { | ||
| 119 | throw new InvalidKeyException("Invalid RSA public key", e); | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | // return a string representation of this key for debugging | ||
| 124 | public String toString() { | ||
| 125 | return "Sun RSA public key, " + n.bitLength() + " bits\n modulus: " | ||
| 126 | + n + "\n public exponent: " + e; | ||
| 127 | } | ||
| 128 | |||
| 129 | protected Object writeReplace() throws java.io.ObjectStreamException { | ||
| 130 | return new KeyRep(KeyRep.Type.PUBLIC, | ||
| 131 | getAlgorithm(), | ||
| 132 | getFormat(), | ||
| 133 | getEncoded()); | ||
| 134 | } | ||
| 135 | } | ||