summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/t_x509.c
diff options
context:
space:
mode:
authorcvs2svn <admin@example.com>2025-04-14 17:32:06 +0000
committercvs2svn <admin@example.com>2025-04-14 17:32:06 +0000
commiteb8dd9dca1228af0cd132f515509051ecfabf6f6 (patch)
treeedb6da6af7e865d488dc1a29309f1e1ec226e603 /src/lib/libcrypto/asn1/t_x509.c
parent247f0352e0ed72a4f476db9dc91f4d982bc83eb2 (diff)
downloadopenbsd-tb_20250414.tar.gz
openbsd-tb_20250414.tar.bz2
openbsd-tb_20250414.zip
This commit was manufactured by cvs2git to create tag 'tb_20250414'.tb_20250414
Diffstat (limited to 'src/lib/libcrypto/asn1/t_x509.c')
-rw-r--r--src/lib/libcrypto/asn1/t_x509.c485
1 files changed, 0 insertions, 485 deletions
diff --git a/src/lib/libcrypto/asn1/t_x509.c b/src/lib/libcrypto/asn1/t_x509.c
deleted file mode 100644
index 7cf4557314..0000000000
--- a/src/lib/libcrypto/asn1/t_x509.c
+++ /dev/null
@@ -1,485 +0,0 @@
1/* $OpenBSD: t_x509.c,v 1.51 2025/02/08 03:41:36 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <limits.h>
60#include <stdint.h>
61#include <stdio.h>
62#include <stdlib.h>
63
64#include <openssl/opensslconf.h>
65
66#include <openssl/asn1.h>
67#include <openssl/bio.h>
68#include <openssl/err.h>
69#include <openssl/evp.h>
70#include <openssl/objects.h>
71#include <openssl/sha.h>
72#include <openssl/x509.h>
73#include <openssl/x509v3.h>
74
75#include "evp_local.h"
76#include "x509_local.h"
77
78int
79X509_print_fp(FILE *fp, X509 *x)
80{
81 return X509_print_ex_fp(fp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
82}
83LCRYPTO_ALIAS(X509_print_fp);
84
85int
86X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag, unsigned long cflag)
87{
88 BIO *b;
89 int ret;
90
91 if ((b = BIO_new(BIO_s_file())) == NULL) {
92 X509error(ERR_R_BUF_LIB);
93 return (0);
94 }
95 BIO_set_fp(b, fp, BIO_NOCLOSE);
96 ret = X509_print_ex(b, x, nmflag, cflag);
97 BIO_free(b);
98 return (ret);
99}
100LCRYPTO_ALIAS(X509_print_ex_fp);
101
102int
103X509_print(BIO *bp, X509 *x)
104{
105 return X509_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
106}
107LCRYPTO_ALIAS(X509_print);
108
109int
110X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, unsigned long cflag)
111{
112 long l;
113 int ret = 0, i;
114 char *m = NULL, mlch = ' ';
115 int nmindent = 0;
116 X509_CINF *ci;
117 ASN1_INTEGER *bs;
118 EVP_PKEY *pkey = NULL;
119
120 if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
121 mlch = '\n';
122 nmindent = 12;
123 }
124
125 if (nmflags == X509_FLAG_COMPAT)
126 nmindent = 16;
127
128 ci = x->cert_info;
129 if (!(cflag & X509_FLAG_NO_HEADER)) {
130 if (BIO_write(bp, "Certificate:\n", 13) <= 0)
131 goto err;
132 if (BIO_write(bp, " Data:\n", 10) <= 0)
133 goto err;
134 }
135 if (!(cflag & X509_FLAG_NO_VERSION)) {
136 l = X509_get_version(x);
137 if (l >= 0 && l <= 2) {
138 if (BIO_printf(bp, "%8sVersion: %ld (0x%lx)\n",
139 "", l + 1, l) <= 0)
140 goto err;
141 } else {
142 if (BIO_printf(bp, "%8sVersion: unknown (%ld)\n",
143 "", l) <= 0)
144 goto err;
145 }
146 }
147 if (!(cflag & X509_FLAG_NO_SERIAL)) {
148 if (BIO_write(bp, " Serial Number:", 22) <= 0)
149 goto err;
150
151 bs = X509_get_serialNumber(x);
152 l = -1;
153
154 /*
155 * For historical reasons, non-negative serial numbers are
156 * printed in decimal as long as they fit into a long. Using
157 * ASN1_INTEGER_get_uint64() avoids an error on the stack for
158 * numbers between LONG_MAX and ULONG_MAX. Otherwise fall back
159 * to hexadecimal, also for numbers that are non-conformant
160 * (negative or larger than 2^159 - 1).
161 */
162 if (bs->length <= sizeof(long) && bs->type == V_ASN1_INTEGER) {
163 uint64_t u64;
164
165 if (ASN1_INTEGER_get_uint64(&u64, bs) && u64 <= LONG_MAX)
166 l = (long)u64;
167 }
168 if (l >= 0) {
169 if (BIO_printf(bp, " %ld (0x%lx)\n", l, l) <= 0)
170 goto err;
171 } else {
172 const char *neg = "";
173
174 if (bs->type == V_ASN1_NEG_INTEGER)
175 neg = " (Negative)";
176
177 if (BIO_printf(bp, "\n%12s%s", "", neg) <= 0)
178 goto err;
179 for (i = 0; i < bs->length; i++) {
180 if (BIO_printf(bp, "%02x%c", bs->data[i],
181 ((i + 1 == bs->length) ? '\n' : ':')) <= 0)
182 goto err;
183 }
184 }
185
186 }
187
188 if (!(cflag & X509_FLAG_NO_SIGNAME)) {
189 if (X509_signature_print(bp, x->sig_alg, NULL) <= 0)
190 goto err;
191 }
192
193 if (!(cflag & X509_FLAG_NO_ISSUER)) {
194 if (BIO_printf(bp, " Issuer:%c", mlch) <= 0)
195 goto err;
196 if (X509_NAME_print_ex(bp, X509_get_issuer_name(x),
197 nmindent, nmflags) < (nmflags == X509_FLAG_COMPAT ? 1 : 0))
198 goto err;
199 if (BIO_write(bp, "\n", 1) <= 0)
200 goto err;
201 }
202 if (!(cflag & X509_FLAG_NO_VALIDITY)) {
203 if (BIO_write(bp, " Validity\n", 17) <= 0)
204 goto err;
205 if (BIO_write(bp, " Not Before: ", 24) <= 0)
206 goto err;
207 if (!ASN1_TIME_print(bp, X509_get_notBefore(x)))
208 goto err;
209 if (BIO_write(bp, "\n Not After : ", 25) <= 0)
210 goto err;
211 if (!ASN1_TIME_print(bp, X509_get_notAfter(x)))
212 goto err;
213 if (BIO_write(bp, "\n", 1) <= 0)
214 goto err;
215 }
216 if (!(cflag & X509_FLAG_NO_SUBJECT)) {
217 if (BIO_printf(bp, " Subject:%c", mlch) <= 0)
218 goto err;
219 if (X509_NAME_print_ex(bp, X509_get_subject_name(x),
220 nmindent, nmflags) < (nmflags == X509_FLAG_COMPAT ? 1 : 0))
221 goto err;
222 if (BIO_write(bp, "\n", 1) <= 0)
223 goto err;
224 }
225 if (!(cflag & X509_FLAG_NO_PUBKEY)) {
226 if (BIO_write(bp, " Subject Public Key Info:\n",
227 33) <= 0)
228 goto err;
229 if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0)
230 goto err;
231 if (i2a_ASN1_OBJECT(bp, ci->key->algor->algorithm) <= 0)
232 goto err;
233 if (BIO_puts(bp, "\n") <= 0)
234 goto err;
235
236 pkey = X509_get_pubkey(x);
237 if (pkey == NULL) {
238 BIO_printf(bp, "%12sUnable to load Public Key\n", "");
239 ERR_print_errors(bp);
240 } else {
241 EVP_PKEY_print_public(bp, pkey, 16, NULL);
242 EVP_PKEY_free(pkey);
243 }
244 }
245
246 if (!(cflag & X509_FLAG_NO_EXTENSIONS))
247 X509V3_extensions_print(bp, "X509v3 extensions",
248 ci->extensions, cflag, 8);
249
250 if (!(cflag & X509_FLAG_NO_SIGDUMP)) {
251 if (X509_signature_print(bp, x->sig_alg, x->signature) <= 0)
252 goto err;
253 }
254 if (!(cflag & X509_FLAG_NO_AUX)) {
255 if (!X509_CERT_AUX_print(bp, x->aux, 0))
256 goto err;
257 }
258 ret = 1;
259
260 err:
261 free(m);
262 return (ret);
263}
264LCRYPTO_ALIAS(X509_print_ex);
265
266int
267X509_ocspid_print(BIO *bp, X509 *x)
268{
269 unsigned char *der = NULL;
270 unsigned char *dertmp;
271 int derlen;
272 int i;
273 unsigned char SHA1md[SHA_DIGEST_LENGTH];
274
275 /* display the hash of the subject as it would appear
276 in OCSP requests */
277 if (BIO_printf(bp, " Subject OCSP hash: ") <= 0)
278 goto err;
279 if ((derlen = i2d_X509_NAME(x->cert_info->subject, NULL)) <= 0)
280 goto err;
281 if ((der = dertmp = malloc(derlen)) == NULL)
282 goto err;
283 if (i2d_X509_NAME(x->cert_info->subject, &dertmp) <= 0)
284 goto err;
285
286 if (!EVP_Digest(der, derlen, SHA1md, NULL, EVP_sha1(), NULL))
287 goto err;
288 for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
289 if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
290 goto err;
291 }
292 free (der);
293 der = NULL;
294
295 /* display the hash of the public key as it would appear
296 in OCSP requests */
297 if (BIO_printf(bp, "\n Public key OCSP hash: ") <= 0)
298 goto err;
299
300 if (!EVP_Digest(x->cert_info->key->public_key->data,
301 x->cert_info->key->public_key->length,
302 SHA1md, NULL, EVP_sha1(), NULL))
303 goto err;
304 for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
305 if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0)
306 goto err;
307 }
308 BIO_printf(bp, "\n");
309
310 return (1);
311
312 err:
313 free(der);
314 return (0);
315}
316LCRYPTO_ALIAS(X509_ocspid_print);
317
318int
319X509_signature_dump(BIO *bp, const ASN1_STRING *sig, int indent)
320{
321 const unsigned char *s;
322 int i, n;
323
324 n = sig->length;
325 s = sig->data;
326 for (i = 0; i < n; i++) {
327 if ((i % 18) == 0) {
328 if (BIO_write(bp, "\n", 1) <= 0)
329 return 0;
330 if (BIO_indent(bp, indent, indent) <= 0)
331 return 0;
332 }
333 if (BIO_printf(bp, "%02x%s", s[i],
334 ((i + 1) == n) ? "" : ":") <= 0)
335 return 0;
336 }
337 if (BIO_write(bp, "\n", 1) != 1)
338 return 0;
339
340 return 1;
341}
342LCRYPTO_ALIAS(X509_signature_dump);
343
344int
345X509_signature_print(BIO *bp, const X509_ALGOR *sigalg, const ASN1_STRING *sig)
346{
347 int sig_nid;
348 if (BIO_puts(bp, " Signature Algorithm: ") <= 0)
349 return 0;
350 if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0)
351 return 0;
352
353 sig_nid = OBJ_obj2nid(sigalg->algorithm);
354 if (sig_nid != NID_undef) {
355 int pkey_nid, dig_nid;
356 const EVP_PKEY_ASN1_METHOD *ameth;
357 if (OBJ_find_sigid_algs(sig_nid, &dig_nid, &pkey_nid)) {
358 ameth = EVP_PKEY_asn1_find(NULL, pkey_nid);
359 if (ameth && ameth->sig_print)
360 return ameth->sig_print(bp, sigalg, sig, 9, 0);
361 }
362 }
363 if (sig)
364 return X509_signature_dump(bp, sig, 9);
365 else if (BIO_puts(bp, "\n") <= 0)
366 return 0;
367 return 1;
368}
369LCRYPTO_ALIAS(X509_signature_print);
370
371int
372ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
373{
374 if (tm->type == V_ASN1_UTCTIME)
375 return ASN1_UTCTIME_print(bp, tm);
376 if (tm->type == V_ASN1_GENERALIZEDTIME)
377 return ASN1_GENERALIZEDTIME_print(bp, tm);
378 BIO_write(bp, "Bad time value", 14);
379 return (0);
380}
381LCRYPTO_ALIAS(ASN1_TIME_print);
382
383static const char *mon[12] = {
384 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
385 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
386};
387
388int
389ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
390{
391 char *v;
392 int gmt = 0;
393 int i;
394 int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
395 char *f = "";
396 int f_len = 0;
397
398 i = tm->length;
399 v = (char *)tm->data;
400
401 if (i < 12)
402 goto err;
403 if (v[i-1] == 'Z')
404 gmt = 1;
405 for (i = 0; i < 12; i++)
406 if ((v[i] > '9') || (v[i] < '0'))
407 goto err;
408 y = (v[0] - '0') * 1000 + (v[1] - '0') * 100 +
409 (v[2] - '0') * 10 + (v[3] - '0');
410 M = (v[4] - '0') * 10 + (v[5] - '0');
411 if ((M > 12) || (M < 1))
412 goto err;
413 d = (v[6] - '0') * 10 + (v[7] - '0');
414 h = (v[8] - '0') * 10 + (v[9] - '0');
415 m = (v[10] - '0') * 10 + (v[11] - '0');
416 if (tm->length >= 14 &&
417 (v[12] >= '0') && (v[12] <= '9') &&
418 (v[13] >= '0') && (v[13] <= '9')) {
419 s = (v[12] - '0') * 10 + (v[13] - '0');
420 /* Check for fractions of seconds. */
421 if (tm->length >= 15 && v[14] == '.') {
422 int l = tm->length;
423 f = &v[14]; /* The decimal point. */
424 f_len = 1;
425 while (14 + f_len < l && f[f_len] >= '0' &&
426 f[f_len] <= '9')
427 ++f_len;
428 }
429 }
430
431 if (BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
432 mon[M - 1], d, h, m, s, f_len, f, y, (gmt) ? " GMT" : "") <= 0)
433 return (0);
434 else
435 return (1);
436
437 err:
438 BIO_write(bp, "Bad time value", 14);
439 return (0);
440}
441LCRYPTO_ALIAS(ASN1_GENERALIZEDTIME_print);
442
443int
444ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm)
445{
446 const char *v;
447 int gmt = 0;
448 int i;
449 int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
450
451 i = tm->length;
452 v = (const char *)tm->data;
453
454 if (i < 10)
455 goto err;
456 if (v[i-1] == 'Z')
457 gmt = 1;
458 for (i = 0; i < 10; i++)
459 if ((v[i] > '9') || (v[i] < '0'))
460 goto err;
461 y = (v[0] - '0') * 10 + (v[1] - '0');
462 if (y < 50)
463 y += 100;
464 M = (v[2] - '0') * 10 + (v[3] - '0');
465 if ((M > 12) || (M < 1))
466 goto err;
467 d = (v[4] - '0') * 10 + (v[5] - '0');
468 h = (v[6] - '0') * 10 + (v[7] - '0');
469 m = (v[8] - '0') * 10 + (v[9] - '0');
470 if (tm->length >=12 &&
471 (v[10] >= '0') && (v[10] <= '9') &&
472 (v[11] >= '0') && (v[11] <= '9'))
473 s = (v[10] - '0') * 10 + (v[11] - '0');
474
475 if (BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s",
476 mon[M - 1], d, h, m, s, y + 1900, (gmt) ? " GMT" : "") <= 0)
477 return (0);
478 else
479 return (1);
480
481 err:
482 BIO_write(bp, "Bad time value", 14);
483 return (0);
484}
485LCRYPTO_ALIAS(ASN1_UTCTIME_print);