| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| |
| 6 |
|
| /* G_LIST is the 5.36 name for G_ARRAY */ |
| 7 |
|
| #ifndef G_LIST |
| 8 |
|
| #define G_LIST G_ARRAY |
| 9 |
|
| #endif |
| 10 |
|
| |
| 11 |
|
| /* XS_INTERNAL is 5.16+ and mg_findext 5.14+; the floor is 5.10 */ |
| 12 |
|
| #ifndef XS_INTERNAL |
| 13 |
|
| #define XS_INTERNAL(name) static void name(pTHX_ CV *cv) |
| 14 |
|
| #endif |
| 15 |
|
| #ifndef mg_findext |
| 16 |
|
| static MAGIC * |
| 17 |
|
| pt_mg_findext(pTHX_ const SV *sv, int type, const MGVTBL *vtbl) |
| 18 |
|
| { |
| 19 |
|
| MAGIC *mg; |
| 20 |
|
| if (sv && SvTYPE(sv) >= SVt_PVMG) |
| 21 |
|
| for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) |
| 22 |
|
| if (mg->mg_type == type && mg->mg_virtual == vtbl) |
| 23 |
|
| return mg; |
| 24 |
|
| return NULL; |
| 25 |
|
| } |
| 26 |
|
| #define mg_findext(sv, type, vtbl) pt_mg_findext(aTHX_ (sv), (type), (vtbl)) |
| 27 |
|
| #endif |
| 28 |
|
| |
| 29 |
|
| #include <time.h> |
| 30 |
|
| |
| 31 |
|
| #include "frh_abi.h" |
| 32 |
|
| #include "qr_abi.h" |
| 33 |
|
| #include "ptotp/ptotp_b32.h" |
| 34 |
|
| #include "ptotp/ptotp_rand.h" |
| 35 |
|
| #include "ptotp/ptotp_otp.h" |
| 36 |
|
| |
| 37 |
|
| /* Punk::TOTP - the engine. 100% XS; the .pm is a version number and |
| 38 |
|
| * the POD. HMAC comes from File::Raw::Hash's frh_abi, resolved once |
| 39 |
|
| * at boot; nothing links and nothing here touches libcrypto. */ |
| 40 |
|
| |
| 41 |
|
| #define PTOTP_FRH_NEED 1 |
| 42 |
|
| |
| 43 |
|
| static const frh_abi *PT_FRH = NULL; |
| 44 |
|
| |
| 45 |
|
| typedef struct { |
| 46 |
|
| const char *name; /* as otpauth:// spells it */ |
| 47 |
|
| int frh_id; |
| 48 |
|
| size_t secret_bytes; |
| 49 |
|
| } pt_alg_t; |
| 50 |
|
| |
| 51 |
|
| static pt_alg_t PT_ALGS[3] = { |
| 52 |
|
| { "SHA1", -1, 20 }, |
| 53 |
|
| { "SHA256", -1, 32 }, |
| 54 |
|
| { "SHA512", -1, 64 }, |
| 55 |
|
| }; |
| 56 |
|
| |
| 57 |
6 |
| static void pt_boot(pTHX) |
| 58 |
|
| { |
| 59 |
6 |
| IV p = 0; |
| 60 |
6 |
| dSP; |
| 61 |
|
| |
| 62 |
6 |
| ENTER; SAVETMPS; |
| 63 |
6 |
| eval_pv("require File::Raw::Hash;", TRUE); |
| 64 |
6 |
▶ | PUSHMARK(SP); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); PUTBACK; |
0 |
6 |
6 |
|
| 65 |
6 |
▶ | if (call_pv("File::Raw::Hash::_abi_ptr", G_SCALAR | G_EVAL) > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (call_pv("File::Raw::Hash::_abi_ptr", G_SCALAR | G_EVAL) > 0) { |
6 |
0 |
6 |
|
| 66 |
6 |
| SPAGAIN; |
| 67 |
6 |
| p = POPi; |
| 68 |
6 |
| PUTBACK; |
| 69 |
|
| } |
| 70 |
6 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
6 |
0 |
6 |
|
| 71 |
|
| |
| 72 |
6 |
▶ | PT_FRH = p ? INT2PTR(const frh_abi *, p) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| PT_FRH = p ? INT2PTR(const frh_abi *, p) : NULL; |
6 |
0 |
6 |
|
| 73 |
6 |
▶ | if (!PT_FRH || PT_FRH->version < PTOTP_FRH_NEED) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!PT_FRH || PT_FRH->version < PTOTP_FRH_NEED) |
6 |
0 |
6 |
| if (!PT_FRH || PT_FRH->version < PTOTP_FRH_NEED) |
0 |
6 |
6 |
|
| 74 |
0 |
| croak("Punk::TOTP: File::Raw::Hash with frh_abi version %d or " |
| 75 |
|
| "newer is required", PTOTP_FRH_NEED); |
| 76 |
|
| |
| 77 |
|
| { |
| 78 |
|
| const frh_algo_t *a; |
| 79 |
6 |
▶ | if (!(a = PT_FRH->algo_by_name("sha1", 4)) || !a->hmac_able) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(a = PT_FRH->algo_by_name("sha1", 4)) || !a->hmac_able) |
6 |
0 |
6 |
| if (!(a = PT_FRH->algo_by_name("sha1", 4)) || !a->hmac_able) |
0 |
6 |
6 |
|
| 80 |
0 |
| croak("Punk::TOTP: the frh_abi registry has no hmac-able sha1"); |
| 81 |
6 |
| PT_ALGS[0].frh_id = a->id; |
| 82 |
6 |
▶ | if (!(a = PT_FRH->algo_by_name("sha256", 6)) || !a->hmac_able) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(a = PT_FRH->algo_by_name("sha256", 6)) || !a->hmac_able) |
6 |
0 |
6 |
| if (!(a = PT_FRH->algo_by_name("sha256", 6)) || !a->hmac_able) |
0 |
6 |
6 |
|
| 83 |
0 |
| croak("Punk::TOTP: the frh_abi registry has no hmac-able sha256"); |
| 84 |
6 |
| PT_ALGS[1].frh_id = a->id; |
| 85 |
6 |
▶ | if (!(a = PT_FRH->algo_by_name("sha512", 6)) || !a->hmac_able) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(a = PT_FRH->algo_by_name("sha512", 6)) || !a->hmac_able) |
6 |
0 |
6 |
| if (!(a = PT_FRH->algo_by_name("sha512", 6)) || !a->hmac_able) |
0 |
6 |
6 |
|
| 86 |
0 |
| croak("Punk::TOTP: the frh_abi registry has no hmac-able sha512"); |
| 87 |
6 |
| PT_ALGS[2].frh_id = a->id; |
| 88 |
|
| } |
| 89 |
6 |
| } |
| 90 |
|
| |
| 91 |
|
| /* ---- options ------------------------------------------------------------- */ |
| 92 |
|
| |
| 93 |
75 |
| static HV *pt_pairs(pTHX_ const char *what, SV **base, I32 from, I32 items) |
| 94 |
|
| { |
| 95 |
75 |
| HV *hv = newHV(); |
| 96 |
|
| I32 i; |
| 97 |
|
| |
| 98 |
75 |
| sv_2mortal((SV *)hv); |
| 99 |
75 |
▶ | if ((items - from) % 2) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((items - from) % 2) |
1 |
74 |
75 |
|
| 100 |
1 |
| croak("odd number of options given to %s", what); |
| 101 |
183 |
▶ | for (i = from; i < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = from; i < items; i += 2) { |
109 |
74 |
183 |
|
| 102 |
|
| STRLEN kl; |
| 103 |
109 |
| const char *k = SvPV_const(base[i], kl); |
| 104 |
109 |
| (void)hv_store(hv, k, (I32)kl, SvREFCNT_inc(base[i + 1]), 0); |
| 105 |
|
| } |
| 106 |
74 |
| return hv; |
| 107 |
|
| } |
| 108 |
|
| |
| 109 |
74 |
| static void pt_check_keys(pTHX_ const char *what, HV *hv, |
| 110 |
|
| const char *const *ok, int nok) |
| 111 |
|
| { |
| 112 |
|
| HE *he; |
| 113 |
|
| |
| 114 |
74 |
| hv_iterinit(hv); |
| 115 |
182 |
▶ | while ((he = hv_iternext(hv))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(hv))) { |
109 |
73 |
182 |
|
| 116 |
109 |
▶ | const char *k = HePV(he, PL_na); |
Branch: 1 branch
| Branch | True | False | Total |
| const char *k = HePV(he, PL_na); |
0 |
109 |
109 |
|
| 117 |
109 |
| int j, found = 0; |
| 118 |
317 |
▶ | for (j = 0; j < nok; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < nok; j++) |
316 |
1 |
317 |
|
| 119 |
316 |
▶ | if (strEQ(k, ok[j])) { found = 1; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(k, ok[j])) { found = 1; break; } |
108 |
208 |
316 |
|
| 120 |
109 |
▶ | if (!found) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!found) |
1 |
108 |
109 |
|
| 121 |
1 |
| croak("unknown %s option '%s'", what, k); |
| 122 |
|
| } |
| 123 |
73 |
| } |
| 124 |
|
| |
| 125 |
279 |
| static SV *pt_fetch(pTHX_ HV *hv, const char *key) |
| 126 |
|
| { |
| 127 |
279 |
| SV **svp = hv_fetch(hv, key, (I32)strlen(key), 0); |
| 128 |
279 |
▶ | return (svp && SvOK(*svp)) ? *svp : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| return (svp && SvOK(*svp)) ? *svp : NULL; |
108 |
171 |
279 |
| return (svp && SvOK(*svp)) ? *svp : NULL; |
108 |
0 |
108 |
|
| 129 |
|
| } |
| 130 |
|
| |
| 131 |
72 |
| static const pt_alg_t *pt_alg_of(pTHX_ SV *sv) |
| 132 |
|
| { |
| 133 |
|
| STRLEN n; |
| 134 |
|
| const char *p; |
| 135 |
|
| int i; |
| 136 |
|
| |
| 137 |
72 |
▶ | if (!sv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv) |
49 |
23 |
72 |
|
| 138 |
49 |
| return &PT_ALGS[0]; |
| 139 |
23 |
| p = SvPV_const(sv, n); |
| 140 |
48 |
▶ | for (i = 0; i < 3; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 3; i++) { |
47 |
1 |
48 |
|
| 141 |
47 |
| const char *want = PT_ALGS[i].name; |
| 142 |
47 |
| size_t wl = strlen(want); |
| 143 |
|
| size_t j; |
| 144 |
47 |
▶ | if (n != wl) |
Branch: 1 branch
| Branch | True | False | Total |
| if (n != wl) |
18 |
29 |
47 |
|
| 145 |
18 |
| continue; |
| 146 |
168 |
▶ | for (j = 0; j < wl; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < wl; j++) |
146 |
22 |
168 |
|
| 147 |
146 |
▶ | if (toUPPER(p[j]) != want[j]) |
Branch: 2 branches
| Branch | True | False | Total |
| if (toUPPER(p[j]) != want[j]) |
87 |
59 |
146 |
| if (toUPPER(p[j]) != want[j]) |
7 |
139 |
146 |
|
| 148 |
7 |
| break; |
| 149 |
29 |
▶ | if (j == wl) |
Branch: 1 branch
| Branch | True | False | Total |
| if (j == wl) |
22 |
7 |
29 |
|
| 150 |
22 |
| return &PT_ALGS[i]; |
| 151 |
|
| } |
| 152 |
1 |
| croak("algorithm must be sha1, sha256 or sha512, not '%.*s'", |
| 153 |
|
| (int)n, p); |
| 154 |
|
| return NULL; |
| 155 |
|
| } |
| 156 |
|
| |
| 157 |
63 |
| static int pt_digits_of(pTHX_ SV *sv) |
| 158 |
|
| { |
| 159 |
|
| IV d; |
| 160 |
63 |
▶ | if (!sv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv) |
41 |
22 |
63 |
|
| 161 |
41 |
| return 6; |
| 162 |
22 |
| d = SvIV(sv); |
| 163 |
22 |
▶ | if (d < 6 || d > 8) |
Branch: 2 branches
| Branch | True | False | Total |
| if (d < 6 || d > 8) |
21 |
1 |
22 |
| if (d < 6 || d > 8) |
1 |
20 |
21 |
|
| 164 |
2 |
| croak("digits must be 6, 7 or 8, not %" IVdf, d); |
| 165 |
20 |
| return (int)d; |
| 166 |
|
| } |
| 167 |
|
| |
| 168 |
46 |
| static unsigned pt_period_of(pTHX_ SV *sv) |
| 169 |
|
| { |
| 170 |
|
| IV s; |
| 171 |
46 |
▶ | if (!sv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv) |
42 |
4 |
46 |
|
| 172 |
42 |
| return 30; |
| 173 |
4 |
| s = SvIV(sv); |
| 174 |
4 |
▶ | if (s < 1 || s > 300) |
Branch: 2 branches
| Branch | True | False | Total |
| if (s < 1 || s > 300) |
3 |
1 |
4 |
| if (s < 1 || s > 300) |
0 |
3 |
3 |
|
| 175 |
1 |
| croak("period must be 1 to 300 seconds, not %" IVdf, s); |
| 176 |
3 |
| return (unsigned)s; |
| 177 |
|
| } |
| 178 |
|
| |
| 179 |
42 |
| static ptotp_u64 pt_time_of(pTHX_ SV *sv) |
| 180 |
|
| { |
| 181 |
42 |
▶ | if (!sv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv) |
0 |
42 |
42 |
|
| 182 |
0 |
| return (ptotp_u64)time(NULL); |
| 183 |
|
| /* through NV, deliberately: the RFC 6238 vectors reach |
| 184 |
|
| * 20000000000, past IV on the 32-bit smokers */ |
| 185 |
42 |
| return (ptotp_u64)SvNV(sv); |
| 186 |
|
| } |
| 187 |
|
| |
| 188 |
|
| /* Decode a base32 secret into a mortal buffer. */ |
| 189 |
62 |
| static unsigned char *pt_secret_of(pTHX_ SV *sv, size_t *outlen) |
| 190 |
|
| { |
| 191 |
|
| STRLEN n; |
| 192 |
62 |
| const char *p = SvPV_const(sv, n); |
| 193 |
|
| unsigned char *buf; |
| 194 |
|
| long got; |
| 195 |
|
| |
| 196 |
62 |
▶ | Newx(buf, n ? (n * 5) / 8 + 1 : 1, unsigned char); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(buf, n ? (n * 5) / 8 + 1 : 1, unsigned char); |
62 |
0 |
62 |
|
| 197 |
62 |
| SAVEFREEPV(buf); |
| 198 |
62 |
| got = ptotp_b32_decode(buf, p, n); |
| 199 |
62 |
▶ | if (got < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (got < 0) |
2 |
60 |
62 |
|
| 200 |
2 |
| croak("secret is not valid base32"); |
| 201 |
60 |
▶ | if (got == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (got == 0) |
0 |
60 |
60 |
|
| 202 |
0 |
| croak("secret is empty"); |
| 203 |
60 |
| *outlen = (size_t)got; |
| 204 |
60 |
| return buf; |
| 205 |
|
| } |
| 206 |
|
| |
| 207 |
|
| /* ---- uri ----------------------------------------------------------------- */ |
| 208 |
|
| |
| 209 |
6 |
| static void pt_uri_escape(pTHX_ SV *out, const char *p, STRLEN n) |
| 210 |
|
| { |
| 211 |
|
| static const char hexd[] = "0123456789ABCDEF"; |
| 212 |
|
| STRLEN i; |
| 213 |
|
| |
| 214 |
90 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
84 |
6 |
90 |
|
| 215 |
84 |
| unsigned char c = (unsigned char)p[i]; |
| 216 |
84 |
▶ | if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || |
Branch: 5 branches
| Branch | True | False | Total |
| if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || |
68 |
16 |
84 |
| if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || |
64 |
4 |
68 |
| if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || |
64 |
16 |
80 |
| if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || |
0 |
64 |
64 |
| if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || |
3 |
13 |
16 |
|
| 217 |
16 |
▶ | (c >= '0' && c <= '9') || c == '-' || c == '_' || |
Branch: 4 branches
| Branch | True | False | Total |
| (c >= '0' && c <= '9') || c == '-' || c == '_' || |
2 |
1 |
3 |
| (c >= '0' && c <= '9') || c == '-' || c == '_' || |
13 |
2 |
15 |
| (c >= '0' && c <= '9') || c == '-' || c == '_' || |
13 |
0 |
13 |
| (c >= '0' && c <= '9') || c == '-' || c == '_' || |
9 |
4 |
13 |
|
| 218 |
9 |
▶ | c == '.' || c == '~') { |
Branch: 1 branch
| Branch | True | False | Total |
| c == '.' || c == '~') { |
0 |
9 |
9 |
|
| 219 |
75 |
| sv_catpvn(out, (const char *)&p[i], 1); |
| 220 |
|
| } else { |
| 221 |
|
| char esc[3]; |
| 222 |
9 |
| esc[0] = '%'; |
| 223 |
9 |
| esc[1] = hexd[c >> 4]; |
| 224 |
9 |
| esc[2] = hexd[c & 15]; |
| 225 |
9 |
| sv_catpvn(out, esc, 3); |
| 226 |
|
| } |
| 227 |
|
| } |
| 228 |
6 |
| } |
| 229 |
|
| |
| 230 |
|
| #include "ptotp/ptotp_plugin.h" /* the plugin: needs the helpers above */ |
| 231 |
|
| |
| 232 |
|
| MODULE = Punk::TOTP PACKAGE = Punk::TOTP |
| 233 |
|
| |
| 234 |
|
| PROTOTYPES: DISABLE |
| 235 |
|
| |
| 236 |
|
| BOOT: |
| 237 |
6 |
| pt_boot(aTHX); |
| 238 |
|
| |
| 239 |
|
| SV * |
| 240 |
|
| secret(class, ...) |
| 241 |
|
| SV *class |
| 242 |
|
| CODE: |
| 243 |
|
| { |
| 244 |
|
| static const char *const ok[] = { "algorithm", "bytes" }; |
| 245 |
7 |
| HV *opt = pt_pairs(aTHX_ "secret", &ST(0), 1, items); |
| 246 |
|
| const pt_alg_t *alg; |
| 247 |
|
| unsigned char raw[128]; |
| 248 |
|
| char b32[((sizeof raw + 4) / 5) * 8 + 1]; |
| 249 |
|
| size_t nbytes; |
| 250 |
|
| SV *sv; |
| 251 |
|
| |
| 252 |
|
| PERL_UNUSED_VAR(class); |
| 253 |
7 |
| pt_check_keys(aTHX_ "secret", opt, ok, 2); |
| 254 |
7 |
| alg = pt_alg_of(aTHX_ pt_fetch(aTHX_ opt, "algorithm")); |
| 255 |
7 |
| nbytes = alg->secret_bytes; |
| 256 |
7 |
▶ | if ((sv = pt_fetch(aTHX_ opt, "bytes"))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((sv = pt_fetch(aTHX_ opt, "bytes"))) { |
2 |
5 |
7 |
|
| 257 |
2 |
| IV b = SvIV(sv); |
| 258 |
2 |
▶ | if (b < 16 || b > (IV)sizeof raw) |
Branch: 2 branches
| Branch | True | False | Total |
| if (b < 16 || b > (IV)sizeof raw) |
1 |
1 |
2 |
| if (b < 16 || b > (IV)sizeof raw) |
0 |
1 |
1 |
|
| 259 |
1 |
| croak("bytes must be 16 to %d, not %" IVdf, |
| 260 |
|
| (int)sizeof raw, b); |
| 261 |
1 |
| nbytes = (size_t)b; |
| 262 |
|
| } |
| 263 |
6 |
▶ | if (ptotp_random_bytes(raw, nbytes) != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ptotp_random_bytes(raw, nbytes) != 0) |
0 |
6 |
6 |
|
| 264 |
0 |
| croak("Punk::TOTP: the entropy source failed; refusing to " |
| 265 |
|
| "degrade"); |
| 266 |
6 |
| ptotp_b32_encode(b32, raw, nbytes); |
| 267 |
6 |
| RETVAL = newSVpv(b32, 0); |
| 268 |
|
| } |
| 269 |
|
| OUTPUT: |
| 270 |
|
| RETVAL |
| 271 |
|
| |
| 272 |
|
| SV * |
| 273 |
|
| uri(class, secret, ...) |
| 274 |
|
| SV *class |
| 275 |
|
| SV *secret |
| 276 |
|
| CODE: |
| 277 |
|
| { |
| 278 |
|
| static const char *const ok[] = |
| 279 |
|
| { "issuer", "account", "algorithm", "digits", "period" }; |
| 280 |
4 |
| HV *opt = pt_pairs(aTHX_ "uri", &ST(0), 2, items); |
| 281 |
|
| const pt_alg_t *alg; |
| 282 |
|
| SV *issuer, *account; |
| 283 |
|
| STRLEN sn, in, an; |
| 284 |
|
| const char *sec, *ip, *ap; |
| 285 |
|
| int digits; |
| 286 |
|
| unsigned period; |
| 287 |
|
| SV *out; |
| 288 |
|
| |
| 289 |
|
| PERL_UNUSED_VAR(class); |
| 290 |
4 |
| pt_check_keys(aTHX_ "uri", opt, ok, 5); |
| 291 |
4 |
| issuer = pt_fetch(aTHX_ opt, "issuer"); |
| 292 |
4 |
| account = pt_fetch(aTHX_ opt, "account"); |
| 293 |
4 |
▶ | if (!issuer || !account) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!issuer || !account) |
4 |
0 |
4 |
| if (!issuer || !account) |
1 |
3 |
4 |
|
| 294 |
1 |
| croak("uri needs both issuer and account"); |
| 295 |
3 |
| alg = pt_alg_of(aTHX_ pt_fetch(aTHX_ opt, "algorithm")); |
| 296 |
3 |
| digits = pt_digits_of(aTHX_ pt_fetch(aTHX_ opt, "digits")); |
| 297 |
3 |
| period = pt_period_of(aTHX_ pt_fetch(aTHX_ opt, "period")); |
| 298 |
|
| |
| 299 |
|
| /* validate the secret decodes before advertising it */ |
| 300 |
|
| { |
| 301 |
|
| size_t slen; |
| 302 |
3 |
| (void)pt_secret_of(aTHX_ secret, &slen); |
| 303 |
|
| } |
| 304 |
|
| |
| 305 |
2 |
▶ | sec = SvPV_const(secret, sn); |
Branch: 1 branch
| Branch | True | False | Total |
| sec = SvPV_const(secret, sn); |
2 |
0 |
2 |
|
| 306 |
2 |
▶ | ip = SvPV_const(issuer, in); |
Branch: 1 branch
| Branch | True | False | Total |
| ip = SvPV_const(issuer, in); |
2 |
0 |
2 |
|
| 307 |
2 |
| ap = SvPV_const(account, an); |
| 308 |
|
| |
| 309 |
|
| /* The issuer appears twice - label prefix and query parameter - |
| 310 |
|
| * because different apps read different ones. The label colon |
| 311 |
|
| * is the separator and is NEVER percent-encoded; everything |
| 312 |
|
| * else is. Non-default algorithm/digits/period are included, |
| 313 |
|
| * defaults omitted to keep the QR payload small. */ |
| 314 |
2 |
| out = newSVpvs("otpauth://totp/"); |
| 315 |
2 |
| pt_uri_escape(aTHX_ out, ip, in); |
| 316 |
2 |
| sv_catpvs(out, ":"); |
| 317 |
2 |
| pt_uri_escape(aTHX_ out, ap, an); |
| 318 |
2 |
| sv_catpvs(out, "?secret="); |
| 319 |
2 |
| sv_catpvn(out, sec, sn); |
| 320 |
2 |
| sv_catpvs(out, "&issuer="); |
| 321 |
2 |
| pt_uri_escape(aTHX_ out, ip, in); |
| 322 |
2 |
▶ | if (alg != &PT_ALGS[0]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (alg != &PT_ALGS[0]) { |
1 |
1 |
2 |
|
| 323 |
1 |
| sv_catpvs(out, "&algorithm="); |
| 324 |
1 |
| sv_catpv(out, alg->name); |
| 325 |
|
| } |
| 326 |
2 |
▶ | if (digits != 6) |
Branch: 1 branch
| Branch | True | False | Total |
| if (digits != 6) |
1 |
1 |
2 |
|
| 327 |
1 |
| sv_catpvf(out, "&digits=%d", digits); |
| 328 |
2 |
▶ | if (period != 30) |
Branch: 1 branch
| Branch | True | False | Total |
| if (period != 30) |
1 |
1 |
2 |
|
| 329 |
1 |
| sv_catpvf(out, "&period=%u", period); |
| 330 |
2 |
| RETVAL = out; |
| 331 |
|
| } |
| 332 |
|
| OUTPUT: |
| 333 |
|
| RETVAL |
| 334 |
|
| |
| 335 |
|
| SV * |
| 336 |
|
| hotp(class, secret, counter, ...) |
| 337 |
|
| SV *class |
| 338 |
|
| SV *secret |
| 339 |
|
| SV *counter |
| 340 |
|
| CODE: |
| 341 |
|
| { |
| 342 |
|
| static const char *const ok[] = { "algorithm", "digits" }; |
| 343 |
14 |
| HV *opt = pt_pairs(aTHX_ "hotp", &ST(0), 3, items); |
| 344 |
|
| const pt_alg_t *alg; |
| 345 |
|
| unsigned char *sec; |
| 346 |
|
| size_t slen; |
| 347 |
|
| unsigned long code; |
| 348 |
|
| int digits; |
| 349 |
|
| char buf[9]; |
| 350 |
|
| |
| 351 |
|
| PERL_UNUSED_VAR(class); |
| 352 |
14 |
| pt_check_keys(aTHX_ "hotp", opt, ok, 2); |
| 353 |
14 |
| alg = pt_alg_of(aTHX_ pt_fetch(aTHX_ opt, "algorithm")); |
| 354 |
14 |
| digits = pt_digits_of(aTHX_ pt_fetch(aTHX_ opt, "digits")); |
| 355 |
14 |
| sec = pt_secret_of(aTHX_ secret, &slen); |
| 356 |
|
| |
| 357 |
14 |
| code = ptotp_hotp(PT_FRH, alg->frh_id, sec, slen, |
| 358 |
14 |
| (ptotp_u64)SvNV(counter), digits); |
| 359 |
14 |
▶ | if (code == (unsigned long)-1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (code == (unsigned long)-1) |
0 |
14 |
14 |
|
| 360 |
0 |
| croak("Punk::TOTP: hmac failed"); |
| 361 |
14 |
| ptotp_render(code, digits, buf); |
| 362 |
14 |
| RETVAL = newSVpv(buf, (STRLEN)digits); |
| 363 |
|
| } |
| 364 |
|
| OUTPUT: |
| 365 |
|
| RETVAL |
| 366 |
|
| |
| 367 |
|
| SV * |
| 368 |
|
| code(class, secret, ...) |
| 369 |
|
| SV *class |
| 370 |
|
| SV *secret |
| 371 |
|
| CODE: |
| 372 |
|
| { |
| 373 |
|
| static const char *const ok[] = |
| 374 |
|
| { "algorithm", "digits", "period", "time" }; |
| 375 |
29 |
| HV *opt = pt_pairs(aTHX_ "code", &ST(0), 2, items); |
| 376 |
|
| const pt_alg_t *alg; |
| 377 |
|
| unsigned char *sec; |
| 378 |
|
| size_t slen; |
| 379 |
|
| unsigned long c; |
| 380 |
|
| int digits; |
| 381 |
|
| char buf[9]; |
| 382 |
|
| |
| 383 |
|
| PERL_UNUSED_VAR(class); |
| 384 |
28 |
| pt_check_keys(aTHX_ "code", opt, ok, 4); |
| 385 |
27 |
| alg = pt_alg_of(aTHX_ pt_fetch(aTHX_ opt, "algorithm")); |
| 386 |
26 |
| digits = pt_digits_of(aTHX_ pt_fetch(aTHX_ opt, "digits")); |
| 387 |
24 |
| sec = pt_secret_of(aTHX_ secret, &slen); |
| 388 |
|
| |
| 389 |
23 |
| c = ptotp_totp_at(PT_FRH, alg->frh_id, sec, slen, |
| 390 |
|
| pt_time_of(aTHX_ pt_fetch(aTHX_ opt, "time")), |
| 391 |
|
| pt_period_of(aTHX_ pt_fetch(aTHX_ opt, "period")), |
| 392 |
|
| digits); |
| 393 |
22 |
▶ | if (c == (unsigned long)-1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == (unsigned long)-1) |
0 |
22 |
22 |
|
| 394 |
0 |
| croak("Punk::TOTP: hmac failed"); |
| 395 |
22 |
| ptotp_render(c, digits, buf); |
| 396 |
22 |
| RETVAL = newSVpv(buf, (STRLEN)digits); |
| 397 |
|
| } |
| 398 |
|
| OUTPUT: |
| 399 |
|
| RETVAL |
| 400 |
|
| |
| 401 |
|
| void |
| 402 |
|
| verify(class, secret, code, ...) |
| 403 |
|
| SV *class |
| 404 |
|
| SV *secret |
| 405 |
|
| SV *code |
| 406 |
|
| PPCODE: |
| 407 |
|
| { |
| 408 |
|
| static const char *const ok[] = |
| 409 |
|
| { "algorithm", "digits", "period", "time", "skew", |
| 410 |
|
| "last_counter" }; |
| 411 |
21 |
| HV *opt = pt_pairs(aTHX_ "verify", &ST(0), 3, items); |
| 412 |
|
| const pt_alg_t *alg; |
| 413 |
|
| unsigned char *sec; |
| 414 |
|
| size_t slen; |
| 415 |
|
| STRLEN cn; |
| 416 |
|
| const char *cp; |
| 417 |
|
| SV *sv; |
| 418 |
21 |
| unsigned skew = 1; |
| 419 |
21 |
| ptotp_u64 last = (ptotp_u64)-1, matched = 0; |
| 420 |
|
| int ok_rc; |
| 421 |
|
| |
| 422 |
|
| PERL_UNUSED_VAR(class); |
| 423 |
21 |
| pt_check_keys(aTHX_ "verify", opt, ok, 6); |
| 424 |
21 |
| alg = pt_alg_of(aTHX_ pt_fetch(aTHX_ opt, "algorithm")); |
| 425 |
21 |
| sec = pt_secret_of(aTHX_ secret, &slen); |
| 426 |
21 |
| cp = SvPV_const(code, cn); |
| 427 |
|
| |
| 428 |
21 |
▶ | if ((sv = pt_fetch(aTHX_ opt, "skew"))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((sv = pt_fetch(aTHX_ opt, "skew"))) { |
3 |
18 |
21 |
|
| 429 |
3 |
| IV s = SvIV(sv); |
| 430 |
3 |
▶ | if (s < 0 || s > 10) |
Branch: 2 branches
| Branch | True | False | Total |
| if (s < 0 || s > 10) |
3 |
0 |
3 |
| if (s < 0 || s > 10) |
1 |
2 |
3 |
|
| 431 |
1 |
| croak("skew must be 0 to 10 steps, not %" IVdf, s); |
| 432 |
2 |
| skew = (unsigned)s; |
| 433 |
|
| } |
| 434 |
20 |
▶ | if ((sv = pt_fetch(aTHX_ opt, "last_counter"))) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((sv = pt_fetch(aTHX_ opt, "last_counter"))) |
5 |
15 |
20 |
|
| 435 |
5 |
| last = (ptotp_u64)SvNV(sv); |
| 436 |
|
| |
| 437 |
20 |
| ok_rc = ptotp_verify(PT_FRH, alg->frh_id, sec, slen, |
| 438 |
|
| pt_time_of(aTHX_ pt_fetch(aTHX_ opt, "time")), |
| 439 |
|
| pt_period_of(aTHX_ pt_fetch(aTHX_ opt, "period")), |
| 440 |
|
| pt_digits_of(aTHX_ pt_fetch(aTHX_ opt, "digits")), |
| 441 |
|
| cp, (size_t)cn, skew, last, &matched); |
| 442 |
|
| |
| 443 |
20 |
▶ | if (GIMME_V == G_LIST) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GIMME_V == G_LIST) { |
5 |
15 |
20 |
|
| 444 |
5 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
5 |
5 |
|
| 445 |
5 |
| mPUSHi(ok_rc); |
| 446 |
5 |
▶ | if (ok_rc) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ok_rc) |
3 |
2 |
5 |
|
| 447 |
3 |
| mPUSHs(newSVnv((NV)matched)); |
| 448 |
|
| else |
| 449 |
2 |
| mPUSHs(newSV(0)); |
| 450 |
|
| } else { |
| 451 |
15 |
▶ | mXPUSHi(ok_rc); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHi(ok_rc); |
0 |
15 |
15 |
|
| 452 |
|
| } |
| 453 |
|
| } |
| 454 |
|
| |
| 455 |
|
| SV * |
| 456 |
|
| b32_encode(class, bytes) |
| 457 |
|
| SV *class |
| 458 |
|
| SV *bytes |
| 459 |
|
| CODE: |
| 460 |
|
| { |
| 461 |
|
| STRLEN n; |
| 462 |
|
| const unsigned char *p = |
| 463 |
86 |
| (const unsigned char *)SvPVbyte(bytes, n); |
| 464 |
|
| char *out; |
| 465 |
|
| |
| 466 |
|
| PERL_UNUSED_VAR(class); |
| 467 |
86 |
| Newx(out, ptotp_b32_elen(n) + 1, char); |
| 468 |
86 |
| SAVEFREEPV(out); |
| 469 |
86 |
| ptotp_b32_encode(out, p, n); |
| 470 |
86 |
| RETVAL = newSVpv(out, 0); |
| 471 |
|
| } |
| 472 |
|
| OUTPUT: |
| 473 |
|
| RETVAL |
| 474 |
|
| |
| 475 |
|
| SV * |
| 476 |
|
| b32_decode(class, text) |
| 477 |
|
| SV *class |
| 478 |
|
| SV *text |
| 479 |
|
| CODE: |
| 480 |
|
| { |
| 481 |
|
| STRLEN n; |
| 482 |
103 |
| const char *p = SvPV_const(text, n); |
| 483 |
|
| unsigned char *out; |
| 484 |
|
| long got; |
| 485 |
|
| |
| 486 |
|
| PERL_UNUSED_VAR(class); |
| 487 |
103 |
▶ | Newx(out, n ? (n * 5) / 8 + 1 : 1, unsigned char); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(out, n ? (n * 5) / 8 + 1 : 1, unsigned char); |
101 |
2 |
103 |
|
| 488 |
103 |
| SAVEFREEPV(out); |
| 489 |
103 |
| got = ptotp_b32_decode(out, p, n); |
| 490 |
103 |
▶ | if (got < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (got < 0) |
14 |
89 |
103 |
|
| 491 |
14 |
| croak("not valid base32"); |
| 492 |
89 |
| RETVAL = newSVpvn((const char *)out, (STRLEN)got); |
| 493 |
|
| } |
| 494 |
|
| OUTPUT: |
| 495 |
|
| RETVAL |
| 496 |
|
| |
| 497 |
|
| INCLUDE: xs/plugin.xs |