<!-- --- BEGIN COPYRIGHT BLOCK ---
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; version 2 of the License.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License along
     with this program; if not, write to the Free Software Foundation, Inc.,
     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

     Copyright (C) 2007 Red Hat, Inc.
     All rights reserved.
     --- END COPYRIGHT BLOCK --- -->

<style type="text/css">
div#advance
{
  margin: 0px 20px 0px 20px;
  display: none;
}
div#simple
{
  margin: 0px 20px 0px 20px;
  display: block;
}
</style>

<SCRIPT type="text/JavaScript">

var rsalist="${rsalist}";
var ecclist="${ecclist}"; 
var curvelist="${curvelist}";
var displaycurvelist = "${displaycurvelist}";
var rsaTags = "${rsaTags}";
var additionalMessage = "";
if (rsaTags.length > 0) {
  additionalMessage = (rsaTags.indexOf(",") != -1)?
    "<p><b>IMPORTANT:</b> <i>Currently, the Audit Log Signing, Transport, and Storage functionality <b>ONLY</b> support RSA keys.  Users that require ECC keys <b>MUST</b> first select the ECC key type and then verify on the Advanced tab that RSA keys are selected for the Audit Log Signing Certificate, Transport Certificate, and Storage Certificate. All other keys can be ECC.</i>":
    "<p><b>IMPORTANT:</b> <i>Currently, the Audit Log Signing functionality <b>ONLY</b> supports RSA keys.  Users that require ECC keys <b>MUST</b> first select the ECC key type and then verify on the Advanced tab that an RSA key is selected for the Audit Log Signing Certificate. All other keys can be ECC.</i>";
}

function myOnLoad() {
   var form = document.forms[0];
   var keyTypeSelect = form.elements['keytype'];

   setSigningAlgOptions(keyTypeSelect.value, "commontag");
   setAllSigningAlgOptions(keyTypeSelect.value);

   setAlgOptions(keyTypeSelect.value, "commontag");
   setAllAlgOptions(keyTypeSelect.value);
}

function performPanel() {
    with (document.forms[0]) {
        submit();
    }
}

function toggleLayer(whichLayer)
{
  if (document.getElementById) {
    // this is the way the standards work
    var style2 = document.getElementById(whichLayer).style;
    if (style2.display == "block") {
      style2.display = "none";
    } else {
      style2.display = "block";
    }
  }
}

function toggleLayer1(whichLayer)
{
  if (document.getElementById) {
    // this is the way the standards work
    var style2 = document.getElementById(whichLayer).style;
    if (style2.display == "block") {
      style2.display = "none";
    } else if (style2.display == "") {
      style2.display = "none";
    } else {
      style2.display = "block";
    }
  }
}

function toggleOn(whichLayer)
{
  if (document.getElementById) {
    var style2 = document.getElementById(whichLayer).style;
    style2.display = "block";
  }
}

function toggleOff(whichLayer)
{
  if (document.getElementById) {
    var style2 = document.getElementById(whichLayer).style;
    style2.display = "none";
  }
}

function keyAlgorithmChange()
{
  var form = document.forms[0];
  var keyTypeSelect = document.forms[0].elements['keyalgorithm'];
  for (var i = 0; i < form.length; i++) {
    var name = form[i].name;
    if (name.indexOf('_keyalgorithm') != -1) {
      form.elements[name].selectedIndex = keyTypeSelect.selectedIndex;
    }
  }
}

function signingAlgorithmChange()
{
  var form = document.forms[0];
  var keyTypeSelect = document.forms[0].elements['signingalgorithm'];
  for (var i = 0; i < form.length; i++) {
    var name = form[i].name;
    if (name.indexOf('_signingalgorithm') != -1) {
      form.elements[name].selectedIndex = keyTypeSelect.selectedIndex;
    }
  }
}

function setAllAlgOptions(keyType)
{
  var form = document.forms[0];
  for (var i = 0; i < form.length; i++) {
    var name = form[i].name;
    var ind = name.indexOf('_keyalgorithm');
    if (ind != -1) {
      var tag = name.substring(0,ind);
      setAlgOptions(keyType, tag);
    }
  }
}

