Copy OID files that are getting moved to tf-psa-crypto
The OID module is used by both crypto and X.509. It has moved to the
`tf-psa-crypto` subdirectory, and the sibling commit
08d8cc57dbe7be54fe3f88ecbc2729300c48d450 removes this subdirectory from the
`mbedtls` repository in order to make `tf-psa-crypto` a submodule. We want
to access the relevant parts directly from X.509 rather than go through the
crypto repository, because OID functions are only accessible as private
interfaces, and crypto doesn't know when a particular OID function is needed
in the build since it depends on X.509 configuration options.
Make a copy of the OID module and its unit tests. In a follow-up, the X.509
module will switch to consuming this copy rather than the one that went into
TF-PSA-Crypto.
Rename the files from `*oid*` to `*x509_oid*` to follow the naming
convention that submodules of X.509 are prefixed with `x509`. This also
avoids file name clashes with TF-PSA-Crypto.
Since OID is not a public interface of Mbed TLS 4.x, move the header file
into `library`.
This commit only makes the files available. Subsequent commits will take
care of making these files used in the build.
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
diff --git a/tests/suites/test_suite_x509_oid.function b/tests/suites/test_suite_x509_oid.function
new file mode 100644
index 0000000..e96425e
--- /dev/null
+++ b/tests/suites/test_suite_x509_oid.function
@@ -0,0 +1,120 @@
+/* BEGIN_HEADER */
+#include "mbedtls/oid.h"
+#include "mbedtls/asn1.h"
+#include "mbedtls/asn1write.h"
+#include "string.h"
+/* END_HEADER */
+
+/* BEGIN_DEPENDENCIES
+ * depends_on:MBEDTLS_OID_C:!MBEDTLS_X509_REMOVE_INFO
+ * END_DEPENDENCIES
+ */
+
+/* BEGIN_CASE */
+void oid_get_certificate_policies(data_t *oid, char *result_str)
+{
+ mbedtls_asn1_buf asn1_buf = { 0, 0, NULL };
+ int ret;
+ const char *desc;
+
+ asn1_buf.tag = MBEDTLS_ASN1_OID;
+ asn1_buf.p = oid->x;
+ asn1_buf.len = oid->len;
+
+ ret = mbedtls_oid_get_certificate_policies(&asn1_buf, &desc);
+ if (strlen(result_str) == 0) {
+ TEST_ASSERT(ret == MBEDTLS_ERR_OID_NOT_FOUND);
+ } else {
+ TEST_ASSERT(ret == 0);
+ TEST_ASSERT(strcmp((char *) desc, result_str) == 0);
+ }
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
+void oid_get_extended_key_usage(data_t *oid, char *result_str)
+{
+ mbedtls_asn1_buf asn1_buf = { 0, 0, NULL };
+ int ret;
+ const char *desc;
+
+ asn1_buf.tag = MBEDTLS_ASN1_OID;
+ asn1_buf.p = oid->x;
+ asn1_buf.len = oid->len;
+
+ ret = mbedtls_oid_get_extended_key_usage(&asn1_buf, &desc);
+ if (strlen(result_str) == 0) {
+ TEST_ASSERT(ret == MBEDTLS_ERR_OID_NOT_FOUND);
+ } else {
+ TEST_ASSERT(ret == 0);
+ TEST_ASSERT(strcmp((char *) desc, result_str) == 0);
+ }
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
+void oid_get_x509_extension(data_t *oid, int exp_type)
+{
+ mbedtls_asn1_buf ext_oid = { 0, 0, NULL };
+ int ret;
+ int ext_type;
+
+ ext_oid.tag = MBEDTLS_ASN1_OID;
+ ext_oid.p = oid->x;
+ ext_oid.len = oid->len;
+
+ ret = mbedtls_oid_get_x509_ext_type(&ext_oid, &ext_type);
+ if (exp_type == 0) {
+ TEST_ASSERT(ret == MBEDTLS_ERR_OID_NOT_FOUND);
+ } else {
+ TEST_ASSERT(ret == 0);
+ TEST_ASSERT(ext_type == exp_type);
+ }
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
+void oid_get_md_alg_id(data_t *oid, int exp_md_id)
+{
+ mbedtls_asn1_buf md_oid = { 0, 0, NULL };
+ int ret;
+ mbedtls_md_type_t md_id = 0;
+
+ md_oid.tag = MBEDTLS_ASN1_OID;
+ md_oid.p = oid->x;
+ md_oid.len = oid->len;
+
+ ret = mbedtls_oid_get_md_alg(&md_oid, &md_id);
+
+ if (exp_md_id < 0) {
+ TEST_ASSERT(ret == MBEDTLS_ERR_OID_NOT_FOUND);
+ TEST_ASSERT(md_id == 0);
+ } else {
+ TEST_ASSERT(ret == 0);
+ TEST_ASSERT((mbedtls_md_type_t) exp_md_id == md_id);
+ }
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
+void mbedtls_oid_get_md_hmac(data_t *oid, int exp_md_id)
+{
+ mbedtls_asn1_buf md_oid = { 0, 0, NULL };
+ int ret;
+ mbedtls_md_type_t md_id = 0;
+
+ md_oid.tag = MBEDTLS_ASN1_OID;
+ md_oid.p = oid->x;
+ md_oid.len = oid->len;
+
+ ret = mbedtls_oid_get_md_hmac(&md_oid, &md_id);
+
+ if (exp_md_id < 0) {
+ TEST_ASSERT(ret == MBEDTLS_ERR_OID_NOT_FOUND);
+ TEST_ASSERT(md_id == 0);
+ } else {
+ TEST_ASSERT(ret == 0);
+ TEST_ASSERT((mbedtls_md_type_t) exp_md_id == md_id);
+ }
+}
+/* END_CASE */