/** * Returns the DSA parameters associated with this key, or null if the * parameters could not be parsed. */ public DSAParams getParams() { try { if (algid instanceof DSAParams) { return (DSAParams)algid; } else { DSAParameterSpec paramSpec; AlgorithmParameters algParams = algid.getParameters(); if (algParams == null) { return null; } paramSpec = algParams.getParameterSpec(DSAParameterSpec.class); return (DSAParams)paramSpec; } } catch (InvalidParameterSpecException e) { return null; } }
/** * Internal method to create a new key with inherited key parameters. * * @param keyValueKey key from which to obtain key value * @param keyParamsKey key from which to obtain key parameters * @return new public key having value and parameters * @throws CertPathValidatorException if keys are not appropriate types * for this operation */ static PublicKey makeInheritedParamsKey(PublicKey keyValueKey, PublicKey keyParamsKey) throws CertPathValidatorException { if (!(keyValueKey instanceof DSAPublicKey) || !(keyParamsKey instanceof DSAPublicKey)) throw new CertPathValidatorException("Input key is not " + "appropriate type for " + "inheriting parameters"); DSAParams params = ((DSAPublicKey)keyParamsKey).getParams(); if (params == null) throw new CertPathValidatorException("Key parameters missing"); try { BigInteger y = ((DSAPublicKey)keyValueKey).getY(); KeyFactory kf = KeyFactory.getInstance("DSA"); DSAPublicKeySpec ks = new DSAPublicKeySpec(y, params.getP(), params.getQ(), params.getG()); return kf.generatePublic(ks); } catch (GeneralSecurityException e) { throw new CertPathValidatorException("Unable to generate key with" + " inherited parameters: " + e.getMessage(), e); } }
@Override void marshalPublicKey(XmlWriter xwriter, DSAPublicKey publicKey, String dsPrefix, XMLCryptoContext context) throws MarshalException { DSAParams params = publicKey.getParams(); xwriter.writeStartElement(dsPrefix, "DSAKeyValue", XMLSignature.XMLNS); // parameters J, Seed & PgenCounter are not included writeBase64BigIntegerElement(xwriter, dsPrefix, "P", XMLSignature.XMLNS, params.getP()); writeBase64BigIntegerElement(xwriter, dsPrefix, "Q", XMLSignature.XMLNS, params.getQ()); writeBase64BigIntegerElement(xwriter, dsPrefix, "G", XMLSignature.XMLNS, params.getG()); writeBase64BigIntegerElement(xwriter, dsPrefix, "Y", XMLSignature.XMLNS, publicKey.getY() ); xwriter.writeEndElement(); // "DSAKeyValue" }
/** * Constructor DSAKeyValue * * @param doc * @param key * @throws IllegalArgumentException */ public DSAKeyValue(Document doc, Key key) throws IllegalArgumentException { super(doc); addReturnToSelf(); if (key instanceof DSAPublicKey) { DSAParams params = ((DSAPublicKey) key).getParams(); this.addBigIntegerElement(params.getP(), Constants._TAG_P); this.addBigIntegerElement(params.getQ(), Constants._TAG_Q); this.addBigIntegerElement(params.getG(), Constants._TAG_G); this.addBigIntegerElement(((DSAPublicKey) key).getY(), Constants._TAG_Y); } else { Object exArgs[] = { Constants._TAG_DSAKEYVALUE, key.getClass().getName() }; throw new IllegalArgumentException(I18n.translate("KeyValue.IllegalArgument", exArgs)); } }
public void initialize(DSAParams params, SecureRandom random) throws InvalidParameterException { if (params == null || !(params instanceof DSAParameterSpec)) throw new InvalidParameterException( "Parameters argument is either null or is not an instance, or " + "sub-instance, of java.security.spec.DSAParameterSpec"); DSAParameterSpec spec = (DSAParameterSpec) params; try { this.initialize((AlgorithmParameterSpec) spec, random); } catch (InvalidAlgorithmParameterException x) { InvalidParameterException y = new InvalidParameterException(x.getMessage()); y.initCause(x); throw y; } }
private ASN1Sequence createPublicKeyAndChallenge() throws SpkacException { ASN1EncodableVector publicKeyAlgorithm = new ASN1EncodableVector(); publicKeyAlgorithm.add(new ASN1ObjectIdentifier(getPublicKeyAlg().oid())); if (getPublicKey() instanceof RSAPublicKey) { publicKeyAlgorithm.add(DERNull.INSTANCE); } else { DSAParams dsaParams = ((DSAPublicKey) getPublicKey()).getParams(); ASN1EncodableVector dssParams = new ASN1EncodableVector(); dssParams.add(new ASN1Integer(dsaParams.getP())); dssParams.add(new ASN1Integer(dsaParams.getQ())); dssParams.add(new ASN1Integer(dsaParams.getG())); publicKeyAlgorithm.add(new DERSequence(dssParams)); } ASN1EncodableVector spki = new ASN1EncodableVector(); spki.add(new DERSequence(publicKeyAlgorithm)); spki.add(encodePublicKeyAsBitString(getPublicKey())); ASN1EncodableVector publicKeyAndChallenge = new ASN1EncodableVector(); publicKeyAndChallenge.add(new DERSequence(spki)); publicKeyAndChallenge.add(new DERIA5String(getChallenge())); return new DERSequence(publicKeyAndChallenge); }
final void initFromJson() throws KeyczarException { BigInteger localBigInteger1 = new BigInteger(Base64Coder.decodeWebSafe(this.y)); BigInteger localBigInteger2 = new BigInteger(Base64Coder.decodeWebSafe(this.p)); BigInteger localBigInteger3 = new BigInteger(Base64Coder.decodeWebSafe(this.q)); BigInteger localBigInteger4 = new BigInteger(Base64Coder.decodeWebSafe(this.g)); try { this.jcePublicKey = ((DSAPublicKey)KeyFactory.getInstance("DSA").generatePublic(new DSAPublicKeySpec(localBigInteger1, localBigInteger2, localBigInteger3, localBigInteger4))); DSAParams localDSAParams = this.jcePublicKey.getParams(); byte[][] arrayOfByte = new byte[4][]; arrayOfByte[0] = Util.stripLeadingZeros(localDSAParams.getP().toByteArray()); arrayOfByte[1] = Util.stripLeadingZeros(localDSAParams.getQ().toByteArray()); arrayOfByte[2] = Util.stripLeadingZeros(localDSAParams.getG().toByteArray()); arrayOfByte[3] = Util.stripLeadingZeros(this.jcePublicKey.getY().toByteArray()); System.arraycopy(Util.prefixHash(arrayOfByte), 0, this.hash, 0, this.hash.length); return; } catch (GeneralSecurityException localGeneralSecurityException) { throw new KeyczarException(localGeneralSecurityException); } }
public void writePublicKey(PublicKey pubKey) throws IOException { if (!(pubKey instanceof DSAPublicKey)) throw new UnsupportedOperationException( "Key types other than DSA are not supported at the moment."); DSAPublicKey dsaKey = (DSAPublicKey) pubKey; writeShort(0); DSAParams dsaParams = dsaKey.getParams(); writeBigInt(dsaParams.getP()); writeBigInt(dsaParams.getQ()); writeBigInt(dsaParams.getG()); writeBigInt(dsaKey.getY()); }
public void regenerateLocalPublicKey(KeyFactory factory, String fullUserId, DSAPrivateKey privKey) { String userId = Address.stripResource(fullUserId); BigInteger x = privKey.getX(); DSAParams params = privKey.getParams(); BigInteger y = params.getG().modPow(x, params.getP()); DSAPublicKeySpec keySpec = new DSAPublicKeySpec(y, params.getP(), params.getQ(), params.getG()); PublicKey pubKey; try { pubKey = factory.generatePublic(keySpec); storeLocalPublicKey(userId, pubKey); } catch (Exception e) { throw new RuntimeException(e); } }
public PublicKey getPublicKey() throws GeneralSecurityException { if (privateKey instanceof DSAPrivateKey) { DSAPrivateKey dsa = (DSAPrivateKey) privateKey; DSAParams params = dsa.getParams(); BigInteger g = params.getG(); BigInteger p = params.getP(); BigInteger q = params.getQ(); BigInteger x = dsa.getX(); BigInteger y = q.modPow( x, p ); DSAPublicKeySpec dsaKeySpec = new DSAPublicKeySpec(y, p, q, g); return KeyFactory.getInstance("DSA").generatePublic(dsaKeySpec); } else if (privateKey instanceof RSAPrivateCrtKey) { RSAPrivateCrtKey rsa = (RSAPrivateCrtKey) privateKey; RSAPublicKeySpec rsaKeySpec = new RSAPublicKeySpec( rsa.getModulus(), rsa.getPublicExponent() ); return KeyFactory.getInstance("RSA").generatePublic(rsaKeySpec); } else { throw new GeneralSecurityException("Not an RSA or DSA key"); } }
/** * @tests java.security.KeyPairGenerator#initialize(java.security.spec.AlgorithmParameterSpec) */ @TestTargetNew( level = TestLevel.PARTIAL, notes = "InvalidAlgorithmParameterException checking missed", method = "initialize", args = {java.security.spec.AlgorithmParameterSpec.class} ) public void test_initializeLjava_security_spec_AlgorithmParameterSpec() throws Exception { // create DSAParams KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA"); keyPairGenerator.initialize(1024); DSAPublicKey key = (DSAPublicKey) keyPairGenerator.genKeyPair() .getPublic(); DSAParams params = key.getParams(); KeyPairGenerator keyPair = KeyPairGenerator.getInstance("DSA"); keyPair.initialize(new DSAParameterSpec(params.getP(), params.getQ(), params.getG())); }
/** * @tests java.security.KeyPairGenerator#initialize(java.security.spec.AlgorithmParameterSpec, * java.security.SecureRandom) */ @TestTargetNew( level = TestLevel.PARTIAL, notes = "InvalidAlgorithmParameterException checking missed", method = "initialize", args = {java.security.spec.AlgorithmParameterSpec.class, java.security.SecureRandom.class} ) public void test_initializeLjava_security_spec_AlgorithmParameterSpecLjava_security_SecureRandom() throws Exception { // create DSAParams KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA"); keyPairGenerator.initialize(1024); DSAPublicKey key = (DSAPublicKey) keyPairGenerator.genKeyPair() .getPublic(); DSAParams params = key.getParams(); KeyPairGenerator keyPair = KeyPairGenerator.getInstance("DSA"); keyPair.initialize(new DSAParameterSpec(params.getP(), params.getQ(), params.getG()), new SecureRandom()); }
public DOMKeyValue(PublicKey key) throws KeyException { if (key == null) { throw new NullPointerException("key cannot be null"); } this.publicKey = key; if (key instanceof DSAPublicKey) { DSAPublicKey dkey = (DSAPublicKey) key; DSAParams params = dkey.getParams(); p = new DOMCryptoBinary(params.getP()); q = new DOMCryptoBinary(params.getQ()); g = new DOMCryptoBinary(params.getG()); y = new DOMCryptoBinary(dkey.getY()); } else if (key instanceof RSAPublicKey) { RSAPublicKey rkey = (RSAPublicKey) key; exponent = new DOMCryptoBinary(rkey.getPublicExponent()); modulus = new DOMCryptoBinary(rkey.getModulus()); } else { throw new KeyException("unsupported key algorithm: " + key.getAlgorithm()); } }