| 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 <openssl/x509.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <inttypes.h> |
| 19 | #include <string.h> |
| 20 |
| 21 | #include <openssl/asn1.h> |
| 22 | #include <openssl/bio.h> |
| 23 | #include <openssl/obj.h> |
| 24 |
| 25 |
| 26 | static int maybe_write(BIO *out, const void *buf, int len) { |
| 27 | // If |out| is NULL, ignore the output but report the length. |
| 28 | return out == NULL || BIO_write(out, buf, len) == len; |
| 29 | } |
| 30 |
| 31 | // do_indent prints |indent| spaces to |out|. |
| 32 | static int do_indent(BIO *out, int indent) { |
| 33 | for (int i = 0; i < indent; i++) { |
| 34 | if (!maybe_write(out, " ", 1)) { |
| 35 | return 0; |
| 36 | } |
| 37 | } |
| 38 | return 1; |
| 39 | } |
| 40 |
| 41 | #define FN_WIDTH_LN 25 |
| 42 | #define FN_WIDTH_SN 10 |
| 43 |
| 44 | static int do_name_ex(BIO *out, const X509_NAME *n, int indent, |
| 45 | unsigned long flags) { |
| 46 | int prev = -1, orflags; |
| 47 | char objtmp[80]; |
| 48 | const char *objbuf; |
| 49 | int outlen, len; |
| 50 | const char *sep_dn, *sep_mv, *sep_eq; |
| 51 | int sep_dn_len, sep_mv_len, sep_eq_len; |
| 52 | if (indent < 0) { |
| 53 | indent = 0; |
| 54 | } |
| 55 | outlen = indent; |
| 56 | if (!do_indent(out, indent)) { |
| 57 | return -1; |
| 58 | } |
| 59 | switch (flags & XN_FLAG_SEP_MASK) { |
| 60 | case XN_FLAG_SEP_MULTILINE: |
| 61 | sep_dn = "\n"; |
| 62 | sep_dn_len = 1; |
| 63 | sep_mv = " + "; |
| 64 | sep_mv_len = 3; |
| 65 | break; |
| 66 |
| 67 | case XN_FLAG_SEP_COMMA_PLUS: |
| 68 | sep_dn = ","; |
| 69 | sep_dn_len = 1; |
| 70 | sep_mv = "+"; |
| 71 | sep_mv_len = 1; |
| 72 | indent = 0; |
| 73 | break; |
| 74 |
| 75 | case XN_FLAG_SEP_CPLUS_SPC: |
| 76 | sep_dn = ", "; |
| 77 | sep_dn_len = 2; |
| 78 | sep_mv = " + "; |
| 79 | sep_mv_len = 3; |
| 80 | indent = 0; |
| 81 | break; |
| 82 |
| 83 | case XN_FLAG_SEP_SPLUS_SPC: |
| 84 | sep_dn = "; "; |
| 85 | sep_dn_len = 2; |
| 86 | sep_mv = " + "; |
| 87 | sep_mv_len = 3; |
| 88 | indent = 0; |
| 89 | break; |
| 90 |
| 91 | default: |
| 92 | return -1; |
| 93 | } |
| 94 |
| 95 | if (flags & XN_FLAG_SPC_EQ) { |
| 96 | sep_eq = " = "; |
| 97 | sep_eq_len = 3; |
| 98 | } else { |
| 99 | sep_eq = "="; |
| 100 | sep_eq_len = 1; |
| 101 | } |
| 102 |
| 103 | int cnt = X509_NAME_entry_count(n); |
| 104 | for (int i = 0; i < cnt; i++) { |
| 105 | const X509_NAME_ENTRY *ent; |
| 106 | if (flags & XN_FLAG_DN_REV) { |
| 107 | ent = X509_NAME_get_entry(n, cnt - i - 1); |
| 108 | } else { |
| 109 | ent = X509_NAME_get_entry(n, i); |
| 110 | } |
| 111 | if (prev != -1) { |
| 112 | if (prev == X509_NAME_ENTRY_set(ent)) { |
| 113 | if (!maybe_write(out, sep_mv, sep_mv_len)) { |
| 114 | return -1; |
| 115 | } |
| 116 | outlen += sep_mv_len; |
| 117 | } else { |
| 118 | if (!maybe_write(out, sep_dn, sep_dn_len)) { |
| 119 | return -1; |
| 120 | } |
| 121 | outlen += sep_dn_len; |
| 122 | if (!do_indent(out, indent)) { |
| 123 | return -1; |
| 124 | } |
| 125 | outlen += indent; |
| 126 | } |
| 127 | } |
| 128 | prev = X509_NAME_ENTRY_set(ent); |
| 129 | const ASN1_OBJECT *fn = X509_NAME_ENTRY_get_object(ent); |
| 130 | const ASN1_STRING *val = X509_NAME_ENTRY_get_data(ent); |
| 131 | assert((flags & XN_FLAG_FN_MASK) == XN_FLAG_FN_SN); |
| 132 | int fn_nid = OBJ_obj2nid(fn); |
| 133 | if (fn_nid == NID_undef) { |
| 134 | OBJ_obj2txt(objtmp, sizeof(objtmp), fn, 1); |
| 135 | objbuf = objtmp; |
| 136 | } else { |
| 137 | objbuf = OBJ_nid2sn(fn_nid); |
| 138 | } |
| 139 | int objlen = strlen(objbuf); |
| 140 | if (!maybe_write(out, objbuf, objlen) || |
| 141 | !maybe_write(out, sep_eq, sep_eq_len)) { |
| 142 | return -1; |
| 143 | } |
| 144 | outlen += objlen + sep_eq_len; |
| 145 | // If the field name is unknown then fix up the DER dump flag. We |
| 146 | // might want to limit this further so it will DER dump on anything |
| 147 | // other than a few 'standard' fields. |
| 148 | if ((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS)) { |
| 149 | orflags = ASN1_STRFLGS_DUMP_ALL; |
| 150 | } else { |
| 151 | orflags = 0; |
| 152 | } |
| 153 |
| 154 | len = ASN1_STRING_print_ex(out, val, flags | orflags); |
| 155 | if (len < 0) { |
| 156 | return -1; |
| 157 | } |
| 158 | outlen += len; |
| 159 | } |
| 160 | return outlen; |
| 161 | } |
| 162 |
| 163 | int X509_NAME_print_ex(BIO *out, const X509_NAME *nm, int indent, |
| 164 | unsigned long flags) { |
| 165 | if (flags == XN_FLAG_COMPAT) { |
| 166 | return X509_NAME_print(out, nm, indent); |
| 167 | } |
| 168 | return do_name_ex(out, nm, indent, flags); |
| 169 | } |
| 170 |
| 171 | int X509_NAME_print_ex_fp(FILE *fp, const X509_NAME *nm, int indent, |
| 172 | unsigned long flags) { |
| 173 | BIO *bio = NULL; |
| 174 | if (fp != NULL) { |
| 175 | // If |fp| is NULL, this function returns the number of bytes without |
| 176 | // writing. |
| 177 | bio = BIO_new_fp(fp, BIO_NOCLOSE); |
| 178 | if (bio == NULL) { |
| 179 | return -1; |
| 180 | } |
| 181 | } |
| 182 | int ret = X509_NAME_print_ex(bio, nm, indent, flags); |
| 183 | BIO_free(bio); |
| 184 | return ret; |
| 185 | } |
| 186 |