Back to index

lightning-sunbird  0.9+nobinonly
addbuiltin.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 /*
00038  * Tool for converting builtin CA certs.
00039  *
00040  * $Id: addbuiltin.c,v 1.13 2005/04/13 23:03:15 wtchang%redhat.com Exp $
00041  */
00042 
00043 #include "nssrenam.h"
00044 #include "nss.h"
00045 #include "cert.h"
00046 #include "certdb.h"
00047 #include "secutil.h"
00048 #include "pk11func.h"
00049 
00050 #if defined(WIN32)
00051 #include <fcntl.h>
00052 #include <io.h>
00053 #endif
00054 
00055 void dumpbytes(unsigned char *buf, int len)
00056 {
00057     int i;
00058     for (i=0; i < len; i++) {
00059        if ((i !=0) && ((i & 0xf) == 0)) {
00060            printf("\n");
00061        }
00062        printf("\\%03o",buf[i]);
00063     }
00064     printf("\n");
00065 }
00066 
00067 char *getTrustString(unsigned int trust)
00068 {
00069     if (trust & CERTDB_TRUSTED) {
00070        if (trust & CERTDB_TRUSTED_CA) {
00071               return "CKT_NETSCAPE_TRUSTED_DELEGATOR|CKT_NETSCAPE_TRUSTED";
00072        } else {
00073               return "CKT_NETSCAPE_TRUSTED";
00074        }
00075     } else {
00076        if (trust & CERTDB_TRUSTED_CA) {
00077               return "CKT_NETSCAPE_TRUSTED_DELEGATOR";
00078        } else {
00079               return "CKT_NETSCAPE_VALID";
00080        }
00081     }
00082     return "CKT_NETSCAPE_VALID"; /* not reached */
00083 }
00084 
00085 static const SEC_ASN1Template serialTemplate[] = {
00086     { SEC_ASN1_INTEGER, offsetof(CERTCertificate,serialNumber) },
00087     { 0 }
00088 };
00089 
00090 static SECStatus
00091 ConvertCertificate(SECItem *sdder, char *nickname, CERTCertTrust *trust)
00092 {
00093     SECStatus rv = SECSuccess;
00094     CERTCertificate *cert;
00095     unsigned char sha1_hash[SHA1_LENGTH];
00096     unsigned char md5_hash[MD5_LENGTH];
00097     SECItem *serial = NULL;
00098 
00099     cert = CERT_DecodeDERCertificate(sdder, PR_FALSE, nickname);
00100     if (!cert) {
00101        return SECFailure;
00102     }
00103     serial = SEC_ASN1EncodeItem(NULL,NULL,cert,serialTemplate);
00104     if (!serial) {
00105        return SECFailure;
00106     }
00107 
00108     printf("\n#\n# Certificate \"%s\"\n#\n",nickname);
00109     printf("CKA_CLASS CK_OBJECT_CLASS CKO_CERTIFICATE\n");
00110     printf("CKA_TOKEN CK_BBOOL CK_TRUE\n");
00111     printf("CKA_PRIVATE CK_BBOOL CK_FALSE\n");
00112     printf("CKA_MODIFIABLE CK_BBOOL CK_FALSE\n");
00113     printf("CKA_LABEL UTF8 \"%s\"\n",nickname);
00114     printf("CKA_CERTIFICATE_TYPE CK_CERTIFICATE_TYPE CKC_X_509\n");
00115     printf("CKA_SUBJECT MULTILINE_OCTAL\n");
00116     dumpbytes(cert->derSubject.data,cert->derSubject.len);
00117     printf("END\n");
00118     printf("CKA_ID UTF8 \"0\"\n");
00119     printf("CKA_ISSUER MULTILINE_OCTAL\n");
00120     dumpbytes(cert->derIssuer.data,cert->derIssuer.len);
00121     printf("END\n");
00122     printf("CKA_SERIAL_NUMBER MULTILINE_OCTAL\n");
00123     dumpbytes(serial->data,serial->len);
00124     printf("END\n");
00125     printf("CKA_VALUE MULTILINE_OCTAL\n");
00126     dumpbytes(sdder->data,sdder->len);
00127     printf("END\n");
00128 
00129     PK11_HashBuf(SEC_OID_SHA1, sha1_hash, sdder->data, sdder->len);
00130     PK11_HashBuf(SEC_OID_MD5, md5_hash, sdder->data, sdder->len);
00131     printf("\n# Trust for Certificate \"%s\"\n",nickname);
00132     printf("CKA_CLASS CK_OBJECT_CLASS CKO_NETSCAPE_TRUST\n");
00133     printf("CKA_TOKEN CK_BBOOL CK_TRUE\n");
00134     printf("CKA_PRIVATE CK_BBOOL CK_FALSE\n");
00135     printf("CKA_MODIFIABLE CK_BBOOL CK_FALSE\n");
00136     printf("CKA_LABEL UTF8 \"%s\"\n",nickname);
00137     printf("CKA_CERT_SHA1_HASH MULTILINE_OCTAL\n");
00138     dumpbytes(sha1_hash,SHA1_LENGTH);
00139     printf("END\n");
00140     printf("CKA_CERT_MD5_HASH MULTILINE_OCTAL\n");
00141     dumpbytes(md5_hash,MD5_LENGTH);
00142     printf("END\n");
00143 
00144     printf("CKA_ISSUER MULTILINE_OCTAL\n");
00145     dumpbytes(cert->derIssuer.data,cert->derIssuer.len);
00146     printf("END\n");
00147     printf("CKA_SERIAL_NUMBER MULTILINE_OCTAL\n");
00148     dumpbytes(serial->data,serial->len);
00149     printf("END\n");
00150     
00151     printf("CKA_TRUST_SERVER_AUTH CK_TRUST %s\n",
00152                              getTrustString(trust->sslFlags));
00153     printf("CKA_TRUST_EMAIL_PROTECTION CK_TRUST %s\n",
00154                              getTrustString(trust->emailFlags));
00155     printf("CKA_TRUST_CODE_SIGNING CK_TRUST %s\n",
00156                              getTrustString(trust->objectSigningFlags));
00157 #ifdef notdef
00158     printf("CKA_TRUST_CLIENT_AUTH CK_TRUST CKT_NETSCAPE_TRUSTED\n");*/
00159     printf("CKA_TRUST_DIGITAL_SIGNATURE CK_TRUST CKT_NETSCAPE_TRUSTED_DELEGATOR\n");
00160     printf("CKA_TRUST_NON_REPUDIATION CK_TRUST CKT_NETSCAPE_TRUSTED_DELEGATOR\n");
00161     printf("CKA_TRUST_KEY_ENCIPHERMENT CK_TRUST CKT_NETSCAPE_TRUSTED_DELEGATOR\n");
00162     printf("CKA_TRUST_DATA_ENCIPHERMENT CK_TRUST CKT_NETSCAPE_TRUSTED_DELEGATOR\n");
00163     printf("CKA_TRUST_KEY_AGREEMENT CK_TRUST CKT_NETSCAPE_TRUSTED_DELEGATOR\n");
00164     printf("CKA_TRUST_KEY_CERT_SIGN CK_TRUST CKT_NETSCAPE_TRUSTED_DELEGATOR\n");
00165 #endif
00166     printf("CKA_TRUST_STEP_UP_APPROVED CK_BBOOL %s\n",
00167                 trust->sslFlags & CERTDB_GOVT_APPROVED_CA ? 
00168                 "CK_TRUE" : "CK_FALSE");
00169 
00170 
00171     PORT_Free(sdder->data);
00172     return(rv);
00173 
00174 }
00175 
00176 void printheader() {
00177     printf("# \n"
00178 "# ***** BEGIN LICENSE BLOCK *****\n"
00179 "# Version: MPL 1.1/GPL 2.0/LGPL 2.1\n"
00180 "#\n"
00181 "# The contents of this file are subject to the Mozilla Public License Version\n"
00182 "# 1.1 (the \"License\"); you may not use this file except in compliance with\n"
00183 "# the License. You may obtain a copy of the License at\n"
00184 "# http://www.mozilla.org/MPL/\n"
00185 "#\n"
00186 "# Software distributed under the License is distributed on an \"AS IS\" basis,\n"
00187 "# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License\n"
00188 "# for the specific language governing rights and limitations under the\n"
00189 "# License.\n"
00190 "#\n"
00191 "# The Original Code is the Netscape security libraries..\n"
00192 "#\n"
00193 "# The Initial Developer of the Original Code is\n"
00194 "# Netscape Communications Corporation.\n"
00195 "# Portions created by the Initial Developer are Copyright (C) 1994-2000\n"
00196 "# the Initial Developer. All Rights Reserved.\n"
00197 "#\n"
00198 "# Contributor(s):\n"
00199 "#\n"
00200 "# Alternatively, the contents of this file may be used under the terms of\n"
00201 "# either the GNU General Public License Version 2 or later (the \"GPL\"), or\n"
00202 "# the GNU Lesser General Public License Version 2.1 or later (the \"LGPL\"),\n"
00203 "# in which case the provisions of the GPL or the LGPL are applicable instead\n"
00204 "# of those above. If you wish to allow use of your version of this file only\n"
00205 "# under the terms of either the GPL or the LGPL, and not to allow others to\n"
00206 "# use your version of this file under the terms of the MPL, indicate your\n"
00207 "# decision by deleting the provisions above and replace them with the notice\n"
00208 "# and other provisions required by the GPL or the LGPL. If you do not delete\n"
00209 "# the provisions above, a recipient may use your version of this file under\n"
00210 "# the terms of any one of the MPL, the GPL or the LGPL.\n"
00211 "#\n"
00212 "# ***** END LICENSE BLOCK *****\n"
00213      "#\n"
00214      "CVS_ID \"@(#) $RCSfile: addbuiltin.c,v $ $Revision: 1.13 $ $Date: 2005/04/13 23:03:15 $\"\n"
00215      "\n"
00216      "#\n"
00217      "# certdata.txt\n"
00218      "#\n"
00219      "# This file contains the object definitions for the certs and other\n"
00220      "# information \"built into\" NSS.\n"
00221      "#\n"
00222      "# Object definitions:\n"
00223      "#\n"
00224      "#    Certificates\n"
00225      "#\n"
00226      "#  -- Attribute --          -- type --              -- value --\n"
00227      "#  CKA_CLASS                CK_OBJECT_CLASS         CKO_CERTIFICATE\n"
00228      "#  CKA_TOKEN                CK_BBOOL                CK_TRUE\n"
00229      "#  CKA_PRIVATE              CK_BBOOL                CK_FALSE\n"
00230      "#  CKA_MODIFIABLE           CK_BBOOL                CK_FALSE\n"
00231      "#  CKA_LABEL                UTF8                    (varies)\n"
00232      "#  CKA_CERTIFICATE_TYPE     CK_CERTIFICATE_TYPE     CKC_X_509\n"
00233      "#  CKA_SUBJECT              DER+base64              (varies)\n"
00234      "#  CKA_ID                   byte array              (varies)\n"
00235      "#  CKA_ISSUER               DER+base64              (varies)\n"
00236      "#  CKA_SERIAL_NUMBER        DER+base64              (varies)\n"
00237      "#  CKA_VALUE                DER+base64              (varies)\n"
00238      "#  CKA_NETSCAPE_EMAIL       ASCII7                  (unused here)\n"
00239      "#\n"
00240      "#    Trust\n"
00241      "#\n"
00242      "#  -- Attribute --              -- type --          -- value --\n"
00243      "#  CKA_CLASS                    CK_OBJECT_CLASS     CKO_TRUST\n"
00244      "#  CKA_TOKEN                    CK_BBOOL            CK_TRUE\n"
00245      "#  CKA_PRIVATE                  CK_BBOOL            CK_FALSE\n"
00246      "#  CKA_MODIFIABLE               CK_BBOOL            CK_FALSE\n"
00247      "#  CKA_LABEL                    UTF8                (varies)\n"
00248      "#  CKA_ISSUER                   DER+base64          (varies)\n"
00249      "#  CKA_SERIAL_NUMBER            DER+base64          (varies)\n"
00250      "#  CKA_CERT_HASH                binary+base64       (varies)\n"
00251      "#  CKA_EXPIRES                  CK_DATE             (not used here)\n"
00252      "#  CKA_TRUST_DIGITAL_SIGNATURE  CK_TRUST            (varies)\n"
00253      "#  CKA_TRUST_NON_REPUDIATION    CK_TRUST            (varies)\n"
00254      "#  CKA_TRUST_KEY_ENCIPHERMENT   CK_TRUST            (varies)\n"
00255      "#  CKA_TRUST_DATA_ENCIPHERMENT  CK_TRUST            (varies)\n"
00256      "#  CKA_TRUST_KEY_AGREEMENT      CK_TRUST            (varies)\n"
00257      "#  CKA_TRUST_KEY_CERT_SIGN      CK_TRUST            (varies)\n"
00258      "#  CKA_TRUST_CRL_SIGN           CK_TRUST            (varies)\n"
00259      "#  CKA_TRUST_SERVER_AUTH        CK_TRUST            (varies)\n"
00260      "#  CKA_TRUST_CLIENT_AUTH        CK_TRUST            (varies)\n"
00261      "#  CKA_TRUST_CODE_SIGNING       CK_TRUST            (varies)\n"
00262      "#  CKA_TRUST_EMAIL_PROTECTION   CK_TRUST            (varies)\n"
00263      "#  CKA_TRUST_IPSEC_END_SYSTEM   CK_TRUST            (varies)\n"
00264      "#  CKA_TRUST_IPSEC_TUNNEL       CK_TRUST            (varies)\n"
00265      "#  CKA_TRUST_IPSEC_USER         CK_TRUST            (varies)\n"
00266      "#  CKA_TRUST_TIME_STAMPING      CK_TRUST            (varies)\n"
00267      "#  (other trust attributes can be defined)\n"
00268      "#\n"
00269      "\n"
00270      "#\n"
00271      "# The object to tell NSS that this is a root list and we don't\n"
00272      "# have to go looking for others.\n"
00273      "#\n"
00274      "BEGINDATA\n"
00275      "CKA_CLASS CK_OBJECT_CLASS CKO_NETSCAPE_BUILTIN_ROOT_LIST\n"
00276      "CKA_TOKEN CK_BBOOL CK_TRUE\n"
00277      "CKA_PRIVATE CK_BBOOL CK_FALSE\n"
00278      "CKA_MODIFIABLE CK_BBOOL CK_FALSE\n"
00279      "CKA_LABEL UTF8 \"Mozilla Builtin Roots\"\n");
00280 }
00281 
00282 static void Usage(char *progName)
00283 {
00284     fprintf(stderr, "%s -n nickname -t trust [-i certfile]\n", progName);
00285     fprintf(stderr, 
00286             "\tRead a der-encoded cert from certfile or stdin, and output\n"
00287             "\tit to stdout in a format suitable for the builtin root module.\n"
00288             "\tExample: %s -n MyCA -t \"C,C,C\" -i myca.der >> certdata.txt\n"
00289             "\t(pipe through atob if the cert is b64-encoded)\n", progName);
00290     fprintf(stderr, "%-15s nickname to assign to builtin cert.\n", 
00291                     "-n nickname");
00292     fprintf(stderr, "%-15s trust flags (cCTpPuw).\n", "-t trust");
00293     fprintf(stderr, "%-15s file to read (default stdin)\n", "-i certfile");
00294     exit(-1);
00295 }
00296 
00297 enum {
00298     opt_Input = 0,
00299     opt_Nickname,
00300     opt_Trust
00301 };
00302 
00303 static secuCommandFlag addbuiltin_options[] =
00304 {
00305        { /* opt_Input         */  'i', PR_TRUE, 0, PR_FALSE },
00306        { /* opt_Nickname      */  'n', PR_TRUE, 0, PR_FALSE },
00307        { /* opt_Trust         */  't', PR_TRUE, 0, PR_FALSE }
00308 };
00309 
00310 int main(int argc, char **argv)
00311 {
00312     SECStatus rv;
00313     char *nickname;
00314     char *trusts;
00315     char *progName;
00316     PRFileDesc *infile;
00317     CERTCertTrust trust = { 0 };
00318     SECItem derCert = { 0 };
00319 
00320     secuCommand addbuiltin = { 0 };
00321     addbuiltin.numOptions = sizeof(addbuiltin_options)/sizeof(secuCommandFlag);
00322     addbuiltin.options = addbuiltin_options;
00323 
00324     progName = strrchr(argv[0], '/');
00325     progName = progName ? progName+1 : argv[0];
00326 
00327     rv = SECU_ParseCommandLine(argc, argv, progName, &addbuiltin);
00328 
00329     if (rv != SECSuccess)
00330        Usage(progName);
00331 
00332     if (!addbuiltin.options[opt_Nickname].activated &&
00333         !addbuiltin.options[opt_Trust].activated) {
00334        fprintf(stderr, "%s: you must specify both a nickname and trust.\n",
00335               progName);
00336        Usage(progName);
00337     }
00338 
00339     if (addbuiltin.options[opt_Input].activated) {
00340        infile = PR_Open(addbuiltin.options[opt_Input].arg, PR_RDONLY, 00660);
00341        if (!infile) {
00342            fprintf(stderr, "%s: failed to open input file.\n", progName);
00343            exit(1);
00344        }
00345     } else {
00346 #if defined(WIN32)
00347        /* If we're going to read binary data from stdin, we must put stdin
00348        ** into O_BINARY mode or else incoming \r\n's will become \n's,
00349        ** and latin-1 characters will be altered.
00350        */
00351 
00352        int smrv = _setmode(_fileno(stdin), _O_BINARY);
00353        if (smrv == -1) {
00354            fprintf(stderr,
00355            "%s: Cannot change stdin to binary mode. Use -i option instead.\n",
00356                    progName);
00357            exit(1);
00358        }
00359 #endif
00360        infile = PR_STDIN;
00361     }
00362 
00363     nickname = strdup(addbuiltin.options[opt_Nickname].arg);
00364     trusts = strdup(addbuiltin.options[opt_Trust].arg);
00365 
00366     NSS_NoDB_Init(NULL);
00367 
00368     rv = CERT_DecodeTrustString(&trust, trusts);
00369     if (rv) {
00370        fprintf(stderr, "%s: incorrectly formatted trust string.\n", progName);
00371        Usage(progName);
00372     }
00373 
00374     SECU_FileToItem(&derCert, infile);
00375     
00376     /*printheader();*/
00377 
00378     rv = ConvertCertificate(&derCert, nickname, &trust);
00379     if (rv) {
00380        fprintf(stderr, "%s: failed to convert certificate.\n", progName);
00381        exit(1);
00382     }
00383     
00384     if (NSS_Shutdown() != SECSuccess) {
00385         exit(1);
00386     }
00387 
00388     return(SECSuccess);
00389 }