function setAlgOptions(keyType, certTag)
{
  var algSelect;
  var list;
  if (certTag == "commontag") {
    algSelect = document.forms[0].elements['keyalgorithm'];
  } else {
    algSelect = document.forms[0].elements[certTag + '_keyalgorithm'];
  }
  if (typeof(algSelect) == "undefined") {
    return;
  }
  algSelect.options.length=0;
  if (keyType == "rsa") {
    list = rsalist.split(",");
  } else {
    list = ecclist.split(",");
  }
  for (i=0; i < list.length; i++) {
    algSelect.options[algSelect.options.length] = new Option(list[i], list[i]);
  }
}

function setAllSigningAlgOptions(keyType)
{
  var form = document.forms[0];
  for (var i = 0; i < form.length; i++) {
    var name = form[i].name;
    var ind = name.indexOf('_signingalgorithm');
    if (ind != -1) {
      var tag = name.substring(0,ind);
      setSigningAlgOptions(keyType, tag);
    }
  }
}

function setSigningAlgOptions(keyType, certTag)
{
  var algSelect;
  var list;
  if (certTag == "commontag") {
    algSelect = document.forms[0].elements['signingalgorithm'];
  } else {
    algSelect = document.forms[0].elements[certTag + '_signingalgorithm'];
  }
  if (typeof(algSelect) == "undefined") {
    return;
  }
  algSelect.options.length=0;
  if (keyType == "rsa") {
    list = rsalist.split(",");
  } else {
    list = ecclist.split(",");
  }
  for (i=0; i < list.length; i++) {
    algSelect.options[algSelect.options.length] = new Option(list[i], list[i]);
  }
}

function toggleKeyCurve(keyType, certTag)
{
  if (keyType == "rsa") {
    toggleOn(certTag + '_custom_display_keysize');
    toggleOff(certTag + '_custom_display_curvename');
  } else {
    toggleOff(certTag + '_custom_display_keysize');
    toggleOn(certTag + '_custom_display_curvename');
  }
}

function toggleAllKeyCurves(keyType)
{
  var form = document.forms[0];
  if (keyType == "rsa") {
    toggleOn('simple_keysize');
    toggleOff('simple_curvename');
  } else {
    toggleOn('simple_curvename');
    toggleOff('simple_keysize');
  } 

  for (var i = 0; i < form.length; i++) {
    var name = form[i].name;
    var ind = name.indexOf('_keytype');
    
    if (ind != -1) {
      var tag = name.substring(0,ind);
      if (keyType =="rsa") {
          toggleOff(tag + '_custom_display_curvename');
          toggleOn(tag + '_custom_display_keysize');
      } else {
          toggleOn(tag + '_custom_display_curvename');
          toggleOff(tag + '_custom_display_keysize');
      }
    }
  }
}

function indexOfTag(tag)
{
  var index = rsaTags.indexOf(tag);
  if (index > 0) {
    if (rsaTags.charAt(index-1) != ',') {
      index = -1;
    }
  }
  return index;
}

function keyTypeChange(certTag)
{
  var form = document.forms[0];
  var keyTypeSelect;
  if (certTag == "commontag") {
    keyTypeSelect = document.forms[0].elements['keytype'];
    for (var i = 0; i < form.length; i++) {
      var name = form[i].name;
      var k = name.indexOf('_keytype');
      if (k != -1) {
        var tag = name.substring(0, k);
        if ((keyTypeSelect.value.indexOf('ecc') != -1) &&
            (indexOfTag(tag) == -1)) {
          form.elements[name].selectedIndex = keyTypeSelect.selectedIndex;
          setAlgOptions(keyTypeSelect.value, tag);
          setSigningAlgOptions(keyTypeSelect.value, tag);
          toggleKeyCurve(keyTypeSelect.value, tag);
        }
      }
    }
  } else {
      keyTypeSelect = document.forms[0].elements[certTag + '_keytype'];
      toggleKeyCurve(keyTypeSelect.value, certTag);
  }
  setAlgOptions(keyTypeSelect.value, certTag);
  setSigningAlgOptions(keyTypeSelect.value, certTag);
}

