/* * Copyright (c) 2004 Apple Computer, Inc. All Rights Reserved. * * @APPLE_LICENSE_HEADER_START@ * * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ * * SecWrappedKeys.cpp - SecExportRep and SecImportRep methods dealing with * wrapped private keys (other than PKCS8 format). */ #include "SecExternalRep.h" #include "SecImportExportUtils.h" #include "SecImportExportPem.h" #include "SecImportExportCrypto.h" #include #include #include #include #include #include #include #include using namespace Security; using namespace KeychainCore; static int hexToDigit( char digit, uint8 *rtn) // RETURNED { if((digit >= '0') && (digit <= '9')) { *rtn = digit - '0'; return 0; } if((digit >= 'a') && (digit <= 'f')) { *rtn = digit - 'a' + 10; return 0; } if((digit >= 'A') && (digit <= 'F')) { *rtn = digit - 'A' + 10; return 0; } return -1; } /* * Convert two ascii characters starting at cp to an unsigned char. * Returns nonzero on error. */ static int hexToUchar( const char *cp, uint8 *rtn) // RETURNED { uint8 rtnc = 0; uint8 c; if(hexToDigit(*cp++, &c)) { return -1; } rtnc = c << 4; if(hexToDigit(*cp, &c)) { return -1; } rtnc |= c; *rtn = rtnc; return 0; } /* * Given an array of PEM parameter lines, infer parameters for key derivation and * encryption. */ static OSStatus opensslPbeParams( CFArrayRef paramLines, // elements are CFStrings SecNssCoder &coder, // IV allocd with this /* remaining arguments RETURNED */ CSSM_ALGORITHMS &pbeAlg, CSSM_ALGORITHMS &keyAlg, CSSM_ALGORITHMS &encrAlg, CSSM_ENCRYPT_MODE &encrMode, CSSM_PADDING &encrPad, uint32 &keySizeInBits, unsigned &blockSizeInBytes, CSSM_DATA &iv) { /* * This format requires PEM parameter lines. We could have gotten here * without them if caller specified wrong format. */ if(paramLines == NULL) { SecImpExpDbg("importWrappedKeyOpenssl: no PEM parameter lines"); return errSecUnknownFormat; } CFStringRef dekInfo = NULL; CFIndex numLines = CFArrayGetCount(paramLines); for(CFIndex dex=0; dex