mirror of https://github.com/openssl/openssl.git
OID code tidy up.
Tidy up and simplify OBJ_dup() and OBJ_create(). Sanity check added OIDs: don't allow duplicates. Reviewed-by: Richard Levitte <levitte@openssl.org>
This commit is contained in:
parent
9e20068958
commit
52832e470f
|
@ -679,30 +679,36 @@ int OBJ_create_objects(BIO *in)
|
||||||
|
|
||||||
int OBJ_create(const char *oid, const char *sn, const char *ln)
|
int OBJ_create(const char *oid, const char *sn, const char *ln)
|
||||||
{
|
{
|
||||||
int ok = 0;
|
ASN1_OBJECT *tmpoid = NULL;
|
||||||
ASN1_OBJECT *op = NULL;
|
int ok;
|
||||||
unsigned char *buf;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = a2d_ASN1_OBJECT(NULL, 0, oid, -1);
|
/* Check to see if short or long name already present */
|
||||||
if (i <= 0)
|
if (OBJ_sn2nid(sn) != NID_undef || OBJ_ln2nid(ln) != NID_undef) {
|
||||||
return (0);
|
OBJerr(OBJ_F_OBJ_CREATE, OBJ_R_OID_EXISTS);
|
||||||
|
return 0;
|
||||||
if ((buf = OPENSSL_malloc(i)) == NULL) {
|
|
||||||
OBJerr(OBJ_F_OBJ_CREATE, ERR_R_MALLOC_FAILURE);
|
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
i = a2d_ASN1_OBJECT(buf, i, oid, -1);
|
|
||||||
if (i == 0)
|
/* Convert numerical OID string to an ASN1_OBJECT structure */
|
||||||
|
tmpoid = OBJ_txt2obj(oid, 1);
|
||||||
|
|
||||||
|
/* If NID is not NID_undef then object already exists */
|
||||||
|
if (OBJ_obj2nid(tmpoid) != NID_undef) {
|
||||||
|
OBJerr(OBJ_F_OBJ_CREATE, OBJ_R_OID_EXISTS);
|
||||||
goto err;
|
goto err;
|
||||||
op = (ASN1_OBJECT *)ASN1_OBJECT_create(OBJ_new_nid(1), buf, i, sn, ln);
|
}
|
||||||
if (op == NULL)
|
|
||||||
goto err;
|
tmpoid->nid = OBJ_new_nid(1);
|
||||||
ok = OBJ_add_object(op);
|
tmpoid->sn = (char *)sn;
|
||||||
|
tmpoid->ln = (char *)ln;
|
||||||
|
|
||||||
|
ok = OBJ_add_object(tmpoid);
|
||||||
|
|
||||||
|
tmpoid->sn = NULL;
|
||||||
|
tmpoid->ln = NULL;
|
||||||
|
|
||||||
err:
|
err:
|
||||||
ASN1_OBJECT_free(op);
|
ASN1_OBJECT_free(tmpoid);
|
||||||
OPENSSL_free(buf);
|
return ok;
|
||||||
return (ok);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t OBJ_length(const ASN1_OBJECT *obj)
|
size_t OBJ_length(const ASN1_OBJECT *obj)
|
||||||
|
|
|
@ -36,6 +36,7 @@ static ERR_STRING_DATA OBJ_str_functs[] = {
|
||||||
|
|
||||||
static ERR_STRING_DATA OBJ_str_reasons[] = {
|
static ERR_STRING_DATA OBJ_str_reasons[] = {
|
||||||
{ERR_REASON(OBJ_R_MALLOC_FAILURE), "malloc failure"},
|
{ERR_REASON(OBJ_R_MALLOC_FAILURE), "malloc failure"},
|
||||||
|
{ERR_REASON(OBJ_R_OID_EXISTS), "oid exists"},
|
||||||
{ERR_REASON(OBJ_R_UNKNOWN_NID), "unknown nid"},
|
{ERR_REASON(OBJ_R_UNKNOWN_NID), "unknown nid"},
|
||||||
{0, NULL}
|
{0, NULL}
|
||||||
};
|
};
|
||||||
|
|
|
@ -17,59 +17,42 @@
|
||||||
ASN1_OBJECT *OBJ_dup(const ASN1_OBJECT *o)
|
ASN1_OBJECT *OBJ_dup(const ASN1_OBJECT *o)
|
||||||
{
|
{
|
||||||
ASN1_OBJECT *r;
|
ASN1_OBJECT *r;
|
||||||
int i;
|
|
||||||
char *ln = NULL, *sn = NULL;
|
|
||||||
unsigned char *data = NULL;
|
|
||||||
|
|
||||||
if (o == NULL)
|
if (o == NULL)
|
||||||
return (NULL);
|
return NULL;
|
||||||
|
/* If object isn't dynamic it's an internal OID which is never freed */
|
||||||
if (!(o->flags & ASN1_OBJECT_FLAG_DYNAMIC))
|
if (!(o->flags & ASN1_OBJECT_FLAG_DYNAMIC))
|
||||||
return ((ASN1_OBJECT *)o); /* XXX: ugh! Why? What kind of duplication
|
return ((ASN1_OBJECT *)o);
|
||||||
* is this??? */
|
|
||||||
|
|
||||||
r = ASN1_OBJECT_new();
|
r = ASN1_OBJECT_new();
|
||||||
if (r == NULL) {
|
if (r == NULL) {
|
||||||
OBJerr(OBJ_F_OBJ_DUP, ERR_R_ASN1_LIB);
|
OBJerr(OBJ_F_OBJ_DUP, ERR_R_ASN1_LIB);
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
data = OPENSSL_malloc(o->length);
|
|
||||||
if (data == NULL)
|
|
||||||
goto err;
|
|
||||||
if (o->data != NULL)
|
|
||||||
memcpy(data, o->data, o->length);
|
|
||||||
/* once data attached to object it remains const */
|
|
||||||
r->data = data;
|
|
||||||
r->length = o->length;
|
|
||||||
r->nid = o->nid;
|
|
||||||
r->ln = r->sn = NULL;
|
|
||||||
if (o->ln != NULL) {
|
|
||||||
i = strlen(o->ln) + 1;
|
|
||||||
ln = OPENSSL_malloc(i);
|
|
||||||
if (ln == NULL)
|
|
||||||
goto err;
|
|
||||||
memcpy(ln, o->ln, i);
|
|
||||||
r->ln = ln;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (o->sn != NULL) {
|
/* Set dynamic flags so everything gets freed up on error */
|
||||||
i = strlen(o->sn) + 1;
|
|
||||||
sn = OPENSSL_malloc(i);
|
|
||||||
if (sn == NULL)
|
|
||||||
goto err;
|
|
||||||
memcpy(sn, o->sn, i);
|
|
||||||
r->sn = sn;
|
|
||||||
}
|
|
||||||
r->flags = o->flags | (ASN1_OBJECT_FLAG_DYNAMIC |
|
r->flags = o->flags | (ASN1_OBJECT_FLAG_DYNAMIC |
|
||||||
ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
|
ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
|
||||||
ASN1_OBJECT_FLAG_DYNAMIC_DATA);
|
ASN1_OBJECT_FLAG_DYNAMIC_DATA);
|
||||||
return (r);
|
|
||||||
|
if (o->length > 0 && (r->data = OPENSSL_memdup(o->data, o->length)) == NULL)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
r->length = o->length;
|
||||||
|
r->nid = o->nid;
|
||||||
|
|
||||||
|
if (o->ln != NULL && (r->ln = OPENSSL_strdup(o->ln)) == NULL)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
if (o->sn != NULL && (r->sn = OPENSSL_strdup(o->sn)) == NULL)
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
return r;
|
||||||
err:
|
err:
|
||||||
|
ASN1_OBJECT_free(r);
|
||||||
OBJerr(OBJ_F_OBJ_DUP, ERR_R_MALLOC_FAILURE);
|
OBJerr(OBJ_F_OBJ_DUP, ERR_R_MALLOC_FAILURE);
|
||||||
OPENSSL_free(ln);
|
return NULL;
|
||||||
OPENSSL_free(sn);
|
|
||||||
OPENSSL_free(data);
|
|
||||||
OPENSSL_free(r);
|
|
||||||
return (NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int OBJ_cmp(const ASN1_OBJECT *a, const ASN1_OBJECT *b)
|
int OBJ_cmp(const ASN1_OBJECT *a, const ASN1_OBJECT *b)
|
||||||
|
|
|
@ -1088,6 +1088,7 @@ void ERR_load_OBJ_strings(void);
|
||||||
|
|
||||||
/* Reason codes. */
|
/* Reason codes. */
|
||||||
# define OBJ_R_MALLOC_FAILURE 100
|
# define OBJ_R_MALLOC_FAILURE 100
|
||||||
|
# define OBJ_R_OID_EXISTS 102
|
||||||
# define OBJ_R_UNKNOWN_NID 101
|
# define OBJ_R_UNKNOWN_NID 101
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
Loading…
Reference in New Issue