Skip to content

Commit 3975e21

Browse files
author
jsing
committed
Provide t2i_ASN1_OBJECT_internal() and use it for OBJ_txt2obj()
The current OBJ_txt2obj() implementation converts the text to ASN.1 object content octets, builds a full DER encoding from it, then feeds the entire thing back through the DER to ASN.1 object conversion. Rather than doing this crazy dance, provide an t2i_ASN1_OBJECT_internal() function that converts the text to ASN.1 object content octets, then creates a new ASN1_OBJECT and attaches the content octets to it. ok inoguchi@ tb@
1 parent 529edd3 commit 3975e21

File tree

3 files changed

+43
-32
lines changed

3 files changed

+43
-32
lines changed

src/lib/libcrypto/asn1/a_object.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: a_object.c,v 1.42 2022/03/19 17:35:52 jsing Exp $ */
1+
/* $OpenBSD: a_object.c,v 1.43 2022/03/19 17:49:32 jsing Exp $ */
22
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
33
* All rights reserved.
44
*
@@ -475,6 +475,43 @@ i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *aobj)
475475
return i2t_ASN1_OBJECT_internal(aobj, buf, buf_len, 0);
476476
}
477477

478+
ASN1_OBJECT *
479+
t2i_ASN1_OBJECT_internal(const char *oid)
480+
{
481+
ASN1_OBJECT *aobj = NULL;
482+
uint8_t *data = NULL;
483+
size_t data_len;
484+
CBB cbb;
485+
CBS cbs;
486+
487+
memset(&cbb, 0, sizeof(cbb));
488+
489+
CBS_init(&cbs, oid, strlen(oid));
490+
491+
if (!CBB_init(&cbb, 0))
492+
goto err;
493+
if (!a2c_ASN1_OBJECT_internal(&cbb, &cbs))
494+
goto err;
495+
if (!CBB_finish(&cbb, &data, &data_len))
496+
goto err;
497+
498+
if (data_len > INT_MAX)
499+
goto err;
500+
501+
if ((aobj = ASN1_OBJECT_new()) == NULL)
502+
goto err;
503+
504+
aobj->data = data;
505+
aobj->length = (int)data_len;
506+
data = NULL;
507+
508+
err:
509+
CBB_cleanup(&cbb);
510+
free(data);
511+
512+
return aobj;
513+
}
514+
478515
int
479516
i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *aobj)
480517
{

src/lib/libcrypto/asn1/asn1_locl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: asn1_locl.h,v 1.22 2022/03/13 14:58:14 jsing Exp $ */
1+
/* $OpenBSD: asn1_locl.h,v 1.23 2022/03/19 17:49:32 jsing Exp $ */
22
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
33
* project 2006.
44
*/
@@ -201,5 +201,6 @@ int asn1_tag2charwidth(int tag);
201201

202202
int i2t_ASN1_OBJECT_internal(const ASN1_OBJECT *aobj, char *buf, int buf_len,
203203
int no_name);
204+
ASN1_OBJECT *t2i_ASN1_OBJECT_internal(const char *oid);
204205

205206
__END_HIDDEN_DECLS

src/lib/libcrypto/objects/obj_dat.c

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: obj_dat.c,v 1.48 2022/03/02 11:28:00 jsing Exp $ */
1+
/* $OpenBSD: obj_dat.c,v 1.49 2022/03/19 17:49:32 jsing Exp $ */
22
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
33
* All rights reserved.
44
*
@@ -485,42 +485,15 @@ OBJ_obj2nid(const ASN1_OBJECT *a)
485485
ASN1_OBJECT *
486486
OBJ_txt2obj(const char *s, int no_name)
487487
{
488-
int nid = NID_undef;
489-
ASN1_OBJECT *op = NULL;
490-
unsigned char *buf;
491-
unsigned char *p;
492-
const unsigned char *cp;
493-
int i, j;
488+
int nid;
494489

495490
if (!no_name) {
496491
if (((nid = OBJ_sn2nid(s)) != NID_undef) ||
497492
((nid = OBJ_ln2nid(s)) != NID_undef) )
498493
return OBJ_nid2obj(nid);
499494
}
500495

501-
/* Work out size of content octets */
502-
i = a2d_ASN1_OBJECT(NULL, 0, s, -1);
503-
if (i <= 0) {
504-
/* Don't clear the error */
505-
/*ERR_clear_error();*/
506-
return NULL;
507-
}
508-
/* Work out total size */
509-
j = ASN1_object_size(0, i, V_ASN1_OBJECT);
510-
511-
if ((buf = malloc(j)) == NULL)
512-
return NULL;
513-
514-
p = buf;
515-
/* Write out tag+length */
516-
ASN1_put_object(&p, 0, i, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
517-
/* Write out contents */
518-
a2d_ASN1_OBJECT(p, i, s, -1);
519-
520-
cp = buf;
521-
op = d2i_ASN1_OBJECT(NULL, &cp, j);
522-
free(buf);
523-
return op;
496+
return t2i_ASN1_OBJECT_internal(s);
524497
}
525498

526499
int

0 commit comments

Comments
 (0)