1*b0d17251Schristos /*
2*b0d17251Schristos * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b0d17251Schristos *
4*b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
5*b0d17251Schristos * this file except in compliance with the License. You can obtain a copy
6*b0d17251Schristos * in the file LICENSE in the source distribution or at
7*b0d17251Schristos * https://www.openssl.org/source/license.html
8*b0d17251Schristos */
9*b0d17251Schristos
10*b0d17251Schristos #include <string.h>
11*b0d17251Schristos #include "apps.h"
12*b0d17251Schristos
13*b0d17251Schristos /*
14*b0d17251Schristos * X509_ctrl_str() is sorely lacking in libcrypto, but is still needed to
15*b0d17251Schristos * allow the application to process verification options in a manner similar
16*b0d17251Schristos * to signature or other options that pass through EVP_PKEY_CTX_ctrl_str(),
17*b0d17251Schristos * for uniformity.
18*b0d17251Schristos *
19*b0d17251Schristos * As soon as more stuff is added, the code will need serious rework. For
20*b0d17251Schristos * the moment, it only handles the FIPS 196 / SM2 distinguishing ID.
21*b0d17251Schristos */
22*b0d17251Schristos #ifdef EVP_PKEY_CTRL_SET1_ID
mk_octet_string(void * value,size_t value_n)23*b0d17251Schristos static ASN1_OCTET_STRING *mk_octet_string(void *value, size_t value_n)
24*b0d17251Schristos {
25*b0d17251Schristos ASN1_OCTET_STRING *v = ASN1_OCTET_STRING_new();
26*b0d17251Schristos
27*b0d17251Schristos if (v == NULL) {
28*b0d17251Schristos BIO_printf(bio_err, "error: allocation failed\n");
29*b0d17251Schristos } else if (!ASN1_OCTET_STRING_set(v, value, (int)value_n)) {
30*b0d17251Schristos ASN1_OCTET_STRING_free(v);
31*b0d17251Schristos v = NULL;
32*b0d17251Schristos }
33*b0d17251Schristos return v;
34*b0d17251Schristos }
35*b0d17251Schristos #endif
36*b0d17251Schristos
x509_ctrl(void * object,int cmd,void * value,size_t value_n)37*b0d17251Schristos static int x509_ctrl(void *object, int cmd, void *value, size_t value_n)
38*b0d17251Schristos {
39*b0d17251Schristos switch (cmd) {
40*b0d17251Schristos #ifdef EVP_PKEY_CTRL_SET1_ID
41*b0d17251Schristos case EVP_PKEY_CTRL_SET1_ID:
42*b0d17251Schristos {
43*b0d17251Schristos ASN1_OCTET_STRING *v = mk_octet_string(value, value_n);
44*b0d17251Schristos
45*b0d17251Schristos if (v == NULL) {
46*b0d17251Schristos BIO_printf(bio_err,
47*b0d17251Schristos "error: setting distinguishing ID in certificate failed\n");
48*b0d17251Schristos return 0;
49*b0d17251Schristos }
50*b0d17251Schristos
51*b0d17251Schristos X509_set0_distinguishing_id(object, v);
52*b0d17251Schristos return 1;
53*b0d17251Schristos }
54*b0d17251Schristos #endif
55*b0d17251Schristos default:
56*b0d17251Schristos break;
57*b0d17251Schristos }
58*b0d17251Schristos return -2; /* typical EVP_PKEY return for "unsupported" */
59*b0d17251Schristos }
60*b0d17251Schristos
x509_req_ctrl(void * object,int cmd,void * value,size_t value_n)61*b0d17251Schristos static int x509_req_ctrl(void *object, int cmd, void *value, size_t value_n)
62*b0d17251Schristos {
63*b0d17251Schristos switch (cmd) {
64*b0d17251Schristos #ifdef EVP_PKEY_CTRL_SET1_ID
65*b0d17251Schristos case EVP_PKEY_CTRL_SET1_ID:
66*b0d17251Schristos {
67*b0d17251Schristos ASN1_OCTET_STRING *v = mk_octet_string(value, value_n);
68*b0d17251Schristos
69*b0d17251Schristos if (v == NULL) {
70*b0d17251Schristos BIO_printf(bio_err,
71*b0d17251Schristos "error: setting distinguishing ID in certificate signing request failed\n");
72*b0d17251Schristos return 0;
73*b0d17251Schristos }
74*b0d17251Schristos
75*b0d17251Schristos X509_REQ_set0_distinguishing_id(object, v);
76*b0d17251Schristos return 1;
77*b0d17251Schristos }
78*b0d17251Schristos #endif
79*b0d17251Schristos default:
80*b0d17251Schristos break;
81*b0d17251Schristos }
82*b0d17251Schristos return -2; /* typical EVP_PKEY return for "unsupported" */
83*b0d17251Schristos }
84*b0d17251Schristos
do_x509_ctrl_string(int (* ctrl)(void * object,int cmd,void * value,size_t value_n),void * object,const char * value)85*b0d17251Schristos static int do_x509_ctrl_string(int (*ctrl)(void *object, int cmd,
86*b0d17251Schristos void *value, size_t value_n),
87*b0d17251Schristos void *object, const char *value)
88*b0d17251Schristos {
89*b0d17251Schristos int rv = 0;
90*b0d17251Schristos char *stmp, *vtmp = NULL;
91*b0d17251Schristos size_t vtmp_len = 0;
92*b0d17251Schristos int cmd = 0; /* Will get command values that make sense somehow */
93*b0d17251Schristos
94*b0d17251Schristos stmp = OPENSSL_strdup(value);
95*b0d17251Schristos if (stmp == NULL)
96*b0d17251Schristos return -1;
97*b0d17251Schristos vtmp = strchr(stmp, ':');
98*b0d17251Schristos if (vtmp != NULL) {
99*b0d17251Schristos *vtmp = 0;
100*b0d17251Schristos vtmp++;
101*b0d17251Schristos vtmp_len = strlen(vtmp);
102*b0d17251Schristos }
103*b0d17251Schristos
104*b0d17251Schristos if (strcmp(stmp, "distid") == 0) {
105*b0d17251Schristos #ifdef EVP_PKEY_CTRL_SET1_ID
106*b0d17251Schristos cmd = EVP_PKEY_CTRL_SET1_ID; /* ... except we put it in X509 */
107*b0d17251Schristos #endif
108*b0d17251Schristos } else if (strcmp(stmp, "hexdistid") == 0) {
109*b0d17251Schristos if (vtmp != NULL) {
110*b0d17251Schristos void *hexid;
111*b0d17251Schristos long hexid_len = 0;
112*b0d17251Schristos
113*b0d17251Schristos hexid = OPENSSL_hexstr2buf((const char *)vtmp, &hexid_len);
114*b0d17251Schristos OPENSSL_free(stmp);
115*b0d17251Schristos stmp = vtmp = hexid;
116*b0d17251Schristos vtmp_len = (size_t)hexid_len;
117*b0d17251Schristos }
118*b0d17251Schristos #ifdef EVP_PKEY_CTRL_SET1_ID
119*b0d17251Schristos cmd = EVP_PKEY_CTRL_SET1_ID; /* ... except we put it in X509 */
120*b0d17251Schristos #endif
121*b0d17251Schristos }
122*b0d17251Schristos
123*b0d17251Schristos rv = ctrl(object, cmd, vtmp, vtmp_len);
124*b0d17251Schristos
125*b0d17251Schristos OPENSSL_free(stmp);
126*b0d17251Schristos return rv;
127*b0d17251Schristos }
128*b0d17251Schristos
x509_ctrl_string(X509 * x,const char * value)129*b0d17251Schristos int x509_ctrl_string(X509 *x, const char *value)
130*b0d17251Schristos {
131*b0d17251Schristos return do_x509_ctrl_string(x509_ctrl, x, value);
132*b0d17251Schristos }
133*b0d17251Schristos
x509_req_ctrl_string(X509_REQ * x,const char * value)134*b0d17251Schristos int x509_req_ctrl_string(X509_REQ *x, const char *value)
135*b0d17251Schristos {
136*b0d17251Schristos return do_x509_ctrl_string(x509_req_ctrl, x, value);
137*b0d17251Schristos }
138