Exemplo n.º 1
0
static int get_days_remaining(ASN1_UTCTIME *tm)
{
    apr_time_t then, now = apr_time_now();
    apr_time_exp_t exp = {0};
    int diff;

    /* Fail if the time isn't a valid ASN.1 UTCTIME; RFC3280 mandates
     * that the seconds digits are present even though ASN.1
     * doesn't. */
    if (tm->length < 11 || !ASN1_UTCTIME_check(tm))
        return 0;

    exp.tm_year = DIGIT2NUM(tm->data);
    exp.tm_mon  = DIGIT2NUM(tm->data + 2) - 1;
    exp.tm_mday = DIGIT2NUM(tm->data + 4) + 1;
    exp.tm_hour = DIGIT2NUM(tm->data + 6);
    exp.tm_min  = DIGIT2NUM(tm->data + 8);
    exp.tm_sec  = DIGIT2NUM(tm->data + 10);

    if (exp.tm_year <= 50)
        exp.tm_year += 100;
    if (apr_time_exp_gmt_get(&then, &exp) != APR_SUCCESS)
        return 0;

    diff = (int)((apr_time_sec(then) - apr_time_sec(now)) / (60*60*24));
    return diff > 0 ? diff : 0;
}
Exemplo n.º 2
0
bool LLDate::fromStream(std::istream& s)
{
	struct apr_time_exp_t exp_time;
	apr_int32_t tm_part;
	int c;
	
	s >> tm_part;
	exp_time.tm_year = tm_part - 1900;
	c = s.get(); // skip the hypen
	if (c != '-') { return false; }
	s >> tm_part;
	exp_time.tm_mon = tm_part - 1;
	c = s.get(); // skip the hypen
	if (c != '-') { return false; }
	s >> tm_part;
	exp_time.tm_mday = tm_part;
	
	c = s.get(); // skip the T
	if (c != 'T') { return false; }
	
	s >> tm_part;
	exp_time.tm_hour = tm_part;
	c = s.get(); // skip the :
	if (c != ':') { return false; }
	s >> tm_part;
	exp_time.tm_min = tm_part;
	c = s.get(); // skip the :
	if (c != ':') { return false; }
	s >> tm_part;
	exp_time.tm_sec = tm_part;

	// zero out the unused fields
	exp_time.tm_usec = 0;
	exp_time.tm_wday = 0;
	exp_time.tm_yday = 0;
	exp_time.tm_isdst = 0;
	exp_time.tm_gmtoff = 0;

	// generate a time_t from that
	apr_time_t time;
	if (apr_time_exp_gmt_get(&time, &exp_time) != APR_SUCCESS)
	{
		return false;
	}
	
	F64 seconds_since_epoch = time / LL_APR_USEC_PER_SEC;

	// check for fractional
	c = s.peek();
	if(c == '.')
	{
		F64 fractional = 0.0;
		s >> fractional;
		seconds_since_epoch += fractional;
	}
Exemplo n.º 3
0
int lua_apr_time_implode(lua_State *L)
{
  apr_status_t status;
  apr_time_exp_t components = { 0 };
  apr_time_t time;

  time_check_exploded(L, 1, &components, 0);
  status = apr_time_exp_gmt_get(&time, &components);
  if (status != APR_SUCCESS)
    return push_error_status(L, status);

  return time_push(L, time);
}
Exemplo n.º 4
0
static void test_imp_gmt(CuTest *tc)
{
    apr_status_t rv;
    apr_time_exp_t xt;
    apr_time_t imp;

    rv = apr_time_exp_gmt(&xt, now);
    CuAssertTrue(tc, rv == APR_SUCCESS);
    rv = apr_time_exp_gmt_get(&imp, &xt);
    if (rv == APR_ENOTIMPL) {
        CuNotImpl(tc, "apr_time_exp_gmt_get");
    }
    CuAssertTrue(tc, rv == APR_SUCCESS);
    CuAssertTrue(tc, now == imp);
}
Exemplo n.º 5
0
SWITCH_DECLARE(switch_status_t) switch_time_exp_gmt_get(switch_time_t *result, switch_time_exp_t *input)
{
	return apr_time_exp_gmt_get((apr_time_t *) result, (apr_time_exp_t *) input);
}
Exemplo n.º 6
0
svn_error_t *
svn_time_from_cstring(apr_time_t *when, const char *data, apr_pool_t *pool)
{
    apr_time_exp_t exploded_time;
    apr_status_t apr_err;
    char wday[4], month[4];
    char *c;

    /* Open-code parsing of the new timestamp format, as this
       is a hot path for reading the entries file.  This format looks
       like:  "2001-08-31T04:24:14.966996Z"  */
    exploded_time.tm_year = strtol(data, &c, 10);
    if (*c++ != '-') goto fail;
    exploded_time.tm_mon = strtol(c, &c, 10);
    if (*c++ != '-') goto fail;
    exploded_time.tm_mday = strtol(c, &c, 10);
    if (*c++ != 'T') goto fail;
    exploded_time.tm_hour = strtol(c, &c, 10);
    if (*c++ != ':') goto fail;
    exploded_time.tm_min = strtol(c, &c, 10);
    if (*c++ != ':') goto fail;
    exploded_time.tm_sec = strtol(c, &c, 10);
    if (*c++ != '.') goto fail;
    exploded_time.tm_usec = strtol(c, &c, 10);
    if (*c++ != 'Z') goto fail;

    exploded_time.tm_year  -= 1900;
    exploded_time.tm_mon   -= 1;
    exploded_time.tm_wday   = 0;
    exploded_time.tm_yday   = 0;
    exploded_time.tm_isdst  = 0;
    exploded_time.tm_gmtoff = 0;

    apr_err = apr_time_exp_gmt_get(when, &exploded_time);
    if (apr_err == APR_SUCCESS)
        return SVN_NO_ERROR;

    return svn_error_create(SVN_ERR_BAD_DATE, NULL, NULL);

fail:
    /* Try the compatibility option.  This does not need to be fast,
       as this format is no longer generated and the client will convert
       an old-format entries file the first time it reads it.  */
    if (sscanf(data,
               OLD_TIMESTAMP_FORMAT,
               wday,
               &exploded_time.tm_mday,
               month,
               &exploded_time.tm_year,
               &exploded_time.tm_hour,
               &exploded_time.tm_min,
               &exploded_time.tm_sec,
               &exploded_time.tm_usec,
               &exploded_time.tm_yday,
               &exploded_time.tm_isdst,
               &exploded_time.tm_gmtoff) == 11)
    {
        exploded_time.tm_year -= 1900;
        exploded_time.tm_yday -= 1;
        /* Using hard coded limits for the arrays - they are going away
           soon in any case. */
        exploded_time.tm_wday = find_matching_string(wday, 7, apr_day_snames);
        exploded_time.tm_mon = find_matching_string(month, 12, apr_month_snames);

        apr_err = apr_time_exp_gmt_get(when, &exploded_time);
        if (apr_err != APR_SUCCESS)
            return svn_error_create(SVN_ERR_BAD_DATE, NULL, NULL);

        return SVN_NO_ERROR;
    }
    /* Timestamp is something we do not recognize. */
    else
        return svn_error_create(SVN_ERR_BAD_DATE, NULL, NULL);
}
Exemplo n.º 7
0
/* Retrieve the date from the X.509 cert data between *P and END in either
 * UTCTime or GeneralizedTime format (as defined in RFC 5280 s. 4.1.2.5.1 and
 * 4.1.2.5.2 respectively) and place the result in WHEN using  SCRATCH_POOL
 * for temporary allocations. */
static svn_error_t *
x509_get_date(apr_time_t *when,
              const unsigned char **p,
              const unsigned char *end,
              apr_pool_t *scratch_pool)
{
  svn_error_t *err;
  apr_status_t ret;
  int tag;
  ptrdiff_t len;
  char *date;
  apr_time_exp_t xt = { 0 };
  char tz;

  err = asn1_get_tag(p, end, &len, ASN1_UTC_TIME);
  if (err && err->apr_err == SVN_ERR_ASN1_UNEXPECTED_TAG)
    {
      svn_error_clear(err);
      err = asn1_get_tag(p, end, &len, ASN1_GENERALIZED_TIME);
      tag = ASN1_GENERALIZED_TIME;
    }
  else
    {
      tag = ASN1_UTC_TIME;
    }
  if (err)
    return svn_error_create(SVN_ERR_X509_CERT_INVALID_DATE, err, NULL);

  date = apr_pstrndup(scratch_pool, (const char *) *p, len);
  switch (tag)
    {
    case ASN1_UTC_TIME:
      if (sscanf(date, "%2d%2d%2d%2d%2d%2d%c",
                 &xt.tm_year, &xt.tm_mon, &xt.tm_mday,
                 &xt.tm_hour, &xt.tm_min, &xt.tm_sec, &tz) < 6)
        return svn_error_create(SVN_ERR_X509_CERT_INVALID_DATE, NULL, NULL);

      /* UTCTime only provides a 2 digit year.  X.509 specifies that years
       * greater than or equal to 50 must be interpreted as 19YY and years
       * less than 50 be interpreted as 20YY.  This format is not used for
       * years greater than 2049. apr_time_exp_t wants years as the number
       * of years since 1900, so don't convert to 4 digits here. */
      xt.tm_year += 100 * (xt.tm_year < 50);
      break;

    case ASN1_GENERALIZED_TIME:
      if (sscanf(date, "%4d%2d%2d%2d%2d%2d%c",
                 &xt.tm_year, &xt.tm_mon, &xt.tm_mday,
                 &xt.tm_hour, &xt.tm_min, &xt.tm_sec, &tz) < 6)
        return svn_error_create(SVN_ERR_X509_CERT_INVALID_DATE, NULL, NULL);

      /* GeneralizedTime has the full 4 digit year.  But apr_time_exp_t
       * wants years as the number of years since 1900. */
      xt.tm_year -= 1900;
      break;

    default:
      /* shouldn't ever get here because we should error out above in the
       * asn1_get_tag() bits but doesn't hurt to be extra paranoid. */
      return svn_error_create(SVN_ERR_X509_CERT_INVALID_DATE, NULL, NULL);
      break;
    }

  /* check that the timezone is GMT
   * ASN.1 allows for the timezone to be specified but X.509 says it must
   * always be GMT.  A little bit of extra paranoia here seems like a good
   * idea. */
  if (tz != 'Z')
    return svn_error_create(SVN_ERR_X509_CERT_INVALID_DATE, NULL, NULL);

  /* apr_time_exp_t expects months to be zero indexed, 0=Jan, 11=Dec. */
  xt.tm_mon -= 1;

  /* range checks (as per definition of apr_time_exp_t in apr_time.h) */
  if (xt.tm_usec < 0 ||
      xt.tm_sec < 0 || xt.tm_sec > 61 ||
      xt.tm_min < 0 || xt.tm_min > 59 ||
      xt.tm_hour < 0 || xt.tm_hour > 23 ||
      xt.tm_mday < 1 || xt.tm_mday > 31 ||
      xt.tm_mon < 0 || xt.tm_mon > 11 ||
      xt.tm_year < 0 ||
      xt.tm_wday < 0 || xt.tm_wday > 6 ||
      xt.tm_yday < 0 || xt.tm_yday > 365)
    return svn_error_create(SVN_ERR_X509_CERT_INVALID_DATE, NULL, NULL);

  ret = apr_time_exp_gmt_get(when, &xt);
  if (ret)
    return svn_error_wrap_apr(ret, NULL);

  *p += len;

  return SVN_NO_ERROR;
}
Exemplo n.º 8
0
/* Deprecated */
APR_DECLARE(apr_status_t) apr_implode_gmt(apr_time_t *t,
                                          apr_time_exp_t *xt)
{
    return apr_time_exp_gmt_get(t, xt);
}
Exemplo n.º 9
0
static int
badge_get_time_arg(time_t * result, badge_timestamp * ts, int yearerr,
	int montherr, int dayerr, int hourerr, int minerr, int secerr)

{
	apr_time_exp_t exptime;
	apr_time_t aprtime;
	long l;
	char * cp;
	int errflags;

	/**
	***	Convert time components in the given timestamp into a
	***		time_t. Return ored-in error flags.
	**/

	memset((char *) &exptime, 0, sizeof exptime);
	errflags = 0;

	if (ts->year && *ts->year &&
	    (l = strtol(ts->year, &cp, 10)) >= 0 && !*cp) {
		if (l >= 1900)
			l -= 1900;

		exptime.tm_year = l;
		}
	else
		errflags |= yearerr;

	if (ts->month && *ts->month &&
	    (l = strtol(ts->month, &cp, 10)) > 0 && !*cp && l  <= 12)
		exptime.tm_mon = l - 1;
	else
		errflags |= montherr;

	if (ts->day && *ts->day &&
	    (l = strtol(ts->day, &cp, 10)) > 0 && !*cp && l <= 31)
		exptime.tm_mday = l;
	else
		errflags |= dayerr;

	if (ts->hour && *ts->hour &&
	    (l = strtol(ts->hour, &cp, 10)) >= 0 && !*cp && l <= 23)
		exptime.tm_hour = l;
	else
		errflags |= hourerr;

	if (ts->min && *ts->min &&
	    (l = strtol(ts->min, &cp, 10)) >= 0 && !*cp && l <= 59)
		exptime.tm_min = l;
	else
		errflags |= minerr;

	if (ts->sec && *ts->sec &&
	    (l = strtol(ts->sec, &cp, 10)) >= 0 && !*cp && l <= 59)
		exptime.tm_sec = l;
	else
		errflags |= secerr;

	if (errflags)
		return errflags;

	if (apr_time_exp_gmt_get(&aprtime, &exptime) == OK) {
		*result = apr_time_sec(aprtime);

		/* Check for overflow. */

		if (*result == apr_time_sec(aprtime))
			return 0;
		}

	return yearerr | montherr | dayerr;	/* Date problem. */
}