[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/se3master/var/www/se3/ -> setup_keys.php (source)

   1  <?php
   2  
   3     /**
   4     
   5     * Gestion de la cle public pour l'authentification  
   6     * @Version $Id: setup_keys.php 2927 2008-05-03 17:32:05Z plouf $ 
   7     
   8     * @Projet LCS / SambaEdu 
   9     
  10     * @auteurs JLC Jean Luc Chertien (Caen) 
  11  
  12     * @Licence Distribue selon les termes de la licence GPL
  13     
  14     * @note 
  15     
  16     */
  17  
  18     /**
  19  
  20     * @Repertoire: /
  21     * file: setup_keys.php
  22     */
  23  
  24  
  25  include "config.inc.php";
  26  include "functions.inc.php";
  27  include  "ldap.inc.php";
  28  include  "ihm.inc.php";
  29  
  30  require_once ("lang.inc.php");
  31  bindtextdomain('se3-core',"/var/www/se3/locale");
  32  textdomain ('se3-core');
  33  
  34  
  35  $login=isauth();
  36  if ($login == "") header("Location:$urlauth");
  37  
  38  // Calcul du random seed
  39  $allow = "abcdef0123456789";
  40  srand((double)microtime()*1000000);
  41  for($j=0; $j<2; $j++) {
  42          $RandomSeed .=  $allow[rand()%strlen($allow)];
  43  }
  44  for  ($i=0; $i<1023; $i++) {
  45          $tmp="";
  46          for($j=0; $j<2; $j++) {
  47                  $tmp .= $allow[rand()%strlen($allow)];
  48          }
  49          $RandomSeed.=" ".$tmp." ";
  50  }
  51  
  52  
  53  ?>
  54                  <script language = 'javascript' type = 'text/javascript' src="crypto.js"></script>
  55                  <script language = 'javascript' type = 'text/javascript' src="public_key.js"></script>
  56                  <script language = 'javascript' type = 'text/javascript'>
  57                  <!--
  58  // seed the random number generator with entropy in s
  59  function seed(s) {
  60   rSeed=[];
  61   var n=0,nn=0;
  62   while(n < s.length) {
  63    while(n<s.length && s.charCodeAt(n)<=32) n++;
  64    if(n < s.length) rSeed[nn]=parseInt("0x"+s.substr(n,2));
  65    n+=3; nn++;
  66   }
  67  
  68   var x, y, t;
  69   Rs=[];
  70   Rsl=rSeed.length;
  71   Sr= r(256)
  72   Rbits=0
  73  
  74   if(Rs.lengh==0) {for (x=0; x<256; x++) Rs[x]=x;}
  75   y=0
  76   for (x=0; x<256; x++) {
  77    y=(rSeed[x] + s[x] + y) % 256
  78    t=s[x]; s[x]=s[y]; s[y]=t
  79   }
  80   Rx=Ry=0;
  81  //alert("Random seed updated. Seed size is: "+Rsl);
  82  }
  83  // generate a random number 0 .. 255
  84  // uses entropy from seed
  85  function rc() {
  86    // this first bit is basically RC4
  87    Rx=++Rx & 255;
  88    Ry=( Rs[Rx] + Ry) & 255;
  89    var t=Rs[Rx]; Rs[Rx]=Rs[Ry]; Rs[Ry]=t;
  90    Sr^= Rs[(Rs[Rx] + Rs[Ry]) & 255];
  91  
  92    // xor with javascripts rand, just in case there's good entropy there
  93    Sr^= r(256);
  94  
  95    Sr^= ror(rSeed[r(Rsl)],r(8));
  96    Sr^= ror(rSeed[r(Rsl)],r(8));
  97    return Sr;
  98  }
  99  // javascript's random 0 .. n
 100  function r(n) {return  Math.floor(Math.random()*n);}
 101  // rotate right
 102  //function ror(a,b) {return b?((a<<b)|(a>>(8-b))&255):a;}
 103  // random number between 0 .. n -- based on repeated calls to rc
 104  function rand(n) {
 105   if(n==2) {
 106    if(! Rbits) {
 107     Rbits=8;
 108     Rbits2=rc(256);
 109    }
 110    Rbits--;
 111    var r=Rbits2 & 1;
 112    Rbits2>>=1;
 113    return r;
 114   }
 115   var m=1, r=0;
 116   while (n>m && m > 0) {
 117    m<<=8; r=(r<<8) |rc();
 118   }
 119   if(r<0) r ^= 0x80000000;
 120   return r % n;
 121  }
 122  
 123  tstval=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
 124  
 125  // functions for generating keys-----------------------------
 126  function bgcd(uu,vv) { // return greatest common divisor
 127   // algorythm from http://algo.inria.fr/banderier/Seminar/Vallee/index.html
 128   var d, t, v=vv.concat(), u=uu.concat()
 129   for(;;) {
 130    d=bsub(v,u)
 131    if(beq(d,[0])) return u
 132    if(d.length) {
 133     while((d[0] & 1) ==0)
 134      d=brshift(d).a // v=(v-u)/2^val2(v-u)
 135     v=d
 136    } else {
 137     t=v; v=u; u=t // swap u and v
 138    }
 139   }
 140  }
 141  
 142  function rnum(bits) {
 143   var n,b=1,c=0
 144   var a=[]
 145   if(bits==0) bits=1
 146   for(n=bits; n>0; n--) {
 147    if(rand(2)) {
 148     a[c]|=b
 149    }
 150    b<<=1
 151    if(b==bx2) {
 152     b=1; c++
 153    }
 154   }
 155   return a
 156  }
 157  
 158  // function to generate keys
 159  function genkey(bits,f) {
 160   bits=parseInt(bits)*8
 161   var q,p,p1q1,n,factorMe,d,e,r
 162   var c,cc,ccc,pq
 163   q=mpp(bits); p=mpp(bits)
 164   f.p.value=p; f.q.value=q
 165   p1q1=bmul(bsub(p,[1]),bsub(q,[1]))
 166   for(c=5; c<Primes.length; c++) {
 167    e=[Primes[c]]
 168    d=modinverse(e,p1q1)
 169    if(d.length != 1 || d[0]!=0) break
 170   }
 171   f.d.value=d; f.e.value=e; f.pq.value=(pq=bmul(p,q))
 172   // test
 173   c=bmod(tstval,pq)
 174   cc=bmodexp(c,e,pq)
 175   ccc=crt_RSA(cc,d,p,q)
 176   return
 177  }
 178  
 179  function parseArray(a) {
 180   a=a.split(",")
 181   for(var n=0; n<a.length; n++) {
 182    a[n]=parseInt(a[n])
 183   }
 184   return a
 185  }
 186  
 187  function enc(f) {
 188   f.text.value=rsaEncode(parseArray(f.e.value),parseArray(f.pq.value),f.text.value)
 189  }
 190  function dec(f) {
 191   f.text.value=rsaDecode([parseArray(f.d.value),
 192    parseArray(f.p.value),
 193    parseArray(f.q.value)],
 194    f.text.value)
 195  }
 196  
 197  function encrypt(f) {
 198          encode = f.p.value+"|"+f.q.value+"|"+f.pq.value+"|"+f.d.value+"|"+f.e.value;
 199          f.keys.value=rsaEncode(public_key_e,public_key_pq,encode);
 200          // Reset des valeurs des cles pour ne pas les transmettre en clair
 201          f.p.value="";
 202          f.q.value="";
 203          f.pq.value="";
 204          f.d.value="";
 205          f.e.value="";
 206  }
 207  
 208  Primes=[3, 5, 7, 11, 13, 17, 19,
 209      23, 29, 31, 37, 41, 43, 47, 53,
 210      59, 61, 67, 71, 73, 79, 83, 89,
 211      97, 101, 103, 107, 109, 113, 127, 131,
 212      137, 139, 149, 151, 157, 163, 167, 173,
 213      179, 181, 191, 193, 197, 199, 211, 223,
 214      227, 229, 233, 239, 241, 251, 257, 263,
 215      269, 271, 277, 281, 283, 293, 307, 311,
 216      313, 317, 331, 337, 347, 349, 353, 359,
 217      367, 373, 379, 383, 389, 397, 401, 409,
 218      419, 421, 431, 433, 439, 443, 449, 457,
 219      461, 463, 467, 479, 487, 491, 499, 503,
 220      509, 521, 523, 541, 547, 557, 563, 569,
 221      571, 577, 587, 593, 599, 601, 607, 613,
 222      617, 619, 631, 641, 643, 647, 653, 659,
 223      661, 673, 677, 683, 691, 701, 709, 719,
 224      727, 733, 739, 743, 751, 757, 761, 769,
 225      773, 787, 797, 809, 811, 821, 823, 827,
 226      829, 839, 853, 857, 859, 863, 877, 881,
 227      883, 887, 907, 911, 919, 929, 937, 941,
 228      947, 953, 967, 971, 977, 983, 991, 997,
 229      1009, 1013, 1019, 1021]
 230  
 231  
 232  sieveSize=4000
 233  sieve0=-1* sieveSize
 234  sieve=[]
 235  
 236  lastPrime=0
 237  function nextPrime(p) { // returns the next prime > p
 238   var n
 239   if(p == Primes[lastPrime] && lastPrime <Primes.length-1) {
 240    return Primes[++lastPrime]
 241   }
 242   if(p<Primes[Primes.length-1]) {
 243    for(n=Primes.length-2; n>0; n--) {
 244     if(Primes[n] <= p) {
 245      lastPrime=n+1
 246      return Primes[n+1]
 247     }
 248    }
 249   }
 250   // use sieve and find the next one
 251   var pp,m
 252   // start with p
 253   p++; if((p & 1)==0) p++
 254   for(;;) {
 255    // new sieve if p is outside of this sieve's range
 256    if(p-sieve0 > sieveSize || p < sieve0) {
 257     // start new sieve
 258     for(n=sieveSize-1; n>=0; n--) sieve[n]=0
 259     sieve0=p
 260     primes=Primes.concat()
 261    }
 262  
 263    // next p if sieve hit
 264    if(sieve[p-sieve0]==0) { // sieve miss
 265  
 266     // update sieve if p divisable
 267  
 268     // find a prime divisor
 269     for(n=0; n<primes.length; n++) {
 270      if((pp=primes[n]) && p % pp ==0) {
 271       for(m=p-sieve0; m<sieveSize; m+=pp) sieve[m]=pp
 272       p+=2;
 273       primes[n]=0
 274       break
 275      }
 276     }
 277     if(n >= primes.length) {
 278      // possible prime
 279      return p
 280     }
 281    } else {
 282      p+=2;
 283    }
 284   }
 285  
 286  }
 287  
 288  function divisable(n,max) { //return a factor if n is divisable by a prime less than max, else return 0
 289   if((n[0] & 1) == 0) return 2
 290   for(c=0; c<Primes.length; c++) {
 291    if(Primes[c] >= max) return 0
 292    if(simplemod(n,Primes[c])==0)
 293     return Primes[c]
 294   }
 295   c=Primes[Primes.length-1]
 296   for(;;) {
 297    c=nextPrime(c)
 298    if(c >= max) return 0
 299    if(simplemod(n,c)==0)
 300     return c
 301   }
 302  }
 303  
 304  function bPowOf2(pow) { // return a bigint
 305   var r=[], n, m=Math.floor(pow/bs)
 306   for(n=m-1; n>=0; n--) r[n]=0;
 307   r[m]= 1<<(pow % bs)
 308   return r
 309  }
 310  
 311  function mpp(bits) { //returns a Maurer Provable Prime, see HAC chap 4 (c) CRC press
 312   if(bits < 10) return [Primes[rand(Primes.length)]]
 313   if(bits <=20) return [nextPrime(rand(1<<bits))]
 314   var c=10, m=20, B=bits*bits/c, r, q, I, R, n, a, b, d, R2, nMinus1
 315   if(bits > m*2) {
 316    for(;;) {
 317     r=Math.pow(2,Math.random()-1)
 318     if(bits - r * bits > m) break
 319    }
 320   } else {
 321    r=0.5
 322   }
 323   q=mpp(Math.floor(r*bits)+1)
 324   I=bPowOf2(bits-2)
 325   I=bdiv(I,q).q
 326   Il=I.length
 327   for(;;) {
 328    // generate R => I < R < 2I
 329    R=[]; for(n=0; n<Il; n++) R[n]=rand(bx2);
 330    R[Il-1] %= I[Il-1]; R=bmod(R,I);
 331    if(! R[0]) R[0]|=1 // must be greater or equal to 1
 332    R=badd(R,I)
 333    n=blshift(bmul(R,q),1) // 2Rq+1
 334    n[0]|=1
 335    if(!divisable(n,B)) {
 336     a=rnum(bits-1)
 337     a[0]|=2 // must be greater than 2
 338     nMinus1=bsub(n,[1])
 339     var x=bmodexp(a,nMinus1,n)
 340     if(beq(x,[1])) {
 341      R2=blshift(R,1)
 342      b=bsub(bmodexp(a,R2,n),[1])
 343      d=bgcd(b,n)
 344      if(beq(d,[1])) return n
 345     }
 346    }
 347   }
 348  }
 349  
 350  seed('<?php echo $RandomSeed
 351            ?>');
 352  // -->
 353  </script>
 354  <?php
 355  
 356  include  ("includes/entete.inc.php");
 357  
 358  //aide 
 359  $_SESSION["pageaide"]="L\'interface_web_administrateur#Partie_:_Param.C3.A9trage_de_l.27interface_SambaEdu.";
 360  
 361  if (is_admin("Annu_is_admin",$login)=="Y") {
 362  ?>
 363  <h1><?php echo gettext("G&#233;n&#233;ration d'un nouveau jeu de cl&#233;s d'authentification"); ?></h1>
 364  <p>
 365  <form name="t" action="save_keys.php" method="post">
 366  <p>Longueur de la cl&#233; en octets : <input type=text name=keylen size=3 value=8>&nbsp;&nbsp;
 367  <input type="hidden" name="keygen" value="true">
 368  <input type="button" value="G&#233;n&#233;ration des cl&#233;s" onClick="genkey(document.t.keylen.value,document.t)"><br>
 369  <hr width="80%">
 370          prime factor p : <input type=text name=p value="<?php echo $p ?>" size=30><br>
 371          prime factor q : <input type=text name=q value="<?php echo $q ?>" size=30><br>
 372          Public Modulo (p*q): <input type=text name=pq value="<?php echo $pq ?>" size=50><br>
 373          Private exponent (d): <input type=text name=d  value="<?php echo $d ?>" size=50 ><br>
 374          Public exponent (e): <input type=text name=e value="<?php echo $e ?>" size=5><br>
 375  <hr width="80%">
 376  <div align='center'>
 377          <h2><?php echo gettext("V&#233;rification des cl&#233;s"); ?></h2>
 378          <input name='text' rows='8', cols='50' value='Texte &#224; crypter'>
 379          <input type='button' value='Cryptage' onClick='enc(document.t)'>
 380          <input type='button' value='D&#233;cryptage' onClick='dec(document.t)'>
 381          <input type='hidden' name='keys' value=''>
 382          <p><input type='submit' value='<?php echo gettext("V&#233;rification des cl&#233;s OK ? Sauvegarde des cl&#233;s !"); ?>' onClick='encrypt(document.t)'>
 383  </div>
 384  <?php
 385  } else {
 386          echo "<div class=alert_msg>".gettext("Cette fonctionnalit&#233;, n&#233;cessite les droits d'administrateur du serveur LCS !")."</div>";
 387  }
 388  
 389  include  ("includes/pdp.inc.php");
 390  ?>
 391  
 392  
 393  
 394  


Generated: Tue Mar 17 22:47:18 2015 Cross-referenced by PHPXref 0.7.1