java.security.interfaces.DSAPublicKey Java Examples

The following examples show how to use java.security.interfaces.DSAPublicKey. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BasicChecker.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #2
Source File: DSAKeyValue.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor DSAKeyValue
 *
 * @param doc
 * @param key
 * @throws IllegalArgumentException
 */
public DSAKeyValue(Document doc, Key key) throws IllegalArgumentException {
    super(doc);

    XMLUtils.addReturnToElement(this.constructionElement);

    if (key instanceof java.security.interfaces.DSAPublicKey) {
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().getP(), Constants._TAG_P);
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().getQ(), Constants._TAG_Q);
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().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));
    }
}
 
Example #3
Source File: AlgorithmUtil.java    From xipki with Apache License 2.0 6 votes vote down vote up
public static AlgorithmIdentifier getSigAlgId(PublicKey pubKey, HashAlgo hashAlgo,
    SignatureAlgoControl algoControl) throws NoSuchAlgorithmException {
  Args.notNull(hashAlgo, "hashAlgo");

  if (pubKey instanceof RSAPublicKey) {
    boolean rsaMgf1 = (algoControl == null) ? false : algoControl.isRsaMgf1();
    return getRSASigAlgId(hashAlgo, rsaMgf1);
  } else if (pubKey instanceof ECPublicKey) {
    boolean dsaPlain = (algoControl == null) ? false : algoControl.isDsaPlain();
    boolean gm =  (algoControl == null) ? false : algoControl.isGm();
    return getECSigAlgId(hashAlgo, dsaPlain, gm);
  } else if (pubKey instanceof DSAPublicKey) {
    return getDSASigAlgId(hashAlgo);
  } else {
    throw new NoSuchAlgorithmException("Unknown public key '" + pubKey.getClass().getName());
  }
}
 
Example #4
Source File: DSAKeyValue.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructor DSAKeyValue
 *
 * @param doc
 * @param key
 * @throws IllegalArgumentException
 */
public DSAKeyValue(Document doc, Key key) throws IllegalArgumentException {
    super(doc);

    XMLUtils.addReturnToElement(this.constructionElement);

    if (key instanceof java.security.interfaces.DSAPublicKey) {
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().getP(), Constants._TAG_P);
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().getQ(), Constants._TAG_Q);
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().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));
    }
}
 
Example #5
Source File: DSAKeyValue.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructor DSAKeyValue
 *
 * @param doc
 * @param key
 * @throws IllegalArgumentException
 */
public DSAKeyValue(Document doc, Key key) throws IllegalArgumentException {
    super(doc);

    XMLUtils.addReturnToElement(this.constructionElement);

    if (key instanceof java.security.interfaces.DSAPublicKey) {
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().getP(), Constants._TAG_P);
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().getQ(), Constants._TAG_Q);
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().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));
    }
}
 
Example #6
Source File: BasicChecker.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #7
Source File: STSServiceImpl.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
private String processHolderOfKeyCredentials(Credential hokCred, String request) throws TechnicalConnectorException, CertificateEncodingException {
   if (hokCred != null && hokCred.getCertificate() != null) {
      request = StringUtils.replace(request, "${holder.of.key}", new String(Base64.encode(hokCred.getCertificate().getEncoded())));
      PublicKey publicKey = hokCred.getCertificate().getPublicKey();
      if (publicKey instanceof RSAPublicKey) {
         RSAPublicKey rsaPublicKey = (RSAPublicKey)publicKey;
         request = StringUtils.replace(request, "${publickey.rsa.modulus}", new String(Base64.encode(convertTo(rsaPublicKey.getModulus()))));
         request = StringUtils.replace(request, "${publickey.rsa.exponent}", new String(Base64.encode(convertTo(rsaPublicKey.getPublicExponent()))));
         request = StringUtils.replace(request, "<ds:DSAKeyValue><ds:G>${publickey.dsa.g}<ds:G><ds:P>${publickey.dsa.p}</ds:P><ds:Q>${publickey.dsa.q}</ds:Q></ds:DSAKeyValue>", "");
      } else if (publicKey instanceof DSAPublicKey) {
         DSAPublicKey dsaPublicKey = (DSAPublicKey)publicKey;
         request = StringUtils.replace(request, "${publickey.dsa.g}", new String(Base64.encode(convertTo(dsaPublicKey.getParams().getG()))));
         request = StringUtils.replace(request, "${publickey.dsa.p}", new String(Base64.encode(convertTo(dsaPublicKey.getParams().getP()))));
         request = StringUtils.replace(request, "${publickey.dsa.q}", new String(Base64.encode(convertTo(dsaPublicKey.getParams().getQ()))));
         request = StringUtils.replace(request, "<ds:RSAKeyValue><ds:Modulus>${publickey.rsa.modulus}</ds:Modulus><ds:Exponent>${publickey.rsa.exponent}</ds:Exponent></ds:RSAKeyValue>", "");
      } else {
         LOG.info("Unsupported public key: [" + publicKey.getClass().getName() + "+]");
      }
   }

   return request;
}
 
