certcgi.c

来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· C语言 代码 · 共 2,362 行 · 第 1/5 页

C
2,362
字号
/* * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ *  * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. *  * The Original Code is the Netscape security libraries. *  * The Initial Developer of the Original Code is Netscape * Communications Corporation.  Portions created by Netscape are  * Copyright (C) 1994-2000 Netscape Communications Corporation.  All * Rights Reserved. *  * Contributor(s): *  * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL"), in which case the provisions of the GPL are applicable  * instead of those above.  If you wish to allow use of your  * version of this file only under the terms of the GPL and not to * allow others to use your version of this file under the MPL, * indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by * the GPL.  If you do not delete the provisions above, a recipient * may use your version of this file under either the MPL or the * GPL. *//* Cert-O-Matic CGI */#include "nspr.h"#include "prtypes.h"#include "prtime.h"#include "prlong.h"#include "pk11func.h"#include "cert.h"#include "cdbhdl.h"#include "cryptohi.h"#include "secoid.h"#include "secder.h"#include "genname.h"#include "xconst.h"#include "secutil.h"#include "pqgutil.h"#include "certxutl.h"#include "secrng.h"	/* for RNG_ *//* #define TEST           1 *//* #define FILEOUT        1 *//* #define OFFLINE        1 */#define START_FIELDS   100#define PREFIX_LEN     6#define SERIAL_FILE    "../serial"#define DB_DIRECTORY   ".."typedef struct PairStr Pair;struct PairStr {    char *name;    char *data;};char prefix[PREFIX_LEN];const SEC_ASN1Template CERTIA5TypeTemplate[] = {    { SEC_ASN1_IA5_STRING }};SECKEYPrivateKey *privkeys[9] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL,				 NULL, NULL};#ifdef notdefconst SEC_ASN1Template CERT_GeneralNameTemplate[] = {    { SEC_ASN1_SEQUENCE_OF, 0, SEC_AnyTemplate }};#endifstatic voiderror_out(char  *error_string){    printf("Content-type: text/plain\n\n");    printf(error_string);    fflush(stderr);    fflush(stdout);    exit(1);}static voiderror_allocate(void){    error_out("ERROR: Unable to allocate memory");}static char *make_copy_string(char  *read_pos, 		 int   length, 		 char  sentinal_value)    /* copys string from to a new string it creates and        returns a pointer to the new string */{    int                remaining = length;    char               *write_pos;    char               *new;    new = write_pos = (char *) PORT_Alloc (length);    if (new == NULL) {	error_allocate();    }    while (*read_pos != sentinal_value) {	if (remaining == 1) {	    remaining += length;	    length = length * 2;	    new = PORT_Realloc(new,length);	    if (new == NULL) {		error_allocate();	    }	    write_pos = new + length - remaining;	}	*write_pos = *read_pos;	++write_pos;	++read_pos;	remaining = remaining - 1;    }    *write_pos = '\0';    return new;}static char *PasswordStub(PK11SlotInfo  *slot, 	     void          *cx){	return NULL;}static SECStatusclean_input(Pair *data)    /* converts the non-alphanumeric characters in a form post        from hex codes back to characters */{    int           length;    int           hi_digit;    int           low_digit;    char          character;    char          *begin_pos;    char          *read_pos;    char          *write_pos;    PRBool        name = PR_TRUE;    begin_pos = data->name;    while (begin_pos != NULL) {	length = strlen(begin_pos);	read_pos = write_pos = begin_pos;	while ((read_pos - begin_pos) < length) {	    if (*read_pos == '+') {		*read_pos = ' ';	    }	    if (*read_pos == '%') {		hi_digit = *(read_pos + 1);		low_digit = *(read_pos +2);		read_pos += 3;		if (isdigit(hi_digit)){		    hi_digit = hi_digit - '0';		} else {		    hi_digit = toupper(hi_digit);		    if (isxdigit(hi_digit)) {			hi_digit = (hi_digit - 'A') + 10;		    } else {			error_out("ERROR: Form data incorrectly formated");		    }		}		if (isdigit(low_digit)){		    low_digit = low_digit - '0';		} else {		    low_digit = toupper(low_digit);		    if ((low_digit >='A') && (low_digit <= 'F')) {			low_digit = (low_digit - 'A') + 10;		    } else {			error_out("ERROR: Form data incorrectly formated");		    }		}		character = (hi_digit << 4) | low_digit;		if (character != 10) {		    *write_pos = character;		    ++write_pos;		}	    } else {		*write_pos = *read_pos;		++write_pos;		++read_pos;	    }	}	*write_pos = '\0';	if (name == PR_TRUE) {	    begin_pos = data->data;	    name = PR_FALSE;	} else {	    data++;	    begin_pos = data->name;	    name = PR_TRUE;	}    }    return SECSuccess;}static char *make_name(char  *new_data)    /* gets the next field name in the input string and returns       a pointer to a string containing a copy of it */{    int         length = 20;    char        *name;    name = make_copy_string(new_data, length, '=');    return name;}	static char *make_data(char  *new_data)    /* gets the data for the next field in the input string        and returns a pointer to a string containing it */{    int         length = 100;    char        *data;    char        *read_pos;    read_pos = new_data;    while (*(read_pos - 1) != '=') {	++read_pos;    }    data = make_copy_string(read_pos, length, '&');    return data;}static Pairmake_pair(char  *new_data)    /* makes a pair name/data pair from the input string */{    Pair        temp;    temp.name = make_name(new_data);    temp.data = make_data(new_data);    return temp;}static Pair *make_datastruct(char  *data, int len)    /* parses the input from the form post into a data        structure of field name/data pairs */{    Pair              *datastruct;    Pair              *current;    char              *curr_pos;    int               fields = START_FIELDS;    int               remaining = START_FIELDS;    curr_pos = data;    datastruct = current = (Pair *) PORT_Alloc(fields * sizeof(Pair));    if (datastruct == NULL) {	error_allocate();    }    while (curr_pos - data < len) {	if (remaining == 1) {	    remaining += fields;	    fields = fields * 2;	    datastruct = (Pair *) PORT_Realloc		(datastruct, fields * sizeof(Pair));	    if (datastruct == NULL) {		error_allocate;	    }	    current = datastruct + (fields - remaining);	}	*current = make_pair(curr_pos);	while (*curr_pos != '&') {	    ++curr_pos;	}	++curr_pos;	++current;	remaining = remaining - 1;    }    current->name = NULL;    return datastruct;}static char *return_name(Pair  *data_struct,	    int   n)    /* returns a pointer to the name of the nth        (starting from 0) item in the data structure */{    char          *name;    if ((data_struct + n)->name != NULL) {	name = (data_struct + n)->name;	return name;    } else {	return NULL;    }}static char *return_data(Pair  *data_struct,int n)    /* returns a pointer to the data of the nth (starting from 0)        itme in the data structure */{    char          *data;    data = (data_struct + n)->data;    return data;}static char *add_prefix(char  *field_name){    extern char  prefix[PREFIX_LEN];    int          i = 0;    char         *rv;    char         *write;    rv = write = PORT_Alloc(PORT_Strlen(prefix) + PORT_Strlen(field_name) + 1);    for(i = 0; i < PORT_Strlen(prefix); i++) {	*write = prefix[i];	write++;    }    *write = '\0';    rv = PORT_Strcat(rv,field_name);    return rv;}static char *find_field(Pair    *data, 	   char    *field_name, 	   PRBool  add_pre)    /* returns a pointer to the data of the first pair        thats name matches the string it is passed */{    int            i = 0;    char           *retrieved;    int            found = 0;    if (add_pre) {	field_name = add_prefix(field_name);    }    while(return_name(data, i) != NULL) {	if (PORT_Strcmp(return_name(data, i), field_name) == 0) {	    retrieved = return_data(data, i);	    found = 1;	    break;	}	i++;    }    if (!found) {	retrieved = NULL;    }    return retrieved;}static PRBoolfind_field_bool(Pair    *data, 		char    *fieldname, 		PRBool  add_pre){    char                *rv;    rv = find_field(data, fieldname, add_pre);	    if  ((rv != NULL) && (PORT_Strcmp(rv, "true")) == 0) {	return PR_TRUE;    } else {	return PR_FALSE;    }}static char *update_data_by_name(Pair  *data, 		    char  *field_name,                    char  *new_data)    /* replaces the data in the data structure associated with        a name with new data, returns null if not found */{    int                   i = 0;    int                   found = 0;    int                   length = 100;    char                  *new;    while (return_name(data, i) != NULL) {	if (PORT_Strcmp(return_name(data, i), field_name) == 0) {	    new = make_copy_string( new_data, length, '\0');	    PORT_Free(return_data(data, i));	    found = 1;	    (*(data + i)).data = new;	    break;	}	i++;    }    if (!found) {	new = NULL;    }    return new;}static char *update_data_by_index(Pair  *data, 		     int   n, 		     char  *new_data)    /* replaces the data of a particular index in the data structure */{    int                    length = 100;    char                   *new;    new = make_copy_string(new_data, length, '\0');    PORT_Free(return_data(data, n));    (*(data + n)).data = new;    return new;}static Pair *add_field(Pair   *data, 	  char*  field_name, 	  char*  field_data)    /* adds a new name/data pair to the data structure */{    int          i = 0;    int          j;    int          name_length = 100;    int          data_length = 100;    while(return_name(data, i) != NULL) {	i++;    }    j = START_FIELDS;    while ( j < (i + 1) ) {	j = j * 2;    }    if (j == (i + 1)) {	data = (Pair *) PORT_Realloc(data, (j * 2) * sizeof(Pair));	if (data == NULL) {	    error_allocate();	}    }    (*(data + i)).name = make_copy_string(field_name, name_length, '\0');    (*(data + i)).data = make_copy_string(field_data, data_length, '\0');    (data + i + 1)->name = NULL;    return data;}static CERTCertificateRequest *makeCertReq(Pair             *form_data,	    int              which_priv_key)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?