Consolidate code/templates for ASN.1 types.
authorjsing <jsing@openbsd.org>
Sat, 25 Dec 2021 08:52:44 +0000 (08:52 +0000)
committerjsing <jsing@openbsd.org>
Sat, 25 Dec 2021 08:52:44 +0000 (08:52 +0000)
Where an ASN.1 type has its own file, move the ASN.1 item template and
template related functions into the file.

Discussed with tb@

lib/libcrypto/asn1/a_bitstr.c
lib/libcrypto/asn1/a_enum.c
lib/libcrypto/asn1/a_int.c
lib/libcrypto/asn1/a_object.c
lib/libcrypto/asn1/a_octet.c
lib/libcrypto/asn1/a_type.c
lib/libcrypto/asn1/tasn_typ.c

index 207d80a..4ffafd5 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: a_bitstr.c,v 1.32 2021/12/25 07:48:09 jsing Exp $ */
+/* $OpenBSD: a_bitstr.c,v 1.33 2021/12/25 08:52:44 jsing Exp $ */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
 #include <string.h>
 
 #include <openssl/asn1.h>
+#include <openssl/asn1t.h>
 #include <openssl/conf.h>
 #include <openssl/err.h>
 #include <openssl/x509v3.h>
 
+const ASN1_ITEM ASN1_BIT_STRING_it = {
+       .itype = ASN1_ITYPE_PRIMITIVE,
+       .utype = V_ASN1_BIT_STRING,
+       .sname = "ASN1_BIT_STRING",
+};
+
+ASN1_BIT_STRING *
+ASN1_BIT_STRING_new(void)
+{
+       return (ASN1_BIT_STRING *)ASN1_item_new(&ASN1_BIT_STRING_it);
+}
+
+void
+ASN1_BIT_STRING_free(ASN1_BIT_STRING *a)
+{
+       ASN1_item_free((ASN1_VALUE *)a, &ASN1_BIT_STRING_it);
+}
+
 int
 ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len)
 {
@@ -313,3 +332,16 @@ c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, const unsigned char **pp, long len)
 
        return (NULL);
 }
+
+int
+i2d_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **out)
+{
+       return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASN1_BIT_STRING_it);
+}
+
+ASN1_BIT_STRING *
+d2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, const unsigned char **in, long len)
+{
+       return (ASN1_BIT_STRING *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
+           &ASN1_BIT_STRING_it);
+}
index e0e64f0..f48543e 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: a_enum.c,v 1.21 2021/12/15 18:00:31 jsing Exp $ */
+/* $OpenBSD: a_enum.c,v 1.22 2021/12/25 08:52:44 jsing Exp $ */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
@@ -60,6 +60,7 @@
 #include <stdio.h>
 
 #include <openssl/asn1.h>
+#include <openssl/asn1t.h>
 #include <openssl/bn.h>
 #include <openssl/buffer.h>
 #include <openssl/err.h>
  * for comments on encoding see a_int.c
  */
 
+const ASN1_ITEM ASN1_ENUMERATED_it = {
+       .itype = ASN1_ITYPE_PRIMITIVE,
+       .utype = V_ASN1_ENUMERATED,
+       .sname = "ASN1_ENUMERATED",
+};
+
+ASN1_ENUMERATED *
+ASN1_ENUMERATED_new(void)
+{
+       return (ASN1_ENUMERATED *)ASN1_item_new(&ASN1_ENUMERATED_it);
+}
+
+void
+ASN1_ENUMERATED_free(ASN1_ENUMERATED *a)
+{
+       ASN1_item_free((ASN1_VALUE *)a, &ASN1_ENUMERATED_it);
+}
+
 int
 ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v)
 {
@@ -320,3 +339,16 @@ err:
        free(s);
        return (ret);
 }
+
+int
+i2d_ASN1_ENUMERATED(ASN1_ENUMERATED *a, unsigned char **out)
+{
+       return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASN1_ENUMERATED_it);
+}
+
+ASN1_ENUMERATED *
+d2i_ASN1_ENUMERATED(ASN1_ENUMERATED **a, const unsigned char **in, long len)
+{
+       return (ASN1_ENUMERATED *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
+           &ASN1_ENUMERATED_it);
+}
index 9218a17..eac7546 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: a_int.c,v 1.36 2021/12/25 07:48:09 jsing Exp $ */
+/* $OpenBSD: a_int.c,v 1.37 2021/12/25 08:52:44 jsing Exp $ */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
 #include <string.h>
 
 #include <openssl/asn1.h>
