File: rsa_pem_openssl.c

package info (click to toggle)
tgl 2.0.1%2Bgit20160323.ffb04cac-5
  • links: PTS
  • area: main
  • in suites: sid
  • size: 1,960 kB
  • sloc: ansic: 22,237; makefile: 100; awk: 37
file content (73 lines) | stat: -rw-r--r-- 2,214 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* 
    This file is part of tgl-library

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    Copyright Ben Wiederhake 2015
*/

#include "../config.h"

#ifndef TGL_AVOID_OPENSSL

//#include <stddef.h> /* NULL */

#include <openssl/rsa.h>
#include <openssl/pem.h>

#include "bn.h"
#include "meta.h"
#include "rsa_pem.h"

TGLC_WRAPPER_ASSOC(rsa,RSA)

// TODO: Refactor crucial struct-identity into its own header.
TGLC_WRAPPER_ASSOC(bn,BIGNUM)

/*
 * Note: Since OpenSSL version 1.1.0-pre5 the RSA struct (rsa_st) is opaque,
 * see https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes
 */
TGLC_rsa *TGLC_rsa_new (unsigned long e, int n_bytes, const unsigned char *n) {
  RSA *ret = RSA_new ();
  BIGNUM *ret_e = unwrap_bn (TGLC_bn_new ());
  BIGNUM *ret_n = unwrap_bn (TGLC_bn_bin2bn (n, n_bytes, NULL));
  RSA_set0_key (ret, ret_n, ret_e, NULL);
  TGLC_bn_set_word (wrap_bn (ret_e), e);
  return wrap_rsa (ret);
}

#define RSA_GETTER(M)                       \
TGLC_bn *TGLC_rsa_ ## M (TGLC_rsa *key) {   \
    BIGNUM *rsa_n, *rsa_e, *rsa_d;          \
    RSA_get0_key(unwrap_rsa (key),          \
        (const BIGNUM **) &rsa_n,           \
        (const BIGNUM **) &rsa_e,           \
        (const BIGNUM **) &rsa_d);          \
    return wrap_bn (rsa_ ## M);             \
}

RSA_GETTER(n);
RSA_GETTER(e);

void TGLC_rsa_free (TGLC_rsa *p) {
  RSA_free (unwrap_rsa (p));
}

TGLC_rsa *TGLC_pem_read_RSAPublicKey (FILE *fp) {
  return wrap_rsa (PEM_read_RSAPublicKey (fp, NULL, NULL, NULL));
}

#endif