2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 #include <credentials/certificates/certificate.h>
19 #include <credentials/certificates/x509.h>
22 * Verify a certificate signature
26 certificate_t
*cert
, *ca
;
27 char *file
= NULL
, *cafile
= NULL
;
33 switch (command_getopt(&arg
))
36 return command_usage(NULL
);
46 return command_usage("invalid --verify option");
53 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
54 BUILD_FROM_FILE
, file
, BUILD_END
);
58 cert
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
59 BUILD_FROM_FD
, 0, BUILD_END
);
63 fprintf(stderr
, "parsing certificate failed\n");
68 ca
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
, CERT_X509
,
69 BUILD_FROM_FILE
, cafile
, BUILD_END
);
72 fprintf(stderr
, "parsing CA certificate failed\n");
80 if (cert
->issued_by(cert
, ca
, NULL
))
82 if (cert
->get_validity(cert
, NULL
, NULL
, NULL
))
86 if (ca
->get_validity(ca
, NULL
, NULL
, NULL
))
88 printf("signature good, certificates valid\n");
93 printf("signature good, CA certificates not valid now\n");
98 printf("signature good, certificate valid\n");
104 printf("certificate not valid now\n");
109 printf("signature invalid\n");
121 * Register the command.
123 static void __attribute__ ((constructor
))reg()
125 command_register((command_t
) {
126 verify
, 'v', "verify",
127 "verify a certificate using the CA certificate",
128 {"[--in file] [--cacert file]"},
130 {"help", 'h', 0, "show usage information"},
131 {"in", 'i', 1, "X.509 certificate to verify, default: stdin"},
132 {"cacert", 'c', 1, "CA certificate, default: verify self signed"},