+#include <openssl/asn1t.h>
 #include <openssl/bn.h>
 #include <openssl/buffer.h>
 #include <openssl/err.h>
 
+const ASN1_ITEM ASN1_INTEGER_it = {
+       .itype = ASN1_ITYPE_PRIMITIVE,
+       .utype = V_ASN1_INTEGER,
+       .sname = "ASN1_INTEGER",
+};
+
+ASN1_INTEGER *
+ASN1_INTEGER_new(void)
+{
+       return (ASN1_INTEGER *)ASN1_item_new(&ASN1_INTEGER_it);
+}
+
+void
+ASN1_INTEGER_free(ASN1_INTEGER *a)
+{
+       ASN1_item_free((ASN1_VALUE *)a, &ASN1_INTEGER_it);
+}
+
 static int
 ASN1_INTEGER_valid(const ASN1_INTEGER *a)
 {
@@ -567,6 +586,18 @@ err:
        return (NULL);
 }
 
+int
+i2d_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **out)
+{
+       return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASN1_INTEGER_it);
+}
+
+ASN1_INTEGER *
+d2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **in, long len)
+{
+       return (ASN1_INTEGER *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
+           &ASN1_INTEGER_it);
+}
 
 /* This is a version of d2i_ASN1_INTEGER that ignores the sign bit of
  * ASN1 integers: some broken software can encode a positive INTEGER
index cc4f5dd..a029a12 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: a_object.c,v 1.34 2021/12/25 07:48:09 jsing Exp $ */
+/* $OpenBSD: a_object.c,v 1.35 2021/12/25 08:52:44 jsing Exp $ */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
 #include <string.h>
 
 #include <openssl/asn1.h>
+#include <openssl/asn1t.h>
 #include <openssl/bn.h>
 #include <openssl/err.h>
 #include <openssl/buffer.h>
 #include <openssl/objects.h>
 
+const ASN1_ITEM ASN1_OBJECT_it = {
+       .itype = ASN1_ITYPE_PRIMITIVE,
+       .utype = V_ASN1_OBJECT,
+       .sname = "ASN1_OBJECT",
+};
+
 ASN1_OBJECT *
 ASN1_OBJECT_new(void)
 {
index d998675..c1a2520 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: a_octet.c,v 1.10 2015/07/29 14:58:34 jsing Exp $ */
+/* $OpenBSD: a_octet.c,v 1.11 2021/12/25 08:52:44 jsing Exp $ */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
 #include <stdio.h>
 
 #include <openssl/asn1.h>
+#include <openssl/asn1t.h>
+
+const ASN1_ITEM ASN1_OCTET_STRING_it = {
+       .itype = ASN1_ITYPE_PRIMITIVE,
+       .utype = V_ASN1_OCTET_STRING,
+       .sname = "ASN1_OCTET_STRING",
+};
+
+ASN1_OCTET_STRING *
+ASN1_OCTET_STRING_new(void)
+{
+       return (ASN1_OCTET_STRING *)ASN1_item_new(&ASN1_OCTET_STRING_it);
+}
+
+void
+ASN1_OCTET_STRING_free(ASN1_OCTET_STRING *a)
+{
+       ASN1_item_free((ASN1_VALUE *)a, &ASN1_OCTET_STRING_it);
+}
+
 
 ASN1_OCTET_STRING *
 ASN1_OCTET_STRING_dup(const ASN1_OCTET_STRING *x)
@@ -77,3 +97,16 @@ ASN1_OCTET_STRING_set(ASN1_OCTET_STRING *x, const unsigned char *d, int len)
 {
        return ASN1_STRING_set(x, d, len);
 }
+
+int
+i2d_ASN1_OCTET_STRING(ASN1_OCTET_STRING *a, unsigned char **out)
+{
+       return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASN1_OCTET_STRING_it);
+}
+
+ASN1_OCTET_STRING *
+d2i_ASN1_OCTET_STRING(ASN1_OCTET_STRING **a, const unsigned char **in, long len)
+{
+       return (ASN1_OCTET_STRING *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
+           &ASN1_OCTET_STRING_it);
+}
index a18ffe6..23fa9f6 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: a_type.c,v 1.21 2019/10/24 16:36:10 jsing Exp $ */
+/* $OpenBSD: a_type.c,v 1.22 2021/12/25 08:52:44 jsing Exp $ */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
 #include <openssl/asn1t.h>
 #include <openssl/objects.h>
 
