better ASN.1
authorVolker Birk <vb@pep-project.org>
Mon, 16 May 2016 15:36:26 +0200
changeset 633a2a4ad55e19e
parent 632 a213fafe3f4f
child 634 a1ba6e8a32eb
better ASN.1
asn.1/Makefile
asn.1/devicegroup.asn1
asn.1/pEp.asn1
src/pEpEngine.c
src/pEp_internal.h
     1.1 --- a/asn.1/Makefile	Mon May 16 11:55:36 2016 +0200
     1.2 +++ b/asn.1/Makefile	Mon May 16 15:36:26 2016 +0200
     1.3 @@ -1,11 +1,11 @@
     1.4  include ../Makefile.conf
     1.5  
     1.6 -all: Message.c Message.h
     1.7 +all: Message.c
     1.8  
     1.9  %.o: %.c %.h
    1.10  	$(CC) $(CFLAGS) -I$(ASN1C_INCLUDE) -c $< -o $@
    1.11  
    1.12 -Message.c Message.h: devicegroup.asn1
    1.13 +Message.c: devicegroup.asn1
    1.14  	$(ASN1C) -fcompound-names -pdu=PEP.Message pEp.asn1 $<
    1.15  
    1.16  .PHONY: clean
     2.1 --- a/asn.1/devicegroup.asn1	Mon May 16 11:55:36 2016 +0200
     2.2 +++ b/asn.1/devicegroup.asn1	Mon May 16 15:36:26 2016 +0200
     2.3 @@ -1,25 +1,30 @@
     2.4 -DEVICEGROUP DEFINITIONS AUTOMATIC TAGS ::=
     2.5 +DEVICEGROUP
     2.6 +
     2.7 +DEFINITIONS AUTOMATIC TAGS EXTENSIBILITY IMPLIED ::=
     2.8 +
     2.9  BEGIN
    2.10  
    2.11 -Beacon ::= SEQUENCE {
    2.12 -    header PEP.Header,
    2.13 -    me PEP.Identity
    2.14 +IMPORTS Identity, KeyList, Header FROM PEP;
    2.15 +
    2.16 +Beacon ::= [APPLICATION 1] SEQUENCE {
    2.17 +    header Header,
    2.18 +    me Identity
    2.19  }
    2.20  
    2.21 -HandshakeRequest ::= SEQUENCE {
    2.22 -    header PEP.Header,
    2.23 -    you PEP.Identity,
    2.24 -    me PEP.Identity
    2.25 +HandshakeRequest ::= [APPLICATION 2] SEQUENCE {
    2.26 +    header Header,
    2.27 +    you Identity,
    2.28 +    me Identity
    2.29  }
    2.30  
    2.31 -OwnKeys ::= SEQUENCE {
    2.32 -    header PEP.Header,
    2.33 -    you PEP.Identity,
    2.34 -    me PEP.Identity,
    2.35 -    keylist PEP.Stringlist
    2.36 +OwnKeys ::= [APPLICATION 3] SEQUENCE {
    2.37 +    header Header,
    2.38 +    you Identity,
    2.39 +    me Identity,
    2.40 +    keylist KeyList
    2.41  }
    2.42  
    2.43 -Message ::= CHOICE {
    2.44 +SyncMessage ::= CHOICE {
    2.45      beacon Beacon,
    2.46      handshakeRequest HandshakeRequest,
    2.47      ownKeys OwnKeys
     3.1 --- a/asn.1/pEp.asn1	Mon May 16 11:55:36 2016 +0200
     3.2 +++ b/asn.1/pEp.asn1	Mon May 16 15:36:26 2016 +0200
     3.3 @@ -1,26 +1,36 @@
     3.4 -PEP DEFINITIONS ::=
     3.5 +PEP
     3.6 +
     3.7 +DEFINITIONS AUTOMATIC TAGS EXTENSIBILITY IMPLIED ::=
     3.8 +
     3.9  BEGIN
    3.10  
    3.11 +EXPORTS Identity, KeyList, Header;
    3.12 +
    3.13 +major-version INTEGER ::= 1
    3.14 +minor-version INTEGER ::= 0
    3.15 +
    3.16 +ISO936-1 ::= PrintableString(FROM ("a".."z")) (SIZE(2))
    3.17 +Hex ::= PrintableString(FROM ("A".."F") | FROM ("0".."9"))
    3.18 +Hash ::= Hex(SIZE(50..128)) -- SHA1 to SHA512 in hex
    3.19 +
    3.20  Identity ::= SEQUENCE {
    3.21 -    address     UTF8String,
    3.22 -    fpr         UTF8String,
    3.23 -    user-id     UTF8String,
    3.24 -    username    UTF8String,
    3.25 -    comm-type   INTEGER (0..255),
    3.26 -    lang        PrintableString
    3.27 +    address     UTF8String  (SIZE(1..1024)) OPTIONAL,
    3.28 +    fpr         Hash,
    3.29 +    user-id     UTF8String  (SIZE(1..1024)) OPTIONAL,
    3.30 +    username    UTF8String  (SIZE(1..1024)) OPTIONAL,
    3.31 +    comm-type   INTEGER     (0..255) OPTIONAL,
    3.32 +    lang        ISO936-1    DEFAULT "en"
    3.33  }
    3.34  
    3.35 -Stringlist ::= SEQUENCE OF UTF8String
    3.36 +KeyList ::= SEQUENCE OF Hash
    3.37  
    3.38  Version ::= SEQUENCE {
    3.39 -    major       INTEGER (0..255),
    3.40 -    minor       INTEGER (0..255)
    3.41 +    major       INTEGER (0..255) DEFAULT major-version,
    3.42 +    minor       INTEGER (0..255) DEFAULT minor-version
    3.43  }
    3.44  
    3.45  Header ::= SEQUENCE {
    3.46 -    protocol    INTEGER (0..65535),
    3.47      version     Version,
    3.48 -    message     INTEGER (0..65535),
    3.49      sequence    INTEGER
    3.50  }
    3.51  
     4.1 --- a/src/pEpEngine.c	Mon May 16 11:55:36 2016 +0200
     4.2 +++ b/src/pEpEngine.c	Mon May 16 15:36:26 2016 +0200
     4.3 @@ -35,7 +35,8 @@
     4.4      static const char *sql_own_key_is_listed;
     4.5      static const char *sql_own_key_retrieve;
     4.6  
     4.7 -    static const char *sql_sequence_value;
     4.8 +    static const char *sql_sequence_value1;
     4.9 +    static const char *sql_sequence_value2;
    4.10  
    4.11      bool in_first = false;
    4.12  
    4.13 @@ -264,11 +265,11 @@
    4.14  
    4.15          sql_own_key_retrieve = "select * from own_keys ;";
    4.16   
    4.17 -        sql_sequence_value = "insert or replace into sequences (name, value) "
    4.18 -                             "values (?1, "
    4.19 -                             "(select coalesce((select value + 1 from sequences "
    4.20 -                             "where name = ?1), 1 ))) ; "
    4.21 -                             "select value from sequences where name = ?1 ;";
    4.22 +        sql_sequence_value1 = "insert or replace into sequences (name, value) "
    4.23 +                              "values (?1, "
    4.24 +                              "(select coalesce((select value + 1 from sequences "
    4.25 +                              "where name = ?1), 1 ))) ; ";
    4.26 +        sql_sequence_value2 = "select value from sequences where name = ?1 ;";
    4.27      }
    4.28  
    4.29      int_result = sqlite3_prepare_v2(_session->db, sql_log, (int)strlen(sql_log),
    4.30 @@ -344,17 +345,27 @@
    4.31      // Own keys
    4.32      
    4.33      int_result = sqlite3_prepare_v2(_session->db, sql_own_key_add,
    4.34 -                                    (int)strlen(sql_own_key_add), &_session->own_key_add, NULL);
    4.35 +            (int)strlen(sql_own_key_add), &_session->own_key_add, NULL);
    4.36      assert(int_result == SQLITE_OK);
    4.37      
    4.38      int_result = sqlite3_prepare_v2(_session->db, sql_own_key_is_listed,
    4.39 -                                    (int)strlen(sql_own_key_is_listed), &_session->own_key_is_listed, NULL);
    4.40 +            (int)strlen(sql_own_key_is_listed), &_session->own_key_is_listed, NULL);
    4.41      assert(int_result == SQLITE_OK);
    4.42      
    4.43      int_result = sqlite3_prepare_v2(_session->db, sql_own_key_retrieve,
    4.44 -                                    (int)strlen(sql_own_key_retrieve), &_session->own_key_retrieve, NULL);
    4.45 +            (int)strlen(sql_own_key_retrieve), &_session->own_key_retrieve, NULL);
    4.46      assert(int_result == SQLITE_OK);
    4.47 -    
    4.48 + 
    4.49 +    // Sequence
    4.50 +
    4.51 +    int_result = sqlite3_prepare_v2(_session->db, sql_sequence_value1,
    4.52 +            (int)strlen(sql_sequence_value1), &_session->sequence_value1, NULL);
    4.53 +    assert(int_result == SQLITE_OK);
    4.54 +
    4.55 +    int_result = sqlite3_prepare_v2(_session->db, sql_sequence_value2,
    4.56 +            (int)strlen(sql_sequence_value2), &_session->sequence_value2, NULL);
    4.57 +    assert(int_result == SQLITE_OK);
    4.58 +
    4.59      status = init_cryptotech(_session, in_first);
    4.60      if (status != PEP_STATUS_OK)
    4.61          goto pep_error;
    4.62 @@ -1432,22 +1443,30 @@
    4.63  
    4.64      *value = 0;
    4.65  
    4.66 -    sqlite3_reset(session->sequence_value);
    4.67 -    sqlite3_bind_text(session->sequence_value, 1, name, -1, SQLITE_STATIC);
    4.68 -
    4.69 -    result = sqlite3_step(session->sequence_value);
    4.70 -    switch (result) {
    4.71 -        case SQLITE_ROW: {
    4.72 -            int64_t _value = (int64_t)
    4.73 -                    sqlite3_column_int64(session->sequence_value, 0);
    4.74 -            *value = _value;
    4.75 -            break;
    4.76 +    sqlite3_reset(session->sequence_value1);
    4.77 +    sqlite3_bind_text(session->sequence_value1, 1, name, -1, SQLITE_STATIC);
    4.78 +    result = sqlite3_step(session->sequence_value1);
    4.79 +    assert(result == SQLITE_DONE);
    4.80 +    sqlite3_reset(session->sequence_value1);
    4.81 +    if (result != SQLITE_DONE) {
    4.82 +        status = PEP_UNKNOWN_ERROR;
    4.83 +    }
    4.84 +    else {
    4.85 +        sqlite3_reset(session->sequence_value2);
    4.86 +        sqlite3_bind_text(session->sequence_value2, 1, name, -1, SQLITE_STATIC);
    4.87 +        result = sqlite3_step(session->sequence_value2);
    4.88 +        switch (result) {
    4.89 +            case SQLITE_ROW: {
    4.90 +                int64_t _value = (int64_t)
    4.91 +                        sqlite3_column_int64(session->sequence_value2, 0);
    4.92 +                *value = _value;
    4.93 +                break;
    4.94 +            }
    4.95 +            default:
    4.96 +                status = PEP_UNKNOWN_ERROR;
    4.97          }
    4.98 -        default:
    4.99 -            status = PEP_CANNOT_FIND_IDENTITY;
   4.100 +        sqlite3_reset(session->sequence_value2);
   4.101      }
   4.102 -
   4.103 -    sqlite3_reset(session->sequence_value);
   4.104      return status;
   4.105  }
   4.106  
     5.1 --- a/src/pEp_internal.h	Mon May 16 11:55:36 2016 +0200
     5.2 +++ b/src/pEp_internal.h	Mon May 16 15:36:26 2016 +0200
     5.3 @@ -113,7 +113,8 @@
     5.4      sqlite3_stmt *own_key_retrieve;
     5.5  
     5.6      // sequence value
     5.7 -    sqlite3_stmt *sequence_value;
     5.8 +    sqlite3_stmt *sequence_value1;
     5.9 +    sqlite3_stmt *sequence_value2;
    5.10  
    5.11      // callbacks   
    5.12      examine_identity_t examine_identity;