t_req.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. X509err(X509_F_X509_REQ_PRINT_FP, 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 >= 0 && l <= 2) {
  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, "%12sa0:00\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. get_next:
  120. at = X509_ATTRIBUTE_get0_type(a, ii);
  121. type = at->type;
  122. bs = at->value.asn1_string;
  123. }
  124. for (j = 25 - j; j > 0; j--)
  125. if (BIO_write(bp, " ", 1) != 1)
  126. goto err;
  127. if (BIO_puts(bp, ":") <= 0)
  128. goto err;
  129. switch (type) {
  130. case V_ASN1_PRINTABLESTRING:
  131. case V_ASN1_T61STRING:
  132. case V_ASN1_NUMERICSTRING:
  133. case V_ASN1_UTF8STRING:
  134. case V_ASN1_IA5STRING:
  135. if (BIO_write(bp, (char *)bs->data, bs->length)
  136. != bs->length)
  137. goto err;
  138. if (BIO_puts(bp, "\n") <= 0)
  139. goto err;
  140. break;
  141. default:
  142. if (BIO_puts(bp, "unable to print attribute\n") <= 0)
  143. goto err;
  144. break;
  145. }
  146. if (++ii < count)
  147. goto get_next;
  148. }
  149. }
  150. }
  151. if (!(cflag & X509_FLAG_NO_EXTENSIONS)) {
  152. exts = X509_REQ_get_extensions(x);
  153. if (exts) {
  154. if (BIO_printf(bp, "%8sRequested Extensions:\n", "") <= 0)
  155. goto err;
  156. for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
  157. ASN1_OBJECT *obj;
  158. X509_EXTENSION *ex;
  159. int critical;
  160. ex = sk_X509_EXTENSION_value(exts, i);
  161. if (BIO_printf(bp, "%12s", "") <= 0)
  162. goto err;
  163. obj = X509_EXTENSION_get_object(ex);
  164. if (i2a_ASN1_OBJECT(bp, obj) <= 0)
  165. goto err;
  166. critical = X509_EXTENSION_get_critical(ex);
  167. if (BIO_printf(bp, ": %s\n", critical ? "critical" : "") <= 0)
  168. goto err;
  169. if (!X509V3_EXT_print(bp, ex, cflag, 16)) {
  170. if (BIO_printf(bp, "%16s", "") <= 0
  171. || ASN1_STRING_print(bp,
  172. X509_EXTENSION_get_data(ex)) <= 0)
  173. goto err;
  174. }
  175. if (BIO_write(bp, "\n", 1) <= 0)
  176. goto err;
  177. }
  178. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  179. }
  180. }
  181. if (!(cflag & X509_FLAG_NO_SIGDUMP)) {
  182. const X509_ALGOR *sig_alg;
  183. const ASN1_BIT_STRING *sig;
  184. X509_REQ_get0_signature(x, &sig, &sig_alg);
  185. if (!X509_signature_print(bp, sig_alg, sig))
  186. goto err;
  187. }
  188. return 1;
  189. err:
  190. X509err(X509_F_X509_REQ_PRINT_EX, ERR_R_BUF_LIB);
  191. return 0;
  192. }
  193. int X509_REQ_print(BIO *bp, X509_REQ *x)
  194. {
  195. return X509_REQ_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
  196. }