Add X509_REVOKED_get0_* OpenSSL shims
[folly.git] / folly / portability / test / OpenSSLPortabilityTest.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <ctime>
18
19 #include <folly/portability/GTest.h>
20 #include <folly/ssl/OpenSSLPtrTypes.h>
21
22 using namespace folly;
23 using namespace folly::ssl;
24 using namespace testing;
25
26 TEST(OpenSSLPortabilityTest, TestRSASetter) {
27   RsaUniquePtr r(RSA_new());
28   BIGNUM* n = BN_new();
29   BIGNUM* e = BN_new();
30   BIGNUM* d = BN_new();
31   BIGNUM* n_actual;
32   BIGNUM* e_actual;
33   BIGNUM* d_actual;
34   EXPECT_TRUE(BN_set_bit(n, 1));
35   EXPECT_TRUE(BN_set_bit(e, 3));
36   EXPECT_TRUE(BN_set_bit(d, 2));
37   RSA_set0_key(r.get(), n, e, d);
38   RSA_get0_key(
39       r.get(),
40       (const BIGNUM**)&n_actual,
41       (const BIGNUM**)&e_actual,
42       (const BIGNUM**)&d_actual);
43   // BN_cmp returns 0 if the two BIGNUMs are equal
44   EXPECT_FALSE(BN_cmp(n, n_actual));
45   EXPECT_FALSE(BN_cmp(e, e_actual));
46   EXPECT_FALSE(BN_cmp(d, d_actual));
47
48   RsaUniquePtr public_key(RSA_new());
49   BIGNUM* n_public = BN_new();
50   BIGNUM* e_public = BN_new();
51   EXPECT_TRUE(BN_set_bit(n_public, 1));
52   EXPECT_TRUE(BN_set_bit(e_public, 3));
53   RSA_set0_key(public_key.get(), n_public, e_public, nullptr);
54   BIGNUM* n_public_actual;
55   BIGNUM* e_public_actual;
56   RSA_get0_key(
57       public_key.get(),
58       (const BIGNUM**)&n_public_actual,
59       (const BIGNUM**)&e_public_actual,
60       nullptr);
61   EXPECT_FALSE(BN_cmp(n_public, n_public_actual));
62   EXPECT_FALSE(BN_cmp(e_public, e_public_actual));
63 }
64
65 TEST(OpenSSLPortabilityTest, TestEcdsaSigPortability) {
66   EcdsaSigUniquePtr ecdsa(ECDSA_SIG_new());
67   BIGNUM* r = BN_new();
68   BIGNUM* s = BN_new();
69   BIGNUM* r_actual;
70   BIGNUM* s_actual;
71   EXPECT_TRUE(BN_set_bit(r, 1));
72   EXPECT_TRUE(BN_set_bit(s, 2));
73   EXPECT_TRUE(ECDSA_SIG_set0(ecdsa.get(), r, s));
74   ECDSA_SIG_get0(
75       ecdsa.get(), (const BIGNUM**)&r_actual, (const BIGNUM**)&s_actual);
76   // BN_cmp returns 0 if the two BIGNUMs are equal
77   EXPECT_FALSE(BN_cmp(r, r_actual));
78   EXPECT_FALSE(BN_cmp(s, s_actual));
79 }
80
81 TEST(OpenSSLPortabilityTest, TestX509RevokedApi) {
82   X509_REVOKED* rev = X509_REVOKED_new();
83
84   ASN1_INTEGER* serial = ASN1_INTEGER_new();
85   ASN1_INTEGER_set(serial, 1234L);
86
87   ASN1_TIME* revocation_date = ASN1_TIME_new();
88   time_t t = time(nullptr);
89   ASN1_TIME_set(revocation_date, t);
90
91   X509_REVOKED_set_serialNumber(rev, serial);
92   X509_REVOKED_set_revocationDate(rev, revocation_date);
93
94   const ASN1_INTEGER* retrieved_serial = X509_REVOKED_get0_serialNumber(rev);
95   const ASN1_TIME* retrieved_date = X509_REVOKED_get0_revocationDate(rev);
96
97   EXPECT_EQ(0, ASN1_INTEGER_cmp(serial, retrieved_serial));
98
99   int diff_days;
100   int diff_secs;
101
102   ASN1_TIME_diff(&diff_days, &diff_secs, revocation_date, retrieved_date);
103   EXPECT_EQ(0, diff_days);
104   EXPECT_EQ(0, diff_secs);
105
106   ASN1_INTEGER_free(serial);
107   ASN1_TIME_free(revocation_date);
108   X509_REVOKED_free(rev);
109 }