/java/code/src/org/keyczar/DsaPublicKey.java

https://code.google.com/ · Java · 208 lines · 149 code · 26 blank · 33 comment · 0 complexity · c7dfee49035c8946ed9691baab050de7 MD5 · raw file

  1. /*
  2. * Copyright 2008 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.keyczar;
  17. import com.google.gson.annotations.Expose;
  18. import org.keyczar.exceptions.KeyczarException;
  19. import org.keyczar.interfaces.KeyType;
  20. import org.keyczar.interfaces.Stream;
  21. import org.keyczar.interfaces.VerifyingStream;
  22. import org.keyczar.util.Base64Coder;
  23. import org.keyczar.util.Util;
  24. import java.math.BigInteger;
  25. import java.nio.ByteBuffer;
  26. import java.security.GeneralSecurityException;
  27. import java.security.KeyFactory;
  28. import java.security.PublicKey;
  29. import java.security.Signature;
  30. import java.security.SignatureException;
  31. import java.security.interfaces.DSAParams;
  32. import java.security.interfaces.DSAPrivateKey;
  33. import java.security.interfaces.DSAPublicKey;
  34. import java.security.spec.DSAPublicKeySpec;
  35. /**
  36. * Wrapping class for DSA Public Keys. These must be exported from existing DSA
  37. * private key sets.
  38. *
  39. * @author steveweis@gmail.com (Steve Weis)
  40. */
  41. public class DsaPublicKey extends KeyczarPublicKey {
  42. private static final String KEY_GEN_ALGORITHM = "DSA";
  43. private static final String SIG_ALGORITHM = "SHA1withDSA";
  44. private static final int DSA_DIGEST_SIZE = 48;
  45. private DSAPublicKey jcePublicKey;
  46. private final byte[] hash = new byte[Keyczar.KEY_HASH_SIZE];
  47. @Expose final String y;
  48. @Expose final String p;
  49. @Expose final String q;
  50. @Expose final String g;
  51. static DsaPublicKey read(String input) throws KeyczarException {
  52. DsaPublicKey key = Util.gson().fromJson(input, DsaPublicKey.class);
  53. key.initFromJson();
  54. return key;
  55. }
  56. /**
  57. * Constructs a new {@link DsaPublicKey} from the provided JCE {@link DSAPublicKey}.
  58. */
  59. DsaPublicKey(DSAPublicKey jcePublicKey) throws KeyczarException {
  60. this(jcePublicKey.getY(), jcePublicKey.getParams());
  61. }
  62. /**
  63. * Constructs a new {@link DsaPublicKey} from the provided JCE {@link DSAPrivateKey}.
  64. */
  65. DsaPublicKey(DSAPrivateKey jcePrivateKey) throws KeyczarException {
  66. this(computeY(jcePrivateKey), jcePrivateKey.getParams());
  67. }
  68. // Used by GSON, which will overwrite the values set here.
  69. private DsaPublicKey() {
  70. super(0);
  71. jcePublicKey = null;
  72. y = p = q = g = null;
  73. }
  74. private DsaPublicKey(BigInteger yVal, DSAParams params) throws KeyczarException {
  75. super(params.getP().bitLength());
  76. BigInteger pVal = params.getP();
  77. BigInteger qVal = params.getQ();
  78. BigInteger gVal = params.getG();
  79. y = Base64Coder.encodeWebSafe(yVal.toByteArray());
  80. p = Base64Coder.encodeWebSafe(pVal.toByteArray());
  81. q = Base64Coder.encodeWebSafe(qVal.toByteArray());
  82. g = Base64Coder.encodeWebSafe(gVal.toByteArray());
  83. initializeJceKey(yVal, pVal, qVal, gVal);
  84. initializeHash();
  85. }
  86. private static BigInteger computeY(DSAPrivateKey jcePrivateKey) {
  87. final BigInteger p = jcePrivateKey.getParams().getP();
  88. final BigInteger g = jcePrivateKey.getParams().getG();
  89. final BigInteger x = jcePrivateKey.getX();
  90. return g.modPow(x, p);
  91. }
  92. @Override
  93. protected Stream getStream() throws KeyczarException {
  94. return new DsaVerifyingStream();
  95. }
  96. @Override
  97. public KeyType getType() {
  98. return DefaultKeyType.DSA_PUB;
  99. }
  100. @Override
  101. public byte[] hash() {
  102. return hash;
  103. }
  104. /**
  105. * Initialize JCE key from JSON data. Must be called after an instance is read from JSON.
  106. * In default scope so {@link DsaPrivateKey} can call it when a private key string (which
  107. * contains a public key string) is deserialized.
  108. */
  109. void initFromJson() throws KeyczarException {
  110. BigInteger yVal = new BigInteger(Base64Coder.decodeWebSafe(y));
  111. BigInteger pVal = new BigInteger(Base64Coder.decodeWebSafe(p));
  112. BigInteger qVal = new BigInteger(Base64Coder.decodeWebSafe(q));
  113. BigInteger gVal = new BigInteger(Base64Coder.decodeWebSafe(g));
  114. initializeJceKey(yVal, pVal, qVal, gVal);
  115. initializeHash();
  116. }
  117. private void initializeJceKey(BigInteger yVal, BigInteger pVal, BigInteger qVal,
  118. BigInteger gVal) throws KeyczarException {
  119. try {
  120. KeyFactory kf = KeyFactory.getInstance(KEY_GEN_ALGORITHM);
  121. jcePublicKey = (DSAPublicKey) kf.generatePublic(new DSAPublicKeySpec(yVal, pVal, qVal, gVal));
  122. } catch (GeneralSecurityException e) {
  123. throw new KeyczarException(e);
  124. }
  125. }
  126. private void initializeHash() throws KeyczarException {
  127. final DSAParams dsaParams = jcePublicKey.getParams();
  128. final byte[] fullHash = Util.prefixHash(
  129. Util.stripLeadingZeros(dsaParams.getP().toByteArray()),
  130. Util.stripLeadingZeros(dsaParams.getQ().toByteArray()),
  131. Util.stripLeadingZeros(dsaParams.getG().toByteArray()),
  132. Util.stripLeadingZeros(jcePublicKey.getY().toByteArray()));
  133. System.arraycopy(fullHash, 0, hash, 0, hash.length);
  134. }
  135. @Override
  136. protected PublicKey getJceKey() {
  137. return jcePublicKey;
  138. }
  139. @Override
  140. protected boolean isSecret() {
  141. return false;
  142. }
  143. private class DsaVerifyingStream implements VerifyingStream {
  144. private Signature signature;
  145. public DsaVerifyingStream() throws KeyczarException {
  146. try {
  147. signature = Signature.getInstance(SIG_ALGORITHM);
  148. } catch (GeneralSecurityException e) {
  149. throw new KeyczarException(e);
  150. }
  151. }
  152. @Override
  153. public int digestSize() {
  154. return DSA_DIGEST_SIZE;
  155. }
  156. @Override
  157. public void initVerify() throws KeyczarException {
  158. try {
  159. signature.initVerify(jcePublicKey);
  160. } catch (GeneralSecurityException e) {
  161. throw new KeyczarException(e);
  162. }
  163. }
  164. @Override
  165. public void updateVerify(ByteBuffer input) throws KeyczarException {
  166. try {
  167. signature.update(input);
  168. } catch (SignatureException e) {
  169. throw new KeyczarException(e);
  170. }
  171. }
  172. @Override
  173. public boolean verify(ByteBuffer sig) throws KeyczarException {
  174. try {
  175. return signature.verify(sig.array(), sig.position(), sig.limit()
  176. - sig.position());
  177. } catch (GeneralSecurityException e) {
  178. throw new KeyczarException(e);
  179. }
  180. }
  181. }
  182. }