function defaultChange()
{
  var form = document.forms[0];
  var choiceSelect = document.forms[0].elements['choice'];
  for (var i = 0; i < form.length; i++) {
    var name = form[i].name;
    if (name.indexOf('_choice') != -1) {
      for (var j = 0; j < form.elements[name].length; j++) {
        var c = form.elements[name];
        c[j].checked = choiceSelect[j].checked;
      }
    }
  }
}

function customChange()
{
  var form = document.forms[0];
  var choiceSelect = document.forms[0].elements['choice'];
  for (var i = 0; i < form.length; i++) {
    var name = form[i].name;
    if (name.indexOf('_choice') != -1) {
      for (var j = 0; j < form.elements[name].length; j++) {
        var c = form.elements[name];
        c[j].checked = choiceSelect[j].checked;
      }
    }
  }
}

function textChange()
{
  var customSize = document.forms[0].elements['custom_size'];
  var form = document.forms[0];
  for (var i = 0; i < form.length; i++) {
    var name = form[i].name;
    if (name.indexOf('_custom_size') != -1) {
      form.elements[name].value = customSize.value;
    }
  }
}

function matchCurve(curve)
{
  var list = curvelist.split(",");
  for (var i=0; i < list.length; i++) {
    if (list[i] == curve) return true;
  }
  return false;
}

function curveChange()
{
  var customCurve = document.forms[0].elements['custom_curvename'];
  var check = matchCurve(customCurve.value);
  if (check == false) {
    alert("The curve name " + customCurve.value + " is not in the set of allowed curves.\n" +
          "To see a list of allowed curves, click on Details.");
    return;
  }
  var form = document.forms[0];
  for (var i = 0; i < form.length; i++) {
    var name = form[i].name;
    if (name.indexOf('_custom_curvename') != -1) {
      form.elements[name].value = customCurve.value;
    }
  }
}

function displayCurveList()
{
  var list = displaycurvelist.split(",");
  var linelen = 0;
  for (var i=0; i < list.length -1 ; i++) {
    document.write(list[i] + ",");
    linelen = linelen + list[i].length;
    if (linelen >= 60) {
      document.write("<br/>");
      linelen=0;
    }
  }
  document.write(list[list.length -1]);
}
  
</SCRIPT>
Select the key pair type(s), associated key pair size(s) or curve name(s), and signature algorithm(s) from the pulldown menus.  
<SCRIPT type="text/JavaScript">
document.write(additionalMessage);
</SCRIPT>
  <a href="javascript:toggle_details();">[Details]</a>
<SCRIPT type="text/JavaScript">
function toggle_details()
{
  d = document.getElementById('details');
  if (d.style.display == "block") {
    d.style.display="none";
  } else {
    d.style.display="block";
  }
}
</script>
<div id=details style="display: none;">
<br/>
<p>
Each certificate can have its own key pair generated with its own independent settings or common settings can be applied to all key pairs. At minimum, each key pair has to define what <i>type</i> it is by identifying a cipher family and then has to set a <i>strength</i> for that key.
</p>
<ul>
<li>
<b><i>Key Type</i></b>. Sets the cipher family to use to generate the key pair. RSA and ECC key types have slightly different strength options.
</li>
<li>
<b><i>RSA strength: Key Size</i></b>. Sets the key length for the generated pair. Longer keys are stronger, which makes them more secure. 
However, longer key pair sizes also increase the time required to perform operations such as signing certificates, so long keys can affect performance.
</li>
<li>
<b><i>ECC strength: Curve Name</i></b>. Sets the curve algorithm to use, which can be any one of the curves listed below.  The curves that are included in parenthesis are equivalent - and either name can be used.  Note that not all curves may be supported by the token.
<br/><ul style="list-style:none"><li><i>
<SCRIPT type="text/JavaScript">
displayCurveList();
</SCRIPT></i></li></ul>
</li>
<li>
<b><i>Signing Algorithm</i></b>. <i>Signing certificates only.</i> Sets the signing algorithm which will be used to sign objects issued by the subsystem. This is only displayed for certificates which are used for object signing, such as the CA signing certificate or the OCSP signing certificate.
</li>
<li><b><i>Signed With</i></b>. <i>Root CAs only.</i> Sets the signing algorithm used to sign the CA signing certificate itself.
</li>
</ul>
<br/>
</div>
#if ($select == "clone")
For a cloned subsystem, only the key for an SSL server certificate is generated.
#end
<br/>
#if ($errorString != "")
<img src="/pki/images/icon_crit_update.gif">&nbsp;<font color="red">$errorString</font>
#end
<br/>
<div id="simple">
<br/>
<table width=100%>
<tr>
  <td align=right><a href="javascript:toggleLayer1('simple'); toggleLayer('advance');" title="Advanced">[Advanced]</a></td>
