iPXE
Data Structures | Macros | Functions | Variables
cert_cmd.c File Reference

Certificate management commands. More...

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <ipxe/x509.h>
#include <ipxe/certstore.h>
#include <ipxe/image.h>
#include <ipxe/command.h>
#include <ipxe/parseopt.h>
#include <usr/imgmgmt.h>
#include <usr/certmgmt.h>

Go to the source code of this file.

Data Structures

struct  cert_options
 "cert<xxx>" options More...
 
struct  cert_command_descriptor
 A "cert<xxx>" command descriptor. More...
 

Macros

#define CERT_COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage, _payload)
 Construct "cert<xxx>" command descriptor. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static int cert_exec (int argc, char **argv, struct cert_command_descriptor *certcmd)
 Execute "cert<xxx>" command. More...
 
static int certstat_payload (struct x509_certificate *cert)
 "certstat" payload More...
 
static int certstat_exec (int argc, char **argv)
 The "certstat" command. More...
 
static int certstore_payload (struct x509_certificate *cert)
 "certstore" payload More...
 
static int certstore_exec (int argc, char **argv)
 The "certstore" command. More...
 
static int certfree_payload (struct x509_certificate *cert)
 "certfree" payload More...
 
static int certfree_exec (int argc, char **argv)
 The "certfree" command. More...
 
 COMMAND (certstat, certstat_exec)
 Certificate management commands. More...
 
 COMMAND (certstore, certstore_exec)
 
 COMMAND (certfree, certfree_exec)
 

Variables

union {
   struct option_descriptor   certstore [2]
 
   struct option_descriptor   certstat [1]
 
   struct option_descriptor   certfree [1]
 
opts
 "cert<xxx>" option list More...
 
static struct cert_command_descriptor certstat_cmd
 "certstat" command descriptor More...
 
static struct cert_command_descriptor certstore_cmd
 "certstore" command descriptor More...
 
static struct cert_command_descriptor certfree_cmd
 "certfree" command descriptor More...
 

Detailed Description

Certificate management commands.

Definition in file cert_cmd.c.

Macro Definition Documentation

◆ CERT_COMMAND_DESC

#define CERT_COMMAND_DESC (   _struct,
  _options,
  _min_args,
  _max_args,
  _usage,
  _payload 
)
Value:
{ \
.cmd = COMMAND_DESC ( _struct, _options, _min_args, \
_max_args, _usage ), \
.payload = _payload, \
}
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108

Construct "cert<xxx>" command descriptor.

Parameters
_structOptions structure type
_optionsOption descriptor array
_min_argsMinimum number of non-option arguments
_max_argsMaximum number of non-option arguments
_usageCommand usage
_payloadPayload method
Return values
_commandCommand descriptor

Definition at line 92 of file cert_cmd.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ cert_exec()

static int cert_exec ( int  argc,
char **  argv,
struct cert_command_descriptor certcmd 
)
static

Execute "cert<xxx>" command.

Parameters
argcArgument count
argvArgument list
certcmdCommand descriptor
Return values
rcReturn status code

Definition at line 108 of file cert_cmd.c.

