From d46a632ca35d3f62b29f97c14868a75a30a0ea74 Mon Sep 17 00:00:00 2001 From: jsing <> Date: Sat, 19 Mar 2022 17:49:32 +0000 Subject: 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@ --- src/lib/libcrypto/asn1/a_object.c | 39 ++++++++++++++++++++++++++++++++++++- src/lib/libcrypto/asn1/asn1_locl.h | 3 ++- src/lib/libcrypto/objects/obj_dat.c | 33 +++---------------------------- 3 files changed, 43 insertions(+), 32 deletions(-) (limited to 'src/lib') diff --git a/src/lib/libcrypto/asn1/a_object.c b/src/lib/libcrypto/asn1/a_object.c index 0061ccb880..56a08a3cb4 100644 --- a/src/lib/libcrypto/asn1/a_object.c +++ b/src/lib/libcrypto/asn1/a_object.c @@ -1,4 +1,4 @@ -/* $OpenBSD: a_object.c,v 1.42 2022/03/19 17:35:52 jsing Exp $ */ +/* $OpenBSD: a_object.c,v 1.43 2022/03/19 17:49:32 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -475,6 +475,43 @@ i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *aobj) return i2t_ASN1_OBJECT_internal(aobj, buf, buf_len, 0); } +ASN1_OBJECT * +t2i_ASN1_OBJECT_internal(const char *oid) +{ + ASN1_OBJECT *aobj = NULL; + uint8_t *data = NULL; + size_t data_len; + CBB cbb; + CBS cbs; + + memset(&cbb, 0, sizeof(cbb)); + + CBS_init(&cbs, oid, strlen(oid)); + + if (!CBB_init(&cbb, 0)) + goto err; + if (!a2c_ASN1_OBJECT_internal(&cbb, &cbs)) + goto err; + if (!CBB_finish(&cbb, &data, &data_len)) + goto err; + + if (data_len > INT_MAX) + goto err; + + if ((aobj = ASN1_OBJECT_new()) == NULL) + goto err; + + aobj->data = data; + aobj->length = (int)data_len; + data = NULL; + + err: + CBB_cleanup(&cbb); + free(data); + + return aobj; +} + int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *aobj) { diff --git a/src/lib/libcrypto/asn1/asn1_locl.h b/src/lib/libcrypto/asn1/asn1_locl.h index 9a29a2b13f..12f7eadfb3 100644 --- a/src/lib/libcrypto/asn1/asn1_locl.h +++ b/src/lib/libcrypto/asn1/asn1_locl.h @@ -1,4 +1,4 @@ -/* $OpenBSD: asn1_locl.h,v 1.22 2022/03/13 14:58:14 jsing Exp $ */ +/* $OpenBSD: asn1_locl.h,v 1.23 2022/03/19 17:49:32 jsing Exp $ */ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project 2006. */ @@ -201,5 +201,6 @@ int asn1_tag2charwidth(int tag); int i2t_ASN1_OBJECT_internal(const ASN1_OBJECT *aobj, char *buf, int buf_len, int no_name); +ASN1_OBJECT *t2i_ASN1_OBJECT_internal(const char *oid); __END_HIDDEN_DECLS diff --git a/src/lib/libcrypto/objects/obj_dat.c b/src/lib/libcrypto/objects/obj_dat.c index 786bed6c7a..bcb7ee2dbb 100644 --- a/src/lib/libcrypto/objects/obj_dat.c +++ b/src/lib/libcrypto/objects/obj_dat.c @@ -1,4 +1,4 @@ -/* $OpenBSD: obj_dat.c,v 1.48 2022/03/02 11:28:00 jsing Exp $ */ +/* $OpenBSD: obj_dat.c,v 1.49 2022/03/19 17:49:32 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -485,12 +485,7 @@ OBJ_obj2nid(const ASN1_OBJECT *a) ASN1_OBJECT * OBJ_txt2obj(const char *s, int no_name) { - int nid = NID_undef; - ASN1_OBJECT *op = NULL; - unsigned char *buf; - unsigned char *p; - const unsigned char *cp; - int i, j; + int nid; if (!no_name) { if (((nid = OBJ_sn2nid(s)) != NID_undef) || @@ -498,29 +493,7 @@ OBJ_txt2obj(const char *s, int no_name) return OBJ_nid2obj(nid); } - /* Work out size of content octets */ - i = a2d_ASN1_OBJECT(NULL, 0, s, -1); - if (i <= 0) { - /* Don't clear the error */ - /*ERR_clear_error();*/ - return NULL; - } - /* Work out total size */ - j = ASN1_object_size(0, i, V_ASN1_OBJECT); - - if ((buf = malloc(j)) == NULL) - return NULL; - - p = buf; - /* Write out tag+length */ - ASN1_put_object(&p, 0, i, V_ASN1_OBJECT, V_ASN1_UNIVERSAL); - /* Write out contents */ - a2d_ASN1_OBJECT(p, i, s, -1); - - cp = buf; - op = d2i_ASN1_OBJECT(NULL, &cp, j); - free(buf); - return op; + return t2i_ASN1_OBJECT_internal(s); } int -- cgit v1.2.3-55-g6feb