</tr>
</table>
<br/>
<H2>Common Key Settings</H2>
<br/>
<table width=100% class="details">
      <tr>
        <th width="30%">Key Type:</th>
        <td><select name="keytype" onChange="keyTypeChange('commontag')"><option value="rsa">RSA</option><option value="ecc">ECC</option></select></td>
      </tr>
</table>

#if ($subsystemtype == "ca")
#if ($hselect == "root")
<table width=100% class="details">
      <tr> 
        <th width="30%">Signed With:</th>
        <td><select name="keyalgorithm" onChange="keyAlgorithmChange()">
            </select></td>
      </tr>
</table>
#end
#end

#if ($show_signing == "true")
<table width=100% class="details">
      <tr>
        <th width="30%">Signing Algorithm:</th>
        <td><select name="signingalgorithm" onChange="signingAlgorithmChange()">
            </select></td>
      </tr>
</table>
#end

<div id="simple_keysize"> 
<p>

    <input checked onChange="defaultChange()" type=radio name="choice" value="default"><b>Use the default key size ($default_keysize bits)</b>.
    <p>
    <input onChange="customChange()" type=radio name="choice" value="custom"><b>Use the following custom key size:</b>
                                                                                
    <p>
<table width=100% class="details">
      <tr>
        <th>Key Size:</th>
        <td><input onChange="textChange()" type="text" size="20" name="custom_size" value="2048" /></td>
      </tr>
</table>

</div>

<div id="simple_curvename" style="display: none;">
<p>

    <input checked onChange="defaultChange()" type=radio name="choice" value="default"><b>Use the default curve ($default_ecc_curvename)</b>.
    <p>
    <input onChange="customChange()" type=radio name="choice" value="custom"><b>Use the following curve:</b>

    <p>
<table width=100% class="details">
      <tr>
        <th width=30%>Curve Name:</th>
        <td><input onChange="curveChange()" type="text" size="20" name="custom_curvename" value="$default_ecc_curvename" /></td>
      </tr>
</table>

</div>

<!-- to be used when we can do a google-style horizontal combo-box
<div id="simple_curvename" style="display: none;">

<table width=100% class="details">
      <tr>
        <th width="30%">Curve Name:</th>
        <td><select name="custom_curvename">
                #set ($x=0)
                #foreach ($p in ${curvelist})
                    <option value="$x">$p</option>
                    #set ($x=$x+1)
                #end
            </select></td>
      </tr>
</table>
</div>
-->

</div>
<p>

<div id="advance" style="display: none;">
<p>
<table width=100%>
<tr>
  <td align=right><a href="javascript:toggleLayer1('simple');toggleLayer('advance');" title="Simple">[Simple]</a></td>
</tr>
</table>
#foreach ($item in $certs)
<H2>Key for $item.getUserFriendlyName()</H2>
<p>
<table width=100% class="details">
      <tr>
        <th width="30%">Key Type:</th>
#if ($item.isEnable())
        <td><select name="$item.getCertTag()_keytype" onChange="keyTypeChange('$item.getCertTag()')"><option value=rsa>RSA</option><option value="ecc">ECC</option></select></td>
#else
        <td><select name="$item.getCertTag()_keytype" disabled="disabled" onChange="keyTypeChange('$item.getCertTag()')"><option value=rsa>RSA</option><option value="ecc">ECC</option></select></td>
#end
      </tr>
</table>

#if ($subsystemtype == "ca")
#if ($hselect == "root")
#if ($item.getCertTag() == "signing")
<table width=100% class="details">
      <tr>
        <th width="30%">Signed With:</th>
        <td><select name="$item.getCertTag()_keyalgorithm">
            </select></td>
      </tr>
</table>
#end
#end
#end

