scclib
Stable Cloud Computing C++ Library
der_cert.cc
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <crypto/der.h>
32 #include <gtest/gtest.h>
33 #include <string>
34 #include <fstream>
35 #include <memory>
36 #include <sstream>
37 #include <cstdlib>
38 
51 using std::cout;
52 using std::endl;
53 using std::string;
54 using std::ifstream;
55 using std::shared_ptr;
56 using std::stringstream;
59 
60 struct DerCertTest : public testing::Test
61 {
62  string reldir;
63 
64  DerCertTest()
65  {
66  // detect the bazel workspace environment, and create a relative path to the data files
67  auto sd = getenv("TEST_SRCDIR");
68  auto wd = getenv("TEST_WORKSPACE");
69 
70  stringstream dir;
71 
72  if (sd)
73  {
74  dir << sd << "/" << wd << "/crypto/unittest/openssl/";
75  }
76  else
77  {
78  dir << "openssl/";
79  }
80 
81  reldir = dir.str();
82  }
83 
84  static shared_ptr<PemDocument> pem_doc()
85  {
86  shared_ptr<PemDocument> ret(new PemDocument);
87  return ret;
88  }
89 
90  static shared_ptr<DerDocument> der_doc()
91  {
92  shared_ptr<DerDocument> ret(new DerDocument);
93  return ret;
94  }
95 
96  void parse(shared_ptr<PemDocument> doc, const string& name)
97  {
98  string infile = reldir+name;
99  cout << "opening " << infile << endl;
100  ifstream f(infile);
101  ASSERT_TRUE(f.is_open());
102  ASSERT_NO_THROW(doc->parse(f));
103  cout << "***** PEM " << infile << " label=" << doc->label() << " chars=" << doc->chars_per_line() << ":" << endl;
104  cout << *doc << endl;
105  }
106 
107  void parse(shared_ptr<DerDocument> doc, const string& name)
108  {
109  string infile = reldir+name;
110  cout << "opening " << infile << endl;
111  ifstream f(infile);
112  ASSERT_TRUE(f.is_open());
113  ASSERT_NO_THROW(doc->parse(f));
114  cout << "***** DER " << infile << ":" << endl;
115  cout << *doc << endl;
116  }
117 };
118 
119 TEST_F(DerCertTest, rsapriv)
120 {
121  auto pem = pem_doc();
122  parse(pem, "rsapriv.pem");
123  auto der = der_doc();
124  parse(der, "rsapriv.crt");
125  ASSERT_TRUE(pem->equal(*der));
126 }
127 
128 TEST_F(DerCertTest, rsapub)
129 {
130  auto pem = pem_doc();
131  parse(pem, "rsapub.pem");
132  auto der = der_doc();
133  parse(der, "rsapub.crt");
134  ASSERT_TRUE(pem->equal(*der));
135 }
136 
137 TEST_F(DerCertTest, rsacert)
138 {
139  auto pem = pem_doc();
140  parse(pem, "rsacert.pem");
141  auto der = der_doc();
142  parse(der, "rsacert.crt");
143  ASSERT_TRUE(pem->equal(*der));
144 }
145 
146 TEST_F(DerCertTest, ecpriv)
147 {
148  auto pem = pem_doc();
149  parse(pem, "ecpriv.pem");
150 }
151 
152 TEST_F(DerCertTest, ecpub)
153 {
154  auto pem = pem_doc();
155  parse(pem, "ecpub.pem");
156 }
157 
158 TEST_F(DerCertTest, eccert)
159 {
160  auto pem = pem_doc();
161  parse(pem, "eccert.pem");
162 }
163 
164 TEST_F(DerCertTest, rsacert_dumptest)
165 {
166  auto pem = pem_doc();
167  parse(pem, "rsacert.pem");
168  stringstream outs;
169  pem->dump(outs);
170  cout << "**** NEW CERT:" << endl;
171  cout << outs.str() << endl;
172  // now read a new doc
173  auto newpem = pem_doc();
174  ASSERT_NO_THROW(newpem->parse(outs));
175  ASSERT_EQ(pem->chars_per_line(), newpem->chars_per_line());
176  ASSERT_EQ(pem->label(), newpem->label());
177  ASSERT_TRUE(pem->equal(*newpem));
178 }
DER document.
Definition: der.h:824
PEM formatted DER document.
Definition: der.h:951
Distinguished encoding rules (DER).
@ name
{2, 5, 4, 41} },