Package org.ejbca.core.model.ca

Examples of org.ejbca.core.model.ca.MalformedRequestException


      m_log.debug(">checkAndGetRequestBytes. Received "+method+" request with content length: "+n+" from "+remoteAddress);   
    }
    if (n > LimitLengthASN1Reader.MAX_REQUEST_SIZE) {
      String msg = intres.getLocalizedMessage("ocsp.toolarge", LimitLengthASN1Reader.MAX_REQUEST_SIZE, n);
      m_log.info(msg);
      throw new MalformedRequestException(msg);
    }
    // So we passed basic tests, now we can read the bytes, but still keep an eye on the size
    // we can not fully trust the sent content length.
    if (StringUtils.equals(method, "POST")) {
      final ServletInputStream in = request.getInputStream(); // ServletInputStream does not have to be closed, container handles this
      ret = new LimitLengthASN1Reader(in, n).readFirstASN1Object();
      if (n > ret.length) {
        // The client is sending more data than the OCSP request. It might be slightly broken or trying to bog down the server on purpose.
        // In the interest of not breaking existing systems that might have slightly broken clients we just log for a warning for now.
        String msg = intres.getLocalizedMessage("ocsp.additionaldata", ret.length, n);
        m_log.warn(msg);
        //throw new MalformedRequestException(msg);  // Responding with MALFORMED_REQUEST.
      }
    } else if (StringUtils.equals(method, "GET")) {
      // GET request
      final StringBuffer url = request.getRequestURL();
      // RFC2560 A.1.1 says that request longer than 255 bytes SHOULD be sent by POST, we support GET for longer requests anyway.
      if (url.length() <= LimitLengthASN1Reader.MAX_REQUEST_SIZE) {
        final String decodedRequest;
        try {
          // We have to extract the pathInfo manually, to avoid multiple slashes being converted to a single
          // According to RFC 2396 2.2 chars only have to encoded if they conflict with the purpose, so
          // we can for example expect both '/' and "%2F" in the request.
          final String fullServletpath = request.getContextPath() + request.getServletPath();
          final int paramIx = Math.max(url.indexOf(fullServletpath), 0) + fullServletpath.length() + 1;
          final String requestString = paramIx<url.length() ? url.substring(paramIx) : "";
          decodedRequest = URLDecoder.decode(requestString, "UTF-8").replaceAll(" ", "+");
          //            if (m_log.isDebugEnabled()) {
          //              m_log.debug("URL: "+url.toString());
          //            }
        } catch (Exception e) {
          String msg = intres.getLocalizedMessage("ocsp.badurlenc");
          m_log.info(msg);
          throw new MalformedRequestException(e);
        }
        if (decodedRequest != null && decodedRequest.length() > 0) {
          if (m_log.isDebugEnabled()) {
            // Don't log the request if it's too long, we don't want to cause denial of service by filling log files or buffers.
            if (decodedRequest.length() < 2048) {
              m_log.debug("decodedRequest: "+decodedRequest);
            } else {
              m_log.debug("decodedRequest too long to log: "+decodedRequest.length());
            }
          }
          try {
            ret = org.ejbca.util.Base64.decode(decodedRequest.getBytes());
          } catch (Exception e) {
            String msg = intres.getLocalizedMessage("ocsp.badurlenc");
            m_log.info(msg);
            throw new MalformedRequestException(e);
          }
        } else {
          String msg = intres.getLocalizedMessage("ocsp.missingreq");
          m_log.info(msg);
          throw new MalformedRequestException(msg);
        }
      } else {
        String msg = intres.getLocalizedMessage("ocsp.toolarge", LimitLengthASN1Reader.MAX_REQUEST_SIZE, url.length());
        m_log.info(msg);
        throw new MalformedRequestException(msg);
      }
    } else {
      // Strange, an unknown method
      String msg = intres.getLocalizedMessage("ocsp.unknownmethod", method);
      m_log.info(msg);
      throw new MalformedRequestException(msg);
    }
    // Make a final check that we actually received something
    if ((ret == null) || (ret.length == 0)) {
      String msg = intres.getLocalizedMessage("ocsp.emptyreq", remoteAddress);
      m_log.info(msg);
      throw new MalformedRequestException(msg);
    }
    return ret;
  }
