X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/80e2389990082500d76eb566d4946be3e786c3ef..d8f41ccd20de16f8ebe2ccc84d47bf1cb2b26bbb:/Security/libsecurity_keychain/lib/SecWrappedKeys.cpp?ds=inline diff --git a/Security/libsecurity_keychain/lib/SecWrappedKeys.cpp b/Security/libsecurity_keychain/lib/SecWrappedKeys.cpp new file mode 100644 index 00000000..e42f34c2 --- /dev/null +++ b/Security/libsecurity_keychain/lib/SecWrappedKeys.cpp @@ -0,0 +1,494 @@ +/* + * Copyright (c) 2004,2011-2014 Apple Inc. All Rights Reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * 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 + +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