+ASN1_TYPE *
+ASN1_TYPE_new(void)
+{
+       return (ASN1_TYPE *)ASN1_item_new(&ASN1_ANY_it);
+}
+
+void
+ASN1_TYPE_free(ASN1_TYPE *a)
+{
+       ASN1_item_free((ASN1_VALUE *)a, &ASN1_ANY_it);
+}
+
 int
 ASN1_TYPE_get(const ASN1_TYPE *a)
 {
@@ -185,3 +197,16 @@ ASN1_TYPE_unpack_sequence(const ASN1_ITEM *it, const ASN1_TYPE *t)
                return NULL;
        return ASN1_item_unpack(t->value.sequence, it);
 }
+
+int
+i2d_ASN1_TYPE(ASN1_TYPE *a, unsigned char **out)
+{
+       return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASN1_ANY_it);
+}
+
+ASN1_TYPE *
+d2i_ASN1_TYPE(ASN1_TYPE **a, const unsigned char **in, long len)
+{
+       return (ASN1_TYPE *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
+           &ASN1_ANY_it);
+}
index 7a2b90f..d35913b 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: tasn_typ.c,v 1.15 2021/12/23 18:04:41 tb Exp $ */
+/* $OpenBSD: tasn_typ.c,v 1.16 2021/12/25 08:52:44 jsing Exp $ */
 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  * project 2000.
  */
 
 /* Declarations for string types */
 
-const ASN1_ITEM ASN1_INTEGER_it = {
-       .itype = ASN1_ITYPE_PRIMITIVE,
-       .utype = V_ASN1_INTEGER,
-       .sname = "ASN1_INTEGER",
-};
-
-ASN1_INTEGER *
-d2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **in, long len)
-{
-       return (ASN1_INTEGER *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
-           &ASN1_INTEGER_it);
-}
-
-int
-i2d_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **out)
-{
-       return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASN1_INTEGER_it);
-}
-
-ASN1_INTEGER *
-ASN1_INTEGER_new(void)
-{
-       return (ASN1_INTEGER *)ASN1_item_new(&ASN1_INTEGER_it);
-}
-
-void
-ASN1_INTEGER_free(ASN1_INTEGER *a)
-{
-       ASN1_item_free((ASN1_VALUE *)a, &ASN1_INTEGER_it);
-}
-
-
-const ASN1_ITEM ASN1_ENUMERATED_it = {
-       .itype = ASN1_ITYPE_PRIMITIVE,
-       .utype = V_ASN1_ENUMERATED,
-       .sname = "ASN1_ENUMERATED",
-};
-
-ASN1_ENUMERATED *
-d2i_ASN1_ENUMERATED(ASN1_ENUMERATED **a, const unsigned char **in, long len)
-{
-       return (ASN1_ENUMERATED *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
-           &ASN1_ENUMERATED_it);
-}
-
-int
-i2d_ASN1_ENUMERATED(ASN1_ENUMERATED *a, unsigned char **out)
-{
-       return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASN1_ENUMERATED_it);
-}
-
-ASN1_ENUMERATED *
-ASN1_ENUMERATED_new(void)
-{
-       return (ASN1_ENUMERATED *)ASN1_item_new(&ASN1_ENUMERATED_it);
-}
-
-void
-ASN1_ENUMERATED_free(ASN1_ENUMERATED *a)
-{
-       ASN1_item_free((ASN1_VALUE *)a, &ASN1_ENUMERATED_it);
-}
-
-
-const ASN1_ITEM ASN1_BIT_STRING_it = {
-       .itype = ASN1_ITYPE_PRIMITIVE,
-       .utype = V_ASN1_BIT_STRING,
-       .sname = "ASN1_BIT_STRING",
-};
-
-ASN1_BIT_STRING *
-d2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, const unsigned char **in, long len)
-{
-       return (ASN1_BIT_STRING *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
-           &ASN1_BIT_STRING_it);
-}
-
-int
-i2d_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **out)
-{
-       return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASN1_BIT_STRING_it);
-}
-
-ASN1_BIT_STRING *
-ASN1_BIT_STRING_new(void)
-{
-       return (ASN1_BIT_STRING *)ASN1_item_new(&ASN1_BIT_STRING_it);
-}
-
-void
-ASN1_BIT_STRING_free(ASN1_BIT_STRING *a)
-{
-       ASN1_item_free((ASN1_VALUE *)a, &ASN1_BIT_STRING_it);
-}
-
-
-const ASN1_ITEM ASN1_OCTET_STRING_it = {
-       .itype = ASN1_ITYPE_PRIMITIVE,
-       .utype = V_ASN1_OCTET_STRING,
-       .sname = "ASN1_OCTET_STRING",
-};
-
-ASN1_OCTET_STRING *
-d2i_ASN1_OCTET_STRING(ASN1_OCTET_STRING **a, const unsigned char **in, long len)
-{
-       return (ASN1_OCTET_STRING *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
-           &ASN1_OCTET_STRING_it);
-}
-
-int
-i2d_ASN1_OCTET_STRING(ASN1_OCTET_STRING *a, unsigned char **out)
-{
-       return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASN1_OCTET_STRING_it);
-}
-
-ASN1_OCTET_STRING *
-ASN1_OCTET_STRING_new(void)
-{
-       return (ASN1_OCTET_STRING *)ASN1_item_new(&ASN1_OCTET_STRING_it);
-}
-
-void
-ASN1_OCTET_STRING_free(ASN1_OCTET_STRING *a)
-{
-       ASN1_item_free((ASN1_VALUE *)a, &ASN1_OCTET_STRING_it);
-}
-
-
 const ASN1_ITEM ASN1_NULL_it = {
        .itype = ASN1_ITYPE_PRIMITIVE,
        .utype = V_ASN1_NULL,
@@ -221,13 +93,6 @@ ASN1_NULL_free(ASN1_NULL *a)
 }
 
 
