Beispiel #1
0
  public static byte[] decode(byte[] b) throws Exception {
    ByteArrayInputStream bais = null;
    InputStream b64is = null;
    try {
      bais = new ByteArrayInputStream(b);
      b64is = MimeUtility.decode(bais, "base64");
      byte[] tmp = new byte[b.length];
      int n = b64is.read(tmp);
      byte[] res = new byte[n];
      System.arraycopy(tmp, 0, res, 0, n);

      return res;
    } catch (Exception e) {
      throw new Exception(e);
    } finally {
      try {
        if (bais != null) {
          bais.close();
          bais = null;
        }
      } catch (Exception e) {
      }
      try {
        if (b64is != null) {
          b64is.close();
          b64is = null;
        }
      } catch (Exception e) {
      }
    }
  }
  protected AddressBkDAO_DB() throws AddressBkDAOException {
    final ConfigDAO configDAO;
    try {
      configDAO = DAOFactory.getInstance().getConfigDAO();
      useDataSource = Boolean.valueOf(configDAO.getProperty("useDataSource")).booleanValue();
      logger.debug("useDataSource set to " + useDataSource);
      if (useDataSource) { // using DataSource
        final String dsjn = configDAO.getProperty("dataSourceJndiName");
        final Context ctx = new InitialContext();
        logger.debug("dataSourceJndiName: " + dsjn);
        dataSource = (DataSource) ctx.lookup(dsjn);
      } else { // using DriverManager
        final String dbDriverClassName = configDAO.getProperty("dbDriverClassName");
        Class.forName(dbDriverClassName).newInstance();
        jdbcUrl = configDAO.getProperty("jdbcUrl");
      }
    } catch (Exception e) {
      throw new AddressBkDAOException(e.toString());
    }
    dbUser = configDAO.getProperty("dbUser");
    dbCredentials = configDAO.getProperty("dbCredentials");
    if ("true".equals(configDAO.getProperty("passwordsBase64Encoded")) && dbCredentials != null) {
      try {
        final InputStream in =
            MimeUtility.decode(
                new ByteArrayInputStream(dbCredentials.getBytes("US-ASCII")), "base64");
        final BufferedReader br = new BufferedReader(new InputStreamReader(in));
        // XXX This assumes there is no new line in the password.
        dbCredentials = br.readLine();
        br.close();
      } catch (MessagingException e) {
        final AddressBkDAOException ae =
            new AddressBkDAOException("MessagingException: " + e.getMessage());
        ae.initCause(e);
        throw ae;

      } catch (UnsupportedEncodingException e) {
        final AddressBkDAOException ae =
            new AddressBkDAOException("UnsupportedEncodingException: " + e.getMessage());
        ae.initCause(e);
        throw ae;

      } catch (IOException e) {
        final AddressBkDAOException ae =
            new AddressBkDAOException("IOException: " + e.getMessage());
        ae.initCause(e);
        throw ae;
      }
    }
    if (dbUser == null
        || dbCredentials == null
        || dbUser.trim().equals("")
        || dbCredentials.trim().equals("")) useDMCredentials = false;
  }
  /**
   * Find Base64 encoded certificate used to sign given message. No default constructor: Once
   * content has been created, remains unchanged for life of the instance.
   *
   * @param msg (received) SOAP message to parse
   * @exception JAXRException if any problem at all occurs, wrapping problems decoding content (from
   *     Base64) and any caught CertificateException or SOAPException
   */
  public ReceivedCertificate(SOAPMessage msg) throws JAXRException {
    // @wss:Id attribute value for <BinarySecurityToken/> element of interest
    final String tokenId = CanonicalConstants.CANONICAL_URI_SENDER_CERT;

    try {
      final Name binSecTokenName =
          SOAPFactory.newInstance().createName("BinarySecurityToken", "wsse", securityNS);

      SOAPHeader hdr = msg.getSOAPHeader();
      Iterator hdrElemIter = hdr.examineAllHeaderElements();
      while (hdrElemIter.hasNext()) {
        Object hdrElemObj = hdrElemIter.next();
        if (hdrElemObj instanceof SOAPHeaderElement) {
          // found a SOAP header element of some type
          SOAPHeaderElement hdrElem = (SOAPHeaderElement) hdrElemObj;
          if ((hdrElem.getLocalName().equals("Security"))
              && (hdrElem.getNamespaceURI().equals(securityNS))) {

            // found a <wss:Security/> element
            //                        Name binSecTokenName = SOAPFactory.newInstance().
            //			    createName("BinarySecurityToken", "wsse", securityNS);
            Iterator secTokensIter = hdrElem.getChildElements(binSecTokenName);
            while (secTokensIter.hasNext()) {
              Object binSecTokenObj = secTokensIter.next();
              if (binSecTokenObj instanceof Element) {
                // found a <BinarySecurityToken/> element
                Element binSecTokenElem = (Element) binSecTokenObj;
                String _tokenId = binSecTokenElem.getAttributeNS(securityUtilityNS, "Id");
                if (_tokenId.equals(tokenId)) {
                  // found propery identified element
                  if (null == cert) {
                    // found first cert content
                    InputStream is = null;
                    String encodedData = binSecTokenElem.getFirstChild().getNodeValue();
                    try {
                      try {
                        is = new ByteArrayInputStream(encodedData.getBytes("UTF-8"));
                        is = MimeUtility.decode(is, "base64");
                      } catch (Exception e) {
                        throw new JAXRException(
                            CommonResourceBundle.getInstance()
                                .getString("message.UnableToDecodeData"),
                            e);
                      }

                      CertificateFactory cf = CertificateFactory.getInstance("X.509");
                      cert = (X509Certificate) cf.generateCertificate(is);
                    } finally {
                      if (is != null) {
                        try {
                          is.close();
                        } catch (Exception e) {
                        }
                      }
                    }
                  } else {
                    // found second cert content
                    foundMultiple = true;
                    break;
                  }
                }
              }
            }
          }
        }
      }
    } catch (SOAPException e) {
      throw new JAXRException(
          CommonResourceBundle.getInstance().getString("message.CouldNotGetCertificate"), e);
    } catch (CertificateException e) {
      throw new JAXRException(
          CommonResourceBundle.getInstance().getString("message.CouldNotGetCertificate"), e);
    }
  }