| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <assert.h> |
| 16 | #include <stdio.h> |
| 17 |
| 18 | #include <openssl/bn.h> |
| 19 | #include <openssl/buffer.h> |
| 20 | #include <openssl/err.h> |
| 21 | #include <openssl/objects.h> |
| 22 | #include <openssl/x509.h> |
| 23 |
| 24 | #include "internal.h" |
| 25 |
| 26 |
| 27 | int X509_REQ_print_fp(FILE *fp, X509_REQ *x) { |
| 28 | BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE); |
| 29 | if (bio == NULL) { |
| 30 | OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB); |
| 31 | return 0; |
| 32 | } |
| 33 | int ret = X509_REQ_print(bio, x); |
| 34 | BIO_free(bio); |
| 35 | return ret; |
| 36 | } |
| 37 |
| 38 | int X509_REQ_print_ex(BIO *bio, X509_REQ *x, unsigned long nmflags, |
| 39 | unsigned long cflag) { |
| 40 | long l; |
| 41 | STACK_OF(X509_ATTRIBUTE) *sk; |
| 42 | char mlch = ' '; |
| 43 |
| 44 | int nmindent = 0; |
| 45 |
| 46 | if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) { |
| 47 | mlch = '\n'; |
| 48 | nmindent = 12; |
| 49 | } |
| 50 |
| 51 | if (nmflags == X509_FLAG_COMPAT) { |
| 52 | nmindent = 16; |
| 53 | } |
| 54 |
| 55 | X509_REQ_INFO *ri = x->req_info; |
| 56 | if (!(cflag & X509_FLAG_NO_HEADER)) { |
| 57 | if (BIO_write(bio, "Certificate Request:\n", 21) <= 0 || |
| 58 | BIO_write(bio, " Data:\n", 10) <= 0) { |
| 59 | goto err; |
| 60 | } |
| 61 | } |
| 62 | if (!(cflag & X509_FLAG_NO_VERSION)) { |
| 63 | l = X509_REQ_get_version(x); |
| 64 | // Only zero, |X509_REQ_VERSION_1|, is valid but our parser accepts some |
| 65 | // invalid values for compatibility. |
| 66 | assert(0 <= l && l <= 2); |
| 67 | if (BIO_printf(bio, "%8sVersion: %ld (0x%lx)\n", "", l + 1, |
| 68 | (unsigned long)l) <= 0) { |
| 69 | goto err; |
| 70 | } |
| 71 | } |
| 72 | if (!(cflag & X509_FLAG_NO_SUBJECT)) { |
| 73 | if (BIO_printf(bio, " Subject:%c", mlch) <= 0 || |
| 74 | X509_NAME_print_ex(bio, ri->subject, nmindent, nmflags) < 0 || |
| 75 | BIO_write(bio, "\n", 1) <= 0) { |
| 76 | goto err; |
| 77 | } |
| 78 | } |
| 79 | if (!(cflag & X509_FLAG_NO_PUBKEY)) { |
| 80 | if (BIO_write(bio, " Subject Public Key Info:\n", 33) <= 0 || |
| 81 | BIO_printf(bio, "%12sPublic Key Algorithm: ", "") <= 0 || |
| 82 | i2a_ASN1_OBJECT(bio, ri->pubkey->algor->algorithm) <= 0 || |
| 83 | BIO_puts(bio, "\n") <= 0) { |
| 84 | goto err; |
| 85 | } |
| 86 |
| 87 | const EVP_PKEY *pkey = X509_REQ_get0_pubkey(x); |
| 88 | if (pkey == NULL) { |
| 89 | BIO_printf(bio, "%12sUnable to load Public Key\n", ""); |
| 90 | ERR_print_errors(bio); |
| 91 | } else { |
| 92 | EVP_PKEY_print_public(bio, pkey, 16, NULL); |
| 93 | } |
| 94 | } |
| 95 |
| 96 | if (!(cflag & X509_FLAG_NO_ATTRIBUTES)) { |
| 97 | if (BIO_printf(bio, "%8sAttributes:\n", "") <= 0) { |
| 98 | goto err; |
| 99 | } |
| 100 |
| 101 | sk = x->req_info->attributes; |
| 102 | if (sk_X509_ATTRIBUTE_num(sk) == 0) { |
| 103 | if (BIO_printf(bio, "%12sa0:00\n", "") <= 0) { |
| 104 | goto err; |
| 105 | } |
| 106 | } else { |
| 107 | size_t i; |
| 108 | for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) { |
| 109 | X509_ATTRIBUTE *a = sk_X509_ATTRIBUTE_value(sk, i); |
| 110 | ASN1_OBJECT *aobj = X509_ATTRIBUTE_get0_object(a); |
| 111 |
| 112 | if (X509_REQ_extension_nid(OBJ_obj2nid(aobj))) { |
| 113 | continue; |
| 114 | } |
| 115 |
| 116 | if (BIO_printf(bio, "%12s", "") <= 0) { |
| 117 | goto err; |
| 118 | } |
| 119 |
| 120 | const int num_attrs = X509_ATTRIBUTE_count(a); |
| 121 | const int obj_str_len = i2a_ASN1_OBJECT(bio, aobj); |
| 122 | if (obj_str_len <= 0) { |
| 123 | if (BIO_puts(bio, "(Unable to print attribute ID.)\n") < 0) { |
| 124 | goto err; |
| 125 | } else { |
| 126 | continue; |
| 127 | } |
| 128 | } |
| 129 |
| 130 | int j; |
| 131 | for (j = 0; j < num_attrs; j++) { |
| 132 | const ASN1_TYPE *at = X509_ATTRIBUTE_get0_type(a, j); |
| 133 | const int type = at->type; |
| 134 | ASN1_BIT_STRING *bs = at->value.asn1_string; |
| 135 |
| 136 | int k; |
| 137 | for (k = 25 - obj_str_len; k > 0; k--) { |
| 138 | if (BIO_write(bio, " ", 1) != 1) { |
| 139 | goto err; |
| 140 | } |
| 141 | } |
| 142 |
| 143 | if (BIO_puts(bio, ":") <= 0) { |
| 144 | goto err; |
| 145 | } |
| 146 |
| 147 | if (type == V_ASN1_PRINTABLESTRING || type == V_ASN1_UTF8STRING || |
| 148 | type == V_ASN1_IA5STRING || type == V_ASN1_T61STRING) { |
| 149 | if (BIO_write(bio, (char *)bs->data, bs->length) != bs->length) { |
| 150 | goto err; |
| 151 | } |
| 152 | BIO_puts(bio, "\n"); |
| 153 | } else { |
| 154 | BIO_puts(bio, "unable to print attribute\n"); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 |
| 161 | if (!(cflag & X509_FLAG_NO_EXTENSIONS)) { |
| 162 | STACK_OF(X509_EXTENSION) *exts = X509_REQ_get_extensions(x); |
| 163 | if (exts) { |
| 164 | BIO_printf(bio, "%8sRequested Extensions:\n", ""); |
| 165 |
| 166 | for (size_t i = 0; i < sk_X509_EXTENSION_num(exts); i++) { |
| 167 | const X509_EXTENSION *ex = sk_X509_EXTENSION_value(exts, i); |
| 168 | if (BIO_printf(bio, "%12s", "") <= 0) { |
| 169 | goto err; |
| 170 | } |
| 171 | const ASN1_OBJECT *obj = X509_EXTENSION_get_object(ex); |
| 172 | i2a_ASN1_OBJECT(bio, obj); |
| 173 | const int is_critical = X509_EXTENSION_get_critical(ex); |
| 174 | if (BIO_printf(bio, ": %s\n", is_critical ? "critical" : "") <= 0) { |
| 175 | goto err; |
| 176 | } |
| 177 | if (!X509V3_EXT_print(bio, ex, cflag, 16)) { |
| 178 | BIO_printf(bio, "%16s", ""); |
| 179 | ASN1_STRING_print(bio, X509_EXTENSION_get_data(ex)); |
| 180 | } |
| 181 | if (BIO_write(bio, "\n", 1) <= 0) { |
| 182 | goto err; |
| 183 | } |
| 184 | } |
| 185 | sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free); |
| 186 | } |
| 187 | } |
| 188 |
| 189 | if (!(cflag & X509_FLAG_NO_SIGDUMP) && |
| 190 | !X509_signature_print(bio, x->sig_alg, x->signature)) { |
| 191 | goto err; |
| 192 | } |
| 193 |
| 194 | return 1; |
| 195 |
| 196 | err: |
| 197 | OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB); |
| 198 | return 0; |
| 199 | } |
| 200 |
| 201 | int X509_REQ_print(BIO *bio, X509_REQ *req) { |
| 202 | return X509_REQ_print_ex(bio, req, XN_FLAG_COMPAT, X509_FLAG_COMPAT); |
| 203 | } |
| 204 |