Changeset 1621520
- Timestamp:
- 05/21/12 00:10:44 (12 months ago)
- Branches:
- mei_rt2, mei_rt2_fix_1
- Children:
- adc0815
- Parents:
- ed3dc05
- git-author:
- Mei <mei@…> (05/21/12 00:10:44)
- git-committer:
- Mei <mei@…> (05/21/12 00:10:44)
- File:
-
- 1 edited
-
libabac/abac.hh (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libabac/abac.hh
red3dc05 r1621520 587 587 588 588 589 /*** 590 ABAC::ID 591 An ID holds a principal credential. It maybe imported from an existing 592 ID credential via external files, constructed from a streaming chunk, 593 or instantiated on the fly 594 ***/ 589 595 class ID { 590 596 public: 597 /*** 598 ID() 599 default constructor, do not use, for swig only 600 ID(const ID &) 601 copy constructor, used for cloning an ID 602 ~ID() 603 default destructor 604 ***/ 591 605 ID() : m_id(NULL) { } // do not use: here for swig 592 ID(abac_id_t *id): m_id(abac_id_dup(id))593 { }594 ID(abac_id_credential_t *idcred) {595 if(idcred)596 m_id=abac_id_dup(abac_id_credential_id(idcred));597 else m_id=NULL;598 }599 606 ID(const ID &id) { m_id =abac_id_dup(id.m_id); } 600 607 ~ID() { if(m_id) abac_id_free(m_id); } 601 602 /* load an ID cert from a file 603 Will throw an exception if the cert cannot be loaded */ 608 /*** ??? 609 ID(abac_id_t *) 610 constructor from abac_id_t 611 ID(abac_id_credential_t *) 612 constructor from abac_id_t 613 ***/ 614 ID(abac_id_t *id): m_id(abac_id_dup(id)) 615 { } 616 ID(abac_id_credential_t *idcred) { 617 if(idcred) 618 m_id=abac_id_dup(abac_id_credential_id(idcred)); 619 else m_id=NULL; 620 } 621 622 /*** 623 ID(char *) 624 load an ID cert from a file, will throw an exception 625 if the cert cannot be loaded 626 ***/ 604 627 ID(char *filename) { 605 m_id=abac_id_from_file(filename); 606 if(m_id==NULL) 607 abac_errx(1, "Id creation from filename failed"); 608 } 609 /* generates a new ID with the supplied CN and validity period 610 - CN must be alphanumeric and begin with a letter 611 - validity must be at least one second 612 Will throw an exception if either of the above is violated */ 628 m_id=abac_id_from_file(filename); 629 if(m_id==NULL) 630 abac_errx(1, "Id creation from filename failed"); 631 } 632 /*** 633 ID(char *,int) 634 generates a new ID with the supplied CN and validity period 635 - CN must be alphanumeric and begin with a letter 636 - validity must be at least one second 637 will throw an exception if either of the above is violated 638 ***/ 613 639 ID(char *cn, int validity) { 614 int rt=abac_id_generate(&m_id, cn, validity); 615 if(rt != ABAC_ID_SUCCESS) 616 abac_errx(1, "Id creation failed"); 617 } 618 /* loads the private key associated with the cert 619 will throw an exception if the key cannot be loaded */ 640 int rt=abac_id_generate(&m_id, cn, validity); 641 if(rt != ABAC_ID_SUCCESS) 642 abac_errx(1, "Id creation failed"); 643 } 644 /*** 645 void load_privkey(char *) 646 loads the private key associated with the ID credential 647 will throw an exception if the key cannot be loaded 648 ***/ 620 649 void load_privkey(char *filename) { 621 650 int rt=abac_id_load_privkey_file(m_id, filename); … … 623 652 abac_errx(1, "Failed to load private key"); 624 653 } 654 /*** 655 abac_id_t *id() 656 returns the abac_id_t 657 returns the interal libabac representation of this id 658 ***/ 625 659 abac_id_t *id() { return m_id; } 626 660 627 /* returns the SHA1 keyid of the cert */ 661 /*** 662 char *keyid() 663 returns the SHA1 keyid of the id cert 664 char *name() 665 returns the CN 666 ***/ 628 667 char *keyid() { return abac_id_keyid(m_id); } 629 /* returns the CN */630 668 char *name() { return abac_id_cn(m_id); } 631 /* returns true if the ID has an associated private key */ 632 bool has_privkey() { 633 return abac_id_has_privkey(m_id); 634 } 635 /* writes a PEM-encoded cert to the file handle */ 636 void write_cert(FILE *out) { 637 abac_id_write_cert(m_id, out); 638 } 639 /* writes a PEM-encoded cert to a file named out */ 669 /*** 670 bool has_privkey() 671 returns true if the ID has an associated private key 672 ***/ 673 bool has_privkey() 674 { return abac_id_has_privkey(m_id); } 675 676 /*** 677 void write_cert(FILE *) 678 writes a PEM-encoded cert to the file handle 679 void write_cert(char *) 680 writes a PEM-encoded cert to a file named out 681 ***/ 682 void write_cert(FILE *out) 683 { abac_id_write_cert(m_id, out); } 640 684 void write_cert(char *filename) { 641 FILE *out = fopen(filename, "a"); 642 write_cert(out); 643 fclose(out); 644 } 645 /* writes a PEM-encoded private key to the file handle 646 throws an exception if no private key is loaded */ 685 FILE *out = fopen(filename, "a"); 686 write_cert(out); 687 fclose(out); 688 } 689 /*** 690 void write_privkey(FILE *) 691 writes a PEM-encoded private key to the file handle 692 throws an exception if no private key is loaded 693 void write_privkey(char *) 694 writes a PEM-encoded private key a file named out 695 throws an exception if no private key is loaded 696 ***/ 647 697 void write_privkey(FILE *out) { 648 if(!has_privkey()) 649 abac_errx(1, "No privkey to write"); 650 abac_id_write_privkey(m_id, out); 651 } 652 /* writes a PEM-encoded private key a file named out 653 throws an exception if no private key is loaded */ 698 if(!has_privkey()) 699 abac_errx(1, "No privkey to write"); 700 abac_id_write_privkey(m_id, out); 701 } 654 702 void write_privkey(char *filename) { 655 FILE *out = fopen(filename, "a"); 656 write_privkey(out); 657 fclose(out); 658 } 659 /* returns a DER-encoded binary representation of the X.509 ID cert 660 associated with this ID. 661 can be passed to libabac's Context::load_id_chunk() */ 662 abac_chunk_t cert_chunk() { 663 return abac_id_cert_chunk(m_id); 664 } 703 FILE *out = fopen(filename, "a"); 704 write_privkey(out); 705 fclose(out); 706 } 707 /*** 708 abac_chunk_t cert_chunk() 709 returns a DER-encoded binary representation of the X.509 ID cert 710 associated with this ID. 711 can be passed to libabac's Context::load_id_chunk() 712 ***/ 713 abac_chunk_t cert_chunk() 714 { return abac_id_cert_chunk(m_id); } 715 /*** 716 char *string() 717 returns literal string of the id credential 718 ***/ 665 719 char *string() { 666 char *tmp=NULL;667 if(has_privkey())668 asprintf(&tmp,"(%s,%s,y)",abac_id_name(m_id),abac_id_idtype_string(m_id));669 else asprintf(&tmp,"(%s,%s,n)",abac_id_name(m_id),abac_id_idtype_string(m_id));670 return tmp;671 }720 char *tmp=NULL; 721 if(has_privkey()) 722 asprintf(&tmp,"(%s,%s,y)",abac_id_name(m_id),abac_id_idtype_string(m_id)); 723 else asprintf(&tmp,"(%s,%s,n)",abac_id_name(m_id),abac_id_idtype_string(m_id)); 724 return tmp; 725 } 672 726 public: 673 727 abac_id_t *m_id; … … 675 729 676 730 677 /* N.B., The way you use this class is by instantiating the object, adding 678 subjects to it, and then baking it. Only once it's baked can you access the 679 X.509 cert. Once it's been baked you can no longer add subjects to it. */ 731 /*** 732 ABAC::Attribute 733 This is the attribute representation for the access policy rule 734 LHS <- RHS 735 The sequence of generation is to 736 first, instantiate the object, ie, LHS (head) 737 second, adding subject(s) to it, ie, RHS (tail) 738 and then baking it. 739 Only once it's baked can you access the X.509 cert. 740 Once it's been baked you can no longer add subjects to it 741 ***/ 680 742 class Attribute { 681 743 public: 682 Attribute() : m_attr(NULL) { } // do not use: here for swig 744 /*** 745 Attribute() 746 default constructor, do not use, for swig only 747 Attribute(const Attribute &) 748 copy constructor, used for cloning an attribute 749 ~Attribute() 750 default destructor 751 ***/ 752 Attribute() : m_attr(NULL) { } 753 Attribute(const Attribute &id) 754 { m_attr =abac_attribute_dup(id.m_attr); } 755 ~Attribute() 756 { if(m_attr) abac_attribute_free(m_attr); } 757 /*** 758 XXX 759 ***/ 683 760 Attribute(abac_attribute_t *attr): m_attr(abac_attribute_dup(attr)) 684 761 { } 685 762 Attribute(abac_credential_t *cred) { 686 763 m_attr=abac_attribute_dup(abac_credential_attribute(cred)); 687 }688 Attribute(const Attribute &id) {689 m_attr =abac_attribute_dup(id.m_attr);690 }691 ~Attribute() {692 if(m_attr) abac_attribute_free(m_attr);693 764 } 694 765 /* Create an object to be signed by the given issuer with the given role
Note: See TracChangeset
for help on using the changeset viewer.
