1b077aed3SPierre Pronchery /*
2*e0c4386eSCy Schubert * Copyright 2020-2024 The OpenSSL Project Authors. All Rights Reserved.
3b077aed3SPierre Pronchery *
4b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5b077aed3SPierre Pronchery * this file except in compliance with the License. You can obtain a copy
6b077aed3SPierre Pronchery * in the file LICENSE in the source distribution or at
7b077aed3SPierre Pronchery * https://www.openssl.org/source/license.html
8b077aed3SPierre Pronchery */
9b077aed3SPierre Pronchery
10b077aed3SPierre Pronchery #include <stdio.h>
11b077aed3SPierre Pronchery #include "internal/cryptlib.h"
12b077aed3SPierre Pronchery #include <openssl/conf.h>
13b077aed3SPierre Pronchery #include <openssl/asn1.h>
14b077aed3SPierre Pronchery #include <openssl/asn1t.h>
15b077aed3SPierre Pronchery #include <openssl/x509v3.h>
16b077aed3SPierre Pronchery #include "ext_dat.h"
17b077aed3SPierre Pronchery
18b077aed3SPierre Pronchery /*
19b077aed3SPierre Pronchery * Issuer Sign Tool (1.2.643.100.112) The name of the tool used to signs the subject (ASN1_SEQUENCE)
20b077aed3SPierre Pronchery * This extention is required to obtain the status of a qualified certificate at Russian Federation.
21b077aed3SPierre Pronchery * RFC-style description is available here: https://tools.ietf.org/html/draft-deremin-rfc4491-bis-04#section-5
22b077aed3SPierre Pronchery * Russian Federal Law 63 "Digital Sign" is available here: http://www.consultant.ru/document/cons_doc_LAW_112701/
23b077aed3SPierre Pronchery */
24b077aed3SPierre Pronchery
25b077aed3SPierre Pronchery ASN1_SEQUENCE(ISSUER_SIGN_TOOL) = {
26b077aed3SPierre Pronchery ASN1_SIMPLE(ISSUER_SIGN_TOOL, signTool, ASN1_UTF8STRING),
27b077aed3SPierre Pronchery ASN1_SIMPLE(ISSUER_SIGN_TOOL, cATool, ASN1_UTF8STRING),
28b077aed3SPierre Pronchery ASN1_SIMPLE(ISSUER_SIGN_TOOL, signToolCert, ASN1_UTF8STRING),
29b077aed3SPierre Pronchery ASN1_SIMPLE(ISSUER_SIGN_TOOL, cAToolCert, ASN1_UTF8STRING)
30b077aed3SPierre Pronchery } ASN1_SEQUENCE_END(ISSUER_SIGN_TOOL)
31b077aed3SPierre Pronchery
32b077aed3SPierre Pronchery IMPLEMENT_ASN1_FUNCTIONS(ISSUER_SIGN_TOOL)
33b077aed3SPierre Pronchery
34b077aed3SPierre Pronchery
35b077aed3SPierre Pronchery static ISSUER_SIGN_TOOL *v2i_issuer_sign_tool(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
36b077aed3SPierre Pronchery STACK_OF(CONF_VALUE) *nval)
37b077aed3SPierre Pronchery {
38b077aed3SPierre Pronchery ISSUER_SIGN_TOOL *ist = ISSUER_SIGN_TOOL_new();
39b077aed3SPierre Pronchery int i;
40b077aed3SPierre Pronchery
41b077aed3SPierre Pronchery if (ist == NULL) {
42b077aed3SPierre Pronchery ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
43b077aed3SPierre Pronchery return NULL;
44b077aed3SPierre Pronchery }
45b077aed3SPierre Pronchery for (i = 0; i < sk_CONF_VALUE_num(nval); ++i) {
46b077aed3SPierre Pronchery CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i);
47b077aed3SPierre Pronchery
48b077aed3SPierre Pronchery if (cnf == NULL) {
49b077aed3SPierre Pronchery continue;
50b077aed3SPierre Pronchery }
51b077aed3SPierre Pronchery if (strcmp(cnf->name, "signTool") == 0) {
52b077aed3SPierre Pronchery ist->signTool = ASN1_UTF8STRING_new();
53*e0c4386eSCy Schubert if (ist->signTool == NULL
54*e0c4386eSCy Schubert || cnf->value == NULL
55*e0c4386eSCy Schubert || !ASN1_STRING_set(ist->signTool, cnf->value, strlen(cnf->value))) {
566f1af0d7SPierre Pronchery ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
57aa795734SPierre Pronchery goto err;
58b077aed3SPierre Pronchery }
59b077aed3SPierre Pronchery } else if (strcmp(cnf->name, "cATool") == 0) {
60b077aed3SPierre Pronchery ist->cATool = ASN1_UTF8STRING_new();
61*e0c4386eSCy Schubert if (ist->cATool == NULL
62*e0c4386eSCy Schubert || cnf->value == NULL
63*e0c4386eSCy Schubert || !ASN1_STRING_set(ist->cATool, cnf->value, strlen(cnf->value))) {
646f1af0d7SPierre Pronchery ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
65aa795734SPierre Pronchery goto err;
66b077aed3SPierre Pronchery }
67b077aed3SPierre Pronchery } else if (strcmp(cnf->name, "signToolCert") == 0) {
68b077aed3SPierre Pronchery ist->signToolCert = ASN1_UTF8STRING_new();
69*e0c4386eSCy Schubert if (ist->signToolCert == NULL
70*e0c4386eSCy Schubert || cnf->value == NULL
71*e0c4386eSCy Schubert || !ASN1_STRING_set(ist->signToolCert, cnf->value, strlen(cnf->value))) {
726f1af0d7SPierre Pronchery ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
73aa795734SPierre Pronchery goto err;
74b077aed3SPierre Pronchery }
75b077aed3SPierre Pronchery } else if (strcmp(cnf->name, "cAToolCert") == 0) {
76b077aed3SPierre Pronchery ist->cAToolCert = ASN1_UTF8STRING_new();
77*e0c4386eSCy Schubert if (ist->cAToolCert == NULL
78*e0c4386eSCy Schubert || cnf->value == NULL
79*e0c4386eSCy Schubert || !ASN1_STRING_set(ist->cAToolCert, cnf->value, strlen(cnf->value))) {
806f1af0d7SPierre Pronchery ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
81aa795734SPierre Pronchery goto err;
82b077aed3SPierre Pronchery }
83b077aed3SPierre Pronchery } else {
84b077aed3SPierre Pronchery ERR_raise(ERR_LIB_X509V3, ERR_R_PASSED_INVALID_ARGUMENT);
85aa795734SPierre Pronchery goto err;
86b077aed3SPierre Pronchery }
87b077aed3SPierre Pronchery }
88b077aed3SPierre Pronchery return ist;
89aa795734SPierre Pronchery
90aa795734SPierre Pronchery err:
91aa795734SPierre Pronchery ISSUER_SIGN_TOOL_free(ist);
92aa795734SPierre Pronchery return NULL;
93b077aed3SPierre Pronchery }
94b077aed3SPierre Pronchery
i2r_issuer_sign_tool(X509V3_EXT_METHOD * method,ISSUER_SIGN_TOOL * ist,BIO * out,int indent)95b077aed3SPierre Pronchery static int i2r_issuer_sign_tool(X509V3_EXT_METHOD *method,
96b077aed3SPierre Pronchery ISSUER_SIGN_TOOL *ist, BIO *out,
97b077aed3SPierre Pronchery int indent)
98b077aed3SPierre Pronchery {
99b077aed3SPierre Pronchery int new_line = 0;
100b077aed3SPierre Pronchery
101b077aed3SPierre Pronchery if (ist == NULL) {
102b077aed3SPierre Pronchery ERR_raise(ERR_LIB_X509V3, ERR_R_PASSED_INVALID_ARGUMENT);
103b077aed3SPierre Pronchery return 0;
104b077aed3SPierre Pronchery }
105b077aed3SPierre Pronchery if (ist->signTool != NULL) {
106b077aed3SPierre Pronchery if (new_line == 1) {
107b077aed3SPierre Pronchery BIO_write(out, "\n", 1);
108b077aed3SPierre Pronchery }
109b077aed3SPierre Pronchery BIO_printf(out, "%*ssignTool : ", indent, "");
110b077aed3SPierre Pronchery BIO_write(out, ist->signTool->data, ist->signTool->length);
111b077aed3SPierre Pronchery new_line = 1;
112b077aed3SPierre Pronchery }
113b077aed3SPierre Pronchery if (ist->cATool != NULL) {
114b077aed3SPierre Pronchery if (new_line == 1) {
115b077aed3SPierre Pronchery BIO_write(out, "\n", 1);
116b077aed3SPierre Pronchery }
117b077aed3SPierre Pronchery BIO_printf(out, "%*scATool : ", indent, "");
118b077aed3SPierre Pronchery BIO_write(out, ist->cATool->data, ist->cATool->length);
119b077aed3SPierre Pronchery new_line = 1;
120b077aed3SPierre Pronchery }
121b077aed3SPierre Pronchery if (ist->signToolCert != NULL) {
122b077aed3SPierre Pronchery if (new_line == 1) {
123b077aed3SPierre Pronchery BIO_write(out, "\n", 1);
124b077aed3SPierre Pronchery }
125b077aed3SPierre Pronchery BIO_printf(out, "%*ssignToolCert: ", indent, "");
126b077aed3SPierre Pronchery BIO_write(out, ist->signToolCert->data, ist->signToolCert->length);
127b077aed3SPierre Pronchery new_line = 1;
128b077aed3SPierre Pronchery }
129b077aed3SPierre Pronchery if (ist->cAToolCert != NULL) {
130b077aed3SPierre Pronchery if (new_line == 1) {
131b077aed3SPierre Pronchery BIO_write(out, "\n", 1);
132b077aed3SPierre Pronchery }
133b077aed3SPierre Pronchery BIO_printf(out, "%*scAToolCert : ", indent, "");
134b077aed3SPierre Pronchery BIO_write(out, ist->cAToolCert->data, ist->cAToolCert->length);
135b077aed3SPierre Pronchery new_line = 1;
136b077aed3SPierre Pronchery }
137b077aed3SPierre Pronchery return 1;
138b077aed3SPierre Pronchery }
139b077aed3SPierre Pronchery
140b077aed3SPierre Pronchery const X509V3_EXT_METHOD ossl_v3_issuer_sign_tool = {
141b077aed3SPierre Pronchery NID_issuerSignTool, /* nid */
142b077aed3SPierre Pronchery X509V3_EXT_MULTILINE, /* flags */
143b077aed3SPierre Pronchery ASN1_ITEM_ref(ISSUER_SIGN_TOOL), /* template */
144b077aed3SPierre Pronchery 0, 0, 0, 0, /* old functions, ignored */
145b077aed3SPierre Pronchery 0, /* i2s */
146b077aed3SPierre Pronchery 0, /* s2i */
147b077aed3SPierre Pronchery 0, /* i2v */
148b077aed3SPierre Pronchery (X509V3_EXT_V2I)v2i_issuer_sign_tool, /* v2i */
149b077aed3SPierre Pronchery (X509V3_EXT_I2R)i2r_issuer_sign_tool, /* i2r */
150b077aed3SPierre Pronchery 0, /* r2i */
151b077aed3SPierre Pronchery NULL /* extension-specific data */
152b077aed3SPierre Pronchery };
153