17
17
#include " util.h"
18
18
19
19
using std::string;
20
+ using std::map;
20
21
21
22
// Take binary DER data and return an X509 object suitable for verification or use.
22
23
X509 *parse_der_cert (string cert_data) {
@@ -27,6 +28,18 @@ X509 *parse_der_cert(string cert_data) {
27
28
}
28
29
29
30
int main (int argc, char **argv) {
31
+ std::list<string> expected = split (" rootcertificates,in" , " ," );
32
+
33
+ map<string,string> params;
34
+ if (!parse_command_line (argc, argv, expected, params)) {
35
+ usage (expected);
36
+ exit (1 );
37
+ }
38
+ if (params[" rootcertificates" ].empty ()) {
39
+ // Use the certificate-authority-in-a-box root cert by default:
40
+ params[" rootcertificates" ] = string (" ca_in_a_box/certs/cacert.pem" );
41
+ }
42
+
30
43
SSL_library_init ();
31
44
ERR_load_BIO_strings ();
32
45
SSL_load_error_strings ();
@@ -43,17 +56,13 @@ int main(int argc, char **argv) {
43
56
// how to do it, it doesn't actually look anything up despite the name.
44
57
X509_LOOKUP *lookup = X509_STORE_add_lookup (cert_store, X509_LOOKUP_file ());
45
58
46
- // Use the certificate-authority-in-a-box root cert:
47
- assert (X509_LOOKUP_load_file (lookup, " ca_in_a_box/certs/cacert.pem" , X509_FILETYPE_PEM));
48
-
49
- // Load all the root authority certificates. This file was retrieved from
50
- // http://www.startssl.com/certs/ca-bundle.pem on Nov 18th 2012.
51
- // assert(X509_LOOKUP_load_file(lookup, "ca-bundle-startcom.pem", X509_FILETYPE_PEM));
59
+ assert (X509_LOOKUP_load_file (lookup, params[" rootcertificates" ].c_str (), X509_FILETYPE_PEM));
52
60
53
- // Load the paymentrequest file.
54
- std::ifstream infile (" demo.bitcoin-paymentrequest" , std::ios::in | std::ios::binary);
61
+ // Load the paymentrequest file from stdin
55
62
SignedPaymentRequest signedPaymentRequest;
56
- assert (signedPaymentRequest.ParseFromIstream (&infile));
63
+ if (!signedPaymentRequest.ParseFromIstream (&std::cin)) {
64
+ exit (1 );
65
+ }
57
66
58
67
// Dump in raw text format, obviously this is mostly useless as bulk of
59
68
// the data is binary.
@@ -105,19 +114,28 @@ int main(int argc, char **argv) {
105
114
EVP_MD_CTX ctx;
106
115
EVP_PKEY *pubkey = X509_get_pubkey (signing_cert);
107
116
EVP_MD_CTX_init (&ctx);
108
- assert (EVP_VerifyInit_ex (&ctx, EVP_sha256 (), NULL ));
109
- assert (EVP_VerifyUpdate (&ctx, data_to_verify.data (), data_to_verify.size ()));
110
- assert (EVP_VerifyFinal (&ctx, (const unsigned char *)signature.data (), signature.size (), pubkey));
117
+ if (!EVP_VerifyInit_ex (&ctx, EVP_sha256 (), NULL ) ||
118
+ !EVP_VerifyUpdate (&ctx, data_to_verify.data (), data_to_verify.size ()) ||
119
+ !EVP_VerifyFinal (&ctx, (const unsigned char *)signature.data (), signature.size (), pubkey)) {
120
+
121
+ printf (" Bad signature, invalid SignedPaymentRequest.\n " );
122
+ }
123
+ else {
124
+ // OpenSSL API for getting human printable strings from certs is baroque.
125
+ int textlen = X509_NAME_get_text_by_NID (certname, NID_commonName, NULL , 0 );
126
+ char *website = new char [textlen + 1 ];
127
+ if (X509_NAME_get_text_by_NID (certname, NID_commonName, website, textlen + 1 ) == textlen && textlen > 0 ) {
128
+ printf (" SignedPaymentRequest is valid! Signed by %s\n " , website);
129
+ }
130
+ else {
131
+ printf (" Bad certificate, missing common name\n " );
132
+ }
133
+ delete[] website;
134
+ }
111
135
112
- // OpenSSL API for getting human printable strings from certs is baroque.
113
- int textlen = X509_NAME_get_text_by_NID (certname, NID_commonName, NULL , 0 );
114
- char *website = new char [textlen + 1 ];
115
- assert (X509_NAME_get_text_by_NID (certname, NID_commonName, website, textlen + 1 ) == textlen);
116
- printf (" Paymentrequest is valid! Signed by %s\n " , website);
117
- printf (" Memo: %s\n " , paymentRequest.memo ().c_str ());
136
+ printf (" PaymentRequest data:\n %s\n " , paymentRequest.DebugString ().c_str ());
118
137
119
138
// Avoid reported memory leaks.
120
- delete website;
121
139
X509_STORE_CTX_free (store_ctx);
122
140
for (int i = 0 ; i < certs.size (); i++)
123
141
X509_free (certs[i]);
0 commit comments