ASN1_GENERALIZEDTIME * ASN1_TIME_to_generalizedtime(ASN1_TIME *t, ASN1_GENERALIZEDTIME **out) { ASN1_GENERALIZEDTIME *tmp = NULL; struct tm tm; char *str; if (t->type != V_ASN1_GENERALIZEDTIME && t->type != V_ASN1_UTCTIME) return (NULL); memset(&tm, 0, sizeof(tm)); if (t->type != asn1_time_parse(t->data, t->length, &tm, t->type)) return (NULL); if ((str = gentime_string_from_tm(&tm)) == NULL) return (NULL); if (out != NULL) tmp = *out; if (tmp == NULL && (tmp = ASN1_GENERALIZEDTIME_new()) == NULL) { free(str); return (NULL); } if (out != NULL) *out = tmp; free(tmp->data); tmp->data = str; tmp->length = strlen(str); return (tmp); }
ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s, time_t t, int offset_day, long offset_sec) { char *p; struct tm *ts; struct tm data; size_t len = 20; ASN1_GENERALIZEDTIME *tmps = NULL; if (s == NULL) tmps = ASN1_GENERALIZEDTIME_new(); else tmps = s; if (tmps == NULL) return NULL; ts = OPENSSL_gmtime(&t, &data); if (ts == NULL) goto err; if (offset_day || offset_sec) { if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec)) goto err; } p = (char *)tmps->data; if ((p == NULL) || ((size_t)tmps->length < len)) { p = OPENSSL_malloc(len); if (p == NULL) { ASN1err(ASN1_F_ASN1_GENERALIZEDTIME_ADJ, ERR_R_MALLOC_FAILURE); goto err; } OPENSSL_free(tmps->data); tmps->data = (unsigned char *)p; } BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ", ts->tm_year + 1900, ts->tm_mon + 1, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec); tmps->length = strlen(p); tmps->type = V_ASN1_GENERALIZEDTIME; #ifdef CHARSET_EBCDIC_not ebcdic2ascii(tmps->data, tmps->data, tmps->length); #endif return tmps; err: if (s == NULL) ASN1_GENERALIZEDTIME_free(tmps); return NULL; }
/* Convert an ASN1_TIME structure to GeneralizedTime */ ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(const ASN1_TIME *t, ASN1_GENERALIZEDTIME **out) { ASN1_GENERALIZEDTIME *ret = NULL; char *str; int newlen; if (!ASN1_TIME_check(t)) return NULL; if (out == NULL || *out == NULL) { if ((ret = ASN1_GENERALIZEDTIME_new()) == NULL) goto err; } else ret = *out; /* If already GeneralizedTime just copy across */ if (t->type == V_ASN1_GENERALIZEDTIME) { if (!ASN1_STRING_set(ret, t->data, t->length)) goto err; goto done; } /* grow the string */ if (!ASN1_STRING_set(ret, NULL, t->length + 2)) goto err; /* ASN1_STRING_set() allocated 'len + 1' bytes. */ newlen = t->length + 2 + 1; str = (char *)ret->data; /* Work out the century and prepend */ if (t->data[0] >= '5') OPENSSL_strlcpy(str, "19", newlen); else OPENSSL_strlcpy(str, "20", newlen); OPENSSL_strlcat(str, (const char *)t->data, newlen); done: if (out != NULL && *out == NULL) *out = ret; return ret; err: if (out == NULL || *out != ret) ASN1_GENERALIZEDTIME_free(ret); return NULL; }
static void timestamp_print(BIO *out, uint64_t timestamp) { ASN1_GENERALIZEDTIME *gen; char genstr[20]; gen = ASN1_GENERALIZEDTIME_new(); ASN1_GENERALIZEDTIME_adj(gen, (time_t)0, (int)(timestamp / 86400000), (timestamp % 86400000) / 1000); /* * Note GeneralizedTime from ASN1_GENERALIZETIME_adj is always 15 * characters long with a final Z. Update it with fractional seconds. */ BIO_snprintf(genstr, sizeof(genstr), "%.14s.%03dZ", ASN1_STRING_data(gen), (unsigned int)(timestamp % 1000)); ASN1_GENERALIZEDTIME_set_string(gen, genstr); ASN1_GENERALIZEDTIME_print(out, gen); ASN1_GENERALIZEDTIME_free(gen); }
ASN1_GENERALIZEDTIME * ASN1_TIME_to_generalizedtime(ASN1_TIME *t, ASN1_GENERALIZEDTIME **out) { ASN1_GENERALIZEDTIME *tmp = NULL, *ret; if (!out || !*out) { if (!(tmp = ASN1_GENERALIZEDTIME_new())) return NULL; if (out != NULL) *out = tmp; else out = &tmp; } ret = ASN1_TIME_to_generalizedtime_internal(t, out); if (ret == NULL && tmp != NULL) ASN1_GENERALIZEDTIME_free(tmp); return ret; }
/* Convert an ASN1_TIME structure to GeneralizedTime */ ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(ASN1_TIME *t, ASN1_GENERALIZEDTIME **out) { ASN1_GENERALIZEDTIME *ret; char *str; int newlen; if (!ASN1_TIME_check(t)) return NULL; if (!out || !*out) { if (!(ret = ASN1_GENERALIZEDTIME_new())) return NULL; if (out) *out = ret; } else ret = *out; /* If already GeneralizedTime just copy across */ if (t->type == V_ASN1_GENERALIZEDTIME) { if (!ASN1_STRING_set(ret, t->data, t->length)) return NULL; return ret; } /* grow the string */ if (!ASN1_STRING_set(ret, NULL, t->length + 2)) return NULL; /* ASN1_STRING_set() allocated 'len + 1' bytes. */ newlen = t->length + 2 + 1; str = (char *)ret->data; /* Work out the century and prepend */ if (t->data[0] >= '5') BUF_strlcpy(str, "19", newlen); else BUF_strlcpy(str, "20", newlen); BUF_strlcat(str, (char *)t->data, newlen); return ret; }
ASN1_TIME* DateTime::getGeneralizedTime() const throw(BigIntegerException) { ASN1_TIME *ret; DateVal date; stringstream stream; string gt; date = DateTime::getDate(this->seconds); stream.setf(ios_base::right); stream.fill('0'); stream.width(4); //no maximo 4 digitos para ano stream << date.year; stream.width(2); stream << (date.mon + 1); stream.width(2); stream << date.dayOfMonth; stream.width(2); stream << date.hour; stream.width(2); stream << date.min; stream.width(2); stream << date.sec; stream.width(1); stream << "Z"; gt = stream.str(); ret = ASN1_GENERALIZEDTIME_new(); //pode retornar 1 no caso de falha de alocacao de memoria ASN1_STRING_set(ret, gt.c_str(), gt.size()); return ret; }