Example #8
Source File: DSAKeyValue.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructor DSAKeyValue
 *
 * @param doc
 * @param key
 * @throws IllegalArgumentException
 */
public DSAKeyValue(Document doc, Key key) throws IllegalArgumentException {
    super(doc);

    XMLUtils.addReturnToElement(this.constructionElement);

    if (key instanceof java.security.interfaces.DSAPublicKey) {
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().getP(), Constants._TAG_P);
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().getQ(), Constants._TAG_Q);
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().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));
    }
}
 
Example #9
Source File: STSServiceImpl.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
private String processHolderOfKeyCredentials(Credential hokCred, String request) throws TechnicalConnectorException, CertificateEncodingException {
   if (hokCred != null && hokCred.getCertificate() != null) {
      request = StringUtils.replace(request, "${holder.of.key}", new String(Base64.encode(hokCred.getCertificate().getEncoded())));
      PublicKey publicKey = hokCred.getCertificate().getPublicKey();
      if (publicKey instanceof RSAPublicKey) {
         RSAPublicKey rsaPublicKey = (RSAPublicKey)publicKey;
         request = StringUtils.replace(request, "${publickey.rsa.modulus}", new String(Base64.encode(convertTo(rsaPublicKey.getModulus()))));
         request = StringUtils.replace(request, "${publickey.rsa.exponent}", new String(Base64.encode(convertTo(rsaPublicKey.getPublicExponent()))));
         request = StringUtils.replace(request, "<ds:DSAKeyValue><ds:G>${publickey.dsa.g}<ds:G><ds:P>${publickey.dsa.p}</ds:P><ds:Q>${publickey.dsa.q}</ds:Q></ds:DSAKeyValue>", "");
      } else if (publicKey instanceof DSAPublicKey) {
         DSAPublicKey dsaPublicKey = (DSAPublicKey)publicKey;
         request = StringUtils.replace(request, "${publickey.dsa.g}", new String(Base64.encode(convertTo(dsaPublicKey.getParams().getG()))));
         request = StringUtils.replace(request, "${publickey.dsa.p}", new String(Base64.encode(convertTo(dsaPublicKey.getParams().getP()))));
         request = StringUtils.replace(request, "${publickey.dsa.q}", new String(Base64.encode(convertTo(dsaPublicKey.getParams().getQ()))));
         request = StringUtils.replace(request, "<ds:RSAKeyValue><ds:Modulus>${publickey.rsa.modulus}</ds:Modulus><ds:Exponent>${publickey.rsa.exponent}</ds:Exponent></ds:RSAKeyValue>", "");
      } else {
         LOG.info("Unsupported public key: [" + publicKey.getClass().getName() + "+]");
      }
   }

   return request;
}
 
Example #10
Source File: DSAKeyValue.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructor DSAKeyValue
 *
 * @param doc
 * @param key
 * @throws IllegalArgumentException
 */
public DSAKeyValue(Document doc, Key key) throws IllegalArgumentException {
    super(doc);

    XMLUtils.addReturnToElement(this.constructionElement);

    if (key instanceof java.security.interfaces.DSAPublicKey) {
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().getP(), Constants._TAG_P);
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().getQ(), Constants._TAG_Q);
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().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));
    }
}
 
Example #11
Source File: BasicChecker.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #12
Source File: DSAKeyValue.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
 * Constructor DSAKeyValue
 *
 * @param doc
 * @param key
 * @throws IllegalArgumentException
 */
public DSAKeyValue(Document doc, Key key) throws IllegalArgumentException {
    super(doc);

    XMLUtils.addReturnToElement(this.constructionElement);

    if (key instanceof java.security.interfaces.DSAPublicKey) {
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().getP(), Constants._TAG_P);
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().getQ(), Constants._TAG_Q);
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().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));
    }
}
 
