/* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * 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 the Initial Developer are Copyright (C) 2000 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Ian McGreer <[email protected]> * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ #include "net/third_party/mozilla_security_manager/nsPKCS12Blob.h" #include <pk11pub.h> #include <pkcs12.h> #include <p12plcy.h> #include <secerr.h> #include "base/lazy_instance.h" #include "base/logging.h" #include "base/strings/string_util.h" #include "crypto/nss_util_internal.h" #include "net/base/net_errors.h" namespace mozilla_security_manager { namespace { // unicodeToItem // // For the NSS PKCS#12 library, must convert char16_ts (shorts) to // a buffer of octets. Must handle byte order correctly. // TODO: Is there a Mozilla way to do this? In the string lib? void unicodeToItem(const char16_t* uni, SECItem* item) { … } // write_export_data // write bytes to the exported PKCS#12 data buffer void write_export_data(void* arg, const char* buf, unsigned long len) { … } // nickname_collision // what to do when the nickname collides with one already in the db. // Based on P12U_NicknameCollisionCallback from nss/cmd/pk12util/pk12util.c SECItem* PR_CALLBACK nickname_collision(SECItem *old_nick, PRBool *cancel, void *wincx) { … } // pip_ucs2_ascii_conversion_fn // required to be set by NSS (to do PKCS#12), but since we've already got // unicode make this a no-op. PRBool pip_ucs2_ascii_conversion_fn(PRBool toUnicode, unsigned char *inBuf, unsigned int inBufLen, unsigned char *outBuf, unsigned int maxOutBufLen, unsigned int *outBufLen, PRBool swapBytes) { … } // Based on nsPKCS12Blob::ImportFromFileHelper. int nsPKCS12Blob_ImportHelper(const char* pkcs12_data, size_t pkcs12_len, const std::u16string& password, bool is_extractable, bool try_zero_length_secitem, PK11SlotInfo* slot, net::ScopedCERTCertificateList* imported_certs) { … } // Attempt to read the CKA_EXTRACTABLE attribute on a private key inside // a token. On success, store the attribute in |extractable| and return // SECSuccess. SECStatus isExtractable(SECKEYPrivateKey *privKey, PRBool *extractable) { … } class PKCS12InitSingleton { … }; // Leaky so it can be initialized on worker threads and because there is no // cleanup necessary. static base::LazyInstance<PKCS12InitSingleton>::Leaky g_pkcs12_init_singleton = …; } // namespace void EnsurePKCS12Init() { … } // Based on nsPKCS12Blob::ImportFromFile. int nsPKCS12Blob_Import(PK11SlotInfo* slot, const char* pkcs12_data, size_t pkcs12_len, const std::u16string& password, bool is_extractable, net::ScopedCERTCertificateList* imported_certs) { … } // Based on nsPKCS12Blob::ExportToFile // // Having already loaded the certs, form them into a blob (loading the keys // also), encode the blob, and stuff it into the file. // // TODO: handle slots correctly // mirror "slotToUse" behavior from PSM 1.x // verify the cert array to start off with? // set appropriate error codes int nsPKCS12Blob_Export(std::string* output, const net::ScopedCERTCertificateList& certs, const std::u16string& password) { … } } // namespace mozilla_security_manager