Skip to content

Commit

Permalink
Flense the greasy black guts of unreadble string parsing code out of …
Browse files Browse the repository at this point in the history
…three areas

in asn1 and x509 code, all dealing with an ASN1_TIME. This brings the parsing
together in one function that converts into a struct tm. While we are at it this
also brings us into conformance with RFC 5280 for times allowed in an X509 cert,
as OpenSSL is very liberal with what it allows.
input and fixes from deraadt@ jsing@ guethther@ and others.
ok krw@, guenther@, jsing@
  • Loading branch information
beck committed Oct 2, 2015
1 parent c1746a5 commit de5a7bf
Show file tree
Hide file tree
Showing 8 changed files with 334 additions and 272 deletions.
3 changes: 2 additions & 1 deletion src/lib/libcrypto/crypto/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# $OpenBSD: Makefile,v 1.64 2015/09/13 23:36:21 doug Exp $
# $OpenBSD: Makefile,v 1.65 2015/09/14 01:45:03 doug Exp $

LIB= crypto

Expand Down Expand Up @@ -49,6 +49,7 @@ SRCS+= f_enum.c x_pkey.c a_bool.c x_exten.c bio_asn1.c bio_ndef.c asn_mime.c
SRCS+= asn1_gen.c asn1_par.c asn1_lib.c asn1_err.c a_bytes.c a_strnid.c
SRCS+= evp_asn1.c asn_pack.c p5_pbe.c p5_pbev2.c p8_pkey.c asn_moid.c
SRCS+= a_set.c
SRCS+= a_time_tm.c

# bf/
SRCS+= bf_skey.c bf_ecb.c bf_cfb64.c bf_ofb64.c
Expand Down
106 changes: 13 additions & 93 deletions src/lib/libssl/src/crypto/asn1/a_gentm.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: a_gentm.c,v 1.23 2015/02/07 13:19:15 doug Exp $ */
/* $OpenBSD: a_gentm.c,v 1.24 2015/09/30 18:04:02 jsing Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
Expand Down Expand Up @@ -66,86 +66,14 @@
#include <openssl/err.h>

#include "o_time.h"
#include "asn1_locl.h"

int
ASN1_GENERALIZEDTIME_check(ASN1_GENERALIZEDTIME *d)
{
static const int min[9] = {0, 0, 1, 1, 0, 0, 0, 0, 0};
static const int max[9] = {99, 99, 12, 31, 23, 59, 59, 12, 59};
char *a;
int n, i, l, o;

if (d->type != V_ASN1_GENERALIZEDTIME)
return (0);
l = d->length;
a = (char *)d->data;
o = 0;
/* GENERALIZEDTIME is similar to UTCTIME except the year is
* represented as YYYY. This stuff treats everything as a two digit
* field so make first two fields 00 to 99
*/
if (l < 13)
goto err;
for (i = 0; i < 7; i++) {
if ((i == 6) && ((a[o] == 'Z') ||
(a[o] == '+') || (a[o] == '-'))) {
i++;
break;
}
if ((a[o] < '0') || (a[o] > '9'))
goto err;
n= a[o]-'0';
if (++o > l)
goto err;

if ((a[o] < '0') || (a[o] > '9'))
goto err;
n = (n * 10)+ a[o] - '0';
if (++o > l)
goto err;

if ((n < min[i]) || (n > max[i]))
goto err;
}
/* Optional fractional seconds: decimal point followed by one
* or more digits.
*/
if (a[o] == '.') {
if (++o > l)
goto err;
i = o;
while ((a[o] >= '0') && (a[o] <= '9') && (o <= l))
o++;
/* Must have at least one digit after decimal point */
if (i == o)
goto err;
}