109  {
110  struct command_descriptor *cmd = &certcmd->cmd;
111  struct cert_options opts;
112  struct image *image = NULL;
113  struct x509_certificate *cert;
114  struct x509_certificate *tmp;
115  unsigned int count = 0;
116  size_t offset = 0;
117  int next;
118  int rc;
119 
120  /* Parse options */
121  if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
122  goto err_parse;
123 
124  /* Acquire image, if applicable */
125  if ( ( optind < argc ) &&
126  ( ( rc = imgacquire ( argv[optind], 0, &image ) ) != 0 ) )
127  goto err_acquire;
128 
129  /* Get first entry in certificate store */
130  tmp = list_first_entry ( &certstore.links, struct x509_certificate,
131  store.list );
132 
133  /* Iterate over certificates */
134  while ( 1 ) {
135 
136  /* Get next certificate from image or store as applicable */
137  if ( image ) {
138 
139  /* Get next certificate from image */
140  if ( offset >= image->len )
141  break;
142  next = image_x509 ( image, offset, &cert );
143  if ( next < 0 ) {
144  rc = next;
145  printf ( "Could not parse certificate: %s\n",
146  strerror ( rc ) );
147  goto err_x509;
148  }
149  offset = next;
150 
151  } else {
152 
153  /* Get next certificate from store */
154  cert = tmp;
155  if ( ! cert )
156  break;
157  tmp = list_next_entry ( tmp, &certstore.links,
158  store.list );
159  x509_get ( cert );
160  }
161 
162  /* Skip non-matching names, if a name was specified */
163  if ( opts.name && ( x509_check_name ( cert, opts.name ) != 0 )){
164  x509_put ( cert );
165  continue;
166  }
167 
168  /* Execute payload */
169  if ( ( rc = certcmd->payload ( cert ) ) != 0 ) {
170  x509_put ( cert );
171  goto err_payload;
172  }
173 
174  /* Count number of certificates processed */
175  count++;
176 
177  /* Drop reference to certificate */
178  x509_put ( cert );
179  }
180 
181  /* Fail if a name was specified and no matching certificates
182  * were found.
183  */
184  if ( opts.name && ( count == 0 ) ) {
185  printf ( "\"%s\" : no such certificate\n", opts.name );
186  rc = -ENOENT;
187  goto err_none;
188  }
189 
190  err_none:
191  err_payload:
192  err_x509:
193  if ( image && ( ! opts.keep ) )
195  err_acquire:
196  err_parse:
197  return rc;
198 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
static struct x509_certificate * x509_get(struct x509_certificate *cert)
Get reference to X.509 certificate.
Definition: x509.h:266
int optind
Current option index.
Definition: getopt.c:51
int(* payload)(struct x509_certificate *cert)
Payload.
Definition: cert_cmd.c:78
int x509_check_name(struct x509_certificate *cert, const char *name)
Check X.509 certificate name.
Definition: x509.c:1563
#define ENOENT
No such file or directory.
Definition: errno.h:514
#define list_next_entry(pos, head, member)
Get the container of the next entry in a list.
Definition: list.h:359
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:484
An executable image.
Definition: image.h:23
A command descriptor.
Definition: parseopt.h:77
unsigned long tmp
Definition: linux_pci.h:64
#define list_first_entry(list, type, member)
Get the container of the first entry in a list.
Definition: list.h:333
static unsigned int count
Number of entries.
Definition: dwmac.h:225
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
An X.509 certificate.
Definition: x509.h:215
size_t len
Length of raw file image.
Definition: image.h:55
"cert<xxx>" options
Definition: cert_cmd.c:45
uint32_t next
Next descriptor address.
Definition: dwmac.h:22
struct command_descriptor cmd
Command descriptor.
Definition: cert_cmd.c:72
void unregister_image(struct image *image)
Unregister executable image.
Definition: image.c:357
struct option_descriptor certstore[2]
Certificate store.
Definition: cert_cmd.c:55
static void x509_put(struct x509_certificate *cert)
Drop reference to X.509 certificate.
Definition: x509.h:277
struct x509_link store
Link in certificate store.
Definition: x509.h:220
static union @447 opts
"cert<xxx>" option list
int image_x509(struct image *image, size_t offset, struct x509_certificate **cert)
Extract X.509 certificate object from image.
Definition: x509.c:1960
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29
int imgacquire(const char *name_uri, unsigned long timeout, struct image **image)
Acquire an image.
Definition: imgmgmt.c:142

References certstore, cmd, cert_command_descriptor::cmd, count, ENOENT, image_x509(), imgacquire(), image::len, x509_link::list, list_first_entry, list_next_entry, next, NULL, offset, optind, opts, parse_options(), cert_command_descriptor::payload, printf(), rc, x509_certificate::store, strerror(), tmp, unregister_image(), x509_check_name(), x509_get(), and x509_put().

Referenced by certfree_exec(), certstat_exec(), and certstore_exec().

◆ certstat_payload()

static int certstat_payload ( struct x509_certificate cert)
static

"certstat" payload

Parameters
certX.509 certificate
Return values
rcReturn status code

Definition at line 206 of file cert_cmd.c.

206  {
207 
208  certstat ( cert );
209  return 0;
210 }
struct option_descriptor certstat[1]
Definition: cert_cmd.c:57

References certstat.

◆ certstat_exec()

static int certstat_exec ( int  argc,
char **  argv 
)
static

The "certstat" command.

Parameters
argcArgument count
argvArgument list
Return values
rcReturn status code

Definition at line 224 of file cert_cmd.c.

224  {
225 
226  return cert_exec ( argc, argv, &certstat_cmd );
227 }
static int cert_exec(int argc, char **argv, struct cert_command_descriptor *certcmd)
Execute "cert<xxx>" command.
Definition: cert_cmd.c:108
static struct cert_command_descriptor certstat_cmd
"certstat" command descriptor
Definition: cert_cmd.c:213

References cert_exec(), and certstat_cmd.

◆ certstore_payload()

static int certstore_payload ( struct x509_certificate cert)
static

"certstore" payload

Parameters
certX.509 certificate
Return values
rcReturn status code

Definition at line 235 of file cert_cmd.c.

235  {
236 
237  /* Mark certificate as having been added explicitly */
238  cert->flags |= X509_FL_EXPLICIT;
239 
240  return 0;
241 }
unsigned int flags
Flags.
Definition: x509.h:223
Certificate was added explicitly at run time.
Definition: x509.h:256

References x509_certificate::flags, and X509_FL_EXPLICIT.

◆ certstore_exec()

static int certstore_exec ( int  argc,
char **  argv 
)
static

The "certstore" command.

Parameters
argcArgument count
argvArgument list
Return values
rcReturn status code

Definition at line 255 of file cert_cmd.c.

255  {
256 
257  return cert_exec ( argc, argv, &certstore_cmd );
258 }
static int cert_exec(int argc, char **argv, struct cert_command_descriptor *certcmd)
Execute "cert<xxx>" command.
Definition: cert_cmd.c:108
static struct cert_command_descriptor certstore_cmd
"certstore" command descriptor
Definition: cert_cmd.c:244

References cert_exec(), and certstore_cmd.

◆ certfree_payload()

static int certfree_payload ( struct x509_certificate cert)
static

"certfree" payload

Parameters
certX.509 certificate
Return values
rcReturn status code

Definition at line 266 of file cert_cmd.c.

266  {
267 
268  /* Remove from certificate store */
269  certstore_del ( cert );
270 
271  return 0;
272 }
void certstore_del(struct x509_certificate *cert)
Remove certificate from store.
Definition: certstore.c:115

References certstore_del().

◆ certfree_exec()

static int certfree_exec ( int  argc,
char **  argv 
)
static

The "certfree" command.

Parameters
argcArgument count
argvArgument list
Return values
rcReturn status code

Definition at line 286 of file cert_cmd.c.

286  {
287 
288  return cert_exec ( argc, argv, &certfree_cmd );
289 }
static int cert_exec(int argc, char **argv, struct cert_command_descriptor *certcmd)
Execute "cert<xxx>" command.
Definition: cert_cmd.c:108
static struct cert_command_descriptor certfree_cmd
"certfree" command descriptor
Definition: cert_cmd.c:275

References cert_exec(), and certfree_cmd.

◆ COMMAND() [1/3]

COMMAND ( certstat  ,
certstat_exec   
)

Certificate management commands.

◆ COMMAND() [2/3]

COMMAND ( certstore  ,
certstore_exec   
)

◆ COMMAND() [3/3]

COMMAND ( certfree  ,
certfree_exec   
)

Variable Documentation

◆ certstore

struct option_descriptor certstore[2]

Certificate store.

Definition at line 55 of file cert_cmd.c.

Referenced by cert_exec().

◆ certstat

struct option_descriptor certstat[1]

Definition at line 57 of file cert_cmd.c.

Referenced by certstat_payload().

◆ certfree

struct option_descriptor certfree[1]

Definition at line 59 of file cert_cmd.c.

◆ opts

union { ... } opts
Initial value:
= {
.certstore = {
OPTION_DESC ( "subject", 's', required_argument,
OPTION_DESC ( "keep", 'k', no_argument,
struct cert_options, keep, parse_flag ),
},
}
const char * name
Definition: ath9k_hw.c:1984
int parse_string(char *text, char **value)
Parse string value.
Definition: parseopt.c:73
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:226
"cert<xxx>" options
Definition: cert_cmd.c:45
Option does not take an argument.
Definition: getopt.h:16
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:67
Option requires an argument.
Definition: getopt.h:18

"cert<xxx>" option list

Referenced by attr_get(), attr_off(), attr_on(), attr_set(), cert_exec(), choose_exec(), colour_exec(), colour_set(), config_exec(), console_exec(), cpair_exec(), cpuid_exec(), digest_exec(), dynui_exec(), echo_exec(), exit_exec(), fcels_exec(), fcstat_exec(), fdt_exec(), gdbstub_exec(), goto_exec(), gve_describe(), ibstat_exec(), ifcommon_exec(), ifconf_payload(), iflinkwait_payload(), imgdecrypt_exec(), imgextract_exec(), imgmem_exec(), imgtrust_exec(), imgverify_exec(), inc_exec(), ipstat_exec(), iseq_exec(), isset_exec(), item_exec(), login_exec(), lotest_exec(), nslookup_exec(), nstat_exec(), ntp_exec(), param_exec(), params_exec(), parse_options(), pciscan_exec(), ping_exec(), poweroff_exec(), present_exec(), profstat_exec(), prompt_exec(), pxebs_exec(), read_value(), reboot_exec(), reparse_options(), route_exec(), set_core_exec(), shell_exec(), shim_exec(), show_exec(), sleep_exec(), stoppxe_exec(), sync_exec(), time_exec(), usbscan_exec(), vcreate_exec(), and vdestroy_exec().

◆ certstat_cmd

struct cert_command_descriptor certstat_cmd
static
Initial value:
=
CERT_COMMAND_DESC ( struct cert_options, opts.certstat, 0, 0, NULL,
#define CERT_COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage, _payload)
Construct "cert<xxx>" command descriptor.
Definition: cert_cmd.c:92
"cert<xxx>" options
Definition: cert_cmd.c:45
static int certstat_payload(struct x509_certificate *cert)
"certstat" payload
Definition: cert_cmd.c:206
static union @447 opts
"cert<xxx>" option list
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

"certstat" command descriptor

Definition at line 213 of file cert_cmd.c.

Referenced by certstat_exec().

◆ certstore_cmd

struct cert_command_descriptor certstore_cmd
static
Initial value:
=
CERT_COMMAND_DESC ( struct cert_options, opts.certstore, 0, 1,
"[<uri|image>]", certstore_payload )
static int certstore_payload(struct x509_certificate *cert)
"certstore" payload
Definition: cert_cmd.c:235
#define CERT_COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage, _payload)
Construct "cert<xxx>" command descriptor.
Definition: cert_cmd.c:92
"cert<xxx>" options
Definition: cert_cmd.c:45
static union @447 opts
"cert<xxx>" option list

"certstore" command descriptor

Definition at line 244 of file cert_cmd.c.

Referenced by certstore_exec().

◆ certfree_cmd

struct cert_command_descriptor certfree_cmd
static
Initial value:
=
CERT_COMMAND_DESC ( struct cert_options, opts.certfree, 0, 0, NULL,
#define CERT_COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage, _payload)
Construct "cert<xxx>" command descriptor.
Definition: cert_cmd.c:92
"cert<xxx>" options
Definition: cert_cmd.c:45
static union @447 opts
"cert<xxx>" option list
static int certfree_payload(struct x509_certificate *cert)
"certfree" payload
Definition: cert_cmd.c:266
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

"certfree" command descriptor

Definition at line 275 of file cert_cmd.c.

Referenced by certfree_exec().