Skip to content

Commit ebecdd2

Browse files
committed
SCRAM-SHA-1(-PLUS) + SCRAM-SHA-256(-PLUS) + SCRAM-SHA-512(-PLUS) supports pear#57
1 parent b252263 commit ebecdd2

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

Net/SMTP.php

+144
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,13 @@ class Net_SMTP
199199
*/
200200
protected $gssapi_cname = null;
201201

202+
/**
203+
* SCRAM SHA-Hash algorithm.
204+
*
205+
* @var string
206+
*/
207+
protected $scram_sha_hash_algorithm = null;
208+
202209
/**
203210
* Instantiates a new Net_SMTP object, overriding any defaults
204211
* with parameters that are passed in.
@@ -252,6 +259,11 @@ public function __construct($host = null, $port = null, $localhost = null,
252259
if (@include_once 'Auth/SASL.php') {
253260
$this->setAuthMethod('CRAM-MD5', array($this, 'authCramMD5'));
254261
$this->setAuthMethod('DIGEST-MD5', array($this, 'authDigestMD5'));
262+
$this->setAuthMethod('SCRAM-SHA-1', array($this, 'authScramSHA1'));
263+
$this->setAuthMethod('SCRAM-SHA-224', array($this, 'authScramSHA224'));
264+
$this->setAuthMethod('SCRAM-SHA-256', array($this, 'authScramSHA256'));
265+
$this->setAuthMethod('SCRAM-SHA-384', array($this, 'authScramSHA384'));
266+
$this->setAuthMethod('SCRAM-SHA-512', array($this, 'authScramSHA512'));
255267
}
256268

257269
/* These standard authentication methods are always available. */
@@ -1153,6 +1165,138 @@ public function authXOAuth2($uid, $token, $authz, $conn)
11531165
return true;
11541166
}
11551167

1168+
1169+
1170+
1171+
/**
1172+
* Authenticates the user using the SCRAM-SHA-1 method.
1173+
*
1174+
* @param string $uid The userid to authenticate as.
1175+
* @param string $pwd The password to authenticate with.
1176+
* @param string $authz The optional authorization proxy identifier.
1177+
*
1178+
* @return mixed Returns a PEAR_Error with an error message on any
1179+
* kind of failure, or true on success.
1180+
* @since 1.11.0
1181+
*/
1182+
protected function authScramSHA1($uid, $pwd, $authz = '')
1183+
{
1184+
$this->scram_sha_hash_algorithm = 'SCRAM-SHA-1';
1185+
return $this->authScramSHA($uid, $pwd, $authz);
1186+
}
1187+
1188+
/**
1189+
* Authenticates the user using the SCRAM-SHA-224 method.
1190+
*
1191+
* @param string $uid The userid to authenticate as.
1192+
* @param string $pwd The password to authenticate with.
1193+
* @param string $authz The optional authorization proxy identifier.
1194+
*
1195+
* @return mixed Returns a PEAR_Error with an error message on any
1196+
* kind of failure, or true on success.
1197+
* @since 1.11.0
1198+
*/
1199+
protected function authScramSHA224($uid, $pwd, $authz = '')
1200+
{
1201+
$this->scram_sha_hash_algorithm = 'SCRAM-SHA-224';
1202+
return $this->authScramSHA($uid, $pwd, $authz);
1203+
}
1204+
1205+
/**
1206+
* Authenticates the user using the SCRAM-SHA-256 method.
1207+
*
1208+
* @param string $uid The userid to authenticate as.
1209+
* @param string $pwd The password to authenticate with.
1210+
* @param string $authz The optional authorization proxy identifier.
1211+
*
1212+
* @return mixed Returns a PEAR_Error with an error message on any
1213+
* kind of failure, or true on success.
1214+
* @since 1.11.0
1215+
*/
1216+
protected function authScramSHA256($uid, $pwd, $authz = '')
1217+
{
1218+
$this->scram_sha_hash_algorithm = 'SCRAM-SHA-256';
1219+
return $this->authScramSHA($uid, $pwd, $authz);
1220+
}
1221+
1222+
/**
1223+
* Authenticates the user using the SCRAM-SHA-384 method.
1224+
*
1225+
* @param string $uid The userid to authenticate as.
1226+
* @param string $pwd The password to authenticate with.
1227+
* @param string $authz The optional authorization proxy identifier.
1228+
*
1229+
* @return mixed Returns a PEAR_Error with an error message on any
1230+
* kind of failure, or true on success.
1231+
* @since 1.11.0
1232+
*/
1233+
protected function authScramSHA384($uid, $pwd, $authz = '')
1234+
{
1235+
$this->scram_sha_hash_algorithm = 'SCRAM-SHA-384';
1236+
return $this->authScramSHA($uid, $pwd, $authz);
1237+
}
1238+
1239+
/**
1240+
* Authenticates the user using the SCRAM-SHA-512 method.
1241+
*
1242+
* @param string $uid The userid to authenticate as.
1243+
* @param string $pwd The password to authenticate with.
1244+
* @param string $authz The optional authorization proxy identifier.
1245+
*
1246+
* @return mixed Returns a PEAR_Error with an error message on any
1247+
* kind of failure, or true on success.
1248+
* @since 1.11.0
1249+
*/
1250+
protected function authScramSHA512($uid, $pwd, $authz = '')
1251+
{
1252+
$this->scram_sha_hash_algorithm = 'SCRAM-SHA-512';
1253+
return $this->authScramSHA($uid, $pwd, $authz);
1254+
}
1255+
1256+
/**
1257+
* Authenticates the user using the SCRAM-SHA method.
1258+
*
1259+
* @param string $uid The userid to authenticate as.
1260+
* @param string $pwd The password to authenticate with.
1261+
* @param string $authz The optional authorization proxy identifier.
1262+
*
1263+
* @return mixed Returns a PEAR_Error with an error message on any
1264+
* kind of failure, or true on success.
1265+
* @since 1.11.0
1266+
*/
1267+
protected function authScramSHA($uid, $pwd, $authz = '')
1268+
{
1269+
if (PEAR::isError($error = $this->put('AUTH', $this->scram_sha_hash_algorithm))) {
1270+
return $error;
1271+
}
1272+
/* 334: Continue authentication request */
1273+
if (PEAR::isError($error = $this->parseResponse(334))) {
1274+
/* 503: Error: already authenticated */
1275+
if ($this->code === 503) {
1276+
return true;
1277+
}
1278+
return $error;
1279+
}
1280+
1281+
$auth_sasl = new Auth_SASL;
1282+
$challenge = base64_decode($this->arguments[0]);
1283+
$cram = $auth_sasl->factory($this->scram_sha_hash_algorithm);
1284+
$auth_str = base64_encode($cram->getResponse($uid, $pwd, $challenge));
1285+
1286+
if (PEAR::isError($error = $this->put($auth_str))) {
1287+
return $error;
1288+
}
1289+
1290+
/* 235: Authentication successful */
1291+
if (PEAR::isError($error = $this->parseResponse(235))) {
1292+
return $error;
1293+
}
1294+
}
1295+
1296+
1297+
1298+
1299+
11561300
/**
11571301
* Send the HELO command.
11581302
*

0 commit comments

Comments
 (0)