/**
   * 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());
  }
Exemple #2
0
  private static RSAKeyValueType parseRSAKeyValue(XMLEventReader xmlEventReader)
      throws ParsingException {
    StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
    StaxParserUtil.validate(startElement, WSTrustConstants.XMLDSig.RSA_KEYVALUE);

    XMLEvent xmlEvent = null;
    String tag = null;

    RSAKeyValueType rsaKeyValue = new RSAKeyValueType();

    while (xmlEventReader.hasNext()) {
      xmlEvent = StaxParserUtil.peek(xmlEventReader);
      if (xmlEvent instanceof EndElement) {
        tag = StaxParserUtil.getEndElementName((EndElement) xmlEvent);
        if (tag.equals(WSTrustConstants.XMLDSig.RSA_KEYVALUE)) {
          xmlEvent = StaxParserUtil.getNextEndElement(xmlEventReader);
          break;
        } else throw logger.parserUnknownEndElement(tag);
      }

      startElement = (StartElement) xmlEvent;
      tag = StaxParserUtil.getStartElementName(startElement);
      if (tag.equals(WSTrustConstants.XMLDSig.MODULUS)) {
        startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
        String text = StaxParserUtil.getElementText(xmlEventReader);
        rsaKeyValue.setModulus(text.getBytes());
      } else if (tag.equals(WSTrustConstants.XMLDSig.EXPONENT)) {
        startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
        String text = StaxParserUtil.getElementText(xmlEventReader);
        rsaKeyValue.setExponent(text.getBytes());
      } else throw logger.parserUnknownTag(tag, startElement.getLocation());
    }
    return rsaKeyValue;
  }
  /**
   * Given a dsig:DSAKeyValue element, return {@link DSAKeyValueType}
   *
   * @param element
   * @return
   * @throws ProcessingException
   */
  public static RSAKeyValueType getRSAKeyValue(Element element) throws ParsingException {
    RSAKeyValueType rsa = new RSAKeyValueType();
    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.MODULUS.equals(tag)) {
          rsa.setModulus(text);
        } else if (WSTrustConstants.XMLDSig.EXPONENT.equals(tag)) {
          rsa.setExponent(text);
        }
      }
    }

    return rsa;
  }