2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
14 * X509_ctrl_str() is sorely lacking in libcrypto, but is still needed to
15 * allow the application to process verification options in a manner similar
16 * to signature or other options that pass through EVP_PKEY_CTX_ctrl_str(),
19 * As soon as more stuff is added, the code will need serious rework. For
20 * the moment, it only handles the FIPS 196 / SM2 distinguishing ID.
22 #ifdef EVP_PKEY_CTRL_SET1_ID
23 static ASN1_OCTET_STRING *mk_octet_string(void *value, size_t value_n)
25 ASN1_OCTET_STRING *v = ASN1_OCTET_STRING_new();
28 BIO_printf(bio_err, "error: allocation failed\n");
29 } else if (!ASN1_OCTET_STRING_set(v, value, (int)value_n)) {
30 ASN1_OCTET_STRING_free(v);
37 static int x509_ctrl(void *object, int cmd, void *value, size_t value_n)
40 #ifdef EVP_PKEY_CTRL_SET1_ID
41 case EVP_PKEY_CTRL_SET1_ID:
43 ASN1_OCTET_STRING *v = mk_octet_string(value, value_n);
47 "error: setting distinguishing ID in certificate failed\n");
51 X509_set0_distinguishing_id(object, v);
58 return -2; /* typical EVP_PKEY return for "unsupported" */
61 static int x509_req_ctrl(void *object, int cmd, void *value, size_t value_n)
64 #ifdef EVP_PKEY_CTRL_SET1_ID
65 case EVP_PKEY_CTRL_SET1_ID:
67 ASN1_OCTET_STRING *v = mk_octet_string(value, value_n);
71 "error: setting distinguishing ID in certificate signing request failed\n");
75 X509_REQ_set0_distinguishing_id(object, v);
82 return -2; /* typical EVP_PKEY return for "unsupported" */
85 static int do_x509_ctrl_string(int (*ctrl)(void *object, int cmd,
86 void *value, size_t value_n),
87 void *object, const char *value)
90 char *stmp, *vtmp = NULL;
92 int cmd = 0; /* Will get command values that make sense somehow */
94 stmp = OPENSSL_strdup(value);
97 vtmp = strchr(stmp, ':');
101 vtmp_len = strlen(vtmp);
104 if (strcmp(stmp, "distid") == 0) {
105 #ifdef EVP_PKEY_CTRL_SET1_ID
106 cmd = EVP_PKEY_CTRL_SET1_ID; /* ... except we put it in X509 */
108 } else if (strcmp(stmp, "hexdistid") == 0) {
113 hexid = OPENSSL_hexstr2buf((const char *)vtmp, &hexid_len);
116 vtmp_len = (size_t)hexid_len;
118 #ifdef EVP_PKEY_CTRL_SET1_ID
119 cmd = EVP_PKEY_CTRL_SET1_ID; /* ... except we put it in X509 */
123 rv = ctrl(object, cmd, vtmp, vtmp_len);
129 int x509_ctrl_string(X509 *x, const char *value)
131 return do_x509_ctrl_string(x509_ctrl, x, value);
134 int x509_req_ctrl_string(X509_REQ *x, const char *value)
136 return do_x509_ctrl_string(x509_req_ctrl, x, value);