1 /* 2 * Copyright (c) 2001, 2020, 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 java.security.spec; 27 28 import java.math.BigInteger; 29 import java.util.Objects; 30 31 /** 32 * This class specifies an RSA multi-prime private key, as defined in the 33 * <a href="https://tools.ietf.org/rfc/rfc8017.txt">PKCS#1 v2.2</a> standard 34 * using the Chinese Remainder Theorem (CRT) information values 35 * for efficiency. 36 * 37 * @author Valerie Peng 38 * 39 * 40 * @see java.security.Key 41 * @see java.security.KeyFactory 42 * @see KeySpec 43 * @see PKCS8EncodedKeySpec 44 * @see RSAPrivateKeySpec 45 * @see RSAPublicKeySpec 46 * @see RSAOtherPrimeInfo 47 * 48 * @since 1.4 49 */ 50 51 public class RSAMultiPrimePrivateCrtKeySpec extends RSAPrivateKeySpec { 52 53 private final BigInteger publicExponent; 54 private final BigInteger primeP; 55 private final BigInteger primeQ; 56 private final BigInteger primeExponentP; 57 private final BigInteger primeExponentQ; 58 private final BigInteger crtCoefficient; 59 private final RSAOtherPrimeInfo[] otherPrimeInfo; 60 61 /** 62 * Creates a new {@code RSAMultiPrimePrivateCrtKeySpec}. 63 * 64 * <p>Note that the contents of {@code otherPrimeInfo} 65 * are copied to protect against subsequent modification when 66 * constructing this object. 67 * 68 * @param modulus the modulus n 69 * @param publicExponent the public exponent e 70 * @param privateExponent the private exponent d 71 * @param primeP the prime factor p of n 72 * @param primeQ the prime factor q of n 73 * @param primeExponentP this is d mod (p-1) 74 * @param primeExponentQ this is d mod (q-1) 75 * @param crtCoefficient the Chinese Remainder Theorem 76 * coefficient q-1 mod p 77 * @param otherPrimeInfo triplets of the rest of primes, null can be 78 * specified if there are only two prime factors 79 * (p and q) 80 * @throws NullPointerException if any of the specified parameters 81 * with the exception of {@code otherPrimeInfo} is null 82 * @throws IllegalArgumentException if an empty, i.e. 0-length, 83 * {@code otherPrimeInfo} is specified 84 */ RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus, BigInteger publicExponent, BigInteger privateExponent, BigInteger primeP, BigInteger primeQ, BigInteger primeExponentP, BigInteger primeExponentQ, BigInteger crtCoefficient, RSAOtherPrimeInfo[] otherPrimeInfo)85 public RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus, 86 BigInteger publicExponent, 87 BigInteger privateExponent, 88 BigInteger primeP, 89 BigInteger primeQ, 90 BigInteger primeExponentP, 91 BigInteger primeExponentQ, 92 BigInteger crtCoefficient, 93 RSAOtherPrimeInfo[] otherPrimeInfo) { 94 this(modulus, publicExponent, privateExponent, primeP, primeQ, 95 primeExponentP, primeExponentQ, crtCoefficient, otherPrimeInfo, 96 null); 97 } 98 99 /** 100 * Creates a new {@code RSAMultiPrimePrivateCrtKeySpec} with additional 101 * key parameters. 102 * 103 * <p>Note that the contents of {@code otherPrimeInfo} 104 * are copied to protect against subsequent modification when 105 * constructing this object. 106 * 107 * @param modulus the modulus n 108 * @param publicExponent the public exponent e 109 * @param privateExponent the private exponent d 110 * @param primeP the prime factor p of n 111 * @param primeQ the prime factor q of n 112 * @param primeExponentP this is d mod (p-1) 113 * @param primeExponentQ this is d mod (q-1) 114 * @param crtCoefficient the Chinese Remainder Theorem coefficient 115 * q-1 mod p 116 * @param otherPrimeInfo triplets of the rest of primes, null can be 117 * specified if there are only two prime factors 118 * (p and q) 119 * @param keyParams the parameters associated with key 120 * @throws NullPointerException if any of the specified parameters 121 * with the exception of {@code otherPrimeInfo} and {@code keyParams} 122 * is null 123 * @throws IllegalArgumentException if an empty, i.e. 0-length, 124 * {@code otherPrimeInfo} is specified 125 * @since 11 126 */ RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus, BigInteger publicExponent, BigInteger privateExponent, BigInteger primeP, BigInteger primeQ, BigInteger primeExponentP, BigInteger primeExponentQ, BigInteger crtCoefficient, RSAOtherPrimeInfo[] otherPrimeInfo, AlgorithmParameterSpec keyParams)127 public RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus, 128 BigInteger publicExponent, 129 BigInteger privateExponent, 130 BigInteger primeP, 131 BigInteger primeQ, 132 BigInteger primeExponentP, 133 BigInteger primeExponentQ, 134 BigInteger crtCoefficient, 135 RSAOtherPrimeInfo[] otherPrimeInfo, 136 AlgorithmParameterSpec keyParams) { 137 super(modulus, privateExponent, keyParams); 138 Objects.requireNonNull(modulus, 139 "the modulus parameter must be non-null"); 140 Objects.requireNonNull(privateExponent, 141 "the privateExponent parameter must be non-null"); 142 this.publicExponent = Objects.requireNonNull(publicExponent, 143 "the publicExponent parameter must be non-null"); 144 this.primeP = Objects.requireNonNull(primeP, 145 "the primeP parameter must be non-null"); 146 this.primeQ = Objects.requireNonNull(primeQ, 147 "the primeQ parameter must be non-null"); 148 this.primeExponentP = Objects.requireNonNull(primeExponentP, 149 "the primeExponentP parameter must be non-null"); 150 this.primeExponentQ = Objects.requireNonNull(primeExponentQ, 151 "the primeExponentQ parameter must be non-null"); 152 this.crtCoefficient = Objects.requireNonNull(crtCoefficient, 153 "the crtCoefficient parameter must be non-null"); 154 155 if (otherPrimeInfo == null) { 156 this.otherPrimeInfo = null; 157 } else if (otherPrimeInfo.length == 0) { 158 throw new IllegalArgumentException("the otherPrimeInfo " + 159 "parameter must not be empty"); 160 } else { 161 this.otherPrimeInfo = otherPrimeInfo.clone(); 162 } 163 } 164 165 /** 166 * Returns the public exponent. 167 * 168 * @return the public exponent. 169 */ getPublicExponent()170 public BigInteger getPublicExponent() { 171 return this.publicExponent; 172 } 173 174 /** 175 * Returns the primeP. 176 * 177 * @return the primeP. 178 */ getPrimeP()179 public BigInteger getPrimeP() { 180 return this.primeP; 181 } 182 183 /** 184 * Returns the primeQ. 185 * 186 * @return the primeQ. 187 */ getPrimeQ()188 public BigInteger getPrimeQ() { 189 return this.primeQ; 190 } 191 192 /** 193 * Returns the primeExponentP. 194 * 195 * @return the primeExponentP. 196 */ getPrimeExponentP()197 public BigInteger getPrimeExponentP() { 198 return this.primeExponentP; 199 } 200 201 /** 202 * Returns the primeExponentQ. 203 * 204 * @return the primeExponentQ. 205 */ getPrimeExponentQ()206 public BigInteger getPrimeExponentQ() { 207 return this.primeExponentQ; 208 } 209 210 /** 211 * Returns the crtCoefficient. 212 * 213 * @return the crtCoefficient. 214 */ getCrtCoefficient()215 public BigInteger getCrtCoefficient() { 216 return this.crtCoefficient; 217 } 218 219 /** 220 * Returns a copy of the otherPrimeInfo or null if there are 221 * only two prime factors (p and q). 222 * 223 * @return the otherPrimeInfo. Returns a new array each time this method 224 * is called. 225 */ getOtherPrimeInfo()226 public RSAOtherPrimeInfo[] getOtherPrimeInfo() { 227 if (otherPrimeInfo == null) return null; 228 return otherPrimeInfo.clone(); 229 } 230 } 231