View Full Code Here


        OCSPReq req = null;
        try {
          req = new OCSPReq(reqBytes);         
        } catch (Exception e) {
          // When not being able to parse the request, we want to send a MalformedRequest back
          throw new MalformedRequestException(e);
        }
        if (req.getRequestorName() == null) {
          m_log.debug("Requestorname is null");
        } else {
          if (m_log.isDebugEnabled()) {
            m_log.debug("Requestorname is: "+req.getRequestorName().toString());           
          }
          transactionLogger.paramPut(ITransactionLogger.REQ_NAME, req.getRequestorName().toString());
        }
        // Make sure our signature keys are updated
        loadPrivateKeys(this.data.m_adm, null);

        /**
         * check the signature if contained in request.
         * if the request does not contain a signature
         * and the servlet is configured in the way
         * the a signature is required we send back
         * 'sigRequired' response.
         */
        if (m_log.isDebugEnabled()) {
          m_log.debug("Incoming OCSP request is signed : " + req.isSigned());
        }
        if (req.isSigned()) {
          X509Certificate signercert = OCSPUtil.checkRequestSignature(request.getRemoteAddr(), req, this.data.m_caCertCache);
          String signercertIssuerName = CertTools.getIssuerDN(signercert);
          BigInteger signercertSerNo = CertTools.getSerialNumber(signercert);
          String signercertSubjectName = CertTools.getSubjectDN(signercert);
          transactionLogger.paramPut(ITransactionLogger.SIGN_ISSUER_NAME_DN, signercertIssuerName);
          transactionLogger.paramPut(ITransactionLogger.SIGN_SERIAL_NO, signercert.getSerialNumber().toByteArray());
          transactionLogger.paramPut(ITransactionLogger.SIGN_SUBJECT_NAME, signercertSubjectName);
          transactionLogger.paramPut(IPatternLogger.REPLY_TIME, ITransactionLogger.REPLY_TIME);
          if (OcspConfiguration.getEnforceRequestSigning()) {
            // If it verifies OK, check if it is revoked
            final CertificateStatus status = this.data.certificateStoreSession.getStatus(CertTools.getIssuerDN(signercert), CertTools.getSerialNumber(signercert));
            // If rci == null it means the certificate does not exist in database, we then treat it as ok,
            // because it may be so that only revoked certificates is in the (external) OCSP database.
            if ( status.equals(CertificateStatus.REVOKED) ) {
              String serno = signercertSerNo.toString(16);
              String infoMsg = intres.getLocalizedMessage("ocsp.infosigner.revoked", signercertSubjectName, signercertIssuerName, serno);
              m_log.info(infoMsg);
              throw new SignRequestSignatureException(infoMsg);
            }

            if (m_reqRestrictSignatures) {
              loadTrustDir();
              if ( m_reqRestrictMethod == OcspConfiguration.RESTRICTONSIGNER) {
                if (!OCSPUtil.checkCertInList(signercert, mTrustedReqSigSigners)) {
                  String infoMsg = intres.getLocalizedMessage("ocsp.infosigner.notallowed", signercertSubjectName, signercertIssuerName, signercertSerNo.toString(16));
                  m_log.info(infoMsg);
                  throw new SignRequestSignatureException(infoMsg);
                }
              } else if (m_reqRestrictMethod == OcspConfiguration.RESTRICTONISSUER) {
                X509Certificate signerca = this.data.m_caCertCache.findLatestBySubjectDN(HashID.getFromDN(signercertIssuerName));
                if ((signerca == null) || (!OCSPUtil.checkCertInList(signerca, mTrustedReqSigIssuers)) ) {
                  String infoMsg = intres.getLocalizedMessage("ocsp.infosigner.notallowed", signercertSubjectName, signercertIssuerName, signercertSerNo.toString(16));
                  m_log.info(infoMsg);
                  throw new SignRequestSignatureException(infoMsg);
                }
              } else {
                throw new Exception("m_reqRestrictMethod="+m_reqRestrictMethod); // there must be an internal error. We do not want to send a response, just to be safe.
              }
            }
          }
        } else {
          if (OcspConfiguration.getEnforceRequestSigning()) {
            // Signature required
            throw new SignRequestException("Signature required");
          }
        }
       
        // Get the certificate status requests that are inside this OCSP req
        Req[] requests = req.getRequestList();
        transactionLogger.paramPut(ITransactionLogger.NUM_CERT_ID, requests.length);
        if (requests.length <= 0) {
          String infoMsg = intres.getLocalizedMessage("ocsp.errornoreqentities");
          m_log.info(infoMsg);
          {
            // All this just so we can create an error response
            cacert = this.data.m_caCertCache.findLatestBySubjectDN(HashID.getFromDN(this.data.m_defaultResponderId));
          }
          throw new MalformedRequestException(infoMsg);
        }
        int maxRequests = 100;
        if (requests.length > maxRequests) {
          String infoMsg = intres.getLocalizedMessage("ocsp.errortoomanyreqentities", maxRequests);
          m_log.info(infoMsg);
          {
            // All this just so we can create an error response
            cacert = this.data.m_caCertCache.findLatestBySubjectDN(HashID.getFromDN(this.data.m_defaultResponderId));
          }
          throw new MalformedRequestException(infoMsg);
        }

        if (m_log.isDebugEnabled()) {
          m_log.debug("The OCSP request contains " + requests.length + " simpleRequests.");
        }
