Esempio n. 1
0
  /**
   * Registers implementing class of the Transform algorithm with algorithmURI
   *
   * @param algorithmURI algorithmURI URI representation of <code>Transform algorithm</code>.
   * @param implementingClass <code>implementingClass</code> the implementing class of {@link
   *     SignatureAlgorithmSpi}
   * @throws AlgorithmAlreadyRegisteredException if specified algorithmURI is already registered
   * @throws XMLSignatureException
   */
  public static void register(String algorithmURI, String implementingClass)
      throws AlgorithmAlreadyRegisteredException, XMLSignatureException {

    {
      if (log.isLoggable(java.util.logging.Level.FINE))
        log.log(
            java.util.logging.Level.FINE,
            "Try to register " + algorithmURI + " " + implementingClass);

      // are we already registered?
      Class registeredClassClass = SignatureAlgorithm.getImplementingClass(algorithmURI);
      if (registeredClassClass != null) {
        String registeredClass = registeredClassClass.getName();

        if ((registeredClass != null) && (registeredClass.length() != 0)) {
          Object exArgs[] = {algorithmURI, registeredClass};

          throw new AlgorithmAlreadyRegisteredException("algorithm.alreadyRegistered", exArgs);
        }
      }
      try {
        SignatureAlgorithm._algorithmHash.put(algorithmURI, Class.forName(implementingClass));
      } catch (ClassNotFoundException ex) {
        Object exArgs[] = {algorithmURI, ex.getMessage()};

        throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs, ex);
      } catch (NullPointerException ex) {
        Object exArgs[] = {algorithmURI, ex.getMessage()};

        throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs, ex);
      }
    }
  }
Esempio n. 2
0
  private static SignatureAlgorithmSpi buildSigner(
      String algorithmURI, SignatureAlgorithmSpi result) throws XMLSignatureException {
    try {
      Class implementingClass = SignatureAlgorithm.getImplementingClass(algorithmURI);
      if (log.isLoggable(java.util.logging.Level.FINE))
        log.log(
            java.util.logging.Level.FINE,
            "Create URI \"" + algorithmURI + "\" class \"" + implementingClass + "\"");
      result = (SignatureAlgorithmSpi) implementingClass.newInstance();
      return result;
    } catch (IllegalAccessException ex) {
      Object exArgs[] = {algorithmURI, ex.getMessage()};

      throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs, ex);
    } catch (InstantiationException ex) {
      Object exArgs[] = {algorithmURI, ex.getMessage()};

      throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs, ex);
    } catch (NullPointerException ex) {
      Object exArgs[] = {algorithmURI, ex.getMessage()};

      throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs, ex);
    }
  }
