t_req.c 6.9 KB

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