View Full Code Here

    final byte value[] = new byte[length];
    final int readLength = read(value);
    if ( readLength != length ) {
      final String msg = intres.getLocalizedMessage("request.notcorrectasn1length", Integer.valueOf(length), Integer.valueOf(readLength));
      m_log.info(msg);
      throw new MalformedRequestException(msg);
    }
    this.baos.write(value);
    this.baos.flush();
    return this.baos.toByteArray();
  }
View Full Code Here

  public byte[] readFirstASN1Object() throws IOException, MalformedRequestException {
    final int tag = read() & 0x1f;
    if (tag != SEQUENCE) {
      final String msg = intres.getLocalizedMessage("request.notasequence", Integer.valueOf(tag));
      m_log.info(msg);
      throw new MalformedRequestException(msg);
    }
    final int length = readLength();
    if (length > MAX_REQUEST_SIZE) {
      final String msg = intres.getLocalizedMessage("request.toolarge", Integer.valueOf(MAX_REQUEST_SIZE), Integer.valueOf(length));
      m_log.info(msg);
      throw new MalformedRequestException(msg);
    }
    // If there was an asn.1 stream of undefined length we will try to read it the classic way, limiting the size of bytes read.
    if (length < 0) {// undefined length
      if (this.contentLength > MAX_REQUEST_SIZE) {
        final String msg = intres.getLocalizedMessage("request.toolarge", Integer.valueOf(MAX_REQUEST_SIZE), Integer.valueOf(this.baos.size()));
        m_log.info(msg);
        throw new MalformedRequestException(msg);
      }
      final int tlByteLength = this.baos.toByteArray().length;
      if (this.contentLength < tlByteLength) { // Content-length invalid. Try to read although.
        if (m_log.isTraceEnabled()) {
          m_log.trace("No content-length, reading as much as we have (<MAX_REQUEST_SIZE)");
View Full Code Here

TOP

Related Classes of org.ejbca.core.model.ca.MalformedRequestException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.