Example #13
Source File: DSAKeyUtil.java    From quarkus with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    Provider provider = Security.getProvider("SUN");
    System.out.println(provider.getInfo());
    for (Provider.Service service : provider.getServices()) {
        System.out.println(service);
    }

    KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
    kpg.initialize(2048);
    System.out.println("DSA.provider: " + kpg.getProvider());
    KeyFactory keyFactory = KeyFactory.getInstance("DSA");
    System.out.println("DSA.provider: " + keyFactory.getProvider());

    KeyPair pair = kpg.genKeyPair();
    DSAPublicKey publicKey = (DSAPublicKey) pair.getPublic();
    byte[] encoded = publicKey.getEncoded();
    X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encoded);
    DSAPublicKey publicKey1 = (DSAPublicKey) keyFactory.generatePublic(publicKeySpec);
    System.out.printf("keys are equal: %s%n", publicKey1.equals(publicKey));

    String base64 = Base64.getEncoder().encodeToString(encoded);
    System.out.println(base64);
}
 
Example #14
Source File: DViewPublicKey.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private void populateDialog() throws CryptoException {
	KeyInfo keyInfo = KeyPairUtil.getKeyInfo(publicKey);

	jtfAlgorithm.setText(keyInfo.getAlgorithm());

	Integer keyLength = keyInfo.getSize();

	if (keyLength != null) {
		jtfKeySize.setText(MessageFormat.format(res.getString("DViewPublicKey.jtfKeySize.text"), "" + keyLength));
	} else {
		jtfKeySize.setText(MessageFormat.format(res.getString("DViewPublicKey.jtfKeySize.text"), "?"));
	}

	jtfFormat.setText(publicKey.getFormat());

	jtaEncoded.setText(new BigInteger(1, publicKey.getEncoded()).toString(16).toUpperCase());
	jtaEncoded.setCaretPosition(0);

	if ((publicKey instanceof RSAPublicKey) || (publicKey instanceof DSAPublicKey)) {
		jbFields.setEnabled(true);
	} else {
		jbFields.setEnabled(false);
	}
}
 
Example #15
Source File: BasicChecker.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #16
Source File: CaEnrollBenchKeyEntry.java    From xipki with Apache License 2.0 6 votes vote down vote up
public DSAKeyEntry(int plength) throws Exception {
  if (plength == 1024) {
    init(P_1024, Q_1024, G_1024, Y_1024);
  } else if (plength == 2048) {
    init(P_2048, Q_2048, G_2048, Y_2048);
  } else if (plength == 3072) {
    init(P_3072, Q_3072, G_3072, Y_3072);
  } else {
    if (plength % 1024 != 0) {
      throw new IllegalArgumentException("invalid DSA pLength " + plength);
    }

    int qlength = (plength >= 2048) ? 256 : 160;
    KeyPair kp = KeyUtil.generateDSAKeypair(plength, qlength, new SecureRandom());
    DSAPublicKey pk = (DSAPublicKey) kp.getPublic();

    init(pk.getParams().getP(), pk.getParams().getQ(), pk.getParams().getG(), pk.getY());
  }
}
 
Example #17
Source File: DsaTest.java    From wycheproof with Apache License 2.0 6 votes vote down vote up
/**
 * This is just a test for basic functionality of DSA. The test generates a public and private
 * key, generates a signature and verifies it. This test is slow with some providers, since
 * some providers generate new DSA parameters (p and q) for each new key.
 */
@SlowTest(providers = {ProviderType.BOUNCY_CASTLE, ProviderType.SPONGY_CASTLE})
@SuppressWarnings("InsecureCryptoUsage")
@Test
public void testBasic() throws Exception {
  int keySize = 2048;
  String algorithm = "SHA256WithDSA";
  String message = "Hello";

  byte[] messageBytes = message.getBytes("UTF-8");
  KeyPairGenerator generator = java.security.KeyPairGenerator.getInstance("DSA");
  generator.initialize(keySize);
  KeyPair keyPair = generator.generateKeyPair();
  DSAPublicKey pub = (DSAPublicKey) keyPair.getPublic();
  DSAPrivateKey priv = (DSAPrivateKey) keyPair.getPrivate();
  Signature signer = Signature.getInstance(algorithm);
  Signature verifier = Signature.getInstance(algorithm);
  signer.initSign(priv);
  signer.update(messageBytes);
  byte[] signature = signer.sign();
  verifier.initVerify(pub);
  verifier.update(messageBytes);
  assertTrue(verifier.verify(signature));
}
 
