Back to index

lightning-sunbird  0.9+nobinonly
pk12util.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 #include "nspr.h"
00038 #include "secutil.h"
00039 #include "pk11func.h"
00040 #include "pkcs12.h"
00041 #include "p12plcy.h"
00042 #include "pk12util.h"
00043 #include "nss.h"
00044 #include "secport.h"
00045 #include "certdb.h"
00046 
00047 #define PKCS12_IN_BUFFER_SIZE      200
00048 
00049 static char *progName;
00050 PRBool pk12_debugging = PR_FALSE;
00051 
00052 PRIntn pk12uErrno = 0;
00053 
00054 static void
00055 Usage(char *progName)
00056 {
00057 #define FPS PR_fprintf(PR_STDERR,
00058     FPS "Usage:       %s -i importfile [-d certdir] [-P dbprefix] [-h tokenname]\n",
00059                              progName);
00060     FPS "\t\t [-k slotpwfile | -K slotpw] [-w p12filepwfile | -W p12filepw]\n");
00061     FPS "\t\t [-v]\n");
00062     FPS "Usage:       %s -l listfile [-d certdir] [-P dbprefix] [-h tokenname]\n",
00063                              progName);
00064     FPS "\t\t [-k slotpwfile | -K slotpw] [-w p12filepwfile | -W p12filepw]\n");
00065     FPS "Usage:       %s -o exportfile -n certname [-d certdir] [-P dbprefix]\n", progName);
00066     FPS "\t\t [-k slotpwfile | -K slotpw] [-w p12filepwfile | -W p12filepw]\n");
00067     FPS "\t\t [-v]\n");
00068     exit(PK12UERR_USAGE);
00069 }
00070 
00071 static PRBool
00072 p12u_OpenFile(p12uContext *p12cxt, PRBool fileRead)
00073 {
00074     if(!p12cxt || !p12cxt->filename) {
00075        return PR_FALSE;
00076     }
00077 
00078     if(fileRead) {
00079        p12cxt->file = PR_Open(p12cxt->filename,
00080                              PR_RDONLY, 0400);
00081     } else {
00082        p12cxt->file = PR_Open(p12cxt->filename,
00083                              PR_CREATE_FILE | PR_RDWR | PR_TRUNCATE,
00084                              0600);
00085     }
00086 
00087     if(!p12cxt->file) {
00088        p12cxt->error = PR_TRUE;
00089        return PR_FALSE;
00090     }
00091 
00092     return PR_TRUE;
00093 }
00094 
00095 static void
00096 p12u_DestroyContext(p12uContext **ppCtx, PRBool removeFile)
00097 {
00098     if(!ppCtx || !(*ppCtx)) {
00099        return;
00100     }
00101 
00102     if((*ppCtx)->file != NULL) {
00103        PR_Close((*ppCtx)->file);
00104     }
00105 
00106     if((*ppCtx)->filename != NULL) {
00107        if(removeFile) {
00108            PR_Delete((*ppCtx)->filename);
00109        }
00110        PR_Free((*ppCtx)->filename);
00111     }
00112 
00113     PR_Free(*ppCtx);
00114     *ppCtx = NULL;
00115 }
00116 
00117 static p12uContext *
00118 p12u_InitContext(PRBool fileImport, char *filename)
00119 {
00120     p12uContext *p12cxt;
00121     PRBool fileExist;
00122 
00123     fileExist = fileImport;
00124 
00125     p12cxt = PORT_ZNew(p12uContext);
00126     if(!p12cxt) {
00127        return NULL;
00128     }
00129 
00130     p12cxt->error = PR_FALSE;
00131     p12cxt->errorValue = 0;
00132     p12cxt->filename = strdup(filename);
00133 
00134     if(!p12u_OpenFile(p12cxt, fileImport)) {
00135        p12u_DestroyContext(&p12cxt, PR_FALSE);
00136        return NULL;
00137     }
00138 
00139     return p12cxt;
00140 }
00141 
00142 SECItem *
00143 P12U_NicknameCollisionCallback(SECItem *old_nick, PRBool *cancel, void *wincx)
00144 {
00145     if(cancel == NULL) {
00146       pk12uErrno = PK12UERR_USER_CANCELLED;
00147       return NULL;
00148     }
00149 
00150     if (!old_nick)
00151       fprintf(stdout, "pk12util: no nickname for cert...not handled\n");
00152 
00153     /* XXX not handled yet  */
00154     *cancel = PR_TRUE;
00155     return NULL;
00156 
00157 #if 0
00158     char *nick = NULL;
00159     SECItem *ret_nick = NULL;
00160 
00161     nick = strdup( DEFAULT_CERT_NICKNAME );
00162 
00163     if(old_nick && !PORT_Strcmp((char *)old_nick->data, nick)) {
00164        PORT_Free(nick);
00165        return NULL;
00166     }
00167 
00168     ret_nick = (SECItem *)PORT_ZAlloc(sizeof(SECItem));
00169     if(ret_nick == NULL) {
00170        PORT_Free(nick);
00171        return NULL;
00172     }
00173 
00174     ret_nick->data = (unsigned char *)nick;
00175     ret_nick->len = PORT_Strlen(nick);
00176 
00177     return ret_nick;
00178 #endif
00179 }
00180 
00181 static SECStatus
00182 p12u_SwapUnicodeBytes(SECItem *uniItem)
00183 {
00184     unsigned int i;
00185     unsigned char a;
00186     if((uniItem == NULL) || (uniItem->len % 2)) {
00187        return SECFailure;
00188     }
00189     for(i = 0; i < uniItem->len; i += 2) {
00190        a = uniItem->data[i];
00191        uniItem->data[i] = uniItem->data[i+1];
00192        uniItem->data[i+1] = a;
00193     }
00194     return SECSuccess;
00195 }
00196 
00197 static PRBool
00198 p12u_ucs2_ascii_conversion_function(PRBool          toUnicode,
00199                                 unsigned char *inBuf,
00200                                 unsigned int   inBufLen,
00201                                 unsigned char *outBuf,
00202                                 unsigned int   maxOutBufLen,
00203                                 unsigned int  *outBufLen,
00204                                 PRBool       swapBytes)
00205 {
00206     SECItem it = { 0 };
00207     SECItem *dup = NULL;
00208     PRBool ret;
00209 
00210 #ifdef DEBUG_CONVERSION
00211     if (pk12_debugging) {
00212        int i;
00213        printf("Converted from:\n");
00214        for (i=0; i<inBufLen; i++) {
00215            printf("%2x ", inBuf[i]);
00216            /*if (i%60 == 0) printf("\n");*/
00217        }
00218        printf("\n");
00219     }
00220 #endif
00221     it.data = inBuf;
00222     it.len = inBufLen;
00223     dup = SECITEM_DupItem(&it);
00224     /* If converting Unicode to ASCII, swap bytes before conversion
00225      * as neccessary.
00226      */
00227     if (!toUnicode && swapBytes) {
00228        if (p12u_SwapUnicodeBytes(dup) != SECSuccess) {
00229            SECITEM_ZfreeItem(dup, PR_TRUE);
00230            return PR_FALSE;
00231        }
00232     }
00233     /* Perform the conversion. */
00234     ret = PORT_UCS2_UTF8Conversion(toUnicode, dup->data, dup->len,
00235                                    outBuf, maxOutBufLen, outBufLen);
00236     if (dup)
00237        SECITEM_ZfreeItem(dup, PR_TRUE);
00238 
00239 #ifdef DEBUG_CONVERSION
00240     if (pk12_debugging) {
00241        int i;
00242        printf("Converted to:\n");
00243        for (i=0; i<*outBufLen; i++) {
00244            printf("%2x ", outBuf[i]);
00245            /*if (i%60 == 0) printf("\n");*/
00246        }
00247        printf("\n");
00248     }
00249 #endif
00250     return ret;
00251 }
00252 
00253 SECStatus
00254 P12U_UnicodeConversion(PRArenaPool *arena, SECItem *dest, SECItem *src,
00255                      PRBool toUnicode, PRBool swapBytes)
00256 {
00257     unsigned int allocLen;
00258     if(!dest || !src) {
00259        return SECFailure;
00260     }
00261     allocLen = ((toUnicode) ? (src->len << 2) : src->len);
00262     if(arena) {
00263        dest->data = PORT_ArenaZAlloc(arena, allocLen);
00264     } else {
00265        dest->data = PORT_ZAlloc(allocLen);
00266     }
00267     if(PORT_UCS2_ASCIIConversion(toUnicode, src->data, src->len,
00268                              dest->data, allocLen, &dest->len,
00269                              swapBytes) == PR_FALSE) {
00270        if(!arena) {
00271            PORT_Free(dest->data);
00272        }
00273        dest->data = NULL;
00274        return SECFailure;
00275     }
00276     return SECSuccess;
00277 }
00278 
00279 /*
00280  *
00281  */
00282 SECItem *
00283 P12U_GetP12FilePassword(PRBool confirmPw, secuPWData *p12FilePw)
00284 {
00285     char *p0 = NULL, *p1 = NULL;
00286     SECItem *pwItem = NULL;
00287 
00288     if (p12FilePw == NULL || p12FilePw->source == PW_NONE) {
00289        for (;;) {
00290            p0 = SECU_GetPasswordString(NULL,
00291                                    "Enter password for PKCS12 file: ");
00292            if (!confirmPw)
00293               break;
00294            p1 = SECU_GetPasswordString(NULL, "Re-enter password: ");
00295            if (PL_strcmp(p0, p1) == 0)
00296               break;
00297        }
00298     } else if (p12FilePw->source == PW_FROMFILE) {
00299        p0 = SECU_FilePasswd(NULL, PR_FALSE, p12FilePw->data);
00300     } else { /* Plaintext */
00301        p0 = p12FilePw->data;
00302     }
00303 
00304     if (p0 == NULL) {
00305         return NULL;
00306     }
00307     pwItem = SECITEM_AllocItem(NULL, NULL, PL_strlen(p0) + 1);
00308     memcpy(pwItem->data, p0, pwItem->len);
00309 
00310     PORT_Memset(p0, 0, PL_strlen(p0));
00311     PORT_Free(p0);
00312 
00313     PORT_Memset(p1, 0, PL_strlen(p1));
00314     PORT_Free(p1);
00315 
00316     return pwItem;
00317 }
00318 
00319 SECStatus
00320 P12U_InitSlot(PK11SlotInfo *slot, secuPWData *slotPw)
00321 {
00322     SECStatus rv;
00323 
00324     /* New databases, initialize keydb password. */
00325     if (PK11_NeedUserInit(slot)) {
00326        rv = SECU_ChangePW(slot,
00327                         (slotPw->source == PW_PLAINTEXT) ? slotPw->data : 0,
00328                         (slotPw->source == PW_FROMFILE) ? slotPw->data : 0);
00329        if (rv != SECSuccess) {
00330            SECU_PrintError(progName, "Failed to initialize slot \"%s\"",
00331                                 PK11_GetSlotName(slot));
00332            return SECFailure;
00333        }
00334     }
00335 
00336     if (PK11_Authenticate(slot, PR_TRUE, slotPw) != SECSuccess) {
00337        SECU_PrintError(progName,
00338                       "Failed to authenticate to PKCS11 slot");
00339        PORT_SetError(SEC_ERROR_USER_CANCELLED);
00340        pk12uErrno = PK12UERR_USER_CANCELLED;
00341        return SECFailure;
00342     }
00343 
00344     return SECSuccess;
00345 }
00346 
00347 /* This routine takes care of getting the PKCS12 file password, then reading and
00348  * verifying the file. It returns the decoder context and a filled in password.
00349  * (The password is needed by P12U_ImportPKCS12Object() to import the private
00350  * key.)
00351  */
00352 SEC_PKCS12DecoderContext *
00353 p12U_ReadPKCS12File(SECItem *uniPwp, char *in_file, PK11SlotInfo *slot,
00354                     secuPWData *slotPw, secuPWData *p12FilePw)
00355 {
00356     SEC_PKCS12DecoderContext *p12dcx = NULL;
00357     p12uContext *p12cxt = NULL;
00358     SECItem *pwitem = NULL;
00359     SECItem p12file = { 0 };
00360     SECStatus rv = SECFailure;
00361     PRBool swapUnicode = PR_FALSE;
00362     PRBool trypw;
00363     int error;
00364     
00365 #ifdef IS_LITTLE_ENDIAN
00366     swapUnicode = PR_TRUE;
00367 #endif
00368 
00369     p12cxt = p12u_InitContext(PR_TRUE, in_file);
00370     if(!p12cxt) {
00371        SECU_PrintError(progName,"File Open failed: %s", in_file);
00372        pk12uErrno = PK12UERR_INIT_FILE;
00373         return NULL;
00374     }
00375 
00376     /* get the password */
00377     pwitem = P12U_GetP12FilePassword(PR_FALSE, p12FilePw);
00378     if (!pwitem) {
00379        pk12uErrno = PK12UERR_USER_CANCELLED;
00380        goto done;
00381     }
00382 
00383     if(P12U_UnicodeConversion(NULL, uniPwp, pwitem, PR_TRUE,
00384                               swapUnicode) != SECSuccess) {
00385        SECU_PrintError(progName,"Unicode conversion failed");
00386        pk12uErrno = PK12UERR_UNICODECONV;
00387        goto done;
00388     }
00389     rv = SECU_FileToItem(&p12file, p12cxt->file);
00390     if (rv != SECSuccess) {
00391         SECU_PrintError(progName,"Failed to read from import file");
00392         goto done;
00393     }
00394 
00395     do {
00396         trypw = PR_FALSE;                  /* normally we do this once */
00397         rv = SECFailure;
00398         /* init the decoder context */
00399         p12dcx = SEC_PKCS12DecoderStart(uniPwp, slot, slotPw,
00400                                         NULL, NULL, NULL, NULL, NULL);
00401         if(!p12dcx) {
00402             SECU_PrintError(progName,"PKCS12 decoder start failed");
00403             pk12uErrno = PK12UERR_PK12DECODESTART;
00404             break;
00405         }
00406 
00407         /* decode the item */
00408         rv = SEC_PKCS12DecoderUpdate(p12dcx, p12file.data, p12file.len);
00409 
00410         if(rv != SECSuccess) {
00411             error = PR_GetError();
00412             if(error == SEC_ERROR_DECRYPTION_DISALLOWED) {
00413                 PR_SetError(error, 0);
00414                 break;
00415             }
00416             SECU_PrintError(progName,"PKCS12 decoding failed");
00417             pk12uErrno = PK12UERR_DECODE;
00418         }
00419 
00420         /* does the blob authenticate properly? */
00421         rv = SEC_PKCS12DecoderVerify(p12dcx);
00422         if (rv != SECSuccess) {
00423             if(uniPwp->len == 2) {
00424                 /* this is a null PW, try once more with a zero-length PW
00425                    instead of a null string */
00426                 SEC_PKCS12DecoderFinish(p12dcx);
00427                 uniPwp->len = 0;
00428                 trypw = PR_TRUE;
00429             }
00430             else {
00431                 SECU_PrintError(progName,"PKCS12 decode not verified");
00432                 pk12uErrno = PK12UERR_DECODEVERIFY;
00433                 break;
00434             }
00435         }
00436     } while (trypw == PR_TRUE);
00437     /* rv has been set at this point */
00438 
00439 
00440 done:
00441     if (rv != SECSuccess) {
00442         if (p12dcx != NULL) {
00443             SEC_PKCS12DecoderFinish(p12dcx);
00444             p12dcx = NULL;
00445         }
00446         if (uniPwp->data) {
00447             SECITEM_ZfreeItem(uniPwp, PR_FALSE);
00448             uniPwp->data = NULL;
00449         }
00450     }
00451     PR_Close(p12cxt->file);
00452     p12cxt->file = NULL;
00453     /* PK11_FreeSlot(slot); */
00454     p12u_DestroyContext(&p12cxt, PR_FALSE);
00455 
00456     if (pwitem) {
00457        SECITEM_ZfreeItem(pwitem, PR_TRUE);
00458     }
00459     return p12dcx;
00460 }
00461 
00462 /*
00463  * given a filename for pkcs12 file, imports certs and keys
00464  *
00465  * Change: altitude
00466  *  I've changed this function so that it takes the keydb and pkcs12 file
00467  *  passwords from files.  The "pwdKeyDB" and "pwdP12File"
00468  *  variables have been added for this purpose.
00469  */
00470 PRIntn
00471 P12U_ImportPKCS12Object(char *in_file, PK11SlotInfo *slot,
00472                      secuPWData *slotPw, secuPWData *p12FilePw)
00473 {
00474     SEC_PKCS12DecoderContext *p12dcx = NULL;
00475     SECItem uniPwitem = { 0 };
00476     SECStatus rv = SECFailure;
00477     int error;
00478 
00479     rv = P12U_InitSlot(slot, slotPw);
00480     if (rv != SECSuccess) {
00481        SECU_PrintError(progName, "Failed to authenticate to \"%s\"",
00482                                                   PK11_GetSlotName(slot));
00483        pk12uErrno = PK12UERR_PK11GETSLOT;
00484        return rv;
00485     }
00486 
00487     rv = SECFailure;
00488     p12dcx = p12U_ReadPKCS12File(&uniPwitem, in_file, slot, slotPw, p12FilePw);
00489     
00490     if(p12dcx == NULL) {
00491         goto loser;
00492     }
00493     
00494     /* make sure the bags are okey dokey -- nicknames correct, etc. */
00495     rv = SEC_PKCS12DecoderValidateBags(p12dcx, P12U_NicknameCollisionCallback);
00496     if (rv != SECSuccess) {
00497        if (PORT_GetError() == SEC_ERROR_PKCS12_DUPLICATE_DATA) {
00498            pk12uErrno = PK12UERR_CERTALREADYEXISTS;
00499        } else {
00500            pk12uErrno = PK12UERR_DECODEVALIBAGS;
00501        }
00502        SECU_PrintError(progName,"PKCS12 decode validate bags failed");
00503        goto loser;
00504     }
00505 
00506     /* stuff 'em in */
00507     rv = SEC_PKCS12DecoderImportBags(p12dcx);
00508     if (rv != SECSuccess) {
00509        SECU_PrintError(progName,"PKCS12 decode import bags failed");
00510        pk12uErrno = PK12UERR_DECODEIMPTBAGS;
00511        goto loser;
00512     }
00513 
00514     fprintf(stdout, "%s: PKCS12 IMPORT SUCCESSFUL\n", progName);
00515     rv = SECSuccess;
00516 
00517 loser:
00518     if (p12dcx) {
00519        SEC_PKCS12DecoderFinish(p12dcx);
00520     }
00521     
00522     if (uniPwitem.data) {
00523        SECITEM_ZfreeItem(&uniPwitem, PR_FALSE);
00524     }
00525     
00526     return rv;
00527 }
00528 
00529 static void
00530 p12u_DoPKCS12ExportErrors()
00531 {
00532     int error_value;
00533 
00534     error_value = PORT_GetError();
00535     if ((error_value == SEC_ERROR_PKCS12_UNABLE_TO_EXPORT_KEY) ||
00536        (error_value == SEC_ERROR_PKCS12_UNABLE_TO_LOCATE_OBJECT_BY_NAME) ||
00537        (error_value == SEC_ERROR_PKCS12_UNABLE_TO_WRITE)) {
00538        fprintf(stderr, SECU_ErrorStringRaw((int16)error_value));
00539     } else if(error_value == SEC_ERROR_USER_CANCELLED) {
00540        ;
00541     } else {
00542        fprintf(stderr, SECU_ErrorStringRaw(SEC_ERROR_EXPORTING_CERTIFICATES));
00543     }
00544 }
00545 
00546 static void
00547 p12u_WriteToExportFile(void *arg, const char *buf, unsigned long len)
00548 {
00549     p12uContext *p12cxt = arg;
00550     int writeLen;
00551 
00552     if(!p12cxt || (p12cxt->error == PR_TRUE)) {
00553        return;
00554     }
00555 
00556     if(p12cxt->file == NULL) {
00557        p12cxt->errorValue = SEC_ERROR_PKCS12_UNABLE_TO_WRITE;
00558        p12cxt->error = PR_TRUE;
00559        return;
00560     }
00561 
00562     writeLen = PR_Write(p12cxt->file, (unsigned char *)buf, (int32)len);
00563 
00564     if(writeLen != (int)len) {
00565        PR_Close(p12cxt->file);
00566        PR_Free(p12cxt->filename);
00567        p12cxt->filename = NULL;
00568        p12cxt->file = NULL;
00569        p12cxt->errorValue = SEC_ERROR_PKCS12_UNABLE_TO_WRITE;
00570        p12cxt->error = PR_TRUE;
00571     }
00572 }
00573 
00574 
00575 void
00576 P12U_ExportPKCS12Object(char *nn, char *outfile, PK11SlotInfo *inSlot,
00577                      secuPWData *slotPw, secuPWData *p12FilePw)
00578 {
00579     SEC_PKCS12ExportContext *p12ecx = NULL;
00580     SEC_PKCS12SafeInfo *keySafe = NULL, *certSafe = NULL;
00581     SECItem *pwitem = NULL;
00582     p12uContext *p12cxt = NULL;
00583     CERTCertList* certlist = NULL;
00584     CERTCertListNode* node = NULL;
00585     PK11SlotInfo* slot = NULL;
00586 
00587     if (P12U_InitSlot(inSlot, slotPw) != SECSuccess) {
00588        SECU_PrintError(progName,"Failed to authenticate to \"%s\"",
00589                        PK11_GetSlotName(inSlot));
00590        pk12uErrno = PK12UERR_PK11GETSLOT;
00591        goto loser;
00592     }
00593     certlist = PK11_FindCertsFromNickname(nn, slotPw);
00594     if(!certlist) {
00595        SECU_PrintError(progName,"find user certs from nickname failed");
00596        pk12uErrno = PK12UERR_FINDCERTBYNN;
00597        return;
00598     }
00599 
00600     if ((SECSuccess != CERT_FilterCertListForUserCerts(certlist)) ||
00601         CERT_LIST_EMPTY(certlist)) {
00602         SECU_PrintError(progName,"no user certs from given nickname");
00603         pk12uErrno = PK12UERR_FINDCERTBYNN;
00604         goto loser;
00605     }
00606 
00607     /* Password to use for PKCS12 file.  */
00608     pwitem = P12U_GetP12FilePassword(PR_TRUE, p12FilePw);
00609     if(!pwitem) {
00610        goto loser;
00611     }
00612 
00613     p12cxt = p12u_InitContext(PR_FALSE, outfile); 
00614     if(!p12cxt) {
00615        SECU_PrintError(progName,"Initialization failed: %s", outfile);
00616        pk12uErrno = PK12UERR_INIT_FILE;
00617        goto loser;
00618     }
00619 
00620     if (certlist) {
00621         CERTCertificate* cert = NULL;
00622         node = CERT_LIST_HEAD(certlist);
00623         if (node) {
00624             cert = node->cert;
00625         }
00626         if (cert) {
00627             slot = cert->slot; /* use the slot from the first matching
00628                 certificate to create the context . This is for keygen */
00629         }
00630     }
00631     if (!slot) {
00632         SECU_PrintError(progName,"cert does not have a slot");
00633         pk12uErrno = PK12UERR_FINDCERTBYNN;
00634         goto loser;
00635     }
00636     p12ecx = SEC_PKCS12CreateExportContext(NULL, NULL, slot, slotPw);
00637     if(!p12ecx) {
00638         SECU_PrintError(progName,"export context creation failed");
00639         pk12uErrno = PK12UERR_EXPORTCXCREATE;
00640         goto loser;
00641     }
00642 
00643     if(SEC_PKCS12AddPasswordIntegrity(p12ecx, pwitem, SEC_OID_SHA1)
00644        != SECSuccess) {
00645         SECU_PrintError(progName,"PKCS12 add password integrity failed");
00646         pk12uErrno = PK12UERR_PK12ADDPWDINTEG;
00647         goto loser;
00648     }
00649 
00650     for (node = CERT_LIST_HEAD(certlist);!CERT_LIST_END(node,certlist);node=CERT_LIST_NEXT(node))
00651     {
00652         CERTCertificate* cert = node->cert;
00653         if (!cert->slot) {
00654             SECU_PrintError(progName,"cert does not have a slot");
00655             pk12uErrno = PK12UERR_FINDCERTBYNN;
00656             goto loser;
00657         }
00658     
00659         keySafe = SEC_PKCS12CreateUnencryptedSafe(p12ecx);
00660         if( PK11_IsFIPS()) {
00661             certSafe = keySafe;
00662         } else {
00663             certSafe = SEC_PKCS12CreatePasswordPrivSafe(p12ecx, pwitem,
00664                 SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_40_BIT_RC2_CBC);
00665         }
00666     
00667         if(!certSafe || !keySafe) {
00668             SECU_PrintError(progName,"key or cert safe creation failed");
00669             pk12uErrno = PK12UERR_CERTKEYSAFE;
00670             goto loser;
00671         }
00672     
00673         if(SEC_PKCS12AddCertAndKey(p12ecx, certSafe, NULL, cert,
00674             CERT_GetDefaultCertDB(), keySafe, NULL, PR_TRUE, pwitem,
00675             SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_3KEY_TRIPLE_DES_CBC)
00676             != SECSuccess) {
00677                 SECU_PrintError(progName,"add cert and key failed");
00678                 pk12uErrno = PK12UERR_ADDCERTKEY;
00679                 goto loser;
00680         }
00681     }
00682 
00683     CERT_DestroyCertList(certlist);
00684     certlist = NULL;
00685 
00686     if(SEC_PKCS12Encode(p12ecx, p12u_WriteToExportFile, p12cxt)
00687                         != SECSuccess) {
00688         SECU_PrintError(progName,"PKCS12 encode failed");
00689         pk12uErrno = PK12UERR_ENCODE;
00690         goto loser;
00691     }
00692 
00693     p12u_DestroyContext(&p12cxt, PR_FALSE);
00694     SECITEM_ZfreeItem(pwitem, PR_TRUE);
00695     fprintf(stdout, "%s: PKCS12 EXPORT SUCCESSFUL\n", progName);
00696     SEC_PKCS12DestroyExportContext(p12ecx);
00697 
00698     return;
00699 
00700 loser:
00701     SEC_PKCS12DestroyExportContext(p12ecx);
00702 
00703     if (certlist) {
00704         CERT_DestroyCertList(certlist);
00705         certlist = NULL;
00706     }    
00707 
00708     if (slotPw)
00709         PR_Free(slotPw->data);
00710 
00711     if (p12FilePw)
00712         PR_Free(p12FilePw->data);
00713 
00714     p12u_DestroyContext(&p12cxt, PR_TRUE);
00715     if(pwitem) {
00716         SECITEM_ZfreeItem(pwitem, PR_TRUE);
00717     }
00718     p12u_DoPKCS12ExportErrors();
00719     return;
00720 }
00721 
00722 
00723 PRIntn
00724 P12U_ListPKCS12File(char *in_file, PK11SlotInfo *slot,
00725                      secuPWData *slotPw, secuPWData *p12FilePw)
00726 {
00727     SEC_PKCS12DecoderContext *p12dcx = NULL;
00728     SECItem uniPwitem = { 0 };
00729     SECStatus rv = SECFailure;
00730     const SEC_PKCS12DecoderItem *dip;
00731     int error;
00732 
00733     p12dcx = p12U_ReadPKCS12File(&uniPwitem, in_file, slot, slotPw,
00734                              p12FilePw);
00735     /* did the blob authenticate properly? */
00736     if(p12dcx == NULL) {
00737        SECU_PrintError(progName,"PKCS12 decode not verified");
00738        pk12uErrno = PK12UERR_DECODEVERIFY;
00739         goto loser;
00740     }
00741     rv = SEC_PKCS12DecoderIterateInit(p12dcx);
00742     if(rv != SECSuccess) {
00743        SECU_PrintError(progName,"PKCS12 decode iterate bags failed");
00744        pk12uErrno = PK12UERR_DECODEIMPTBAGS;
00745         rv = SECFailure;
00746     }
00747     else {
00748         while (SEC_PKCS12DecoderIterateNext(p12dcx, &dip) == SECSuccess) {
00749             switch (dip->type) {
00750                 case SEC_OID_PKCS12_V1_CERT_BAG_ID:
00751                     printf("Certificate");
00752                     if (SECU_PrintSignedData(stdout, dip->der,
00753                             (dip->hasKey) ? "(has private key)" : "",
00754                              0, SECU_PrintCertificate) != 0) {
00755                         SECU_PrintError(progName,"PKCS12 print cert bag failed");
00756                     }
00757                     if (dip->friendlyName != NULL) {
00758                         printf("    Friendly Name: %s\n\n",
00759                                 dip->friendlyName->data);
00760                     }
00761                     break;
00762                 case SEC_OID_PKCS12_V1_KEY_BAG_ID:
00763                 case SEC_OID_PKCS12_V1_PKCS8_SHROUDED_KEY_BAG_ID:
00764                     printf("Key");
00765                     if (dip->type == SEC_OID_PKCS12_V1_PKCS8_SHROUDED_KEY_BAG_ID)
00766                         printf("(shrouded)");
00767                     printf(":\n");
00768                     if (dip->friendlyName != NULL) {
00769                         printf("    Friendly Name: %s\n\n",
00770                                 dip->friendlyName->data);
00771                     }
00772                     break;
00773                 default:
00774                     printf("unknown bag type(%d): %s\n\n", dip->type,
00775                             SECOID_FindOIDTagDescription(dip->type));
00776                     break;
00777             }
00778         }
00779         rv = SECSuccess;
00780     }
00781 
00782 loser:
00783     
00784     if (p12dcx) {
00785        SEC_PKCS12DecoderFinish(p12dcx);
00786     }
00787     
00788     if (uniPwitem.data) {
00789        SECITEM_ZfreeItem(&uniPwitem, PR_FALSE);
00790     }
00791     
00792     return rv;
00793 }
00794 
00795 static void
00796 p12u_EnableAllCiphers()
00797 {
00798     SEC_PKCS12EnableCipher(PKCS12_RC4_40, 1);
00799     SEC_PKCS12EnableCipher(PKCS12_RC4_128, 1);
00800     SEC_PKCS12EnableCipher(PKCS12_RC2_CBC_40, 1);
00801     SEC_PKCS12EnableCipher(PKCS12_RC2_CBC_128, 1);
00802     SEC_PKCS12EnableCipher(PKCS12_DES_56, 1);
00803     SEC_PKCS12EnableCipher(PKCS12_DES_EDE3_168, 1);
00804     SEC_PKCS12SetPreferredCipher(PKCS12_DES_EDE3_168, 1);
00805 }
00806 
00807 static PRUintn
00808 P12U_Init(char *dir, char *dbprefix, PRBool listonly)
00809 {
00810     SECStatus rv;
00811     PK11_SetPasswordFunc(SECU_GetModulePassword);
00812 
00813     PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
00814     if (listonly && NSS_NoDB_Init("") == SECSuccess) {
00815         rv = SECSuccess;
00816     }
00817     else {
00818         rv = NSS_Initialize(dir,dbprefix,dbprefix,"secmod.db",0);
00819     }
00820     if (rv != SECSuccess) {
00821        SECU_PrintPRandOSError(progName);
00822         exit(-1);
00823     }
00824 
00825     /* setup unicode callback functions */
00826     PORT_SetUCS2_ASCIIConversionFunction(p12u_ucs2_ascii_conversion_function);
00827     /* use the defaults for UCS4-UTF8 and UCS2-UTF8 */
00828 
00829     p12u_EnableAllCiphers();
00830 
00831     return 0;
00832 }
00833 
00834 enum {
00835     opt_CertDir = 0,
00836     opt_TokenName,
00837     opt_Import,
00838     opt_SlotPWFile,
00839     opt_SlotPW,
00840     opt_List,
00841     opt_Nickname,
00842     opt_Export,
00843     opt_P12FilePWFile,
00844     opt_P12FilePW,
00845     opt_DBPrefix,
00846     opt_Debug
00847 };
00848 
00849 static secuCommandFlag pk12util_options[] =
00850 {
00851     { /* opt_CertDir        */ 'd', PR_TRUE,      0, PR_FALSE },
00852     { /* opt_TokenName             */ 'h', PR_TRUE,      0, PR_FALSE },
00853     { /* opt_Import         */ 'i', PR_TRUE,      0, PR_FALSE },
00854     { /* opt_SlotPWFile            */ 'k', PR_TRUE,      0, PR_FALSE },
00855     { /* opt_SlotPW         */ 'K', PR_TRUE,      0, PR_FALSE },
00856     { /* opt_List              */ 'l', PR_TRUE,  0, PR_FALSE },
00857     { /* opt_Nickname              */ 'n', PR_TRUE,      0, PR_FALSE },
00858     { /* opt_Export         */ 'o', PR_TRUE,      0, PR_FALSE },
00859     { /* opt_P12FilePWFile     */ 'w', PR_TRUE,   0, PR_FALSE },
00860     { /* opt_P12FilePW             */ 'W', PR_TRUE,      0, PR_FALSE },
00861     { /* opt_DBPrefix              */ 'P', PR_TRUE,      0, PR_FALSE },
00862     { /* opt_Debug          */ 'v', PR_FALSE, 0, PR_FALSE }
00863 };
00864 
00865 int
00866 main(int argc, char **argv)
00867 {
00868     secuPWData slotPw = { PW_NONE, NULL };
00869     secuPWData p12FilePw = { PW_NONE, NULL };
00870     PK11SlotInfo *slot;
00871     char *slotname = NULL;
00872     char *import_file = NULL;
00873     char *export_file = NULL;
00874     char *dbprefix = "";
00875     SECStatus rv;
00876 
00877     secuCommand pk12util;
00878     pk12util.numCommands = 0;
00879     pk12util.commands = 0;
00880     pk12util.numOptions = sizeof(pk12util_options) / sizeof(secuCommandFlag);
00881     pk12util.options = pk12util_options;
00882 
00883     progName = strrchr(argv[0], '/');
00884     progName = progName ? progName+1 : argv[0];
00885 
00886     rv = SECU_ParseCommandLine(argc, argv, progName, &pk12util);
00887 
00888     if (rv != SECSuccess)
00889        Usage(progName);
00890 
00891     pk12_debugging = pk12util.options[opt_Debug].activated;
00892 
00893     if ((pk12util.options[opt_Import].activated +
00894        pk12util.options[opt_Export].activated +
00895         pk12util.options[opt_List].activated) != 1) {
00896        Usage(progName);
00897     }
00898 
00899     if (pk12util.options[opt_Export].activated &&
00900        !pk12util.options[opt_Nickname].activated) {
00901        Usage(progName);
00902     }
00903 
00904     slotname = SECU_GetOptionArg(&pk12util, opt_TokenName);
00905 
00906     import_file = (pk12util.options[opt_List].activated) ?
00907                     SECU_GetOptionArg(&pk12util, opt_List) :
00908                     SECU_GetOptionArg(&pk12util, opt_Import);
00909     export_file = SECU_GetOptionArg(&pk12util, opt_Export);
00910 
00911     if (pk12util.options[opt_P12FilePWFile].activated) {
00912        p12FilePw.source = PW_FROMFILE;
00913        p12FilePw.data = PL_strdup(pk12util.options[opt_P12FilePWFile].arg);
00914     }
00915 
00916     if (pk12util.options[opt_P12FilePW].activated) {
00917        p12FilePw.source = PW_PLAINTEXT;
00918        p12FilePw.data = PL_strdup(pk12util.options[opt_P12FilePW].arg);
00919     }
00920 
00921     if (pk12util.options[opt_SlotPWFile].activated) {
00922        slotPw.source = PW_FROMFILE;
00923        slotPw.data = PL_strdup(pk12util.options[opt_SlotPWFile].arg);
00924     }
00925 
00926     if (pk12util.options[opt_SlotPW].activated) {
00927        slotPw.source = PW_PLAINTEXT;
00928        slotPw.data = PL_strdup(pk12util.options[opt_SlotPW].arg);
00929     }
00930 
00931     if (pk12util.options[opt_CertDir].activated) {
00932        SECU_ConfigDirectory(pk12util.options[opt_CertDir].arg);
00933     }
00934     if (pk12util.options[opt_DBPrefix].activated) {
00935        dbprefix = pk12util.options[opt_DBPrefix].arg;
00936     }
00937     P12U_Init(SECU_ConfigDirectory(NULL), dbprefix,
00938                 pk12util.options[opt_List].activated);
00939 
00940     if (!slotname || PL_strcmp(slotname, "internal") == 0)
00941        slot = PK11_GetInternalKeySlot();
00942     else
00943        slot = PK11_FindSlotByName(slotname);
00944 
00945     if (!slot) {
00946        SECU_PrintError(progName,"Invalid slot \"%s\"", slotname);
00947        pk12uErrno = PK12UERR_PK11GETSLOT;
00948        goto done;
00949     }
00950 
00951     if (pk12util.options[opt_Import].activated) {
00952        P12U_ImportPKCS12Object(import_file, slot, &slotPw,
00953                                       &p12FilePw);
00954 
00955     } else if (pk12util.options[opt_Export].activated) {
00956        P12U_ExportPKCS12Object(pk12util.options[opt_Nickname].arg,
00957                             export_file, slot, &slotPw, &p12FilePw);
00958         
00959     } else if (pk12util.options[opt_List].activated) {
00960        P12U_ListPKCS12File(import_file, slot, &slotPw, &p12FilePw);
00961                                            
00962     } else {
00963        Usage(progName);
00964        pk12uErrno = PK12UERR_USAGE;
00965     }
00966 
00967 done:
00968     if (slot) PK11_FreeSlot(slot);
00969     if (NSS_Shutdown() != SECSuccess) {
00970        pk12uErrno = 1;
00971     }
00972     return pk12uErrno;
00973 }