~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/certs/extract-cert.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /certs/extract-cert.c (Version linux-6.11.5) and /certs/extract-cert.c (Version linux-5.17.15)


  1 /* Extract X.509 certificate in DER form from       1 /* Extract X.509 certificate in DER form from PKCS#11 or PEM.
  2  *                                                  2  *
  3  * Copyright © 2014-2015 Red Hat, Inc. All Ri      3  * Copyright © 2014-2015 Red Hat, Inc. All Rights Reserved.
  4  * Copyright © 2015      Intel Corporation.        4  * Copyright © 2015      Intel Corporation.
  5  *                                                  5  *
  6  * Authors: David Howells <dhowells@redhat.com      6  * Authors: David Howells <dhowells@redhat.com>
  7  *          David Woodhouse <dwmw2@infradead.o      7  *          David Woodhouse <dwmw2@infradead.org>
  8  *                                                  8  *
  9  * This program is free software; you can redi      9  * This program is free software; you can redistribute it and/or
 10  * modify it under the terms of the GNU Lesser     10  * modify it under the terms of the GNU Lesser General Public License
 11  * as published by the Free Software Foundatio     11  * as published by the Free Software Foundation; either version 2.1
 12  * of the licence, or (at your option) any lat     12  * of the licence, or (at your option) any later version.
 13  */                                                13  */
 14 #define _GNU_SOURCE                                14 #define _GNU_SOURCE
 15 #include <stdio.h>                                 15 #include <stdio.h>
 16 #include <stdlib.h>                                16 #include <stdlib.h>
 17 #include <stdint.h>                                17 #include <stdint.h>
 18 #include <stdbool.h>                               18 #include <stdbool.h>
 19 #include <string.h>                                19 #include <string.h>
 20 #include <err.h>                                   20 #include <err.h>
 21 #include <openssl/bio.h>                           21 #include <openssl/bio.h>
 22 #include <openssl/pem.h>                           22 #include <openssl/pem.h>
 23 #include <openssl/err.h>                           23 #include <openssl/err.h>
 24 #include <openssl/engine.h>                        24 #include <openssl/engine.h>
 25                                                    25 
 26 /*                                             << 
 27  * OpenSSL 3.0 deprecates the OpenSSL's ENGINE << 
 28  *                                             << 
 29  * Remove this if/when that API is no longer u << 
 30  */                                            << 
 31 #pragma GCC diagnostic ignored "-Wdeprecated-d << 
 32                                                << 
 33 #define PKEY_ID_PKCS7 2                            26 #define PKEY_ID_PKCS7 2
 34                                                    27 
 35 static __attribute__((noreturn))                   28 static __attribute__((noreturn))
 36 void format(void)                                  29 void format(void)
 37 {                                                  30 {
 38         fprintf(stderr,                            31         fprintf(stderr,
 39                 "Usage: extract-cert <source>      32                 "Usage: extract-cert <source> <dest>\n");
 40         exit(2);                                   33         exit(2);
 41 }                                                  34 }
 42                                                    35 
 43 static void display_openssl_errors(int l)          36 static void display_openssl_errors(int l)
 44 {                                                  37 {
 45         const char *file;                          38         const char *file;
 46         char buf[120];                             39         char buf[120];
 47         int e, line;                               40         int e, line;
 48                                                    41 
 49         if (ERR_peek_error() == 0)                 42         if (ERR_peek_error() == 0)
 50                 return;                            43                 return;
 51         fprintf(stderr, "At main.c:%d:\n", l);     44         fprintf(stderr, "At main.c:%d:\n", l);
 52                                                    45 
 53         while ((e = ERR_get_error_line(&file,      46         while ((e = ERR_get_error_line(&file, &line))) {
 54                 ERR_error_string(e, buf);          47                 ERR_error_string(e, buf);
 55                 fprintf(stderr, "- SSL %s: %s:     48                 fprintf(stderr, "- SSL %s: %s:%d\n", buf, file, line);
 56         }                                          49         }
 57 }                                                  50 }
 58                                                    51 
 59 static void drain_openssl_errors(void)             52 static void drain_openssl_errors(void)
 60 {                                                  53 {
 61         const char *file;                          54         const char *file;
 62         int line;                                  55         int line;
 63                                                    56 
 64         if (ERR_peek_error() == 0)                 57         if (ERR_peek_error() == 0)
 65                 return;                            58                 return;
 66         while (ERR_get_error_line(&file, &line     59         while (ERR_get_error_line(&file, &line)) {}
 67 }                                                  60 }
 68                                                    61 
 69 #define ERR(cond, fmt, ...)                        62 #define ERR(cond, fmt, ...)                             \
 70         do {                                       63         do {                                            \
 71                 bool __cond = (cond);              64                 bool __cond = (cond);                   \
 72                 display_openssl_errors(__LINE_     65                 display_openssl_errors(__LINE__);       \
 73                 if (__cond) {                      66                 if (__cond) {                           \
 74                         err(1, fmt, ## __VA_AR     67                         err(1, fmt, ## __VA_ARGS__);    \
 75                 }                                  68                 }                                       \
 76         } while(0)                                 69         } while(0)
 77                                                    70 
 78 static const char *key_pass;                       71 static const char *key_pass;
 79 static BIO *wb;                                    72 static BIO *wb;
 80 static char *cert_dst;                             73 static char *cert_dst;
 81 static bool verbose;                           !!  74 static int kbuild_verbose;
 82                                                    75 
 83 static void write_cert(X509 *x509)                 76 static void write_cert(X509 *x509)
 84 {                                                  77 {
 85         char buf[200];                             78         char buf[200];
 86                                                    79 
 87         if (!wb) {                                 80         if (!wb) {
 88                 wb = BIO_new_file(cert_dst, "w     81                 wb = BIO_new_file(cert_dst, "wb");
 89                 ERR(!wb, "%s", cert_dst);          82                 ERR(!wb, "%s", cert_dst);
 90         }                                          83         }
 91         X509_NAME_oneline(X509_get_subject_nam     84         X509_NAME_oneline(X509_get_subject_name(x509), buf, sizeof(buf));
 92         ERR(!i2d_X509_bio(wb, x509), "%s", cer     85         ERR(!i2d_X509_bio(wb, x509), "%s", cert_dst);
 93         if (verbose)                           !!  86         if (kbuild_verbose)
 94                 fprintf(stderr, "Extracted cer     87                 fprintf(stderr, "Extracted cert: %s\n", buf);
 95 }                                                  88 }
 96                                                    89 
 97 int main(int argc, char **argv)                    90 int main(int argc, char **argv)
 98 {                                                  91 {
 99         char *cert_src;                            92         char *cert_src;
100         char *verbose_env;                     << 
101                                                    93 
102         OpenSSL_add_all_algorithms();              94         OpenSSL_add_all_algorithms();
103         ERR_load_crypto_strings();                 95         ERR_load_crypto_strings();
104         ERR_clear_error();                         96         ERR_clear_error();
105                                                    97 
106         verbose_env = getenv("KBUILD_VERBOSE") !!  98         kbuild_verbose = atoi(getenv("KBUILD_VERBOSE")?:"");
107         if (verbose_env && strchr(verbose_env, << 
108                 verbose = true;                << 
109                                                    99 
110         key_pass = getenv("KBUILD_SIGN_PIN");     100         key_pass = getenv("KBUILD_SIGN_PIN");
111                                                   101 
112         if (argc != 3)                            102         if (argc != 3)
113                 format();                         103                 format();
114                                                   104 
115         cert_src = argv[1];                       105         cert_src = argv[1];
116         cert_dst = argv[2];                       106         cert_dst = argv[2];
117                                                   107 
118         if (!cert_src[0]) {                       108         if (!cert_src[0]) {
119                 /* Invoked with no input; crea    109                 /* Invoked with no input; create empty file */
120                 FILE *f = fopen(cert_dst, "wb"    110                 FILE *f = fopen(cert_dst, "wb");
121                 ERR(!f, "%s", cert_dst);          111                 ERR(!f, "%s", cert_dst);
122                 fclose(f);                        112                 fclose(f);
123                 exit(0);                          113                 exit(0);
124         } else if (!strncmp(cert_src, "pkcs11:    114         } else if (!strncmp(cert_src, "pkcs11:", 7)) {
125                 ENGINE *e;                        115                 ENGINE *e;
126                 struct {                          116                 struct {
127                         const char *cert_id;      117                         const char *cert_id;
128                         X509 *cert;               118                         X509 *cert;
129                 } parms;                          119                 } parms;
130                                                   120 
131                 parms.cert_id = cert_src;         121                 parms.cert_id = cert_src;
132                 parms.cert = NULL;                122                 parms.cert = NULL;
133                                                   123 
134                 ENGINE_load_builtin_engines();    124                 ENGINE_load_builtin_engines();
135                 drain_openssl_errors();           125                 drain_openssl_errors();
136                 e = ENGINE_by_id("pkcs11");       126                 e = ENGINE_by_id("pkcs11");
137                 ERR(!e, "Load PKCS#11 ENGINE")    127                 ERR(!e, "Load PKCS#11 ENGINE");
138                 if (ENGINE_init(e))               128                 if (ENGINE_init(e))
139                         drain_openssl_errors()    129                         drain_openssl_errors();
140                 else                              130                 else
141                         ERR(1, "ENGINE_init");    131                         ERR(1, "ENGINE_init");
142                 if (key_pass)                     132                 if (key_pass)
143                         ERR(!ENGINE_ctrl_cmd_s    133                         ERR(!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0), "Set PKCS#11 PIN");
144                 ENGINE_ctrl_cmd(e, "LOAD_CERT_    134                 ENGINE_ctrl_cmd(e, "LOAD_CERT_CTRL", 0, &parms, NULL, 1);
145                 ERR(!parms.cert, "Get X.509 fr    135                 ERR(!parms.cert, "Get X.509 from PKCS#11");
146                 write_cert(parms.cert);           136                 write_cert(parms.cert);
147         } else {                                  137         } else {
148                 BIO *b;                           138                 BIO *b;
149                 X509 *x509;                       139                 X509 *x509;
150                                                   140 
151                 b = BIO_new_file(cert_src, "rb    141                 b = BIO_new_file(cert_src, "rb");
152                 ERR(!b, "%s", cert_src);          142                 ERR(!b, "%s", cert_src);
153                                                   143 
154                 while (1) {                       144                 while (1) {
155                         x509 = PEM_read_bio_X5    145                         x509 = PEM_read_bio_X509(b, NULL, NULL, NULL);
156                         if (wb && !x509) {        146                         if (wb && !x509) {
157                                 unsigned long     147                                 unsigned long err = ERR_peek_last_error();
158                                 if (ERR_GET_LI    148                                 if (ERR_GET_LIB(err) == ERR_LIB_PEM &&
159                                     ERR_GET_RE    149                                     ERR_GET_REASON(err) == PEM_R_NO_START_LINE) {
160                                         ERR_cl    150                                         ERR_clear_error();
161                                         break;    151                                         break;
162                                 }                 152                                 }
163                         }                         153                         }
164                         ERR(!x509, "%s", cert_    154                         ERR(!x509, "%s", cert_src);
165                         write_cert(x509);         155                         write_cert(x509);
166                 }                                 156                 }
167         }                                         157         }
168                                                   158 
169         BIO_free(wb);                             159         BIO_free(wb);
170                                                   160 
171         return 0;                                 161         return 0;
172 }                                                 162 }
173                                                   163 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php