blob: c500ca2dee47a2c16a48d5c9fc646f624e062a40 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-2005 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.security.rsa;
27
28import java.io.IOException;
29import java.math.BigInteger;
30
31import java.security.*;
32import java.security.interfaces.*;
33
34import sun.security.util.*;
35import 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 */
48public 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) throws InvalidKeyException {
60 this.n = n;
61 this.e = e;
62 RSAKeyFactory.checkKeyLength(n);
63 // generate the encoding
64 algid = RSAPrivateCrtKeyImpl.rsaId;
65 try {
66 DerOutputStream out = new DerOutputStream();
67 out.putInteger(n);
68 out.putInteger(e);
69 DerValue val =
70 new DerValue(DerValue.tag_Sequence, out.toByteArray());
71 key = val.toByteArray();
72 } catch (IOException exc) {
73 // should never occur
74 throw new InvalidKeyException(exc);
75 }
76 }
77
78 /**
79 * Construct a key from its encoding. Used by RSAKeyFactory.
80 */
81 public RSAPublicKeyImpl(byte[] encoded) throws InvalidKeyException {
82 decode(encoded);
83 RSAKeyFactory.checkKeyLength(n);
84 }
85
86 // see JCA doc
87 public String getAlgorithm() {
88 return "RSA";
89 }
90
91 // see JCA doc
92 public BigInteger getModulus() {
93 return n;
94 }
95
96 // see JCA doc
97 public BigInteger getPublicExponent() {
98 return e;
99 }
100
101 /**
102 * Parse the key. Called by X509Key.
103 */
104 protected void parseKeyBits() throws InvalidKeyException {
105 try {
106 DerInputStream in = new DerInputStream(key);
107 DerValue derValue = in.getDerValue();
108 if (derValue.tag != DerValue.tag_Sequence) {
109 throw new IOException("Not a SEQUENCE");
110 }
111 DerInputStream data = derValue.data;
112 n = RSAPrivateCrtKeyImpl.getBigInteger(data);
113 e = RSAPrivateCrtKeyImpl.getBigInteger(data);
114 if (derValue.data.available() != 0) {
115 throw new IOException("Extra data available");
116 }
117 } catch (IOException e) {
118 throw new InvalidKeyException("Invalid RSA public key", e);
119 }
120 }
121
122 // return a string representation of this key for debugging
123 public String toString() {
124 return "Sun RSA public key, " + n.bitLength() + " bits\n modulus: "
125 + n + "\n public exponent: " + e;
126 }
127
128 protected Object writeReplace() throws java.io.ObjectStreamException {
129 return new KeyRep(KeyRep.Type.PUBLIC,
130 getAlgorithm(),
131 getFormat(),
132 getEncoded());
133 }
134}