示例#1
0
  /**
   * Creates a {@code KeyValueType} that wraps the specified public key. This method supports DSA
   * and RSA keys.
   *
   * @param key the {@code PublicKey} that will be represented as a {@code KeyValueType}.
   * @return the constructed {@code KeyValueType} or {@code null} if the specified key is neither a
   *     DSA nor a RSA key.
   */
  public static KeyValueType createKeyValue(PublicKey key) {
    if (key instanceof RSAPublicKey) {
      RSAPublicKey pubKey = (RSAPublicKey) key;
      byte[] modulus = pubKey.getModulus().toByteArray();
      byte[] exponent = pubKey.getPublicExponent().toByteArray();

      RSAKeyValueType rsaKeyValue = new RSAKeyValueType();
      rsaKeyValue.setModulus(Base64.encodeBytes(modulus).getBytes());
      rsaKeyValue.setExponent(Base64.encodeBytes(exponent).getBytes());
      return rsaKeyValue;
    } else if (key instanceof DSAPublicKey) {
      DSAPublicKey pubKey = (DSAPublicKey) key;
      byte[] P = pubKey.getParams().getP().toByteArray();
      byte[] Q = pubKey.getParams().getQ().toByteArray();
      byte[] G = pubKey.getParams().getG().toByteArray();
      byte[] Y = pubKey.getY().toByteArray();

      DSAKeyValueType dsaKeyValue = new DSAKeyValueType();
      dsaKeyValue.setP(Base64.encodeBytes(P).getBytes());
      dsaKeyValue.setQ(Base64.encodeBytes(Q).getBytes());
      dsaKeyValue.setG(Base64.encodeBytes(G).getBytes());
      dsaKeyValue.setY(Base64.encodeBytes(Y).getBytes());
      return dsaKeyValue;
    }
    throw logger.unsupportedType(key.toString());
  }
示例#2
0
  /**
   * Given a dsig:DSAKeyValue element, return {@link DSAKeyValueType}
   *
   * @param element
   * @return
   * @throws ProcessingException
   */
  public static DSAKeyValueType getDSAKeyValue(Element element) throws ParsingException {
    DSAKeyValueType dsa = new DSAKeyValueType();
    NodeList nl = element.getChildNodes();
    int length = nl.getLength();

    for (int i = 0; i < length; i++) {
      Node node = nl.item(i);
      if (node instanceof Element) {
        Element childElement = (Element) node;
        String tag = childElement.getLocalName();

        byte[] text = childElement.getTextContent().getBytes();

        if (WSTrustConstants.XMLDSig.P.equals(tag)) {
          dsa.setP(text);
        } else if (WSTrustConstants.XMLDSig.Q.equals(tag)) {
          dsa.setQ(text);
        } else if (WSTrustConstants.XMLDSig.G.equals(tag)) {
          dsa.setG(text);
        } else if (WSTrustConstants.XMLDSig.Y.equals(tag)) {
          dsa.setY(text);
        } else if (WSTrustConstants.XMLDSig.SEED.equals(tag)) {
          dsa.setSeed(text);
        } else if (WSTrustConstants.XMLDSig.PGEN_COUNTER.equals(tag)) {
          dsa.setPgenCounter(text);
        }
      }
    }

    return dsa;
  }