2 * Copyright (c) 2003-2004,2011-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 #include "SecPkcs12.h"
28 #include "pkcs12Coder.h"
29 #include "pkcs12BagAttrs.h"
30 #include "pkcs12SafeBag.h"
31 #include "pkcs12Utils.h"
32 #include <security_cdsa_utilities/cssmerrors.h>
33 #include <Security/SecBasePriv.h>
36 * API function call wrappers, impermeable to C++ exceptions
38 #define BEGIN_P12API \
43 catch (const MacOSError &err) { return err.osStatus(); } \
44 catch (const CommonError &err) { return SecKeychainErrFromOSStatus(err.osStatus()); } \
45 catch (const std::bad_alloc &) { return errSecAllocate; } \
46 catch (...) { return errSecInternalComponent; } \
49 /* catch incoming NULL parameters */
50 static inline void required(
54 MacOSError::throwMe(errSecParam
);
59 * Standard means of casting a SecPkcs12CoderRef to a P12Coder *
61 static inline P12Coder
*P12CoderCast(
62 SecPkcs12CoderRef coder
)
65 return reinterpret_cast<P12Coder
*>(coder
);
69 * Standard means of casting a SecPkcs12AttrsRef to a P12BagAttrs *
70 * This one uses the P12BagAttrsStandAlone version, not tied to
71 * a specific P12Coder (actually, to a P12Coder's SecNssCoder).
73 static inline P12BagAttrsStandAlone
*P12AttrsCast(
74 SecPkcs12AttrsRef attrs
)
77 MacOSError::throwMe(errSecParam
);
79 return reinterpret_cast<P12BagAttrsStandAlone
*>(attrs
);
82 /* optional flavor used in SecPkcs12Add*() */
83 static inline P12BagAttrs
*P12AttrsCastOpt(
84 SecPkcs12AttrsRef attrs
)
86 return reinterpret_cast<P12BagAttrs
*>(attrs
);
89 #pragma mark --- SecPkcs12CoderRef create/destroy ---
92 * Basic SecPkcs12CoderRef create/destroy.
94 OSStatus
SecPkcs12CoderCreate(
95 SecPkcs12CoderRef
*coder
) // RETURNED
100 P12Coder
*p12coder
= new P12Coder
;
107 * Destroy object created in SecPkcs12CoderCreate.
108 * This will go away if we make this object a CoreFoundation type.
110 OSStatus
SecPkcs12CoderRelease(
111 SecPkcs12CoderRef coder
)
115 P12Coder
*p12coder
= P12CoderCast(coder
);
121 OSStatus
SecPkcs12SetMACPassphrase(
122 SecPkcs12CoderRef coder
,
123 CFStringRef passphrase
)
127 P12Coder
*p12coder
= P12CoderCast(coder
);
128 required(passphrase
);
129 p12coder
->setMacPassPhrase(passphrase
);
134 OSStatus
SecPkcs12SetMACPassKey(
135 SecPkcs12CoderRef coder
,
136 const CSSM_KEY
*passKey
)
140 P12Coder
*p12coder
= P12CoderCast(coder
);
142 p12coder
->setMacPassKey(passKey
);
148 * Specify separate passphrase for encrypt/decrypt.
150 OSStatus
SecPkcs12SetCryptPassphrase(
151 SecPkcs12CoderRef coder
,
152 CFStringRef passphrase
)
156 P12Coder
*p12coder
= P12CoderCast(coder
);
157 required(passphrase
);
158 p12coder
->setEncrPassPhrase(passphrase
);
163 OSStatus
SecPkcs12SetCryptPassKey(
164 SecPkcs12CoderRef coder
,
165 const CSSM_KEY
*passKey
)
169 P12Coder
*p12coder
= P12CoderCast(coder
);
171 p12coder
->setEncrPassKey(passKey
);
178 * Target location of decoded keys and certs.
180 OSStatus
SecPkcs12SetKeychain(
181 SecPkcs12CoderRef coder
,
182 SecKeychainRef keychain
)
186 P12Coder
*p12coder
= P12CoderCast(coder
);
188 p12coder
->setKeychain(keychain
);
194 * Required iff SecPkcs12SetKeychain() not called.
196 OSStatus
SecPkcs12SetCspHandle(
197 SecPkcs12CoderRef coder
,
198 CSSM_CSP_HANDLE cspHandle
)
202 P12Coder
*p12coder
= P12CoderCast(coder
);
203 p12coder
->setCsp(cspHandle
);
208 OSStatus
SecPkcs12SetImportToKeychain(
209 SecPkcs12CoderRef coder
,
210 SecPkcs12ImportFlags flags
)
214 P12Coder
*p12coder
= P12CoderCast(coder
);
215 p12coder
->importFlags(flags
);
220 OSStatus
SecPkcs12GetImportToKeychain(
221 SecPkcs12CoderRef coder
,
222 SecPkcs12ImportFlags
*flags
) // RETURNED
226 P12Coder
*p12coder
= P12CoderCast(coder
);
228 *flags
= p12coder
->importFlags();
233 OSStatus
SecPkcs12ExportKeychainItems(
234 SecPkcs12CoderRef coder
,
239 P12Coder
*p12coder
= P12CoderCast(coder
);
241 p12coder
->exportKeychainItems(items
);
246 OSStatus
SecPkcs12SetAccess(
247 SecPkcs12CoderRef coder
,
252 P12Coder
*p12coder
= P12CoderCast(coder
);
253 p12coder
->setAccess(access
);
258 OSStatus
SecPkcs12SetKeyUsage(
259 SecPkcs12CoderRef coder
,
260 CSSM_KEYUSE keyUsage
)
264 P12Coder
*p12coder
= P12CoderCast(coder
);
265 p12coder
->setKeyUsage(keyUsage
);
270 OSStatus
SecPkcs12SetKeyAttrs(
271 SecPkcs12CoderRef coder
,
272 CSSM_KEYATTR_FLAGS keyAttrs
)
276 P12Coder
*p12coder
= P12CoderCast(coder
);
277 p12coder
->setKeyAttrs(keyAttrs
);
282 #pragma mark --- Decoder Functions ---
287 OSStatus
SecPkcs12Decode(
288 SecPkcs12CoderRef coder
,
293 P12Coder
*p12coder
= P12CoderCast(coder
);
296 p12coder
->decode(pfx
);
299 /* abort - clean up - delete stored keys */
300 p12coder
->deleteDecodedItems();
307 * Subsequent to decoding, obtain the components.
308 * These functions can also be used as "getter" functions while encoding.
312 OSStatus
SecPkcs12CertificateCount(
313 SecPkcs12CoderRef coder
,
314 CFIndex
*numCerts
) // RETURNED
318 P12Coder
*p12coder
= P12CoderCast(coder
);
320 *numCerts
= p12coder
->numCerts();
325 OSStatus
SecPkcs12CopyCertificate(
326 SecPkcs12CoderRef coder
,
328 SecCertificateRef
*secCert
, // RETURNED
329 CFStringRef
*friendlyName
, // RETURNED
330 CFDataRef
*localKeyId
, // RETURNED
331 SecPkcs12AttrsRef
*attrs
) // RETURNED
335 P12Coder
*p12coder
= P12CoderCast(coder
);
337 /* others are optional - if NULL, we don't return that param */
338 P12CertBag
*bag
= p12coder
->getCert((unsigned)certNum
);
339 *secCert
= bag
->getSecCert();
341 /* now the optional attrs */
342 P12BagAttrs
*p12Attrs
= NULL
;
343 bag
->copyAllAttrs(friendlyName
, localKeyId
,
344 attrs
? &p12Attrs
: NULL
);
352 * CRLs. The might change if a SecCrl type is defined elsewhere.
353 * We'll typedef it here to preserve the semantics of this function.
355 OSStatus
SecPkcs12CrlCount(
356 SecPkcs12CoderRef coder
,
357 CFIndex
*numCrls
) // RETURNED
361 P12Coder
*p12coder
= P12CoderCast(coder
);
363 *numCrls
= p12coder
->numCrls();
368 OSStatus
SecPkcs12CopyCrl(
369 SecPkcs12CoderRef coder
,
371 SecCrlRef
*crl
, // RETURNED
372 CFStringRef
*friendlyName
, // RETURNED
373 CFDataRef
*localKeyId
, // RETURNED
374 SecPkcs12AttrsRef
*attrs
) // RETURNED
378 P12Coder
*p12coder
= P12CoderCast(coder
);
380 /* others are optional - if NULL, we don't return that param */
381 P12CrlBag
*bag
= p12coder
->getCrl((unsigned)crlNum
);
382 *crl
= p12CssmDataToCf(bag
->crlData());
384 /* now the optional attrs */
385 P12BagAttrs
*p12Attrs
= NULL
;
386 bag
->copyAllAttrs(friendlyName
, localKeyId
,
387 attrs
? &p12Attrs
: NULL
);
398 OSStatus
SecPkcs12PrivateKeyCount(
399 SecPkcs12CoderRef coder
,
400 CFIndex
*numKeys
) // RETURNED
404 P12Coder
*p12coder
= P12CoderCast(coder
);
406 *numKeys
= p12coder
->numKeys();
411 OSStatus
SecPkcs12CopyPrivateKey(
412 SecPkcs12CoderRef coder
,
414 SecKeyRef
*privateKey
, // RETURNED
415 CFStringRef
*friendlyName
, // RETURNED
416 CFDataRef
*localKeyId
, // RETURNED
417 SecPkcs12AttrsRef
*attrs
) // RETURNED
420 /*P12Coder *p12coder = P12CoderCast(coder); */
421 return errSecUnimplemented
;
425 OSStatus
SecPkcs12GetCssmPrivateKey(
426 SecPkcs12CoderRef coder
,
428 CSSM_KEY_PTR
*privateKey
, // RETURNED
429 CFStringRef
*friendlyName
, // RETURNED
430 CFDataRef
*localKeyId
, // RETURNED
431 SecPkcs12AttrsRef
*attrs
) // RETURNED
434 P12Coder
*p12coder
= P12CoderCast(coder
);
435 required(privateKey
);
436 /* others are optional - if NULL, we don't return that param */
437 P12KeyBag
*bag
= p12coder
->getKey((unsigned)keyNum
);
438 *privateKey
= bag
->key();
440 /* now the optional attrs */
441 P12BagAttrs
*p12Attrs
= NULL
;
442 bag
->copyAllAttrs(friendlyName
, localKeyId
,
443 attrs
? &p12Attrs
: NULL
);
452 * Catch-all for other components not currently understood
453 * or supported by this library. An "opaque blob" component
454 * is identified by an OID and is obtained as an opaque data
457 OSStatus
SecPkcs12OpaqueBlobCount(
458 SecPkcs12CoderRef coder
,
459 CFIndex
*numBlobs
) // RETURNED
463 P12Coder
*p12coder
= P12CoderCast(coder
);
465 *numBlobs
= p12coder
->numOpaqueBlobs();
470 OSStatus
SecPkcs12CopyOpaqueBlob(
471 SecPkcs12CoderRef coder
,
473 CFDataRef
*blobOid
, // RETURNED
474 CFDataRef
*opaqueBlob
, // RETURNED
475 CFStringRef
*friendlyName
, // RETURNED
476 CFDataRef
*localKeyId
, // RETURNED
477 SecPkcs12AttrsRef
*attrs
) // RETURNED
481 P12Coder
*p12coder
= P12CoderCast(coder
);
483 required(opaqueBlob
);
485 /* others are optional - if NULL, we don't return that param */
486 P12OpaqueBag
*bag
= p12coder
->getOpaque((unsigned)blobNum
);
487 *opaqueBlob
= p12CssmDataToCf(bag
->blob());
488 *blobOid
= p12CssmDataToCf(bag
->oid());
490 /* now the optional attrs */
491 P12BagAttrs
*p12Attrs
= NULL
;
492 bag
->copyAllAttrs(friendlyName
, localKeyId
,
493 attrs
? &p12Attrs
: NULL
);
501 #pragma mark --- Encoder Functions ---
504 * This the final step to create an encoded PKCS12 PFX blob,
505 * after calling some number of SecPkcs12Set* functions below.
506 * The result is a DER_encoded PFX in PKCS12 lingo.
508 OSStatus
SecPkcs12Encode(
509 SecPkcs12CoderRef coder
,
510 CFDataRef
*pfx
) // RETURNED
513 P12Coder
*p12coder
= P12CoderCast(coder
);
515 p12coder
->encode(pfx
);
520 * Add individual components. "Getter" functions are available
521 * as described above (under "Functions used for decoding").
523 OSStatus
SecPkcs12AddCertificate(
524 SecPkcs12CoderRef coder
,
525 SecCertificateRef cert
,
526 CFStringRef friendlyName
, // optional
527 CFDataRef localKeyId
, // optional
528 SecPkcs12AttrsRef attrs
) // optional
531 P12Coder
*p12coder
= P12CoderCast(coder
);
534 OSStatus ortn
= SecCertificateGetData(cert
, &certData
);
538 CSSM_CERT_TYPE certType
;
539 ortn
= SecCertificateGetType(cert
, &certType
);
543 NSS_P12_CertBagType type
;
545 case CSSM_CERT_X_509v1
:
546 case CSSM_CERT_X_509v2
:
547 case CSSM_CERT_X_509v3
:
550 case CSSM_CERT_SDSIv1
:
557 P12CertBag
*bag
= new P12CertBag(type
, certData
, friendlyName
,
558 localKeyId
, P12AttrsCastOpt(attrs
), p12coder
->coder());
559 p12coder
->addCert(bag
);
563 OSStatus
SecPkcs12AddCrl(
564 SecPkcs12CoderRef coder
,
566 CFStringRef friendlyName
, // optional
567 CFDataRef localKeyId
, // optional
568 SecPkcs12AttrsRef attrs
) // optional
571 P12Coder
*p12coder
= P12CoderCast(coder
);
573 P12CrlBag
*bag
= new P12CrlBag(CRT_X509
, crl
, friendlyName
,
574 localKeyId
, P12AttrsCastOpt(attrs
), p12coder
->coder());
575 p12coder
->addCrl(bag
);
579 OSStatus
SecPkcs12AddPrivateKey(
580 SecPkcs12CoderRef coder
,
581 SecKeyRef privateKey
,
582 CFStringRef friendlyName
, // optional
583 CFDataRef localKeyId
, // optional
584 SecPkcs12AttrsRef attrs
) // optional
588 P12Coder
*p12coder
= P12CoderCast(coder
);
589 required(privateKey
);
590 const CSSM_KEY
*cssmKey
;
591 OSStatus ortn
= SecKeyGetCSSMKey(privateKey
, &cssmKey
);
595 P12KeyBag
*bag
= new P12KeyBag(cssmKey
, p12coder
->cspHand(),
596 friendlyName
, localKeyId
, P12AttrsCastOpt(attrs
), p12coder
->coder());
597 p12coder
->addKey(bag
);
603 OSStatus
SecPkcs12AddCssmPrivateKey(
604 SecPkcs12CoderRef coder
,
605 CSSM_KEY_PTR cssmKey
,
606 CFStringRef friendlyName
, // optional
607 CFDataRef localKeyId
, // optional
608 SecPkcs12AttrsRef attrs
) // optional
612 P12Coder
*p12coder
= P12CoderCast(coder
);
614 P12KeyBag
*bag
= new P12KeyBag(cssmKey
, p12coder
->cspHand(),
615 friendlyName
, localKeyId
, P12AttrsCastOpt(attrs
), p12coder
->coder());
616 p12coder
->addKey(bag
);
622 OSStatus
SecPkcs12AddOpaqueBlob(
623 SecPkcs12CoderRef coder
,
625 CFDataRef opaqueBlob
,
626 CFStringRef friendlyName
, // optional
627 CFDataRef localKeyId
, // optional
628 SecPkcs12AttrsRef attrs
) // optional
632 P12Coder
*p12coder
= P12CoderCast(coder
);
634 required(opaqueBlob
);
635 P12OpaqueBag
*bag
= new P12OpaqueBag(blobOid
, opaqueBlob
, friendlyName
,
636 localKeyId
, P12AttrsCastOpt(attrs
), p12coder
->coder());
637 p12coder
->addOpaque(bag
);
642 #pragma mark --- Optional Functions ---
645 *** SecPkcs12AttrsRef manipulation. Optional and in fact expected to
646 *** be rarely used, if ever.
652 OSStatus
SecPkcs12AttrsCreate(
653 SecPkcs12AttrsRef
*attrs
) // RETURNED
658 P12BagAttrsStandAlone
*bagAttrs
= new P12BagAttrsStandAlone
;
659 *attrs
= (SecPkcs12AttrsRef
)bagAttrs
;
664 OSStatus
SecPkcs12AttrsRelease(
665 SecPkcs12AttrsRef attrs
)
669 P12BagAttrsStandAlone
*bagAttrs
= P12AttrsCast(attrs
);
676 * Add an OID/value set to an existing SecPkcs12AttrsRef.
677 * Values are a CFArray containing an arbitrary number of
680 OSStatus
SecPkcs12AttrsAddAttr(
681 SecPkcs12AttrsRef attrs
,
683 CFArrayRef attrValues
)
687 P12BagAttrsStandAlone
*bagAttrs
= P12AttrsCast(attrs
);
688 bagAttrs
->addAttr(attrOid
, attrValues
);
693 OSStatus
SecPkcs12AttrCount(
694 SecPkcs12AttrsRef attrs
,
695 CFIndex
*numAttrs
) // RETURNED
699 P12BagAttrsStandAlone
*bagAttrs
= P12AttrsCast(attrs
);
701 *numAttrs
= bagAttrs
->numAttrs();
707 * Obtain n'th oid/value set from an existing SecPkcs12AttrsRef.
709 OSStatus
SecPkcs12AttrsGetAttr(
710 SecPkcs12AttrsRef attrs
,
712 CFDataRef
*attrOid
, // RETURNED
713 CFArrayRef
*attrValues
) // RETURNED
717 P12BagAttrsStandAlone
*bagAttrs
= P12AttrsCast(attrs
);
719 required(attrValues
);
720 bagAttrs
->getAttr((unsigned)attrNum
, attrOid
, attrValues
);
724 OSStatus
SecPkcs12SetIntegrityMode(
725 SecPkcs12CoderRef coder
,
730 P12Coder
*p12coder
= P12CoderCast(coder
);
731 p12coder
->integrityMode(mode
);
736 OSStatus
SecPkcs12GetIntegrityMode(
737 SecPkcs12CoderRef coder
,
738 SecPkcs12Mode
*mode
) // RETURNED
742 P12Coder
*p12coder
= P12CoderCast(coder
);
744 *mode
= p12coder
->integrityMode();
749 OSStatus
SecPkcs12SetPrivacyMode(
750 SecPkcs12CoderRef coder
,
755 P12Coder
*p12coder
= P12CoderCast(coder
);
756 p12coder
->privacyMode(mode
);
761 OSStatus
SecPkcs12GetPrivacyMode(
762 SecPkcs12CoderRef coder
,
763 SecPkcs12Mode
*mode
) // RETURNED
767 P12Coder
*p12coder
= P12CoderCast(coder
);
769 *mode
= p12coder
->privacyMode();
775 *** Encryption algorithms
777 OSStatus
SecPkcs12SetKeyEncryptionAlg(
778 SecPkcs12CoderRef coder
,
779 CFDataRef encryptionAlg
)
783 P12Coder
*p12coder
= P12CoderCast(coder
);
784 required(encryptionAlg
);
785 p12coder
->strongEncrAlg(encryptionAlg
);
790 OSStatus
SecPkcs12SetCertCrlEncryptionAlg(
791 SecPkcs12CoderRef coder
,
792 CFDataRef encryptionAlg
)
796 P12Coder
*p12coder
= P12CoderCast(coder
);
797 required(encryptionAlg
);
798 p12coder
->weakEncrAlg(encryptionAlg
);
803 OSStatus
SecPkcs12SetKeyEncryptionIterCount(
804 SecPkcs12CoderRef coder
,
809 P12Coder
*p12coder
= P12CoderCast(coder
);
810 p12coder
->strongEncrIterCount(iterCount
);
815 OSStatus
SecPkcs12SetCertCrlEncryptionIterCount(
816 SecPkcs12CoderRef coder
,
821 P12Coder
*p12coder
= P12CoderCast(coder
);
822 p12coder
->weakEncrIterCount(iterCount
);
827 OSStatus
SecPkcs12SetMacIterCount(
828 SecPkcs12CoderRef coder
,
833 P12Coder
*p12coder
= P12CoderCast(coder
);
834 p12coder
->macEncrIterCount(iterCount
);
839 OSStatus
SecPkcs12CopyKeyEncryptionAlg(
840 SecPkcs12CoderRef coder
,
841 CFDataRef
*encryptionAlg
) // RETURNED
845 P12Coder
*p12coder
= P12CoderCast(coder
);
846 required(encryptionAlg
);
847 *encryptionAlg
= p12coder
->strongEncrAlg();
852 OSStatus
SecPkcs12CopyCertCrlEncryptionAlg(
853 SecPkcs12CoderRef coder
,
854 CFDataRef
*encryptionAlg
) // RETURNED
858 P12Coder
*p12coder
= P12CoderCast(coder
);
859 required(encryptionAlg
);
860 *encryptionAlg
= p12coder
->weakEncrAlg();
865 OSStatus
SecPkcs12CopyKeyEncryptionIterCount(
866 SecPkcs12CoderRef coder
,
867 unsigned *iterCount
) // RETURNED
871 P12Coder
*p12coder
= P12CoderCast(coder
);
873 *iterCount
= p12coder
->strongEncrIterCount();
878 OSStatus
SecPkcs12CopyCertCrlEncryptionIterCount(
879 SecPkcs12CoderRef coder
,
880 unsigned *iterCount
) // RETURNED
884 P12Coder
*p12coder
= P12CoderCast(coder
);
886 *iterCount
= p12coder
->weakEncrIterCount();
891 OSStatus
SecPkcs12CopyMacIterCount(
892 SecPkcs12CoderRef coder
,
893 unsigned *iterCount
) // RETURNED
897 P12Coder
*p12coder
= P12CoderCast(coder
);
899 *iterCount
= p12coder
->macEncrIterCount();
904 OSStatus
SecPkcs12LimitPrivateKeyImport(
905 SecPkcs12CoderRef coder
,
910 P12Coder
*p12coder
= P12CoderCast(coder
);
911 p12coder
->limitPrivKeyImport(foundOneKey
);