Example #18
Source File: DNSSEC.java    From dnsjava with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static byte[] fromDSAPublicKey(DSAPublicKey key) {
  DNSOutput out = new DNSOutput();
  BigInteger q = key.getParams().getQ();
  BigInteger p = key.getParams().getP();
  BigInteger g = key.getParams().getG();
  BigInteger y = key.getY();
  int t = (p.toByteArray().length - 64) / 8;

  out.writeU8(t);
  writeBigInteger(out, q);
  writeBigInteger(out, p);
  writePaddedBigInteger(out, g, 8 * t + 64);
  writePaddedBigInteger(out, y, 8 * t + 64);

  return out.toByteArray();
}
 
Example #19
Source File: DSAKeyValue.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructor DSAKeyValue
 *
 * @param doc
 * @param key
 * @throws IllegalArgumentException
 */
public DSAKeyValue(Document doc, Key key) throws IllegalArgumentException {
    super(doc);

    XMLUtils.addReturnToElement(this.constructionElement);

    if (key instanceof java.security.interfaces.DSAPublicKey) {
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().getP(), Constants._TAG_P);
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().getQ(), Constants._TAG_Q);
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().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));
    }
}
 
Example #20
Source File: P11PrivateKey.java    From xipki with Apache License 2.0 6 votes vote down vote up
public P11PrivateKey(P11CryptService p11CryptService, P11IdentityId identityId)
    throws P11TokenException {
  this.p11CryptService = Args.notNull(p11CryptService, "p11CryptService");
  this.identityId = Args.notNull(identityId, "identityId");

  this.publicKey = p11CryptService.getIdentity(identityId).getPublicKey();

  if (publicKey instanceof RSAPublicKey) {
    algorithm = "RSA";
    keysize = ((RSAPublicKey) publicKey).getModulus().bitLength();
  } else if (publicKey instanceof DSAPublicKey) {
    algorithm = "DSA";
    keysize = ((DSAPublicKey) publicKey).getParams().getP().bitLength();
  } else if (publicKey instanceof ECPublicKey) {
    algorithm = "EC";
    keysize = ((ECPublicKey) publicKey).getParams().getCurve().getField().getFieldSize();
  } else if (publicKey instanceof EdDSAKey) {
    algorithm = publicKey.getAlgorithm();
    keysize = EdECConstants.getKeyBitSize(EdECConstants.getCurveOid(algorithm));
  } else {
    throw new P11TokenException("unknown public key: " + publicKey);
  }
}
 
Example #21
Source File: KeyInfoHelper.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Builds a {@link DSAKeyValue} XMLObject from the Java security DSA public key type.
 * 
 * @param dsaPubKey a native Java {@link DSAPublicKey}
 * @return an {@link DSAKeyValue} XMLObject
 */
public static DSAKeyValue buildDSAKeyValue(DSAPublicKey dsaPubKey) {
    XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();
    DSAKeyValue dsaKeyValue = (DSAKeyValue) builderFactory
        .getBuilder(DSAKeyValue.DEFAULT_ELEMENT_NAME)
        .buildObject(DSAKeyValue.DEFAULT_ELEMENT_NAME);
    Y y = (Y) builderFactory.getBuilder(Y.DEFAULT_ELEMENT_NAME).buildObject(Y.DEFAULT_ELEMENT_NAME);
    G g = (G) builderFactory.getBuilder(G.DEFAULT_ELEMENT_NAME).buildObject(G.DEFAULT_ELEMENT_NAME);
    P p = (P) builderFactory.getBuilder(P.DEFAULT_ELEMENT_NAME).buildObject(P.DEFAULT_ELEMENT_NAME);
    Q q = (Q) builderFactory.getBuilder(Q.DEFAULT_ELEMENT_NAME).buildObject(Q.DEFAULT_ELEMENT_NAME);
    
    y.setValueBigInt(dsaPubKey.getY());
    dsaKeyValue.setY(y);
    
    g.setValueBigInt(dsaPubKey.getParams().getG());
    dsaKeyValue.setG(g);
    
    p.setValueBigInt(dsaPubKey.getParams().getP());
    dsaKeyValue.setP(p);
    
    q.setValueBigInt(dsaPubKey.getParams().getQ());
    dsaKeyValue.setQ(q);
    
    return dsaKeyValue;
}
 
