CWIS Developer Documentation
SPTUser.php
Go to the documentation of this file.
1 <?PHP
2 
3 #
4 # FILE: SPT--SPTUser.php
5 #
6 # Part of the Collection Workflow Integration System (CWIS)
7 # Copyright 2004-2013 Edward Almasy and Internet Scout Research Group
8 # http://scout.wisc.edu/cwis/
9 #
10 
11 class SPTUser extends CWUser
12 {
13 
14  # ---- PUBLIC INTERFACE --------------------------------------------------
15 
25  public static function GetCryptKey()
26  {
27  $DB = new Database();
28 
29  # Clear all keys more than two days old
30  $DB->Query("DELETE FROM UsedLoginTokens WHERE NOW()-KeyCTime > 172800");
31 
32  $DB->Query("LOCK TABLES LoginKeys WRITE");
33  $DB->Query("DELETE FROM LoginKeys WHERE NOW() - CreationTime > 172800");
34 
35  # Get the most recently generated key
36  $DB->Query("SELECT NOW()-CreationTime as Age,"
37  ."KeyPair FROM LoginKeys "
38  ."ORDER BY Age ASC LIMIT 1");
39  $Row = $DB->FetchRow();
40 
41  # If there is no key in the database, or the key is too old
42  if ( ($Row===FALSE) || ($Row["Age"]>=86400) )
43  {
44  # Generate a new OpenSSL format keypair
45  $KeyPair = openssl_pkey_new(
46  array(
47  'private_key_bits' => 512, # Make this a Sysadmin pref later?
48  'private_key_type' => OPENSSL_KEYTYPE_RSA ));
49 
50  # Serialize it for storage
51  openssl_pkey_export($KeyPair, $KeyPairDBFormat);
52 
53  # And stick it into the database
54  $DB->Query("INSERT INTO LoginKeys "
55  ."(KeyPair, CreationTime) VALUES ("
56  ."\"".addslashes($KeyPairDBFormat)."\","
57  ."NOW())");
58  }
59  else
60  {
61  # If we do have a current key in the database,
62  # Convert it to openssl format for usage
63  $KeyPair = openssl_pkey_get_private( $Row["KeyPair"] );
64  }
65  $DB->Query("UNLOCK TABLES");
66 
67  return $KeyPair;
68  }
69 
77  public static function ExtractPubKeyParameters($KeyPair)
78  {
79  # Export the keypair as an ASCII signing request (which contains the data we want)
80  openssl_csr_export(openssl_csr_new(array(), $KeyPair), $Export, FALSE);
81 
82  $Modulus = "";
83  $Exponent = "";
84 
85  // @codingStandardsIgnoreStart
86  $Patterns = array(
87  '/Modulus \([0-9]+ bit\):(.*)Exponent: [0-9]+ \(0x([0-9a-f]+)\)/ms',
88  '/Public-Key: \([0-9]+ bit\).*Modulus:(.*)Exponent: [0-9]+ \(0x([0-9a-f]+)\)/ms',
89  );
90  // @codingStandardsIgnoreEnd
91 
92  foreach ($Patterns as $Pattern)
93  {
94  if (preg_match($Pattern, $Export, $Matches))
95  {
96  $Modulus = $Matches[1];
97  $Exponent = $Matches[2];
98  break;
99  }
100  }
101 
102  # Clean newlines and whitespace out of the modulus
103  $Modulus = preg_replace("/[^0-9a-f]/", "", $Modulus);
104 
105  # Return key material
106  return array( "Modulus" => $Modulus, "Exponent" => $Exponent );
107  }
108 }
$DB
Definition: User.php:976
SQL database abstraction object with smart query caching.
Definition: Database.php:22
static ExtractPubKeyParameters($KeyPair)
Extract the modulus and exponent of the public key from an OpenSSL format keypair to send in login fo...
Definition: SPTUser.php:77
static GetCryptKey()
Get/generate a cryptographic keypair for user login.
Definition: SPTUser.php:25
CWIS-specific user class.
Definition: CWUser.php:13