Skip to content

Commit 6d99ee0

Browse files
authored
Add libks2 support (#133)
1 parent 5e06ce9 commit 6d99ee0

10 files changed

+182
-41
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ stir_shaken_test_11
4747
stir_shaken_test_12
4848
stir_shaken_test_13
4949
stir_shaken_test_14
50+
stir_shaken_test_15
51+
stir_shaken_test_16
52+
stir_shaken_test_17
53+
stir_shaken_test_18
5054
stir_shaken_test_2
5155
stir_shaken_test_3
5256
stir_shaken_test_4
@@ -71,3 +75,5 @@ util/src/.deps/
7175
util/src/.dirstamp
7276
build/stirshaken.pc
7377
sp_1*.pem
78+
debian/autoreconf.after
79+
debian/autoreconf.before

Makefile.am

+7-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ examples = stir_shaken_as_basic stir_shaken_vs_basic stir_shaken_ca stir_shaken_
1616

1717
bin_PROGRAMS = stirshaken $(examples)
1818
stirshaken_SOURCES = util/src/stir_shaken_tool_command.c util/src/stir_shaken_tool.c util/src/stir_shaken_ca.c util/src/mongoose.c
19-
stirshaken_CFLAGS = -Iinclude -Iutil/include -DMG_ENABLE_SSL
19+
stirshaken_CFLAGS = -Iinclude -Iutil/include $(KS_CFLAGS) -DMG_ENABLE_SSL
2020
stirshaken_LDADD = libstirshaken.la $(KS_LIBS)
2121

2222
LIBS += $(KS_LIBS) $(CURL_LIBS) $(JWT_LIBS) -pthread
@@ -94,25 +94,25 @@ stir_shaken_test_18_CFLAGS = -Iinclude
9494
stir_shaken_test_18_LDADD = libstirshaken.la
9595

9696
stir_shaken_as_basic_SOURCES = examples/stir_shaken_as_basic.c
97-
stir_shaken_as_basic_CFLAGS = -Iinclude
97+
stir_shaken_as_basic_CFLAGS = -Iinclude $(KS_CFLAGS)
9898
stir_shaken_as_basic_LDADD = libstirshaken.la
9999

100100
stir_shaken_vs_basic_SOURCES = examples/stir_shaken_vs_basic.c
101-
stir_shaken_vs_basic_CFLAGS = -Iinclude
101+
stir_shaken_vs_basic_CFLAGS = -Iinclude $(KS_CFLAGS)
102102
stir_shaken_vs_basic_LDADD = libstirshaken.la
103103

104104
stir_shaken_ca_SOURCES = examples/stir_shaken_ca.c util/src/stir_shaken_ca.c util/src/mongoose.c
105-
stir_shaken_ca_CFLAGS = -Iinclude -Iutil/include -DMG_ENABLE_SSL
105+
stir_shaken_ca_CFLAGS = -Iinclude -Iutil/include $(KS_CFLAGS) -DMG_ENABLE_SSL
106106
stir_shaken_ca_LDADD = libstirshaken.la
107107

108108
stir_shaken_cert_req_SOURCES = examples/stir_shaken_cert_req.c
109-
stir_shaken_cert_req_CFLAGS = -Iinclude
109+
stir_shaken_cert_req_CFLAGS = -Iinclude $(KS_CFLAGS)
110110
stir_shaken_cert_req_LDADD = libstirshaken.la
111111

112112
stir_shaken_as_easy_SOURCES = examples/stir_shaken_as_easy.c
113-
stir_shaken_as_easy_CFLAGS = -Iinclude
113+
stir_shaken_as_easy_CFLAGS = -Iinclude $(KS_CFLAGS)
114114
stir_shaken_as_easy_LDADD = libstirshaken.la
115115

116116
stir_shaken_vs_easy_SOURCES = examples/stir_shaken_vs_easy.c
117-
stir_shaken_vs_easy_CFLAGS = -Iinclude
117+
stir_shaken_vs_easy_CFLAGS = -Iinclude $(KS_CFLAGS)
118118
stir_shaken_vs_easy_LDADD = libstirshaken.la

configure.ac

+8-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ fi
3232

3333
PKG_CHECK_MODULES([CURL], [libcurl >= 7.19])
3434
PKG_CHECK_MODULES([JWT], [libjwt >= 1.12])
35-
PKG_CHECK_MODULES([KS], [libks >= 1.1.0])
35+
36+
PKG_CHECK_MODULES([KS], [libks2 >= 2.0.0],[
37+
AM_CONDITIONAL([HAVE_KS],[true])],[
38+
PKG_CHECK_MODULES([KS], [libks >= 1.8.2],[
39+
AM_CONDITIONAL([HAVE_KS],[true])],[
40+
AC_MSG_ERROR([You need to either install libks2 or libks])
41+
])
42+
])
3643

3744
# Enable clang address sanitizer bit build
3845
AC_ARG_ENABLE(address_sanitizer,

debian/control

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Source: libstirshaken
22
Section: libs
33
Priority: optional
44
Maintainer: FreeSWITCH Solutions <[email protected]>
5-
Build-Depends: debhelper (>= 9), libcurl4-openssl-dev, libjwt-dev (>= 1.12.0), libks (>= 1.6.0), libssl-dev, pkgconf, uuid-dev
5+
Build-Depends: debhelper (>= 9), libcurl4-openssl-dev, libjwt-dev (>= 1.12.0), libks2 (>= 2.0.0), libssl-dev, pkgconf, uuid-dev
66
Standards-Version: 3.9.6
77

88
Package: libstirshaken1
@@ -18,7 +18,7 @@ Description: STIR-Shaken authentication and verification library
1818
Package: libstirshaken-dev
1919
Section: libdevel
2020
Architecture: any
21-
Depends: libstirshaken1 (= ${binary:Version}), libcurl4-openssl-dev, libjwt-dev (>= 1.12.0), libks (>= 1.6.0)
21+
Depends: libstirshaken1 (= ${binary:Version}), libcurl4-openssl-dev, libjwt-dev (>= 1.12.0), libks2 (>= 2.0.0)
2222
Description: STIR-Shaken authentication and verification library
2323
This library provides building blocks for implementing STIR-Shaken
2424
authentication and verification services, (STI-SP/AS, STI-SP/VS),

src/stir_shaken_acme.c

+37-13
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ stir_shaken_status_t stir_shaken_acme_respond_to_challenge(stir_shaken_context_t
579579
{
580580
ks_json_t *json = NULL, *auth_status = NULL, *challenges_arr = NULL;
581581
stir_shaken_http_req_t http_req = { 0 };
582+
const char *auth_status_value = "";
582583

583584

584585
if (!data) {
@@ -617,8 +618,12 @@ stir_shaken_status_t stir_shaken_acme_respond_to_challenge(stir_shaken_context_t
617618
goto fail;
618619
}
619620

620-
if (strcmp("valid", ks_json_value_string(auth_status)) == 0) {
621-
621+
#if KS_VERSION_NUM >= 20000
622+
ks_json_value_string(auth_status, &auth_status_value);
623+
#else
624+
auth_status_value = ks_json_value_string(auth_status);
625+
#endif
626+
if (strcmp("valid", auth_status_value) == 0) {
622627
// Authorization completed
623628

624629
} else {
@@ -631,8 +636,8 @@ stir_shaken_status_t stir_shaken_acme_respond_to_challenge(stir_shaken_context_t
631636
char *kid = NULL, *nonce = NULL;
632637
char *jwt_encoded = NULL;
633638

634-
if (strcmp("pending", ks_json_value_string(auth_status)) != 0) {
635-
snprintf(err_buf, STIR_SHAKEN_BUFLEN, "ACME authorization challenge malformed, 'status' field is neither 'valid' nor 'pending' (status is: '%s')", ks_json_value_string(auth_status));
639+
if (strcmp("pending", auth_status_value) != 0) {
640+
snprintf(err_buf, STIR_SHAKEN_BUFLEN, "ACME authorization challenge malformed, 'status' field is neither 'valid' nor 'pending' (status is: '%s')", auth_status_value);
636641
stir_shaken_set_error(ss, err_buf, STIR_SHAKEN_ERROR_ACME);
637642
goto fail;
638643
}
@@ -673,7 +678,11 @@ stir_shaken_status_t stir_shaken_acme_respond_to_challenge(stir_shaken_context_t
673678
goto fail;
674679
}
675680

681+
#if KS_VERSION_NUM >= 20000
682+
ks_json_value_string(url_item, &challenge_url);
683+
#else
676684
challenge_url = ks_json_value_string(url_item);
685+
#endif
677686
if (polling_url) {
678687
*polling_url = strdup(challenge_url);
679688
}
@@ -737,6 +746,7 @@ stir_shaken_status_t stir_shaken_acme_poll(stir_shaken_context_t *ss, void *data
737746
ks_json_t *json = NULL, *auth_status = NULL;
738747
int t = 0;
739748
char err_buf[STIR_SHAKEN_ERROR_BUF_LEN] = { 0 };
749+
const char *auth_status_value = "";
740750

741751
if (!url) {
742752
goto fail;
@@ -795,26 +805,31 @@ stir_shaken_status_t stir_shaken_acme_poll(stir_shaken_context_t *ss, void *data
795805

796806
// Check authorization status
797807
// If status is "valid" authorization is completed and can proceed to cert acquisition
798-
if (strcmp("valid", ks_json_value_string(auth_status)) == 0) {
808+
#if KS_VERSION_NUM >= 20000
809+
ks_json_value_string(auth_status, &auth_status_value);
799810

811+
#else
812+
auth_status_value = ks_json_value_string(auth_status);
813+
#endif
814+
if (strcmp("valid", auth_status_value) == 0) {
800815
// Authorization completed
801816
status_is_valid = 1;
802817
fprintif(STIR_SHAKEN_LOGLEVEL_MEDIUM, "\t-> Got 'valid' polling status\n");
803818

804819
} else {
805820

806-
if (strcmp("pending", ks_json_value_string(auth_status)) != 0) {
821+
if (strcmp("pending", auth_status_value) != 0) {
807822

808-
if (0 == strcmp("failed", ks_json_value_string(auth_status))) {
823+
if (0 == strcmp("failed", auth_status_value)) {
809824
fprintif(STIR_SHAKEN_LOGLEVEL_MEDIUM, "\t-> Got 'failed' polling status\n");
810-
snprintf(err_buf, STIR_SHAKEN_BUFLEN, "\t-> Got 'failed' polling status (%s): ACME authorization unsuccessful\n", ks_json_value_string(auth_status));
825+
snprintf(err_buf, STIR_SHAKEN_BUFLEN, "\t-> Got 'failed' polling status (%s): ACME authorization unsuccessful\n", auth_status_value);
811826
stir_shaken_set_error(ss, err_buf, STIR_SHAKEN_ERROR_ACME_AUTHZ_UNSUCCESSFUL);
812827
goto fail;
813828
}
814829

815830
fprintif(STIR_SHAKEN_LOGLEVEL_MEDIUM, "\t-> Got malformed polling status\n");
816831

817-
snprintf(err_buf, STIR_SHAKEN_BUFLEN, "ACME auth status malformed, 'status' field is neither 'valid' nor 'pending' nor 'failed' (status is: '%s')\n", ks_json_value_string(auth_status));
832+
snprintf(err_buf, STIR_SHAKEN_BUFLEN, "ACME auth status malformed, 'status' field is neither 'valid' nor 'pending' nor 'failed' (status is: '%s')\n", auth_status_value);
818833
stir_shaken_set_error(ss, err_buf, STIR_SHAKEN_ERROR_ACME);
819834
goto fail;
820835
}
@@ -866,6 +881,7 @@ stir_shaken_status_t stir_shaken_acme_perform_authorization(stir_shaken_context_
866881
{
867882
ks_json_t *json = NULL, *auth_status = NULL, *auth_arr = NULL;
868883
char err_buf[STIR_SHAKEN_ERROR_BUF_LEN] = { 0 };
884+
const char *auth_status_value = "";
869885

870886

871887
if (!data) {
@@ -907,8 +923,12 @@ stir_shaken_status_t stir_shaken_acme_perform_authorization(stir_shaken_context_
907923
fprintif(STIR_SHAKEN_LOGLEVEL_MEDIUM, "-> Processing authorization challenge...\n");
908924

909925
// If status is "valid" authorization is completed and can proceed to cert acquisition
910-
if (strcmp("valid", ks_json_value_string(auth_status)) == 0) {
911-
926+
#if KS_VERSION_NUM >= 20000
927+
ks_json_value_string(auth_status, &auth_status_value);
928+
#else
929+
auth_status_value = ks_json_value_string(auth_status);
930+
#endif
931+
if (strcmp("valid", auth_status_value) == 0) {
912932
// Authorization completed
913933
fprintif(STIR_SHAKEN_LOGLEVEL_MEDIUM, "-> Authorization completed\n");
914934

@@ -918,8 +938,8 @@ stir_shaken_status_t stir_shaken_acme_perform_authorization(stir_shaken_context_
918938
const char *auth_url = NULL;
919939
stir_shaken_http_req_t http_req = { 0 };
920940

921-
if (strcmp("pending", ks_json_value_string(auth_status)) != 0) {
922-
snprintf(err_buf, STIR_SHAKEN_BUFLEN, "ACME authorization challenge malformed, 'status' field is neither 'valid' nor 'pending' (status is: '%s')", ks_json_value_string(auth_status));
941+
if (strcmp("pending", auth_status_value) != 0) {
942+
snprintf(err_buf, STIR_SHAKEN_BUFLEN, "ACME authorization challenge malformed, 'status' field is neither 'valid' nor 'pending' (status is: '%s')", auth_status_value);
923943
stir_shaken_set_error(ss, err_buf, STIR_SHAKEN_ERROR_ACME);
924944
goto fail;
925945
}
@@ -951,7 +971,11 @@ stir_shaken_status_t stir_shaken_acme_perform_authorization(stir_shaken_context_
951971
goto fail;
952972
}
953973

974+
#if KS_VERSION_NUM >= 20000
975+
ks_json_value_string(auth_item, &auth_url);
976+
#else
954977
auth_url = ks_json_value_string(auth_item);
978+
#endif
955979

956980
/*
957981
* Performing Step 4 of 6.3.5.2 ACME Based Steps for Application for an STI Certificate [ATIS-1000080].

src/stir_shaken_passport.c

+53-4
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,15 @@ stir_shaken_status_t stir_shaken_passport_jwt_init(stir_shaken_context_t *ss, jw
8484
stir_shaken_set_error(ss, "Passport can't create JSON object", STIR_SHAKEN_ERROR_KSJSON_CREATE_OBJECT_JSON_2);
8585
return STIR_SHAKEN_STATUS_ERR;
8686
}
87-
87+
#if KS_VERSION_NUM >= 20000
88+
ks_json_add_number_to_object(json, "iat", iat);
89+
#else
8890
if (!ks_json_add_number_to_object(json, "iat", iat)) {
8991
stir_shaken_set_error(ss, "Failed to add @iat to PASSporT", STIR_SHAKEN_ERROR_KSJSON_ADD_IAT);
9092
ks_json_delete(&json);
9193
return STIR_SHAKEN_STATUS_ERR;
9294
}
95+
#endif
9396

9497
if (!attest) {
9598
stir_shaken_set_error(ss, "Passport @attest is missing", STIR_SHAKEN_ERROR_PASSPORT_ATTEST_MISSING);
@@ -103,23 +106,31 @@ stir_shaken_status_t stir_shaken_passport_jwt_init(stir_shaken_context_t *ss, jw
103106
return STIR_SHAKEN_STATUS_ERR;
104107
}
105108

109+
#if KS_VERSION_NUM >= 20000
110+
ks_json_add_string_to_object(json, "attest", attest);
111+
#else
106112
if (!ks_json_add_string_to_object(json, "attest", attest)) {
107113
stir_shaken_set_error(ss, "Failed to add @attest to PASSporT", STIR_SHAKEN_ERROR_KSJSON_ADD_ATTEST);
108114
ks_json_delete(&json);
109115
return STIR_SHAKEN_STATUS_ERR;
110116
}
117+
#endif
111118

112119
if (!origid) {
113120
stir_shaken_set_error(ss, "Passport @origid is missing", STIR_SHAKEN_ERROR_PASSPORT_ORIGID_MISSING);
114121
ks_json_delete(&json);
115122
return STIR_SHAKEN_STATUS_ERR;
116123
}
117124

125+
#if KS_VERSION_NUM >= 20000
126+
ks_json_add_string_to_object(json, "origid", origid);
127+
#else
118128
if (!ks_json_add_string_to_object(json, "origid", origid)) {
119129
stir_shaken_set_error(ss, "Failed to add @origid to PASSporT", STIR_SHAKEN_ERROR_KSJSON_ADD_ORIGID);
120130
ks_json_delete(&json);
121131
return STIR_SHAKEN_STATUS_ERR;
122132
}
133+
#endif
123134

124135
if (!origtn_val) {
125136

@@ -150,6 +161,10 @@ stir_shaken_status_t stir_shaken_passport_jwt_init(stir_shaken_context_t *ss, jw
150161
return STIR_SHAKEN_STATUS_ERR;
151162
}
152163

164+
#if KS_VERSION_NUM >= 20000
165+
ks_json_add_string_to_array(tn, origtn_val);
166+
ks_json_add_item_to_object(orig, origtn_key, tn);
167+
#else
153168
if (!ks_json_add_string_to_array(tn, origtn_val)) {
154169
stir_shaken_set_error(ss, "Passport create json: Failed to add @orig to array", STIR_SHAKEN_ERROR_KSJSON_ADD_ORIG_TO_ARRAY);
155170
ks_json_delete(&tn);
@@ -165,23 +180,30 @@ stir_shaken_status_t stir_shaken_passport_jwt_init(stir_shaken_context_t *ss, jw
165180
ks_json_delete(&json);
166181
return STIR_SHAKEN_STATUS_ERR;
167182
}
168-
183+
#endif
169184
} else {
170-
185+
#if KS_VERSION_NUM >= 20000
186+
ks_json_add_string_to_object(orig, "tn", origtn_val);
187+
#else
171188
if (!ks_json_add_string_to_object(orig, "tn", origtn_val)) {
172189
stir_shaken_set_error(ss, "Passport create json: Failed to add @origtn", STIR_SHAKEN_ERROR_KSJSON_ADD_TN);
173190
ks_json_delete(&orig);
174191
ks_json_delete(&json);
175192
return STIR_SHAKEN_STATUS_ERR;
176193
}
194+
#endif
177195
}
178196

197+
#if KS_VERSION_NUM >= 20000
198+
ks_json_add_item_to_object(json, "orig", orig);
199+
#else
179200
if (!ks_json_add_item_to_object(json, "orig", orig)) {
180201
stir_shaken_set_error(ss, "Passport create json: Failed to add @orig", STIR_SHAKEN_ERROR_KSJSON_ADD_ORIG);
181202
ks_json_delete(&orig);
182203
ks_json_delete(&json);
183204
return STIR_SHAKEN_STATUS_ERR;
184205
}
206+
#endif
185207
}
186208

187209
if (!desttn_val) {
@@ -211,30 +233,46 @@ stir_shaken_status_t stir_shaken_passport_jwt_init(stir_shaken_context_t *ss, jw
211233
return STIR_SHAKEN_STATUS_ERR;
212234
}
213235

236+
#if KS_VERSION_NUM >= 20000
237+
ks_json_add_string_to_array(tn, desttn_val);
238+
#else
214239
if (!ks_json_add_string_to_array(tn, desttn_val)) {
215240
stir_shaken_set_error(ss, "Passport create json: Failed to add @desttn to array", STIR_SHAKEN_ERROR_KSJSON_ADD_DEST_TO_ARRAY);
216241
ks_json_delete(&tn);
217242
ks_json_delete(&dest);
218243
ks_json_delete(&json);
219244
return STIR_SHAKEN_STATUS_ERR;
220245
}
246+
#endif
221247

222248
// If @desttn_key is NULL or empty, use "tn" form
223249

250+
#if KS_VERSION_NUM >= 20000
251+
if (stir_shaken_zstr(desttn_key)) {
252+
ks_json_add_item_to_object(dest, "tn", tn);
253+
} else {
254+
ks_json_add_item_to_object(dest, desttn_key, tn);
255+
}
256+
#else
224257
if ((stir_shaken_zstr(desttn_key) && !ks_json_add_item_to_object(dest, "tn", tn)) || (!stir_shaken_zstr(desttn_key) && !ks_json_add_item_to_object(dest, desttn_key, tn))) {
225258
stir_shaken_set_error(ss, "Passport create json: Failed to add @dest array", STIR_SHAKEN_ERROR_KSJSON_ADD_DEST_ARRAY);
226259
ks_json_delete(&tn);
227260
ks_json_delete(&dest);
228261
ks_json_delete(&json);
229262
return STIR_SHAKEN_STATUS_ERR;
230263
}
264+
#endif
231265

266+
#if KS_VERSION_NUM >= 20000
267+
ks_json_add_item_to_object(json, "dest", dest);
268+
#else
232269
if (!ks_json_add_item_to_object(json, "dest", dest)) {
233270
stir_shaken_set_error(ss, "Passport create json: Failed to add @dest", STIR_SHAKEN_ERROR_KSJSON_ADD_DEST);
234271
ks_json_delete(&dest);
235272
ks_json_delete(&json);
236273
return STIR_SHAKEN_STATUS_ERR;
237274
}
275+
#endif
238276
}
239277

240278
jstr = ks_json_print_unformatted(json);
@@ -644,9 +682,20 @@ char* stir_shaken_passport_get_identity(stir_shaken_context_t *ss, stir_shaken_p
644682
}
645683

646684
if (ks_json_type_get(item) == KS_JSON_TYPE_STRING) {
647-
id = strdup(ks_json_value_string(item));
685+
const char *itemvalue = NULL;
686+
687+
#if KS_VERSION_NUM >= 20000
688+
ks_json_value_string(item, &itemvalue);
689+
#else
690+
itemvalue = ks_json_value_string(item);
691+
#endif
692+
id = strdup(itemvalue);
648693
} else if (ks_json_type_get(item) == KS_JSON_TYPE_NUMBER) {
694+
#if KS_VERSION_NUM >= 20000
695+
ks_json_value_number_int(item, &id_int);
696+
#else
649697
id_int = ks_json_value_number_int(item);
698+
#endif
650699
id = malloc(20);
651700
if (!id) {
652701
stir_shaken_set_error(ss, "Not enough memory", STIR_SHAKEN_ERROR_MEM_ID);

0 commit comments

Comments
 (0)