Example #22
Source File: DSAKeyValue.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructor DSAKeyValue
 *
 * @param doc
 * @param key
 * @throws IllegalArgumentException
 */
public DSAKeyValue(Document doc, Key key) throws IllegalArgumentException {
    super(doc);

    XMLUtils.addReturnToElement(this.constructionElement);

    if (key instanceof java.security.interfaces.DSAPublicKey) {
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().getP(), Constants._TAG_P);
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().getQ(), Constants._TAG_Q);
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().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));
    }
}
 
Example #23
Source File: Encrypter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Check key encryption parameters for consistency and required values.
 * 
 * @param kekParams the key encryption parameters to check
 * @param allowEmpty if false, a null parameter is treated as an error
 * 
 * @throws EncryptionException thrown if any parameters are missing or have invalid values
 */
protected void checkParams(KeyEncryptionParameters kekParams, boolean allowEmpty) throws EncryptionException {
    if (kekParams == null) {
        if (allowEmpty) {
            return;
        } else {
            log.error("Key encryption parameters are required");
            throw new EncryptionException("Key encryption parameters are required");
        }
    }
    Key key = SecurityHelper.extractEncryptionKey(kekParams.getEncryptionCredential());
    if (key == null) {
        log.error("Key encryption credential and contained key are required");
        throw new EncryptionException("Key encryption credential and contained key are required");
    } else if (key instanceof DSAPublicKey) {
        log.error("Attempt made to use DSA key for encrypted key transport");
        throw new EncryptionException("DSA keys may not be used for encrypted key transport");
    } else if (key instanceof ECPublicKey) {
        log.error("Attempt made to use EC key for encrypted key transport");
        throw new EncryptionException("EC keys may not be used for encrypted key transport");
    } else if (DatatypeHelper.isEmpty(kekParams.getAlgorithm())) {
        log.error("Key encryption algorithm URI is required");
        throw new EncryptionException("Key encryption algorithm URI is required");
    }
}
 
Example #24
Source File: ToolDSA.java    From protools with Apache License 2.0 6 votes vote down vote up
/**
 * 生成密钥
 *
 * @return 密钥对象
 *
 * @throws Exception
 */
public static Map<String, Object> initKey() throws NoSuchAlgorithmException {
    // 初始化密钥对儿生成器
    KeyPairGenerator keygen = KeyPairGenerator.getInstance(ALGORITHM);

    // 实例化密钥对儿生成器
    keygen.initialize(KEY_SIZE, new SecureRandom());

    // 实例化密钥对儿
    KeyPair keys = keygen.genKeyPair();

    DSAPublicKey publicKey = (DSAPublicKey) keys.getPublic();

    DSAPrivateKey privateKey = (DSAPrivateKey) keys.getPrivate();

    // 封装密钥
    Map<String, Object> map = Maps.newHashMapWithExpectedSize(2);

    map.put(PUBLIC_KEY, publicKey);
    map.put(PRIVATE_KEY, privateKey);

    return map;
}
 
Example #25
Source File: BasicChecker.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #26
Source File: BasicChecker.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #27
Source File: BasicChecker.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #28
Source File: DSAKeyValue.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructor DSAKeyValue
 *
 * @param doc
 * @param key
 * @throws IllegalArgumentException
 */
public DSAKeyValue(Document doc, Key key) throws IllegalArgumentException {
    super(doc);

    XMLUtils.addReturnToElement(this.constructionElement);

    if (key instanceof java.security.interfaces.DSAPublicKey) {
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().getP(), Constants._TAG_P);
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().getQ(), Constants._TAG_Q);
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().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));
    }
}
 
Example #29
Source File: DSAKeyValue.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructor DSAKeyValue
 *
 * @param doc
 * @param key
 * @throws IllegalArgumentException
 */
public DSAKeyValue(Document doc, Key key) throws IllegalArgumentException {
    super(doc);

    XMLUtils.addReturnToElement(this.constructionElement);

    if (key instanceof java.security.interfaces.DSAPublicKey) {
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().getP(), Constants._TAG_P);
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().getQ(), Constants._TAG_Q);
        this.addBigIntegerElement(((DSAPublicKey) key).getParams().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));
    }
}
 
Example #30
Source File: BasicChecker.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
    }
}