#if ($item.isSigningRequired())
<table width=100% class="details">
      <tr>
        <th width="30%">Signing Algorithm:</th>
        <td><select name="$item.getCertTag()_signingalgorithm">
            </select></td>
      </tr>
</table>
#end

<div id="$item.getCertTag()_custom_display_keysize">
<br/>
#if ($item.isEnable())
    <input
#if ($item.useDefaultKey())
 checked
#end
 type=radio name=$item.getCertTag()_choice value="default"><b>Use the default key size ($default_keysize bits).
#else
    <input
#if ($item.useDefaultKey())
 checked
#end
 type=radio name=$item.getCertTag()_choice value="default" disabled="disabled"><b>Use the default key size ($default_keysize bits).
#end
    <br/>
#if ($item.isEnable())
    <input
#if (!$item.useDefaultKey())
 checked
#end
 type=radio name=$item.getCertTag()_choice value="custom"><b>Use the following custom key size:</b>
#else
    <input
#if (!$item.useDefaultKey())
 checked
#end
 type=radio name=$item.getCertTag()_choice value="custom" disabled="disabled"><b>Use the following custom key size:</b>
#end
                                                                                
    <br/>
<table width=100% class="details">
      <tr>
        <th>Key Size:</th>
#if ($item.isEnable())
        <td><input type="text" size="20" name=$item.getCertTag()_custom_size value=$item.getCustomKeysize() /></td>
#else
        <td><input type="text" size="20" name=$item.getCertTag()_custom_size value=$item.getCustomKeysize() disabled="disabled"/></td>
#end
      </tr>
</table>
</div>

<div id="$item.getCertTag()_custom_display_curvename" style="display: none;">
<br/>
#if ($item.isEnable())
    <input
#if ($item.useDefaultKey())
 checked
#end
 type=radio name=$item.getCertTag()_choice value="default"><b>Use the default curve ($default_ecc_curvename).
#else
    <input
#if ($item.useDefaultKey())
 checked
#end
 type=radio name=$item.getCertTag()_choice value="default" disabled="disabled"><b>Use the default curve ($default_ecc_curvename).
#end
    <br/>
#if ($item.isEnable())
    <input
#if (!$item.useDefaultKey())
 checked
#end
 type=radio name=$item.getCertTag()_choice value="custom"><b>Use the following curve:</b>
#else
    <input
#if (!$item.useDefaultKey())
 checked
#end
 type=radio name=$item.getCertTag()_choice value="custom" disabled="disabled"><b>Use the following curve:</b>
#end

    <br/>
<table width=100% class="details">
      <tr>
        <th width=30%>Curve Name:</th>
#if ($item.isEnable())
        <td><input type="text" size="20" name=$item.getCertTag()_custom_curvename value=$item.getCustomCurvename() /></td>
#else
        <td><input type="text" size="20" name=$item.getCertTag()_custom_curvename value=$item.getCustomCurvename() disabled="disabled"/></td>
#end
      </tr>
</table>
</div>

<!-- to be used when we can do a google-style combo-box
<div id="$item.getCertTag()_custom_display_curvename" style="display: none;">
<table width=100% class="details">
      <tr>
        <th width="30%">Curve Name:</th>
#if ($item.isEnable())
        <td><select name=$item.getCertTag()_custom_curvename value=$item.getCustomCurvename()>
                #set ($x=0)
                #foreach ($p in $curvelist)
                    <option value="$x">$p</option>
                    #set ($x=$x+1)
                #end
            </select>
        </td>
#else
        <td><select name=$item.getCertTag()_custom_curvename value=$item.getCustomCurvename() disabled="disabled">
                #set ($x=0)
                #foreach ($p in $curvelist)
                    <option value="$x">$p</option>
                    #set ($x=$x+1)
                #end
            </select>
        </td>
#end
      </tr>
</table>
</div>
-->

#end
</div>
<br/>
<br/>
<br/>
#if ($firsttime == 'false')
<input type="CHECKBOX" NAME="generateKeyPair">New Keys<p>
#end
<br/>
    <div align="right">
      <hr />
<i>Note: After pressing Next, keys will be generated on the server, which will take some time to complete.  Please wait for the next panel to appear.</i>
      &nbsp;
    </div>
