OpenVPN 3 Core Library
Loading...
Searching...
No Matches
pkcs1.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#ifndef OPENVPN_PKI_PKCS1_H
13#define OPENVPN_PKI_PKCS1_H
14
15#include <cstring>
16
19
20// from http://www.ietf.org/rfc/rfc3447.txt
21namespace openvpn::PKCS1::DigestPrefix { // CONST GLOBAL
22namespace {
23// clang-format off
24const unsigned char MD2[] = {
25 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08,
26 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
27 0x02, 0x02, 0x05, 0x00, 0x04, 0x10
28};
29const unsigned char MD5[] = {
30 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08,
31 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
32 0x02, 0x05, 0x05, 0x00, 0x04, 0x10
33};
34const unsigned char SHA1[] = {
35 0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
36 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
37 0x00, 0x04, 0x14
38};
39const unsigned char SHA256[] = {
40 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09,
41 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
42 0x04, 0x02, 0x01, 0x05, 0x00, 0x04,
43 0x20 };
44const unsigned char SHA384[] = {
45 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09,
46 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
47 0x04, 0x02, 0x02, 0x05, 0x00, 0x04,
48 0x30
49};
50const unsigned char SHA512[] = {
51 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09,
52 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
53 0x04, 0x02, 0x03, 0x05, 0x00, 0x04,
54 0x40
55};
56// clang-format on
57} // namespace
58
63template <typename T>
64class Parse
65{
66 public:
67 Parse(const T none,
68 const T md2,
69 const T md5,
70 const T sha1,
71 const T sha256,
72 const T sha384,
73 const T sha512)
74 : none_(none),
75 md2_(md2),
76 md5_(md5),
77 sha1_(sha1),
78 sha256_(sha256),
79 sha384_(sha384),
80 sha512_(sha512)
81 {
82 }
83
85 Parse(const T none,
86 const T md5,
87 const T sha1,
88 const T sha256,
89 const T sha384,
90 const T sha512)
91 : none_(none),
92 md2_(none),
93 md5_(md5),
94 sha1_(sha1),
95 sha256_(sha256),
96 sha384_(sha384),
97 sha512_(sha512)
98 {
99 }
100
102 {
103 if (match(buf, MD2, sizeof(MD2)))
104 return md2_;
105 else if (match(buf, MD5, sizeof(MD5)))
106 return md5_;
107 else if (match(buf, SHA1, sizeof(SHA1)))
108 return sha1_;
109 else if (match(buf, SHA256, sizeof(SHA256)))
110 return sha256_;
111 else if (match(buf, SHA384, sizeof(SHA384)))
112 return sha384_;
113 else if (match(buf, SHA512, sizeof(SHA512)))
114 return sha512_;
115 else
116 return none_;
117 }
118
119 private:
120 bool match(Buffer &buf, const unsigned char *data, const size_t size) const
121 {
122 if (buf.size() < size)
123 return false;
124 else if (std::memcmp(buf.c_data(), data, size) == 0)
125 {
126 buf.advance(size);
127 return true;
128 }
129 else
130 return false;
131 }
132
134};
135} // namespace openvpn::PKCS1::DigestPrefix
136
137#endif
const T * c_data() const
Returns a const pointer to the start of the buffer.
Definition buffer.hpp:1194
size_t size() const
Returns the size of the buffer in T objects.
Definition buffer.hpp:1242
void advance(const size_t delta)
Advances the buffer by the specified delta.
Definition buffer.hpp:1277
Parse(const T none, const T md2, const T md5, const T sha1, const T sha256, const T sha384, const T sha512)
Definition pkcs1.hpp:67
Parse(const T none, const T md5, const T sha1, const T sha256, const T sha384, const T sha512)
Definition pkcs1.hpp:85
bool match(Buffer &buf, const unsigned char *data, const size_t size) const
Definition pkcs1.hpp:120
T alg_from_prefix(Buffer &buf) const
Definition pkcs1.hpp:101