xref: /freebsd/crypto/openssl/crypto/x509/v3_ist.c (revision 6f1af0d7d2af54b339b5212434cd6d4fda628d80)
1b077aed3SPierre Pronchery /*
2aa795734SPierre Pronchery  * Copyright 2020-2023 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();
53aa795734SPierre Pronchery             if (ist->signTool == NULL || !ASN1_STRING_set(ist->signTool, cnf->value, strlen(cnf->value))) {
54*6f1af0d7SPierre Pronchery                 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
55aa795734SPierre Pronchery                 goto err;
56b077aed3SPierre Pronchery             }
57b077aed3SPierre Pronchery         } else if (strcmp(cnf->name, "cATool") == 0) {
58b077aed3SPierre Pronchery             ist->cATool = ASN1_UTF8STRING_new();
59aa795734SPierre Pronchery             if (ist->cATool == NULL || !ASN1_STRING_set(ist->cATool, cnf->value, strlen(cnf->value))) {
60*6f1af0d7SPierre Pronchery                 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
61aa795734SPierre Pronchery                 goto err;
62b077aed3SPierre Pronchery             }
63b077aed3SPierre Pronchery         } else if (strcmp(cnf->name, "signToolCert") == 0) {
64b077aed3SPierre Pronchery             ist->signToolCert = ASN1_UTF8STRING_new();
65aa795734SPierre Pronchery             if (ist->signToolCert == NULL || !ASN1_STRING_set(ist->signToolCert, cnf->value, strlen(cnf->value))) {
66*6f1af0d7SPierre Pronchery                 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
67aa795734SPierre Pronchery                 goto err;
68b077aed3SPierre Pronchery             }
69b077aed3SPierre Pronchery         } else if (strcmp(cnf->name, "cAToolCert") == 0) {
70b077aed3SPierre Pronchery             ist->cAToolCert = ASN1_UTF8STRING_new();
71aa795734SPierre Pronchery             if (ist->cAToolCert == NULL || !ASN1_STRING_set(ist->cAToolCert, cnf->value, strlen(cnf->value))) {
72*6f1af0d7SPierre Pronchery                 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
73aa795734SPierre Pronchery                 goto err;
74b077aed3SPierre Pronchery             }
75b077aed3SPierre Pronchery         } else {
76b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_X509V3, ERR_R_PASSED_INVALID_ARGUMENT);
77aa795734SPierre Pronchery             goto err;
78b077aed3SPierre Pronchery         }
79b077aed3SPierre Pronchery     }
80b077aed3SPierre Pronchery     return ist;
81aa795734SPierre Pronchery 
82aa795734SPierre Pronchery err:
83aa795734SPierre Pronchery     ISSUER_SIGN_TOOL_free(ist);
84aa795734SPierre Pronchery     return NULL;
85b077aed3SPierre Pronchery }
86b077aed3SPierre Pronchery 
87b077aed3SPierre Pronchery static int i2r_issuer_sign_tool(X509V3_EXT_METHOD *method,
88b077aed3SPierre Pronchery                                  ISSUER_SIGN_TOOL *ist, BIO *out,
89b077aed3SPierre Pronchery                                  int indent)
90b077aed3SPierre Pronchery {
91b077aed3SPierre Pronchery     int new_line = 0;
92b077aed3SPierre Pronchery 
93b077aed3SPierre Pronchery     if (ist == NULL) {
94b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_X509V3, ERR_R_PASSED_INVALID_ARGUMENT);
95b077aed3SPierre Pronchery         return 0;
96b077aed3SPierre Pronchery     }
97b077aed3SPierre Pronchery     if (ist->signTool != NULL) {
98b077aed3SPierre Pronchery         if (new_line == 1) {
99b077aed3SPierre Pronchery             BIO_write(out, "\n", 1);
100b077aed3SPierre Pronchery         }
101b077aed3SPierre Pronchery         BIO_printf(out, "%*ssignTool    : ", indent, "");
102b077aed3SPierre Pronchery         BIO_write(out, ist->signTool->data, ist->signTool->length);
103b077aed3SPierre Pronchery         new_line = 1;
104b077aed3SPierre Pronchery     }
105b077aed3SPierre Pronchery     if (ist->cATool != NULL) {
106b077aed3SPierre Pronchery         if (new_line == 1) {
107b077aed3SPierre Pronchery             BIO_write(out, "\n", 1);
108b077aed3SPierre Pronchery         }
109b077aed3SPierre Pronchery         BIO_printf(out, "%*scATool      : ", indent, "");
110b077aed3SPierre Pronchery         BIO_write(out, ist->cATool->data, ist->cATool->length);
111b077aed3SPierre Pronchery         new_line = 1;
112b077aed3SPierre Pronchery     }
113b077aed3SPierre Pronchery     if (ist->signToolCert != NULL) {
114b077aed3SPierre Pronchery         if (new_line == 1) {
115b077aed3SPierre Pronchery             BIO_write(out, "\n", 1);
116b077aed3SPierre Pronchery         }
117b077aed3SPierre Pronchery         BIO_printf(out, "%*ssignToolCert: ", indent, "");
118b077aed3SPierre Pronchery         BIO_write(out, ist->signToolCert->data, ist->signToolCert->length);
119b077aed3SPierre Pronchery         new_line = 1;
120b077aed3SPierre Pronchery     }
121b077aed3SPierre Pronchery     if (ist->cAToolCert != NULL) {
122b077aed3SPierre Pronchery         if (new_line == 1) {
123b077aed3SPierre Pronchery             BIO_write(out, "\n", 1);
124b077aed3SPierre Pronchery         }
125b077aed3SPierre Pronchery         BIO_printf(out, "%*scAToolCert  : ", indent, "");
126b077aed3SPierre Pronchery         BIO_write(out, ist->cAToolCert->data, ist->cAToolCert->length);
127b077aed3SPierre Pronchery         new_line = 1;
128b077aed3SPierre Pronchery     }
129b077aed3SPierre Pronchery     return 1;
130b077aed3SPierre Pronchery }
131b077aed3SPierre Pronchery 
132b077aed3SPierre Pronchery const X509V3_EXT_METHOD ossl_v3_issuer_sign_tool = {
133b077aed3SPierre Pronchery     NID_issuerSignTool,                   /* nid */
134b077aed3SPierre Pronchery     X509V3_EXT_MULTILINE,                 /* flags */
135b077aed3SPierre Pronchery     ASN1_ITEM_ref(ISSUER_SIGN_TOOL),      /* template */
136b077aed3SPierre Pronchery     0, 0, 0, 0,                           /* old functions, ignored */
137b077aed3SPierre Pronchery     0,                                    /* i2s */
138b077aed3SPierre Pronchery     0,                                    /* s2i */
139b077aed3SPierre Pronchery     0,                                    /* i2v */
140b077aed3SPierre Pronchery     (X509V3_EXT_V2I)v2i_issuer_sign_tool, /* v2i */
141b077aed3SPierre Pronchery     (X509V3_EXT_I2R)i2r_issuer_sign_tool, /* i2r */
142b077aed3SPierre Pronchery     0,                                    /* r2i */
143b077aed3SPierre Pronchery     NULL                                  /* extension-specific data */
144b077aed3SPierre Pronchery };
145