1: <?php
2:
3: /*
4:
5: *
6: * NOTICE OF LICENSE
7: *
8: * This source file is subject to the Open Software License (OSL 3.0)
9: * or OpenGPL v3 license (GNU Public License V3.0)
10: * that is bundled with this package in the file LICENSE.txt.
11: * It is also available through the world-wide-web at this URL:
12: * http://opensource.org/licenses/osl-3.0.php
13: * or
14: * http://www.gnu.org/licenses/gpl-3.0.txt
15: * If you did not receive a copy of the license and are unable to
16: * obtain it through the world-wide-web, please send an email
17: * to info@e-abi.ee so we can send you a copy immediately.
18: *
19: * DISCLAIMER
20: *
21: * Do not edit or add to this file if you wish to upgrade this module to newer
22: * versions in the future.
23: *
24: * @category Eabi
25: * @package Eabi_Dpd
26: * @copyright Copyright (c) 2014 Aktsiamaailm LLC (http://en.e-abi.ee/)
27: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
28: * @license http://www.gnu.org/licenses/gpl-3.0.txt GNU Public License V3.0
29: * @author Matis Halmann
30: *
31:
32: */
33:
34: /**
35: * <p>Helper class for generating RSA public and private keys</p>
36: *
37: * @author Matis
38: */
39: class Eabi_Livehandler_Helper_Keypair extends Mage_Core_Helper_Abstract {
40:
41:
42: /**
43: * <p>Generates new RSA keypair and returns it as assoc array with following keys:</p>
44: * <ul>
45: <li><strong>privkey</strong> - private key .pem encoded string</li>
46: <li><strong>csr</strong> - public key which has been signed with generated private key</li>
47: <li><strong>pubkey</strong> - public key .pem encoded certificate string</li>
48: </ul>
49: * <p>Input uses following options:</p>
50: * <ul>
51: <li><strong>countryName</strong> - Country code uppercase goes here, like EE</li>
52: <li><strong>stateOrProvinceName</strong> - County or state name goes here</li>
53: <li><strong>localityName</strong> - Name of the city goes here</li>
54: <li><strong>organizationName</strong> - Name of the company goes here</li>
55: <li><strong>organizationalUnitName</strong> - Company www goes here</li>
56: <li><strong>commonName</strong> - Name of the person who generated this cert goes here or name of the person on board</li>
57: <li><strong>emailAddress</strong> - Ggeneric company e-mail goes here</li>
58: </ul>
59: * <p>Private key is not encrypted with any password</p>
60: *
61: * @param array $input
62: * @param array $conf
63: */
64: public function generateKeyPair(array $input, array $conf = array('private_key_bits' => 2048), $numberofdays = 3650) {
65: $publickey = null;
66: $privatekey = null;
67: $csrStr = null;
68: $privkeypass = null;
69: $privkey = openssl_pkey_new($conf);
70: $csr = openssl_csr_new($input, $privkey);
71: $sscert = openssl_csr_sign($csr, null, $privkey, $numberofdays);
72: openssl_x509_export($sscert, $publickey);
73: openssl_pkey_export($privkey, $privatekey, $privkeypass);
74: openssl_csr_export($csr, $csrStr);
75: $pubkey=openssl_pkey_get_details($privkey);
76: $pubkey=$pubkey["key"];
77:
78: return array(
79: 'privkey' => $privatekey,
80: 'pubkey' => $publickey,
81: 'csr' => $csrStr,
82: );
83: }
84:
85: /**
86: * <p>Returns assoc array of values used to initiate public-private certificates for this installation.</p>
87: * <p>Default values are:</p>
88: * <ul>
89: <li><strong>countryName</strong> - Default declared country for this installation</li>
90: <li><strong>stateOrProvinceName</strong> - N/A</li>
91: <li><strong>localityName</strong> - N/A</li>
92: <li><strong>organizationName</strong> - N/A</li>
93: <li><strong>organizationalUnitName</strong> - Store base url hosts, comma separated</li>
94: <li><strong>commonName</strong> - Aktsiamaailm LLC</li>
95: <li><strong>emailAddress</strong> - Default general e-mail address for this installation</li>
96: </ul>
97: * @return array
98: */
99: final public function getDefaultKeyData() {
100: $keyInputData = array(
101: "countryName" => $this->_getStringOrDefault($this->_getStoreConfig('general/country/default'), 'US'),
102: "stateOrProvinceName" => 'N/A',
103: "localityName" => 'N/A',
104: "organizationName" => 'N/A',
105: "organizationalUnitName" => $this->_getStringOrDefault(implode(',', $this->_getEabi()->getAllStoreUrls())),
106: "commonName" => 'Aktsiamaailm LLC',
107: "emailAddress" => $this->_getStringOrDefault($this->_getStoreConfig('trans_email/ident_general/email')),
108: );
109:
110: return $keyInputData;
111: }
112:
113: /**
114: * <p>Returns <code>input</code> or <code>default</code> if input is empty of false or not string</p>
115: * @param string $input
116: * @param string $default
117: * @return string
118: */
119: private function _getStringOrDefault($input, $default = 'N/A') {
120: if (!$input || !is_string($input)) {
121: return (string)$default;
122: }
123: return $input;
124: }
125:
126:
127: /**
128: * <p>Fetches config data when Mage::getStoreConfig is not available</p>
129: * @param string $path
130: * @return boolean|string
131: */
132: protected function _getStoreConfig($path) {
133: $storeId = Mage_Core_Model_App::ADMIN_STORE_ID;
134: $configItem = $this->_getCoreConfigDataModel()
135: ->getCollection()
136: ->addFieldToFilter('path', $path)
137: ->addFieldToFilter('scope_id', Mage_Core_Model_App::ADMIN_STORE_ID)
138: ->getFirstItem();
139: if ($configItem) {
140: return $configItem->getValue();
141: }
142: return false;
143: }
144:
145: /**
146: *
147: * @return Eabi_Livehandler_Helper_Data
148: */
149: protected function _getEabi() {
150: return Mage::helper('eabi');
151: }
152:
153: /**
154: *
155: * @return Mage_Core_Model_Config_Data
156: */
157: protected function _getCoreConfigDataModel() {
158: return Mage::getModel('core/config_data');
159: }
160:
161:
162: }
163: