v3_ist.c 5.6 KB

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