ARC SDK
Credential.h
1 #ifndef __ARC_CREDENTIAL_H__
2 #define __ARC_CREDENTIAL_H__
3 
4 #include <stdlib.h>
5 #include <stdexcept>
6 #include <iostream>
7 #include <string>
8 #include <openssl/asn1.h>
9 #include <openssl/pem.h>
10 #include <openssl/x509.h>
11 #include <openssl/x509v3.h>
12 #include <openssl/pkcs12.h>
13 #include <openssl/err.h>
14 
15 #include <arc/Logger.h>
16 #include <arc/DateTime.h>
17 #include <arc/UserConfig.h>
18 
19 #include <arc/credential/CertUtil.h>
20 #include <arc/credential/PasswordSource.h>
21 
22 namespace Arc {
23 
26 
27 
32 class CredentialError : public std::runtime_error {
33  public:
34  // Constructor
38  CredentialError(const std::string& what="");
39 };
40 
41 typedef enum {CRED_PEM, CRED_DER, CRED_PKCS, CRED_UNKNOWN} Credformat;
43 
46 typedef enum { SIGN_DEFAULT = 0, SIGN_SHA1, SIGN_SHA224, SIGN_SHA256, SIGN_SHA384, SIGN_SHA512 } Signalgorithm;
47 
49 extern Logger CredentialLogger;
50 
52 
68 class Credential {
69  public:
73  Credential();
74 
78  Credential(int keybits);
79 
80  virtual ~Credential();
81 
85  Credential(const std::string& CAfile, const std::string& CAkey,
86  const std::string& CAserial,
87  const std::string& extfile, const std::string& extsect,
88  const std::string& passphrase4key);
89 
94  Credential(const std::string& CAfile, const std::string& CAkey,
95  const std::string& CAserial,
96  const std::string& extfile, const std::string& extsect,
97  PasswordSource& passphrase4key);
98 
138  Credential(Time start, Period lifetime = Period("PT12H"),
139  int keybits = 1024, std::string proxyversion = "rfc",
140  std::string policylang = "inheritAll", std::string policy = "",
141  int pathlength = -1);
142 
157  Credential(const std::string& cert, const std::string& key, const std::string& cadir,
158  const std::string& cafile, const std::string& passphrase4key = "",
159  const bool is_file = true);
160 
165  Credential(const std::string& cert, const std::string& key, const std::string& cadir,
166  const std::string& cafile, PasswordSource& passphrase4key,
167  const bool is_file = true);
168 
176  Credential(const UserConfig& usercfg, const std::string& passphrase4key = "");
177 
182  Credential(const UserConfig& usercfg, PasswordSource& passphrase4key);
183 
185  static void InitProxyCertInfo(void);
186 
191  static bool IsCredentialsValid(const UserConfig& usercfg);
192 
194  void AddCertExtObj(std::string& sn, std::string& oid);
195 
197 
200  void SetSigningAlgorithm(Signalgorithm signing_algorithm = SIGN_DEFAULT);
201 
203 
206  void SetKeybits(int keybits = 0);
207 
208  static std::string NoPassword(void) { return std::string("\0",1); };
209 
210  private:
211 
213  Credential(const Credential&);
214 
215  void InitCredential(const std::string& cert, const std::string& key, const std::string& cadir,
216  const std::string& cafile, PasswordSource& passphrase4key, const bool is_file);
217 
219  //void loadKeyString(const std::string& key, EVP_PKEY* &pkey, const std::string& passphrase = "");
220  void loadKeyString(const std::string& key, EVP_PKEY* &pkey, PasswordSource& passphrase);
221  //void loadKeyFile(const std::string& keyfile, EVP_PKEY* &pkey, const std::string& passphrase = "");
222  void loadKeyFile(const std::string& keyfile, EVP_PKEY* &pkey, PasswordSource& passphrase);
223  //void loadKey(BIO* bio, EVP_PKEY* &pkey, const std::string& passphrase = "", const std::string& prompt_info = "", const bool is_file = true);
224 
228  void loadCertificateString(const std::string& cert, X509* &x509, STACK_OF(X509)** certchain);
229  void loadCertificateFile(const std::string& certfile, X509* &x509, STACK_OF(X509)** certchain);
230  //void loadCertificate(BIO* bio, X509* &x509, STACK_OF(X509)** certchain, const bool is_file=true);
231 
233  void InitVerification(void);
234 
239  bool Verify(void);
240 
246  X509_EXTENSION* CreateExtension(const std::string& name, const std::string& data, bool crit = false);
247 
255  bool SetProxyPeriod(X509* tosign, X509* issuer, const Time& start, const Period& lifetime);
256 
260  bool SignRequestAssistant(Credential* proxy, EVP_PKEY* req_pubkey, X509** tosign);
261 
262  public:
264  void LogError(void) const;
265 
266  /************************************/
267  /*****Get information from "this" object**/
268 
270  bool GetVerification(void) const {return verification_valid; };
271 
273  EVP_PKEY* GetPrivKey(void) const;
274 
276  EVP_PKEY* GetPubKey(void) const;
277 
279  X509* GetCert(void) const;
280 
282  X509_REQ* GetCertReq(void) const;
283 
285  STACK_OF(X509)* GetCertChain(void) const;
286 
290  int GetCertNumofChain(void) const;
291 
296  Credformat getFormat_BIO(BIO * in, const bool is_file = true) const;
297  Credformat getFormat_str(const std::string& source) const;
298 
300  std::string GetDN(void) const;
301 
305  std::string GetIdentityName(void) const;
306 
308  ArcCredential::certType GetType(void) const;
309 
311  std::string GetIssuerName(void) const;
312 
316  std::string GetCAName(void) const;
317 
322 
326  int GetKeybits(void) const;
327 
331  std::string GetProxyPolicy(void) const;
332 
336  void SetProxyPolicy(const std::string& proxyversion, const std::string& policylang,
337  const std::string& policy, int pathlength);
338 
344  bool OutputPrivatekey(std::string &content, bool encryption = false, const std::string& passphrase ="");
345 
352  bool OutputPrivatekey(std::string &content, bool encryption, PasswordSource& passphrase);
353 
355  bool OutputPublickey(std::string &content);
356 
361  bool OutputCertificate(std::string &content, bool is_der=false);
362 
367  bool OutputCertificateChain(std::string &content, bool is_der=false);
368 
370  Period GetLifeTime(void) const;
371 
373  Time GetStartTime() const;
374 
376  Time GetEndTime() const;
377 
379  void SetLifeTime(const Period& period);
380 
382  void SetStartTime(const Time& start_time);
383 
385  bool IsValid(void);
386 
387  /************************************/
388  /*****Generate certificate request, add certificate extension, inquire certificate request,
389  *and sign certificate request
390  **/
391 
398  bool AddExtension(const std::string& name, const std::string& data, bool crit = false);
399 
414  bool AddExtension(const std::string& name, char** binary);
415 
421  std::string GetExtension(const std::string& name);
422 
429  bool GenerateEECRequest(BIO* reqbio, BIO* keybio, const std::string& dn = "");
430 
432  bool GenerateEECRequest(std::string &reqcontent, std::string &keycontent, const std::string& dn = "");
433 
435  bool GenerateEECRequest(const char* request_filename, const char* key_filename, const std::string& dn = "");
436 
441  bool GenerateRequest(BIO* bio, bool if_der = false);
442 
444  bool GenerateRequest(std::string &content, bool if_der = false);
445 
447  bool GenerateRequest(const char* filename, bool if_der = false);
448 
457  bool InquireRequest(BIO* reqbio, bool if_eec = false, bool if_der = false);
458 
460  bool InquireRequest(std::string &content, bool if_eec = false, bool if_der = false);
461 
463  bool InquireRequest(const char* filename, bool if_eec = false, bool if_der = false);
464 
471  bool SignRequest(Credential* proxy, BIO* outputbio, bool if_der = false);
472 
478  bool SignRequest(Credential* proxy, std::string &content, bool if_der = false);
479 
485  bool SignRequest(Credential* proxy, const char* filename, bool if_der = false);
486 
494  bool SelfSignEECRequest(const std::string& dn, const char* extfile, const std::string& extsect, const char* certfile);
495 
496  //The following three methods is about signing an EEC certificate by implementing the same
497  //functionality as a normal CA
499  bool SignEECRequest(Credential* eec, const std::string& dn, BIO* outputbio);
500 
502  bool SignEECRequest(Credential* eec, const std::string& dn, std::string &content);
503 
505  bool SignEECRequest(Credential* eec, const std::string& dn, const char* filename);
506 
507  private:
508  // PKI files
509  std::string cacertfile_;
510  std::string cacertdir_;
511  std::string certfile_;
512  std::string keyfile_;
513 
514  // Verification context
516 
517  //Verification result
518  bool verification_valid;
519 
520  //Certificate structures
521  X509 * cert_; //certificate
522  ArcCredential::certType cert_type_;
523  EVP_PKEY * pkey_; //private key
524  STACK_OF(X509) * cert_chain_; //certificates chain which is parsed
525  //from the certificate, after
526  //verification, the ca certificate
527  //will be included
528  ArcCredential::PROXYCERTINFO* proxy_cert_info_;
529  Credformat format;
530  Time start_;
531  Period lifetime_;
532 
533  //Certificate request
534  X509_REQ* req_;
535  RSA* rsa_key_;
536  EVP_MD* signing_alg_;
537  int keybits_;
538 
539  //Proxy policy
540  std::string proxyversion_;
541  std::string policy_;
542  std::string policylang_;
543  int proxyver_;
544  int pathlength_;
545 
546  //Extensions for certificate, such as certificate policy, attributes, etc.
547  STACK_OF(X509_EXTENSION)* extensions_;
548 
549  //CA functionality related information
550  std::string CAserial_;
551  std::string extfile_;
552  std::string extsect_;
553 
554  static X509_NAME *parse_name(char *subject, long chtype, int multirdn);
555 };
556 
557 }// namespace Arc
558 
559 #endif /* __ARC_CREDENTIAL_H__ */
560