1/*
2 * Copyright (c) 2000-2001,2011,2014 Apple Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19//
20// signclient - client interface to CSSM sign/verify contexts
21//
22#ifndef _H_CDSA_CLIENT_SIGNCLIENT
23#define _H_CDSA_CLIENT_SIGNCLIENT  1
24
25#include <security_cdsa_client/cspclient.h>
26#include <security_cdsa_client/keyclient.h>
27
28namespace Security {
29namespace CssmClient {
30
31
32//
33// A signing/verifying context
34//
35class SigningContext : public Context
36{
37public:
38	SigningContext(const CSP &csp, CSSM_ALGORITHMS alg, CSSM_ALGORITHMS signOnly = CSSM_ALGID_NONE)
39	: Context(csp, alg), mSignOnly(signOnly) { }
40
41	Key key() const { assert(mKey); return mKey; }
42	void key(const Key &k) { mKey = k; set(CSSM_ATTRIBUTE_KEY, mKey); }
43
44    CSSM_ALGORITHMS signOnlyAlgorithm() const	{ return mSignOnly; }
45    void signOnlyAlgorithm(CSSM_ALGORITHMS alg)	{ mSignOnly = alg; }
46
47protected:
48	void activate();
49	CSSM_ALGORITHMS mSignOnly;
50	Key mKey;
51};
52
53
54class Sign : public SigningContext
55{
56public:
57	Sign(const CSP &csp, CSSM_ALGORITHMS alg, CSSM_ALGORITHMS signOnly = CSSM_ALGID_NONE)
58        : SigningContext(csp, alg, signOnly) { }
59
60	// integrated
61	void sign(const CssmData &data, CssmData &signature) { sign(&data, 1, signature); }
62	void sign(const CssmData *data, uint32 count, CssmData &signature);
63
64	// staged
65	void init(); // Optional
66	void sign(const CssmData &data) { sign(&data, 1); }
67	void sign(const CssmData *data, uint32 count);
68	void operator () (CssmData &signature);
69	CssmData operator () () { CssmData signature; (*this)(signature); return signature; }
70};
71
72class Verify : public SigningContext
73{
74public:
75	Verify(const CSP &csp, CSSM_ALGORITHMS alg, CSSM_ALGORITHMS verifyOnly = CSSM_ALGID_NONE)
76        : SigningContext(csp, alg, verifyOnly) { }
77
78	// integrated
79	void verify(const CssmData &data, const CssmData &signature) { verify(&data, 1, signature); }
80	void verify(const CssmData *data, uint32 count, const CssmData &signature);
81
82	// staged
83	void init(); // Optional
84	void verify(const CssmData &data) { verify(&data, 1); }
85	void verify(const CssmData *data, uint32 count);
86	void operator () (const CssmData &signature);
87};
88
89} // end namespace CssmClient
90
91} // end namespace Security
92
93#endif // _H_CDSA_CLIENT_SIGNCLIENT
94