/* Copyright (c) 2007, Thomas Fors * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the copyright holder nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #include #include #include "ppp.h" #include "http.h" #define SERVER "webserver/1.0" #define PROTOCOL "HTTP/1.0" #define RFC1123FMT "%a, %d %b %Y %H:%M:%S GMT" static char secretPath[128] = ""; static const char *codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; int _base64_encode_path(const unsigned char *in, char *out) { unsigned long i, leven; char *p; if (in == NULL) return -1; if (out == NULL) return -1; p = out; leven = 3*(32 / 3); for (i = 0; i < leven; i += 3) { *p++ = codes[(in[0] >> 2) & 0x3F]; *p++ = codes[(((in[0] & 3) << 4) + (in[1] >> 4)) & 0x3F]; *p++ = codes[(((in[1] & 0xf) << 2) + (in[2] >> 6)) & 0x3F]; *p++ = codes[in[2] & 0x3F]; in += 3; } /* Pad it if necessary... */ if (i < 32) { unsigned a = in[0]; unsigned b = (i+1 < 32) ? in[1] : 0; *p++ = codes[(a >> 2) & 0x3F]; *p++ = codes[(((a & 3) << 4) + (b >> 4)) & 0x3F]; *p++ = (i+1 < 32) ? codes[(((b & 0xf) << 2)) & 0x3F] : '='; *p++ = '='; } /* append a NULL byte */ *p = '\0'; /* return ok */ return 0; } static char * _create_obfuscated_path() { int i; uuid_t uuid; unsigned char entropy[32]; unsigned char bytes[32]; uuid_generate_random(uuid); for (i=0; i<16; i++) { entropy[i] = uuid[i]; } uuid_generate_time(uuid); for (i=0; i<16; i++) { entropy[i+16] = uuid[i]; } sha256(entropy, 32, bytes); _base64_encode_path(bytes, secretPath); return secretPath; } void htmlStart(FILE *f) { fprintf(f, "\n"); fprintf(f, " Perfect Paper Passwords \n"); fprintf(f, "\n"); fprintf(f, "
\n"); } void htmlEnd(FILE *f) { fprintf(f, "
\n"); fprintf(f, "
\n"); fprintf(f, "Print this page (in portrait orientation if you would like all three cards
to fit) then either cut out and separate the individual passcards, or
keep the three attached and fold down into one-card size.


Please refer to GRC's Perfect Paper Passwords pages
for information about the operation of this system.
\n"); fprintf(f, "\n"); } void htmlCard(FILE *f, mp_int *nCard) { char groupChar = ','; mp_int start; mp_init(&start); calculatePasscodeNumberFromCardColRow(nCard, 0, 0, &start); char buf[70*4]; getPasscodeBlock(&start, 70, buf); mp_clear(&start); char hname[39]; strncpy(hname, hostname(), 38); mp_int n; mp_init(&n); mp_add_d(nCard, 1, &n); char *cardnumber = mpToDecimalString(&n, groupChar); char *cn = cardnumber; mp_clear(&n); if (strlen(hname) + strlen(cardnumber) + 3 > 38) { if (strlen(hname) > 27) { hname[27] = '\x00'; } int ellipses = strlen(cardnumber) - (38 - strlen(hname) - 3); if (ellipses > 0) { cn = cardnumber+ellipses; cn[0] = cn[1] = cn[2] = '.'; /* When truncating the card number, make sure we don't * begin with a comma after the ellipses */ if (cn[3] == groupChar) { cn[3] = '.'; cn++; } } } fprintf(f, "
\n"); fprintf(f, "
"); fprintf(f, "%s", hname); int j; for (j=0; j<38-strlen(hname)-strlen(cn)-2; j++) fprintf(f, " "); fprintf(f, "[%s]
\n", cn); fprintf(f, "
\n"); fprintf(f, "
"); fprintf(f, "    A    B    C    D    E    F    G\n"); fprintf(f, "
\n"); j = 0; int r, c; for (r=1; r<=10; r++) { if (r < 10) fprintf(f, " "); fprintf(f, "%d: ", r); for (c=0; c<7; c++) { if (c) fprintf(f, " "); fprintf(f, "%c%c%c%c", buf[j*4+0], buf[j*4+1], buf[j*4+2], buf[j*4+3]); j++; } fprintf(f, "
\n"); } fprintf(f, "
\n"); fprintf(f, "
\n"); fprintf(f, "\n"); /* zero passcodes from memory */ memset(buf, 0, 70*4); } void httpSendHeaders(FILE *f, int status, char *title, char *extra, char *mime, int length, time_t date) { time_t now; char timebuf[128]; fprintf(f, "%s %d %s\r\n", PROTOCOL, status, title); fprintf(f, "Server: %s\r\n", SERVER); now = time(NULL); strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&now)); fprintf(f, "Date: %s\r\n", timebuf); if (extra) fprintf(f, "%s\r\n", extra); if (mime) fprintf(f, "Content-Type: %s\r\n", mime); if (length >= 0) fprintf(f, "Content-Length: %d\r\n", length); if (date != -1) { strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&date)); fprintf(f, "Last-Modified: %s\r\n", timebuf); } fprintf(f, "Connection: close\r\n"); fprintf(f, "\r\n"); } void httpSendError(FILE *f, int status, char *title, char *extra, char *text) { httpSendHeaders(f, status, title, extra, "text/html", -1, -1); fprintf(f, "%d %s\r\n", status, title); fprintf(f, "

%d %s

\r\n", status, title); fprintf(f, "%s\r\n", text); fprintf(f, "\r\n"); } int httpProcess(FILE *f) { int rv = 1; char buf[4096]; char *method; char *path; char *protocol; if (!fgets(buf, sizeof(buf), f)) return -1; if (fVerbose) { printf("[HTTP] %s\n", buf); } method = strtok(buf, " "); path = strtok(NULL, " "); protocol = strtok(NULL, "\r"); if (!method || !path || !protocol) return -1; fseek(f, 0, SEEK_CUR); // Force change of stream direction if (strcasecmp(method, "GET") != 0) { httpSendError(f, 501, "Not supported", NULL, "Method is not supported."); } if (strlen(path) != strlen(secretPath)+1) { httpSendError(f, 404, "Not Found", NULL, "File not found."); } if (strncmp(path+1, secretPath, 64) == 0) { httpSendHeaders(f, 200, "OK", NULL, "text/html", /* length */ -1, /* statbuf->st_mtime */ -1); if (fNext) { int i; htmlStart(f); for (i=0; i= 0) { f = fdopen(s, "a+"); httpProcess(f); fflush(f); fclose(f); } close(sock); }