if (a[o] == 'Z')
o++;
else if ((a[o] == '+') || (a[o] == '-')) {
o++;
if (o + 4 > l)
goto err;
for (i = 7; i < 9; i++) {
if ((a[o] < '0') || (a[o] > '9'))
goto err;
n = a[o] - '0';
o++;
if ((a[o] < '0') || (a[o] > '9'))
goto err;
n = (n * 10) + a[o] - '0';
if ((n < min[i]) || (n > max[i]))
goto err;
o++;
}
} else {
/* Missing time zone information. */
goto err;
}
return (o == l);
err:
return (0);
return (d->type == asn1_time_parse(d->data, d->length, NULL, d->type));
}

int
Expand Down Expand Up @@ -179,34 +107,26 @@ ASN1_GENERALIZEDTIME_adj_internal(ASN1_GENERALIZEDTIME *s, time_t t,
int offset_day, long offset_sec)
{
char *p;
struct tm *ts;
struct tm *tm;
struct tm data;
size_t len = 20;

ts = gmtime_r(&t, &data);
if (ts == NULL)
tm = gmtime_r(&t, &data);
if (tm == NULL)
return (NULL);

if (offset_day || offset_sec) {
if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
if (!OPENSSL_gmtime_adj(tm, offset_day, offset_sec))
return NULL;
}

p = (char *)s->data;
if ((p == NULL) || ((size_t)s->length < len)) {
p = malloc(len);
if (p == NULL) {
ASN1err(ASN1_F_ASN1_GENERALIZEDTIME_ADJ,
ERR_R_MALLOC_FAILURE);
return (NULL);
}
free(s->data);
s->data = (unsigned char *)p;
if ((p = gentime_string_from_tm(tm)) == NULL) {
ASN1err(ASN1_F_ASN1_GENERALIZEDTIME_ADJ, ERR_R_MALLOC_FAILURE);
return (NULL);
}

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);
free(s->data);
s->data = p;
s->length = strlen(p);

s->type = V_ASN1_GENERALIZEDTIME;
return (s);
}
Expand Down
25 changes: 11 additions & 14 deletions src/lib/libssl/src/crypto/asn1/a_time.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: a_time.c,v 1.24 2015/07/24 13:49:58 jsing Exp $ */
/* $OpenBSD: a_time.c,v 1.25 2015/09/30 18:04:02 jsing Exp $ */
/* ====================================================================
* Copyright (c) 1999 The OpenSSL Project. All rights reserved.
*
Expand Down Expand Up @@ -68,7 +68,7 @@
#include <openssl/err.h>

#include "o_time.h"

#include "asn1_locl.h"

const ASN1_ITEM ASN1_TIME_it = {
.itype = ASN1_ITYPE_MSTRING,
Expand Down Expand Up @@ -135,11 +135,9 @@ ASN1_TIME_adj(ASN1_TIME *s, time_t t, int offset_day, long offset_sec)
int
ASN1_TIME_check(ASN1_TIME *t)
{
if (t->type == V_ASN1_GENERALIZEDTIME)
return ASN1_GENERALIZEDTIME_check(t);
else if (t->type == V_ASN1_UTCTIME)
return ASN1_UTCTIME_check(t);
return 0;
if (t->type != V_ASN1_GENERALIZEDTIME && t->type != V_ASN1_UTCTIME)
return 0;
return (t->type == asn1_time_parse(t->data, t->length, NULL, t->type));
}

/* Convert an ASN1_TIME structure to GeneralizedTime */
Expand Down Expand Up @@ -210,13 +208,12 @@ ASN1_TIME_set_string(ASN1_TIME *s, const char *str)
t.data = (unsigned char *)str;
t.flags = 0;

t.type = V_ASN1_UTCTIME;

if (!ASN1_TIME_check(&t)) {
t.type = V_ASN1_GENERALIZEDTIME;
if (!ASN1_TIME_check(&t))
return 0;
}
t.type = asn1_time_parse(t.data, t.length, NULL, V_ASN1_UTCTIME);
if (t.type == -1)
t.type = asn1_time_parse(t.data, t.length, NULL,
V_ASN1_GENERALIZEDTIME);
if (t.type == -1)
return 0;

if (s && !ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t))
return 0;
Expand Down

0 comments on commit de5a7bf

Please sign in to comment.