|
| 1 | +# https://github.com/duosecurity/duo_perl |
| 2 | +# |
| 3 | +# Copyright (c) 2012, Duo Security, Inc. |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions |
| 8 | +# are met: |
| 9 | +# |
| 10 | +# 1. Redistributions of source code must retain the above copyright |
| 11 | +# notice, this list of conditions and the following disclaimer. |
| 12 | +# 2. Redistributions in binary form must reproduce the above copyright |
| 13 | +# notice, this list of conditions and the following disclaimer in the |
| 14 | +# documentation and/or other materials provided with the distribution. |
| 15 | +# 3. The name of the author may not be used to endorse or promote products |
| 16 | +# derived from this software without specific prior written permission. |
| 17 | +# |
| 18 | +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 19 | +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 | +# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 21 | +# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 23 | +# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 | +# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + |
| 29 | +package Bugzilla::DuoWeb; |
| 30 | + |
| 31 | +use strict; |
| 32 | +use warnings; |
| 33 | + |
| 34 | +use MIME::Base64; |
| 35 | +use Digest::HMAC_SHA1 qw(hmac_sha1_hex); |
| 36 | + |
| 37 | +my $DUO_PREFIX = 'TX'; |
| 38 | +my $APP_PREFIX = 'APP'; |
| 39 | +my $AUTH_PREFIX = 'AUTH'; |
| 40 | + |
| 41 | +my $DUO_EXPIRE = 300; |
| 42 | +my $APP_EXPIRE = 3600; |
| 43 | + |
| 44 | +my $IKEY_LEN = 20; |
| 45 | +my $SKEY_LEN = 40; |
| 46 | +my $AKEY_LEN = 40; |
| 47 | + |
| 48 | +our $ERR_USER = 'ERR|The username passed to sign_request() is invalid.'; |
| 49 | +our $ERR_IKEY = 'ERR|The Duo integration key passed to sign_request() is invalid.'; |
| 50 | +our $ERR_SKEY = 'ERR|The Duo secret key passed to sign_request() is invalid.'; |
| 51 | +our $ERR_AKEY = "ERR|The application secret key passed to sign_request() must be at least $AKEY_LEN characters."; |
| 52 | +our $ERR_UNKNOWN = 'ERR|An unknown error has occurred.'; |
| 53 | + |
| 54 | + |
| 55 | +sub _sign_vals { |
| 56 | + my ($key, $vals, $prefix, $expire) = @_; |
| 57 | + |
| 58 | + my $exp = time + $expire; |
| 59 | + |
| 60 | + my $val = join '|', @{$vals}, $exp; |
| 61 | + my $b64 = encode_base64($val, ''); |
| 62 | + my $cookie = "$prefix|$b64"; |
| 63 | + |
| 64 | + my $sig = hmac_sha1_hex($cookie, $key); |
| 65 | + |
| 66 | + return "$cookie|$sig"; |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +sub _parse_vals { |
| 71 | + my ($key, $val, $prefix, $ikey) = @_; |
| 72 | + |
| 73 | + my $ts = time; |
| 74 | + |
| 75 | + if (not defined $val) { |
| 76 | + return ''; |
| 77 | + } |
| 78 | + |
| 79 | + my @parts = split /\|/, $val; |
| 80 | + if (scalar(@parts) != 3) { |
| 81 | + return ''; |
| 82 | + } |
| 83 | + my ($u_prefix, $u_b64, $u_sig) = @parts; |
| 84 | +
|
| 85 | + my $sig = hmac_sha1_hex("$u_prefix|$u_b64", $key); |
| 86 | +
|
| 87 | + if (hmac_sha1_hex($sig, $key) ne hmac_sha1_hex($u_sig, $key)) { |
| 88 | + return ''; |
| 89 | + } |
| 90 | +
|
| 91 | + if ($u_prefix ne $prefix) { |
| 92 | + return ''; |
| 93 | + } |
| 94 | +
|
| 95 | + my @cookie_parts = split /\|/, decode_base64($u_b64); |
| 96 | + if (scalar(@cookie_parts) != 3) { |
| 97 | + return ''; |
| 98 | + } |
| 99 | + my ($user, $u_ikey, $exp) = @cookie_parts; |
| 100 | + |
| 101 | + if ($u_ikey ne $ikey) { |
| 102 | + return ''; |
| 103 | + } |
| 104 | + |
| 105 | + if ($ts >= $exp) { |
| 106 | + return ''; |
| 107 | + } |
| 108 | + |
| 109 | + return $user; |
| 110 | +} |
| 111 | + |
| 112 | +=pod |
| 113 | + Generate a signed request for Duo authentication. |
| 114 | + The returned value should be passed into the Duo.init() call! |
| 115 | + in the rendered web page used for Duo authentication. |
| 116 | +
|
| 117 | + Arguments: |
| 118 | +
|
| 119 | + ikey -- Duo integration key |
| 120 | + skey -- Duo secret key |
| 121 | + akey -- Application secret key |
| 122 | + username -- Primary-authenticated username |
| 123 | +=cut |
| 124 | + |
| 125 | +sub sign_request { |
| 126 | + my ($ikey, $skey, $akey, $username) = @_; |
| 127 | + |
| 128 | + if (not $username) { |
| 129 | + return $ERR_USER; |
| 130 | + } |
| 131 | + |
| 132 | + if (index($username, '|') != -1) { |
| 133 | + return $ERR_USER; |
| 134 | + } |
| 135 | + |
| 136 | + if (not $ikey or length $ikey != $IKEY_LEN) { |
| 137 | + return $ERR_IKEY; |
| 138 | + } |
| 139 | + |
| 140 | + if (not $skey or length $skey != $SKEY_LEN) { |
| 141 | + return $ERR_SKEY; |
| 142 | + } |
| 143 | + |
| 144 | + if (not $akey or length $akey < $AKEY_LEN) { |
| 145 | + return $ERR_AKEY; |
| 146 | + } |
| 147 | + |
| 148 | + my $vals = [ $username, $ikey ]; |
| 149 | + |
| 150 | + my $duo_sig = _sign_vals($skey, $vals, $DUO_PREFIX, $DUO_EXPIRE); |
| 151 | + my $app_sig = _sign_vals($akey, $vals, $APP_PREFIX, $APP_EXPIRE); |
| 152 | + |
| 153 | + if (not $duo_sig or not $app_sig) { |
| 154 | + return $ERR_UNKNOWN; |
| 155 | + } |
| 156 | + |
| 157 | + return "$duo_sig:$app_sig"; |
| 158 | +} |
| 159 | + |
| 160 | +=pod |
| 161 | +
|
| 162 | + Validate the signed response returned from Duo. |
| 163 | +
|
| 164 | + Returns the username of the authenticated user, or '' (empty |
| 165 | + string) if secondary authentication was denied. |
| 166 | +
|
| 167 | + Arguments: |
| 168 | +
|
| 169 | + ikey -- Duo integration key |
| 170 | + skey -- Duo secret key |
| 171 | + akey -- Application secret key |
| 172 | + sig_response -- The signed response POST'ed to the server |
| 173 | +
|
| 174 | +=cut |
| 175 | + |
| 176 | +sub verify_response { |
| 177 | + my ($ikey, $skey, $akey, $sig_response) = @_; |
| 178 | + |
| 179 | + if (not defined $sig_response) { |
| 180 | + return ''; |
| 181 | + } |
| 182 | + |
| 183 | + my ($auth_sig, $app_sig) = split /:/, $sig_response; |
| 184 | + my $auth_user = _parse_vals($skey, $auth_sig, $AUTH_PREFIX, $ikey); |
| 185 | + my $app_user = _parse_vals($akey, $app_sig, $APP_PREFIX, $ikey); |
| 186 | + |
| 187 | + if ($auth_user ne $app_user) { |
| 188 | + return ''; |
| 189 | + } |
| 190 | + |
| 191 | + return $auth_user; |
| 192 | +} |
| 193 | +1; |
0 commit comments