aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/tvmauth/src/rw/rw_asn1.c
blob: f9dfe3996d8b6a1f24bcb5e911b778daa183c195 (plain) (blame)
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
74
75
76
77
78
79
80
81
#include "rw.h"

#include <openssl/asn1.h>
#include <openssl/asn1t.h>
#include <openssl/rand.h>

#include <stdio.h>

/* Override the default new methods */
/* This callback is used by OpenSSL's ASN.1 parser */
static int SignatureCallback(int operation, ASN1_VALUE** pval, const ASN1_ITEM* it, void* exarg) {
    (void)it;
    (void)exarg;

    if (operation == ASN1_OP_NEW_PRE) {
        TRwSignature* sig;
        sig = OPENSSL_malloc(sizeof(TRwSignature));
        if (!sig)
            return 0;
        sig->S = NULL;
        *pval = (ASN1_VALUE*)sig;
        return 2;
    }
    return 1;
}

/* ASN.1 structure representing RW signature value */
ASN1_SEQUENCE_cb(TRwSignature, SignatureCallback) = {
    ASN1_SIMPLE(TRwSignature, S, BIGNUM),
} ASN1_SEQUENCE_END_cb(TRwSignature, TRwSignature)

    /* i2d_ and d2i functions implementation for RW */
    IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(TRwSignature, TRwSignature, TRwSignature)

    /* Override the default free and new methods */
    static int RwCallback(int operation, ASN1_VALUE** pval, const ASN1_ITEM* it, void* exarg) {
    (void)it;
    (void)exarg;

    if (operation == ASN1_OP_NEW_PRE) {
        *pval = (ASN1_VALUE*)RwNew();
        if (*pval)
            return 2;
        return 0;
    } else if (operation == ASN1_OP_FREE_PRE) {
        RwFree((TRwKey*)*pval);
        *pval = NULL;
        return 2;
    }
    return 1;
}

/* ASN.1 representation of RW's private key */
ASN1_SEQUENCE_cb(RWPrivateKey, RwCallback) = {
    ASN1_SIMPLE(TRwKey, N, BIGNUM),
    ASN1_SIMPLE(TRwKey, P, CBIGNUM),
    ASN1_SIMPLE(TRwKey, Q, CBIGNUM),
    ASN1_SIMPLE(TRwKey, Iqmp, CBIGNUM),
    ASN1_SIMPLE(TRwKey, Dq, CBIGNUM),
    ASN1_SIMPLE(TRwKey, Dp, CBIGNUM),
    ASN1_SIMPLE(TRwKey, Twomp, CBIGNUM),
    ASN1_SIMPLE(TRwKey, Twomq, CBIGNUM)} ASN1_SEQUENCE_END_cb(TRwKey, RWPrivateKey);

/* i2d_ and d2i_ functions for RW's private key */
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(TRwKey, RWPrivateKey, RWPrivateKey);

/* ASN.1 representation of RW public key */
ASN1_SEQUENCE_cb(RWPublicKey, RwCallback) = {
    ASN1_SIMPLE(TRwKey, N, BIGNUM),
} ASN1_SEQUENCE_END_cb(TRwKey, RWPublicKey);

/* i2d_ and d2i functions for RW public key */
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(TRwKey, RWPublicKey, RWPublicKey);

TRwKey* RwPublicKeyDup(TRwKey* rw) {
    return ASN1_item_dup(ASN1_ITEM_rptr(RWPublicKey), rw);
}

TRwKey* RwPrivateKeyDup(TRwKey* rw) {
    return ASN1_item_dup(ASN1_ITEM_rptr(RWPrivateKey), rw);
}