-const ASN1_ITEM ASN1_OBJECT_it = {
-       .itype = ASN1_ITYPE_PRIMITIVE,
-       .utype = V_ASN1_OBJECT,
-       .sname = "ASN1_OBJECT",
-};
-
-
 const ASN1_ITEM ASN1_UTF8STRING_it = {
        .itype = ASN1_ITYPE_PRIMITIVE,
        .utype = V_ASN1_UTF8STRING,
@@ -552,13 +417,13 @@ ASN1_BMPSTRING_free(ASN1_BMPSTRING *a)
        ASN1_item_free((ASN1_VALUE *)a, &ASN1_BMPSTRING_it);
 }
 
-
 const ASN1_ITEM ASN1_ANY_it = {
        .itype = ASN1_ITYPE_PRIMITIVE,
        .utype = V_ASN1_ANY,
        .sname = "ASN1_ANY",
 };
 
+
 /* Just swallow an ASN1_SEQUENCE in an ASN1_STRING */
 
 const ASN1_ITEM ASN1_SEQUENCE_it = {
@@ -568,31 +433,6 @@ const ASN1_ITEM ASN1_SEQUENCE_it = {
 };
 
 
-ASN1_TYPE *
-d2i_ASN1_TYPE(ASN1_TYPE **a, const unsigned char **in, long len)
-{
-       return (ASN1_TYPE *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
-           &ASN1_ANY_it);
-}
-
-int
-i2d_ASN1_TYPE(ASN1_TYPE *a, unsigned char **out)
-{
-       return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASN1_ANY_it);
-}
-
-ASN1_TYPE *
-ASN1_TYPE_new(void)
-{
-       return (ASN1_TYPE *)ASN1_item_new(&ASN1_ANY_it);
-}
-
-void
-ASN1_TYPE_free(ASN1_TYPE *a)
-{
-       ASN1_item_free((ASN1_VALUE *)a, &ASN1_ANY_it);
-}
-
 /* Multistring types */