netpgp : added pgp_delete_keypair. Fixed broken wrong fingerprint assined to identity in generate_keypair
authorEdouard Tisserant
Tue, 28 Apr 2015 01:52:09 +0200
changeset 2279321f8fb77c2
parent 226 a2079f2f7a8c
child 228 4e6728cddb3f
netpgp : added pgp_delete_keypair. Fixed broken wrong fingerprint assined to identity in generate_keypair
src/pgp_netpgp.c
     1.1 --- a/src/pgp_netpgp.c	Sat Apr 25 01:23:46 2015 +0200
     1.2 +++ b/src/pgp_netpgp.c	Tue Apr 28 01:52:09 2015 +0200
     1.3 @@ -98,7 +98,8 @@
     1.4      return armoured;
     1.5  }
     1.6  
     1.7 -static void id_to_fpr(const uint8_t *userid, char *fpr)
     1.8 +/* return key ID's hexdump as a string */
     1.9 +static void id_to_str(const uint8_t *userid, char *fpr)
    1.10  {
    1.11      int i;
    1.12      static const char *hexes = "0123456789abcdef";
    1.13 @@ -165,7 +166,7 @@
    1.14              pgp_print_keydata(netpgp->io, netpgp->pubring, key, "valid signature ", &key->key.pubkey, 0);
    1.15  #endif //PEP_NETPGP_DEBUG
    1.16  
    1.17 -            id_to_fpr(userid, id);
    1.18 +            id_to_str(userid, id);
    1.19  
    1.20              k = stringlist_add(k, id);
    1.21              if(!k){
    1.22 @@ -509,6 +510,57 @@
    1.23      return result;
    1.24  }
    1.25  
    1.26 +/* return the hexdump as a string */
    1.27 +static unsigned
    1.28 +fpr_to_str (char **str, const uint8_t *fpr, size_t length)
    1.29 +{
    1.30 +	unsigned i;
    1.31 +	int	n;
    1.32 +
    1.33 +    /* 5 char per byte (hexes + space) tuple -1 space at the end + null */
    1.34 +    *str = malloc((length / 2) * 5 - 1 + 1);
    1.35 +
    1.36 +    if(*str == NULL)
    1.37 +        return 0;
    1.38 +
    1.39 +	for (n = 0, i = 0 ; i < length - 1; i += 2) {
    1.40 +		n += snprintf(&((*str)[n]), 6, "%02x%02x ", *fpr++, *fpr++);
    1.41 +	}
    1.42 +    snprintf(&((*str)[n]), 5, "%02x%02x", *fpr++, *fpr++);
    1.43 +
    1.44 +	return 1;
    1.45 +}
    1.46 +
    1.47 +static unsigned
    1.48 +str_to_fpr (const char *str, uint8_t *fpr, size_t *length)
    1.49 +{
    1.50 +    unsigned i,j;
    1.51 +
    1.52 +    *length = 0;
    1.53 +
    1.54 +    while(*str && *length < PGP_FINGERPRINT_SIZE){
    1.55 +        while (*str == ' ') str++;
    1.56 +        for (j = 0; j < 2; j++) {
    1.57 +            uint8_t *byte = &fpr[*length];
    1.58 +            for (i = 0; i < 2; i++) {
    1.59 +                if (i > 0)
    1.60 +                    *byte *= 16;
    1.61 +                if (*str >= 'a' && *str <= 'f')
    1.62 +                    *byte += 10 + *str - 'a';
    1.63 +                else if (*str >= 'A' && *str <= 'F')
    1.64 +                    *byte += 10 + *str - 'A';
    1.65 +                else if (*str >= '0' && *str <= '9')
    1.66 +                    *byte += *str - '0';
    1.67 +                else 
    1.68 +                    return 0;
    1.69 +                str++;
    1.70 +            }
    1.71 +            *length++;
    1.72 +        }
    1.73 +    }
    1.74 +    return 1;
    1.75 +}
    1.76 +
    1.77  PEP_STATUS pgp_generate_keypair(
    1.78      PEP_SESSION session, pEp_identity *identity
    1.79      )
    1.80 @@ -548,7 +600,7 @@
    1.81      // Generate the key
    1.82      if (!pgp_rsa_generate_keypair(&newkey, 4096, 65537UL, hashalg, cipher,
    1.83                                    (const uint8_t *) "", (const size_t) 0) ||
    1.84 -        !pgp_add_selfsigned_userid(&newkey, newid)) {
    1.85 +        !pgp_add_selfsigned_userid(&newkey, (const uint8_t *)newid)) {
    1.86          return PEP_CANNOT_CREATE_KEY;
    1.87  	}
    1.88  
    1.89 @@ -563,14 +615,15 @@
    1.90      // FIXME doesn't check result since always true 
    1.91      // TODO alloc error feedback in netpgp
    1.92  
    1.93 -    // save rings
    1.94 +    // save rings (key ownership transfered)
    1.95      if (netpgp_save_pubring(netpgp) && 
    1.96          netpgp_save_secring(netpgp))
    1.97      {
    1.98 -        char fpr[MAX_ID_LENGTH + 1];
    1.99 -        id_to_fpr(pubkey.sigid, fpr);
   1.100 -
   1.101 -        if ((identity->fpr = strdup(fpr)) == NULL) {
   1.102 +        char *fprstr = NULL;
   1.103 +        fpr_to_str(&fprstr,
   1.104 +                   newkey.sigfingerprint.fingerprint,
   1.105 +                   newkey.sigfingerprint.length);
   1.106 +        if ((identity->fpr = fprstr) == NULL) {
   1.107              result = PEP_OUT_OF_MEMORY;
   1.108          }else{
   1.109              result = PEP_STATUS_OK;
   1.110 @@ -579,30 +632,54 @@
   1.111          result = PEP_UNKNOWN_ERROR;
   1.112      }
   1.113  
   1.114 -    // pgp_keydata_free(key);
   1.115 -
   1.116      return result;
   1.117  }
   1.118  
   1.119 -PEP_STATUS pgp_delete_keypair(PEP_SESSION session, const char *fpr)
   1.120 +PEP_STATUS pgp_delete_keypair(PEP_SESSION session, const char *fprstr)
   1.121  {
   1.122 +    netpgp_t *netpgp;
   1.123 +    uint8_t fpr[PGP_FINGERPRINT_SIZE];
   1.124 +    size_t length;
   1.125 +    unsigned res;
   1.126 +
   1.127 +    PEP_STATUS result;
   1.128 +
   1.129      assert(session);
   1.130      assert(fpr);
   1.131  
   1.132 -    /* TODO get key with given fpr */
   1.133 -        return PEP_KEY_NOT_FOUND;
   1.134 -        return PEP_ILLEGAL_VALUE;
   1.135 -        return PEP_KEY_HAS_AMBIG_NAME;
   1.136 -        return PEP_OUT_OF_MEMORY;
   1.137 +    if (!session || !fpr)
   1.138          return PEP_UNKNOWN_ERROR;
   1.139  
   1.140 -    /* TODO delete that key */
   1.141 -        return PEP_UNKNOWN_ERROR;
   1.142 -        return PEP_KEY_NOT_FOUND;
   1.143 -        return PEP_KEY_HAS_AMBIG_NAME;
   1.144 -        return PEP_UNKNOWN_ERROR;
   1.145 +    netpgp = &session->ctx;
   1.146 +    
   1.147 +    if (str_to_fpr(fprstr, fpr, &length)) {
   1.148 +        if (!pgp_deletekeybyfpr(netpgp->io,
   1.149 +                                (pgp_pubkey_t *)netpgp->secring, 
   1.150 +                                fpr, length)) {
   1.151 +            return PEP_KEY_NOT_FOUND;
   1.152 +        }
   1.153 +    }else{
   1.154 +        return PEP_OUT_OF_MEMORY;
   1.155 +    }
   1.156  
   1.157 -    return PEP_STATUS_OK;
   1.158 +    /* pair was found in secring delete also corresponding pubkey 
   1.159 +     * in pubring if it exists */
   1.160 +    if(res) {
   1.161 +        pgp_deletekeybyfpr(netpgp->io,
   1.162 +                           (pgp_pubkey_t *)netpgp->pubring, 
   1.163 +                           fpr, length);
   1.164 +    }
   1.165 +
   1.166 +    // save rings (key ownership transfered)
   1.167 +    if (netpgp_save_pubring(netpgp) && 
   1.168 +        netpgp_save_secring(netpgp))
   1.169 +    {
   1.170 +        result = PEP_STATUS_OK;
   1.171 +    }else{
   1.172 +        result = PEP_UNKNOWN_ERROR;
   1.173 +    }
   1.174 +
   1.175 +    return result;
   1.176  }
   1.177  
   1.178  PEP_STATUS pgp_import_keydata(PEP_SESSION session, const char *key_data, size_t size)