t_req.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright 1995-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/buffer.h>
  12. #include <openssl/bn.h>
  13. #include <openssl/objects.h>
  14. #include <openssl/x509.h>
  15. #include <openssl/x509v3.h>
  16. #include <openssl/rsa.h>
  17. #include <openssl/dsa.h>
  18. DEFINE_STACK_OF(X509_EXTENSION)
  19. #ifndef OPENSSL_NO_STDIO
  20. int X509_REQ_print_fp(FILE *fp, X509_REQ *x)
  21. {
  22. BIO *b;
  23. int ret;
  24. if ((b = BIO_new(BIO_s_file())) == NULL) {
  25. X509err(X509_F_X509_REQ_PRINT_FP, ERR_R_BUF_LIB);
  26. return 0;
  27. }
  28. BIO_set_fp(b, fp, BIO_NOCLOSE);
  29. ret = X509_REQ_print(b, x);
  30. BIO_free(b);
  31. return ret;
  32. }
  33. #endif
  34. int X509_REQ_print_ex(BIO *bp, X509_REQ *x, unsigned long nmflags,
  35. unsigned long cflag)
  36. {
  37. long l;
  38. int i;
  39. EVP_PKEY *pkey;
  40. STACK_OF(X509_EXTENSION) *exts;
  41. char mlch = ' ';
  42. int nmindent = 0;
  43. if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
  44. mlch = '\n';
  45. nmindent = 12;
  46. }
  47. if (nmflags == X509_FLAG_COMPAT)
  48. nmindent = 16;
  49. if (!(cflag & X509_FLAG_NO_HEADER)) {
  50. if (BIO_write(bp, "Certificate Request:\n", 21) <= 0)
  51. goto err;
  52. if (BIO_write(bp, " Data:\n", 10) <= 0)
  53. goto err;
  54. }
  55. if (!(cflag & X509_FLAG_NO_VERSION)) {
  56. l = X509_REQ_get_version(x);
  57. if (l >= 0 && l <= 2) {
  58. if (BIO_printf(bp, "%8sVersion: %ld (0x%lx)\n", "", l + 1, (unsigned long)l) <= 0)
  59. goto err;
  60. } else {
  61. if (BIO_printf(bp, "%8sVersion: Unknown (%ld)\n", "", l) <= 0)
  62. goto err;
  63. }
  64. }
  65. if (!(cflag & X509_FLAG_NO_SUBJECT)) {
  66. if (BIO_printf(bp, " Subject:%c", mlch) <= 0)
  67. goto err;
  68. if (X509_NAME_print_ex(bp, X509_REQ_get_subject_name(x),
  69. nmindent, nmflags) < 0)
  70. goto err;
  71. if (BIO_write(bp, "\n", 1) <= 0)
  72. goto err;
  73. }
  74. if (!(cflag & X509_FLAG_NO_PUBKEY)) {
  75. X509_PUBKEY *xpkey;
  76. ASN1_OBJECT *koid;
  77. if (BIO_write(bp, " Subject Public Key Info:\n", 33) <= 0)
  78. goto err;
  79. if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0)
  80. goto err;
  81. xpkey = X509_REQ_get_X509_PUBKEY(x);
  82. X509_PUBKEY_get0_param(&koid, NULL, NULL, NULL, xpkey);
  83. if (i2a_ASN1_OBJECT(bp, koid) <= 0)
  84. goto err;
  85. if (BIO_puts(bp, "\n") <= 0)
  86. goto err;
  87. pkey = X509_REQ_get0_pubkey(x);
  88. if (pkey == NULL) {
  89. if (BIO_printf(bp, "%12sUnable to load Public Key\n", "") <= 0)
  90. goto err;
  91. ERR_print_errors(bp);
  92. } else {
  93. if (EVP_PKEY_print_public(bp, pkey, 16, NULL) <= 0)
  94. goto err;
  95. }
  96. }
  97. if (!(cflag & X509_FLAG_NO_ATTRIBUTES)) {
  98. /* may not be */
  99. if (BIO_printf(bp, "%8sAttributes:\n", "") <= 0)
  100. goto err;
  101. if (X509_REQ_get_attr_count(x) == 0) {
  102. if (BIO_printf(bp, "%12sa0:00\n", "") <= 0)
  103. goto err;
  104. } else {
  105. for (i = 0; i < X509_REQ_get_attr_count(x); i++) {
  106. ASN1_TYPE *at;
  107. X509_ATTRIBUTE *a;
  108. ASN1_BIT_STRING *bs = NULL;
  109. ASN1_OBJECT *aobj;
  110. int j, type = 0, count = 1, ii = 0;
  111. a = X509_REQ_get_attr(x, i);
  112. aobj = X509_ATTRIBUTE_get0_object(a);
  113. if (X509_REQ_extension_nid(OBJ_obj2nid(aobj)))
  114. continue;
  115. if (BIO_printf(bp, "%12s", "") <= 0)
  116. goto err;
  117. if ((j = i2a_ASN1_OBJECT(bp, aobj)) > 0) {
  118. ii = 0;
  119. count = X509_ATTRIBUTE_count(a);
  120. if (count == 0) {
  121. X509err(X509_F_X509_REQ_PRINT_EX, X509_R_INVALID_ATTRIBUTES);
  122. return 0;
  123. }
  124. get_next:
  125. at = X509_ATTRIBUTE_get0_type(a, ii);
  126. type = at->type;
  127. bs = at->value.asn1_string;
  128. }
  129. for (j = 25 - j; j > 0; j--)
  130. if (BIO_write(bp, " ", 1) != 1)
  131. goto err;
  132. if (BIO_puts(bp, ":") <= 0)
  133. goto err;
  134. switch (type) {
  135. case V_ASN1_PRINTABLESTRING:
  136. case V_ASN1_T61STRING:
  137. case V_ASN1_NUMERICSTRING:
  138. case V_ASN1_UTF8STRING:
  139. case V_ASN1_IA5STRING:
  140. if (BIO_write(bp, (char *)bs->data, bs->length)
  141. != bs->length)
  142. goto err;
  143. if (BIO_puts(bp, "\n") <= 0)
  144. goto err;
  145. break;
  146. default:
  147. if (BIO_puts(bp, "unable to print attribute\n") <= 0)
  148. goto err;
  149. break;
  150. }
  151. if (++ii < count)
  152. goto get_next;
  153. }
  154. }
  155. }
  156. if (!(cflag & X509_FLAG_NO_EXTENSIONS)) {
  157. exts = X509_REQ_get_extensions(x);
  158. if (exts) {
  159. if (BIO_printf(bp, "%8sRequested Extensions:\n", "") <= 0)
  160. goto err;
  161. for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
  162. ASN1_OBJECT *obj;
  163. X509_EXTENSION *ex;
  164. int critical;
  165. ex = sk_X509_EXTENSION_value(exts, i);
  166. if (BIO_printf(bp, "%12s", "") <= 0)
  167. goto err;
  168. obj = X509_EXTENSION_get_object(ex);
  169. if (i2a_ASN1_OBJECT(bp, obj) <= 0)
  170. goto err;
  171. critical = X509_EXTENSION_get_critical(ex);
  172. if (BIO_printf(bp, ": %s\n", critical ? "critical" : "") <= 0)
  173. goto err;
  174. if (!X509V3_EXT_print(bp, ex, cflag, 16)) {
  175. if (BIO_printf(bp, "%16s", "") <= 0
  176. || ASN1_STRING_print(bp,
  177. X509_EXTENSION_get_data(ex)) <= 0)
  178. goto err;
  179. }
  180. if (BIO_write(bp, "\n", 1) <= 0)
  181. goto err;
  182. }
  183. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  184. }
  185. }
  186. if (!(cflag & X509_FLAG_NO_SIGDUMP)) {
  187. const X509_ALGOR *sig_alg;
  188. const ASN1_BIT_STRING *sig;
  189. X509_REQ_get0_signature(x, &sig, &sig_alg);
  190. if (!X509_signature_print(bp, sig_alg, sig))
  191. goto err;
  192. }
  193. return 1;
  194. err:
  195. X509err(X509_F_X509_REQ_PRINT_EX, ERR_R_BUF_LIB);
  196. return 0;
  197. }
  198. int X509_REQ_print(BIO *bp, X509_REQ *x)
  199. {
  200. return X509_REQ_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
  201. }