Esempio n. 3
0
  /** Method init */
  public static synchronized void init() {

    if (_alreadyInitialized) {
      return;
    }
    long XX_configure_i18n_end = 0;
    long XX_configure_reg_c14n_start = 0;
    long XX_configure_reg_c14n_end = 0;
    long XX_configure_reg_jcemapper_end = 0;
    long XX_configure_reg_keyInfo_start = 0;
    long XX_configure_reg_keyResolver_end = 0;
    long XX_configure_reg_prefixes_start = 0;
    long XX_configure_reg_resourceresolver_start = 0;
    long XX_configure_reg_sigalgos_end = 0;
    long XX_configure_reg_transforms_end = 0;
    long XX_configure_reg_keyInfo_end = 0;
    long XX_configure_reg_keyResolver_start = 0;
    _alreadyInitialized = true;

    try {
      long XX_init_start = System.currentTimeMillis();
      long XX_prng_start = System.currentTimeMillis();

      // PRNG.init(new java.security.SecureRandom());

      long XX_prng_end = System.currentTimeMillis();

      /* read library configuration file */
      long XX_parsing_start = System.currentTimeMillis();
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

      dbf.setNamespaceAware(true);
      dbf.setValidating(false);

      DocumentBuilder db = dbf.newDocumentBuilder();
      // InputStream is =
      // Class.forName("com.sun.org.apache.xml.internal.security.Init").getResourceAsStream("resource/config.xml");
      InputStream is =
          (InputStream)
              AccessController.doPrivileged(
                  new PrivilegedAction() {
                    public Object run() {
                      String cfile =
                          System.getProperty(
                              "com.sun.org.apache.xml.internal.security.resource.config");
                      return getClass()
                          .getResourceAsStream(cfile != null ? cfile : "resource/config.xml");
                    }
                  });

      Document doc = db.parse(is);
      long XX_parsing_end = System.currentTimeMillis();
      long XX_configure_i18n_start = 0;

      {
        XX_configure_reg_keyInfo_start = System.currentTimeMillis();
        try {
          KeyInfo.init();
        } catch (Exception e) {
          e.printStackTrace();

          throw e;
        }
        XX_configure_reg_keyInfo_end = System.currentTimeMillis();
      }

      long XX_configure_reg_transforms_start = 0;
      long XX_configure_reg_jcemapper_start = 0;
      long XX_configure_reg_sigalgos_start = 0;
      long XX_configure_reg_resourceresolver_end = 0;
      long XX_configure_reg_prefixes_end = 0;
      Node config = doc.getFirstChild();
      for (; config != null; config = config.getNextSibling()) {
        if ("Configuration".equals(config.getLocalName())) {
          break;
        }
      }
      for (Node el = config.getFirstChild(); el != null; el = el.getNextSibling()) {
        if (!(el instanceof Element)) {
          continue;
        }
        String tag = el.getLocalName();
        if (tag.equals("ResourceBundles")) {
          XX_configure_i18n_start = System.currentTimeMillis();
          Element resource = (Element) el;
          /* configure internationalization */
          Attr langAttr = resource.getAttributeNode("defaultLanguageCode");
          Attr countryAttr = resource.getAttributeNode("defaultCountryCode");
          String languageCode = (langAttr == null) ? null : langAttr.getNodeValue();
          String countryCode = (countryAttr == null) ? null : countryAttr.getNodeValue();

          I18n.init(languageCode, countryCode);
          XX_configure_i18n_end = System.currentTimeMillis();
        }

        if (tag.equals("CanonicalizationMethods")) {
          XX_configure_reg_c14n_start = System.currentTimeMillis();
          Canonicalizer.init();
          Element[] list =
              XMLUtils.selectNodes(el.getFirstChild(), CONF_NS, "CanonicalizationMethod");

          for (int i = 0; i < list.length; i++) {
            String URI = list[i].getAttributeNS(null, "URI");
            String JAVACLASS = list[i].getAttributeNS(null, "JAVACLASS");
            try {
              Class.forName(JAVACLASS);
              /*                     Method methods[] = c.getMethods();

              for (int j = 0; j < methods.length; j++) {
                 Method currMeth = methods[j];

                 if (currMeth.getDeclaringClass().getName()
                         .equals(JAVACLASS)) {
                    log.log(java.util.logging.Level.FINE, currMeth.getDe claringClass().toString());
                 }
              }*/
              if (true)
                log.log(
                    java.util.logging.Level.FINE,
                    "Canonicalizer.register(" + URI + ", " + JAVACLASS + ")");
              Canonicalizer.register(URI, JAVACLASS);
            } catch (ClassNotFoundException e) {
              Object exArgs[] = {URI, JAVACLASS};

              log.log(
                  java.util.logging.Level.SEVERE,
                  I18n.translate("algorithm.classDoesNotExist", exArgs));
            }
          }
          XX_configure_reg_c14n_end = System.currentTimeMillis();
        }

        if (tag.equals("TransformAlgorithms")) {
          XX_configure_reg_transforms_start = System.currentTimeMillis();
          Transform.init();

          Element[] tranElem =
              XMLUtils.selectNodes(el.getFirstChild(), CONF_NS, "TransformAlgorithm");

          for (int i = 0; i < tranElem.length; i++) {
            String URI = tranElem[i].getAttributeNS(null, "URI");
            String JAVACLASS = tranElem[i].getAttributeNS(null, "JAVACLASS");
            try {
              Class.forName(JAVACLASS);
              if (true)
                log.log(
                    java.util.logging.Level.FINE,
                    "Transform.register(" + URI + ", " + JAVACLASS + ")");
              Transform.register(URI, JAVACLASS);
            } catch (ClassNotFoundException e) {
              Object exArgs[] = {URI, JAVACLASS};

              log.log(
                  java.util.logging.Level.SEVERE,
                  I18n.translate("algorithm.classDoesNotExist", exArgs));

            } catch (NoClassDefFoundError ex) {
              log.log(
                  java.util.logging.Level.WARNING,
                  "Not able to found dependecies for algorithm, I'm keep working.");
            }
          }
          XX_configure_reg_transforms_end = System.currentTimeMillis();
        }

        if ("JCEAlgorithmMappings".equals(tag)) {
          XX_configure_reg_jcemapper_start = System.currentTimeMillis();
          JCEMapper.init((Element) el);
          XX_configure_reg_jcemapper_end = System.currentTimeMillis();
        }

        if (tag.equals("SignatureAlgorithms")) {
          XX_configure_reg_sigalgos_start = System.currentTimeMillis();
          SignatureAlgorithm.providerInit();

          Element[] sigElems =
              XMLUtils.selectNodes(el.getFirstChild(), CONF_NS, "SignatureAlgorithm");

          for (int i = 0; i < sigElems.length; i++) {
            String URI = sigElems[i].getAttributeNS(null, "URI");
            String JAVACLASS = sigElems[i].getAttributeNS(null, "JAVACLASS");

            /** $todo$ handle registering */
            try {
              Class.forName(JAVACLASS);
              //                    Method methods[] = c.getMethods();

              //                     for (int j = 0; j < methods.length; j++) {
              //                        Method currMeth = methods[j];
              //
              //                        if (currMeth.getDeclaringClass().getName()
              //                                .equals(JAVACLASS)) {
              //                           log.log(java.util.logging.Level.FINE, currMeth.getDe
              // claringClass().toString());
              //                        }
              //                     }
              if (true)
                log.log(
                    java.util.logging.Level.FINE,
                    "SignatureAlgorithm.register(" + URI + ", " + JAVACLASS + ")");
              SignatureAlgorithm.register(URI, JAVACLASS);
            } catch (ClassNotFoundException e) {
              Object exArgs[] = {URI, JAVACLASS};

              log.log(
                  java.util.logging.Level.SEVERE,
                  I18n.translate("algorithm.classDoesNotExist", exArgs));
            }
          }
          XX_configure_reg_sigalgos_end = System.currentTimeMillis();
        }

        if (tag.equals("ResourceResolvers")) {
          XX_configure_reg_resourceresolver_start = System.currentTimeMillis();
          ResourceResolver.init();

          Element[] resolverElem = XMLUtils.selectNodes(el.getFirstChild(), CONF_NS, "Resolver");

          for (int i = 0; i < resolverElem.length; i++) {
            String JAVACLASS = resolverElem[i].getAttributeNS(null, "JAVACLASS");
            String Description = resolverElem[i].getAttributeNS(null, "DESCRIPTION");

            if ((Description != null) && (Description.length() > 0)) {
              if (true)
                log.log(
                    java.util.logging.Level.FINE,
                    "Register Resolver: " + JAVACLASS + ": " + Description);
            } else {
              if (true)
                log.log(
                    java.util.logging.Level.FINE,
                    "Register Resolver: " + JAVACLASS + ": For unknown purposes");
            }
            try {
              ResourceResolver.register(JAVACLASS);
            } catch (Throwable e) {
              log.log(
                  java.util.logging.Level.WARNING,
                  "Cannot register:" + JAVACLASS + " perhaps some needed jars are not installed",
                  e);
            }
            XX_configure_reg_resourceresolver_end = System.currentTimeMillis();
          }
        }

        if (tag.equals("KeyResolver")) {
          XX_configure_reg_keyResolver_start = System.currentTimeMillis();
          KeyResolver.init();

          Element[] resolverElem = XMLUtils.selectNodes(el.getFirstChild(), CONF_NS, "Resolver");

          for (int i = 0; i < resolverElem.length; i++) {
            String JAVACLASS = resolverElem[i].getAttributeNS(null, "JAVACLASS");
            String Description = resolverElem[i].getAttributeNS(null, "DESCRIPTION");

            if ((Description != null) && (Description.length() > 0)) {
              if (true)
                log.log(
                    java.util.logging.Level.FINE,
                    "Register Resolver: " + JAVACLASS + ": " + Description);
            } else {
              if (true)
                log.log(
                    java.util.logging.Level.FINE,
                    "Register Resolver: " + JAVACLASS + ": For unknown purposes");
            }

            KeyResolver.register(JAVACLASS);
          }
          XX_configure_reg_keyResolver_end = System.currentTimeMillis();
        }

        if (tag.equals("PrefixMappings")) {
          XX_configure_reg_prefixes_start = System.currentTimeMillis();
          if (true) log.log(java.util.logging.Level.FINE, "Now I try to bind prefixes:");

          Element[] nl = XMLUtils.selectNodes(el.getFirstChild(), CONF_NS, "PrefixMapping");

          for (int i = 0; i < nl.length; i++) {
            String namespace = nl[i].getAttributeNS(null, "namespace");
            String prefix = nl[i].getAttributeNS(null, "prefix");
            if (true)
              log.log(
                  java.util.logging.Level.FINE, "Now I try to bind " + prefix + " to " + namespace);
            com.sun.org.apache.xml.internal.security.utils.ElementProxy.setDefaultPrefix(
                namespace, prefix);
          }
          XX_configure_reg_prefixes_end = System.currentTimeMillis();
        }
      }

      long XX_init_end = System.currentTimeMillis();

      // J-
      if (true) {
        log.log(
            java.util.logging.Level.FINE,
            "XX_init                             " + ((int) (XX_init_end - XX_init_start)) + " ms");
        log.log(
            java.util.logging.Level.FINE,
            "  XX_prng                           " + ((int) (XX_prng_end - XX_prng_start)) + " ms");
        log.log(
            java.util.logging.Level.FINE,
            "  XX_parsing                        "
                + ((int) (XX_parsing_end - XX_parsing_start))
                + " ms");
        log.log(
            java.util.logging.Level.FINE,
            "  XX_configure_i18n                 "
                + ((int) (XX_configure_i18n_end - XX_configure_i18n_start))
                + " ms");
        log.log(
            java.util.logging.Level.FINE,
            "  XX_configure_reg_c14n             "
                + ((int) (XX_configure_reg_c14n_end - XX_configure_reg_c14n_start))
                + " ms");
        log.log(
            java.util.logging.Level.FINE,
            "  XX_configure_reg_jcemapper        "
                + ((int) (XX_configure_reg_jcemapper_end - XX_configure_reg_jcemapper_start))
                + " ms");
        log.log(
            java.util.logging.Level.FINE,
            "  XX_configure_reg_keyInfo          "
                + ((int) (XX_configure_reg_keyInfo_end - XX_configure_reg_keyInfo_start))
                + " ms");
        log.log(
            java.util.logging.Level.FINE,
            "  XX_configure_reg_keyResolver      "
                + ((int) (XX_configure_reg_keyResolver_end - XX_configure_reg_keyResolver_start))
                + " ms");
        log.log(
            java.util.logging.Level.FINE,
            "  XX_configure_reg_prefixes         "
                + ((int) (XX_configure_reg_prefixes_end - XX_configure_reg_prefixes_start))
                + " ms");
        log.log(
            java.util.logging.Level.FINE,
            "  XX_configure_reg_resourceresolver "
                + ((int)
                    (XX_configure_reg_resourceresolver_end
                        - XX_configure_reg_resourceresolver_start))
                + " ms");
        log.log(
            java.util.logging.Level.FINE,
            "  XX_configure_reg_sigalgos         "
                + ((int) (XX_configure_reg_sigalgos_end - XX_configure_reg_sigalgos_start))
                + " ms");
        log.log(
            java.util.logging.Level.FINE,
            "  XX_configure_reg_transforms       "
                + ((int) (XX_configure_reg_transforms_end - XX_configure_reg_transforms_start))
                + " ms");
      }
    } catch (Exception e) {
      log.log(java.util.logging.Level.SEVERE, "Bad: ", e);
      e.printStackTrace();
    }
  }